Skip to content

Commit 747290e

Browse files
committed
fix testnet bugs; add bintree sample;
1 parent e492397 commit 747290e

15 files changed

Lines changed: 1079 additions & 100 deletions

File tree

ex/build_clean.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/bin/bash
22

3-
#&& export ERL_COMPILER_OPTIONS=bin_opt_info \
4-
53
podman run -it --rm -v ../.:/root/node --entrypoint bash erlang_builder -c "echo 'building amadeus..' \
64
&& cd /tmp \
75
&& git clone https://github.com/amadeusprotocol/node \

ex/lib/api/api_proof.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ defmodule API.Proof do
77
key: proof.key,
88
validators: Enum.map(proof.validators, & Base58.encode(&1)),
99
proof: %{
10-
nodes: Enum.map(proof.proof.nodes, & %{direction: &1.direction, hash: Base58.encode(&1.hash)}),
10+
root: Base58.encode(proof.proof.root),
1111
path: Base58.encode(proof.proof.path),
12-
path: Base58.encode(proof.proof.root),
13-
path: Base58.encode(proof.proof.hash),
12+
hash: Base58.encode(proof.proof.hash),
13+
nodes: Enum.map(proof.proof.nodes, & %{direction: &1.direction, hash: Base58.encode(&1.hash)}),
1414
}
1515
}
1616
end
17+
18+
def proof_contractstate(key) do
19+
%{db: db, cf: cf} = :persistent_term.get({:rocksdb, Fabric})
20+
RDB.bintree_contractstate_root_prove(db, key)
21+
end
1722
end

ex/lib/api/api_tx.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ defmodule API.TX do
185185
true -> Base58.encode(arg)
186186
end
187187
end)
188-
Map.put(action, :args, args)
188+
action = Map.put(action, :args, args)
189189

190190
tx = put_in(tx, [:tx, :action], action)
191191

ex/lib/bic/base.ex

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,13 @@
11
defmodule BIC.Base do
22
import ConsensusKV
33

4-
def exec_cost(epoch, txu) do
5-
bytes = byte_size(txu.tx_encoded) + 32 + 96
6-
BIC.Coin.to_cents( 1 + div(bytes, 1024) * 1 )
7-
8-
#for future update
9-
#BIC.Coin.to_tenthousandth( 18 + div(bytes, 256) * 3 )
10-
end
11-
124
def seed_random(vr, txhash, action_index, call_cnt) do
135
seed_bin = <<seed::256-little>> = Blake3.hash(
146
<<vr::binary, txhash::binary, action_index::binary, call_cnt::binary>>)
157
:rand.seed(:exsss, seed)
168
seed_bin
179
end
1810

19-
def call_txs_pre_parallel(env, txus) do
20-
Process.delete(:mutations)
21-
Process.delete(:mutations_reverse)
22-
23-
Enum.each(txus, fn(txu)->
24-
kv_put("bic:base:nonce:#{txu.tx.signer}", txu.tx.nonce, %{to_integer: true})
25-
exec_cost = exec_cost(env.entry_epoch, txu)
26-
kv_increment("bic:coin:balance:#{txu.tx.signer}:AMA", -exec_cost)
27-
28-
#burn 50% to prevent MEV/FreeChainGrowth attack
29-
half_exec_cost = div(exec_cost, 2)
30-
kv_increment("bic:coin:balance:#{env.entry_signer}:AMA", half_exec_cost)
31-
kv_increment("bic:coin:balance:#{BIC.Coin.burn_address()}:AMA", half_exec_cost)
32-
end)
33-
34-
#parallel verify sols
35-
segment_vr_hash = kv_get("bic:epoch:segment_vr_hash")
36-
diff_bits = kv_get("bic:epoch:diff_bits", %{to_integer: true}) || 24
37-
steam = Task.async_stream(txus, fn txu ->
38-
sol = Enum.find_value(txu.tx.actions, fn(a)-> a.function == "submit_sol" and length(a.args) != [] and hd(a.args) end)
39-
if sol do
40-
hash = Blake3.hash(sol)
41-
opts = %{hash: hash, vr_b3: env.entry_vr_b3, segment_vr_hash: segment_vr_hash, diff_bits: diff_bits}
42-
valid = try do BIC.Sol.verify(sol, opts) catch _,_ -> false end
43-
%{hash: hash, valid: valid}
44-
end
45-
end)
46-
47-
sol_verified_cache = for {error, spec} <- steam,
48-
error == :ok and spec != nil,
49-
into: %{},
50-
do: {spec.hash, spec.valid}
51-
52-
Process.put(SolVerifiedCache, sol_verified_cache)
53-
54-
{Process.get(:mutations, []), Process.get(:mutations_reverse, [])}
55-
end
56-
5711
def call_exit(env) do
5812
Process.delete(:mutations)
5913
Process.delete(:mutations_reverse)

ex/lib/consensus/fabric_gen.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ defmodule FabricGen do
312312
}
313313

314314
txus = Enum.map(entry.txs, & Map.put(&1, :tx_cost, TX.exec_cost(0, &1)))
315-
{rtx, m, m_rev, l} = RDB.apply_entry(db, next_entry_trimmed_map, Application.fetch_env!(:ama, :trainer_pk), Application.fetch_env!(:ama, :trainer_sk), txus)
315+
{rtx, m, m_rev, l} = RDB.apply_entry(db, next_entry_trimmed_map,
316+
Application.fetch_env!(:ama, :trainer_pk), Application.fetch_env!(:ama, :trainer_sk), txus,
317+
Application.fetch_env!(:ama, :testnet), Map.keys(Application.fetch_env!(:ama, :keys_by_pk))
318+
)
316319
rebuild_m_fn = fn(m)->
317320
Enum.map(m, fn(inner)->
318321
op = :"#{IO.iodata_to_binary(inner[~c"op"])}"
@@ -333,6 +336,8 @@ defmodule FabricGen do
333336
m_rev = rebuild_m_fn.(m_rev)
334337
l = rebuild_l_fn.(l)
335338

339+
#IO.inspect Enum.map(m, & Map.put(&1, :key, RocksDB.ascii_dump(&1.key))), limit: 11111111111
340+
336341
#call the exit
337342
Process.put({RocksDB, :ctx}, %{rtx: rtx, cf: cf})
338343
#mapenv = make_mapenv(next_entry)

ex/lib/misc/rocksdb.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ defmodule RocksDB do
209209
:ok = RDB.transaction_rollback(rtx)
210210
end
211211

212+
def dumpstate() do
213+
%{db: db, cf: cf} = :persistent_term.get({:rocksdb, Fabric})
214+
d = RocksDB.dump(cf.contractstate)
215+
dd = inspect d, limit: 1111111111111111111, pretty: true
216+
File.write! "/tmp/amastate", dd
217+
end
218+
212219
def dump(cf) do
213220
{:ok, it} = RDB.iterator_cf(cf)
214221
res = RDB.iterator_move(it, :first)

ex/lib/native/rdb.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ defmodule RDB do
4343
def transaction_iterator_cf(_tx, _cf), do: :erlang.nif_error(:nif_not_loaded)
4444
def transaction_iterator_move(_it, _action), do: :erlang.nif_error(:nif_not_loaded)
4545

46-
def apply_entry(_db, _next_entry_trimmed_map, _pk, _sk, _txus), do: :erlang.nif_error(:nif_not_loaded)
46+
def apply_entry(_db, _next_entry_trimmed_map, _pk, _sk, _txus, _testnet, _testnet_peddlebike), do: :erlang.nif_error(:nif_not_loaded)
4747

4848
def vecpak_encode(_map), do: :erlang.nif_error(:nif_not_loaded)
4949
def vecpak_decode(_bin), do: :erlang.nif_error(:nif_not_loaded)
@@ -53,4 +53,5 @@ defmodule RDB do
5353
def bintree_root(_propslist), do: :erlang.nif_error(:nif_not_loaded)
5454
def bintree_root_prove(_propslist, _key), do: :erlang.nif_error(:nif_not_loaded)
5555
def bintree_root_verify(_proof, _key, _value), do: :erlang.nif_error(:nif_not_loaded)
56+
def bintree_contractstate_root_prove(_db, _key), do: :erlang.nif_error(:nif_not_loaded)
5657
end

ex/native/rdb/src/consensus/bic/epoch.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ pub fn call_slash_trainer(env: &mut crate::consensus::consensus_apply::ApplyEnv,
284284
pub fn next(env: &mut ApplyEnv) {
285285
let epoch_cur = env.caller_env.entry_epoch;
286286
let epoch_next = env.caller_env.entry_epoch + 1;
287-
let peddlebike67_map: HashSet<Vec<u8>> = PEDDLEBIKE67.iter().map(|pk| pk.to_vec()).collect();
287+
let mut peddlebike67_map: HashSet<Vec<u8>> = PEDDLEBIKE67.iter().map(|pk| pk.to_vec()).collect();
288+
if env.testnet {
289+
peddlebike67_map = env.testnet_peddlebikes.iter().map(|pk| pk.to_vec()).collect();
290+
}
288291

289292
// slash sols for malicious trainers
290293
//let trainers = kv_get_trainers(env, &bcat(&[b"bic:epoch:trainers:", epoch_cur.to_string().as_bytes()]));

ex/native/rdb/src/consensus/bintree.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ pub enum VerifyStatus {
6767
Invalid, // The proof itself is mathematically invalid (bad root/chain)
6868
}
6969

70+
#[inline]
71+
pub fn compute_namespace_path(namespace: Option<&[u8]>, key: &[u8]) -> Path {
72+
match namespace {
73+
Some(ns) => {
74+
let ns_hash = sha256(ns);
75+
let key_hash = sha256(key);
76+
77+
let mut path = [0u8; 32];
78+
path[0..8].copy_from_slice(&ns_hash[0..8]);
79+
path[8..32].copy_from_slice(&key_hash[0..24]);
80+
path
81+
},
82+
None => {
83+
sha256(key)
84+
}
85+
}
86+
}
87+
7088
// ============================================================================
7189
// BIT HELPERS (Optimized & Inlined)
7290
// ============================================================================

0 commit comments

Comments
 (0)