Skip to content

Commit 7909a9f

Browse files
committed
prepare
1 parent 3515870 commit 7909a9f

15 files changed

Lines changed: 212 additions & 169 deletions

File tree

ex/config/config.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ config :logger, translator_inspect_opts: inspect_opts
1111

1212
config :ama, :version, Mix.Project.config[:version]
1313

14-
config :ama, :entry_size, 524288
15-
config :ama, :tx_size, 393216
14+
config :ama, :entry_size, 1048576
15+
config :ama, :tx_size, 786432
1616
config :ama, :quorum, 3
1717
#config :ama, :quorum, 1
1818

ex/lib/api/api_proof.ex

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule API.Proof do
2-
def proof_validators(entry_hash) do
2+
def validators(entry_hash) do
33
entry_hash = if byte_size(entry_hash) != 32, do: Base58.decode(entry_hash), else: entry_hash
44
proof = Entry.proof_validators(entry_hash)
55
%{
@@ -15,11 +15,32 @@ defmodule API.Proof do
1515
}
1616
end
1717

18-
def proof_contractstate(key) do
18+
def contractstate_namespace(key) do
19+
case key do
20+
<<"account:", pk::binary-48, _::binary>> -> <<"account:", pk>>
21+
<<"coin:", _::binary>> -> "coin"
22+
<<"bic:", _::binary>> -> "bic"
23+
_ -> nil
24+
end
25+
end
26+
27+
def contractstate(key, value \\ nil) do
1928
%{db: db, cf: cf} = :persistent_term.get({:rocksdb, Fabric})
29+
namespace = contractstate_namespace(key)
2030
proof = RDB.bintree_contractstate_root_prove(db, key)
21-
#RDB.bintree_root_verify(proof, "bic:epoch:segment_vr_hash", "7")
22-
#RDB.bintree_
23-
#
31+
map = %{
32+
namespace: Base58.encode(namespace),
33+
key: Base58.encode(key),
34+
proof: %{
35+
root: Base58.encode(proof.root),
36+
path: Base58.encode(proof.path),
37+
hash: Base58.encode(proof.hash),
38+
nodes: Enum.map(proof.nodes, & %{direction: &1.direction, hash: Base58.encode(&1.hash)}),
39+
}
40+
}
41+
if !value do map else
42+
result = RDB.bintree_root_verify(proof, namespace, key, value)
43+
Map.merge(map, %{value: Base58.encode(value), result: result})
44+
end
2445
end
2546
end

ex/lib/api/db_entry.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule DB.Entry do
8080
db_opts[:rtx_commit] && RocksDB.transaction_commit(db_opts.rtx)
8181
end
8282

83-
def apply_into_main_chain(entry, muts_hash, muts_rev, receipts, db_opts = %{rtx: _}) do
83+
def apply_into_main_chain(entry, muts_hash, muts_rev, receipts, root_receipts, root_contractstate, db_opts = %{rtx: _}) do
8484
entry_packed = Entry.pack_for_db(entry)
8585
RocksDB.put(entry.hash, entry_packed, db_handle(db_opts, :entry, %{}))
8686
RocksDB.put("by_height:#{pad_integer(entry.header.height)}:#{entry.hash}", entry.hash, db_handle(db_opts, :entry_meta, %{}))
@@ -91,6 +91,8 @@ defmodule DB.Entry do
9191
RocksDB.put("entry:#{entry.hash}:in_chain", "", db_handle(db_opts, :entry_meta, %{}))
9292
RocksDB.put("entry:#{entry.hash}:muts_hash", muts_hash, db_handle(db_opts, :entry_meta, %{}))
9393
RocksDB.put("entry:#{entry.hash}:muts_rev", RDB.vecpak_encode(muts_rev), db_handle(db_opts, :entry_meta, %{}))
94+
RocksDB.put("entry:#{entry.hash}:root_receipts", root_receipts, db_handle(db_opts, :entry_meta, %{}))
95+
RocksDB.put("entry:#{entry.hash}:root_contractstate", root_contractstate, db_handle(db_opts, :entry_meta, %{}))
9496

9597
Enum.each(Enum.zip(entry.txs, receipts), fn({txu, result})->
9698
case :binary.match(entry_packed, TX.pack(txu)) do
@@ -138,6 +140,8 @@ defmodule DB.Entry do
138140
RocksDB.delete("entry:#{hash}:in_chain", db_handle(db_opts, :entry_meta, %{}))
139141
RocksDB.delete("entry:#{hash}:muts", db_handle(db_opts, :entry_meta, %{}))
140142
RocksDB.delete("entry:#{hash}:muts_rev", db_handle(db_opts, :entry_meta, %{}))
143+
RocksDB.delete("entry:#{hash}:root_receipts", db_handle(db_opts, :entry_meta, %{}))
144+
RocksDB.delete("entry:#{hash}:root_contractstate", db_handle(db_opts, :entry_meta, %{}))
141145
RocksDB.delete_prefix("consensus:#{hash}:", db_handle(db_opts, :attestation, %{}))
142146
RocksDB.delete_prefix("attestation:#{height_padded}:#{hash}:", db_handle(db_opts, :attestation, %{}))
143147

ex/lib/consensus/consensus_kv.ex

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,6 @@ defmodule ConsensusKV do
207207
end
208208
end
209209

210-
def hash_mutations(height, m) do
211-
if height >= 416_00000 do
212-
RDB.vecpak_encode(m)
213-
|> Blake3.hash()
214-
else
215-
m = Enum.map(m, & Map.drop(&1, [:table]))
216-
RDB.vecpak_encode(m)
217-
|> Blake3.hash()
218-
end
219-
end
220-
221210
def merge_nested(left, right) do
222211
Map.merge(left, right, &merge_nested_resolve/3)
223212
end

ex/lib/consensus/fabric_gen.ex

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@ defmodule FabricGen do
308308
entry_slot: entry.header.slot,
309309
entry_prev_slot: entry.header.prev_slot,
310310
entry_height: entry.header.height,
311-
entry_epoch: div(entry.header.height,100_000),
311+
entry_epoch: div(entry.header.height, 100_000),
312312
}
313313

314-
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,
314+
txus = Enum.map(entry.txs, & Map.merge(&1, %{tx_cost: TX.exec_cost(0, &1), tx_size: byte_size(RDB.vecpak_encode(&1))}))
315+
{rtx, m, m_rev, l, root_receipts, root_contractstate} = RDB.apply_entry(db, next_entry_trimmed_map,
316316
Application.fetch_env!(:ama, :trainer_pk), Application.fetch_env!(:ama, :trainer_sk), txus,
317317
!!Application.fetch_env!(:ama, :testnet), Map.keys(Application.fetch_env!(:ama, :keys_by_pk))
318318
)
@@ -329,17 +329,14 @@ defmodule FabricGen do
329329
end
330330
rebuild_l_fn = fn(m)->
331331
Enum.map(m, fn(inner)->
332-
if entry.header.height >= 416_00000 do
333-
%{error: IO.iodata_to_binary(inner["error"]), exec_used: IO.iodata_to_binary(inner["exec_used"])}
334-
else
335-
%{error: :"#{IO.iodata_to_binary(inner["error"])}"}
336-
end
332+
%{error: IO.iodata_to_binary(inner["error"]), exec_used: IO.iodata_to_binary(inner["exec_used"])}
337333
end)
338334
end
339335
m = rebuild_m_fn.(m)
340336
m_rev = rebuild_m_fn.(m_rev)
341337
l = rebuild_l_fn.(l)
342338

339+
#IO.inspect {entry.header.height, :erlang.crc32(root_receipts), :erlang.crc32(root_contractstate)}
343340
#IO.inspect Enum.map(m, & Map.put(&1, :key, RocksDB.ascii_dump(&1.key))), limit: 11111111111
344341

345342
#call the exit
@@ -349,11 +346,11 @@ defmodule FabricGen do
349346
#m = m ++ m_exit
350347
#m_rev = m_rev ++ m_exit_rev
351348

352-
mutations_hash = ConsensusKV.hash_mutations(next_entry.header.height, l ++ m)
349+
mutations_hash = RDB.vecpak_encode(l ++ m) |> Blake3.hash()
353350

354351
RocksDB.put("temporal_tip", next_entry.hash, %{rtx: rtx, cf: cf.sysconf})
355352

356-
DB.Entry.apply_into_main_chain(next_entry, mutations_hash, m_rev, l, %{rtx: rtx})
353+
DB.Entry.apply_into_main_chain(next_entry, mutations_hash, m_rev, l, root_receipts, root_contractstate, %{rtx: rtx})
357354
if Application.fetch_env!(:ama, :archival_node) do
358355
DB.Entry.apply_into_main_chain_muts(next_entry.hash, m, %{rtx: rtx})
359356
end
@@ -365,7 +362,7 @@ defmodule FabricGen do
365362
# rtx = RocksDB.transaction(:persistent_term.get({:rocksdb, Fabric}).db)
366363
# :ok = RocksDB.transaction_commit(rtx)
367364
attestations = Enum.map(my_validators, fn(seed)->
368-
attestation = Attestation.sign(seed.seed, next_entry.hash, mutations_hash)
365+
attestation = Attestation.sign(seed.seed, next_entry.hash, next_entry.header.height, mutations_hash, root_receipts, root_contractstate, :binary.copy(<<0>>, 32))
369366
DB.Attestation.put(attestation, Entry.height(next_entry), %{rtx: rtx})
370367
attestation
371368
end)

ex/lib/consensus/models/entry_genesis.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ defmodule EntryGenesis do
129129
Process.put({RocksDB, :ctx}, %{rtx: rtx, cf: cf})
130130
{mutations, _} = BIC.Base.call_exit(%{entry: entry})
131131

132-
mutations_hash = ConsensusKV.hash_mutations(mutations)
133-
attestation = Attestation.sign(sk, entry_signed.hash, mutations_hash)
132+
mutations_hash = RDB.vecpak_encode(mutations) |> Blake3.hash()
133+
attestation = Attestation.sign(sk, entry_signed.hash, 0, mutations_hash, :binary.copy(<<0>>, 32), :binary.copy(<<0>>, 32), :binary.copy(<<0>>, 32))
134134

135135
pop = BlsEx.sign!(sk, pk, BLS12AggSig.dst_pop())
136136

@@ -176,13 +176,13 @@ defmodule EntryGenesis do
176176
rtx = RocksDB.transaction(db)
177177
Process.put({RocksDB, :ctx}, %{rtx: rtx, cf: cf})
178178

179-
mutations_hash = ConsensusKV.hash_mutations([])
180-
attestation = Attestation.sign(sk, entry_signed.hash, mutations_hash)
179+
mutations_hash = RDB.vecpak_encode([]) |> Blake3.hash()
180+
attestation = Attestation.sign(sk, entry_signed.hash, 0, mutations_hash, :binary.copy(<<0>>, 32), :binary.copy(<<0>>, 32), :binary.copy(<<0>>, 32))
181181

182182
pop = BlsEx.sign!(sk, pk, BLS12AggSig.dst_pop())
183183

184184
DB.Entry.insert(entry_signed, %{rtx: rtx})
185-
DB.Entry.apply_into_main_chain(entry_signed, mutations_hash, [], [], %{rtx: rtx})
185+
DB.Entry.apply_into_main_chain(entry_signed, mutations_hash, [], [], "", "", %{rtx: rtx})
186186
RocksDB.put("temporal_tip", entry_signed.hash, %{rtx: rtx, cf: cf.sysconf})
187187
RocksDB.put("rooted_tip", entry_signed.hash, %{rtx: rtx, cf: cf.sysconf})
188188

ex/lib/http/multiserver.ex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,20 @@ defmodule Ama.MultiServer do
219219

220220
r.method == "GET" and String.starts_with?(r.path, "/api/proof/validators/") ->
221221
entry_hash = String.replace(r.path, "/api/proof/validators/", "")
222-
result = API.Proof.proof_validators(entry_hash)
222+
result = API.Proof.validators(entry_hash)
223223
quick_reply(state, result)
224+
r.method == "GET" and String.starts_with?(r.path, "/api/proof/contractstate/") ->
225+
key_value = String.replace(r.path, "/api/proof/contractstate/", "")
226+
result = case :binary.split(key_value, "/") do
227+
[key] -> API.Proof.validators(Base58.decode(key))
228+
[key, value] -> API.Proof.validators(Base58.decode(key), Base58.decode(value))
229+
end
230+
quick_reply(state, result)
231+
r.method == "POST" and String.starts_with?(r.path, "/api/proof/contractstate") ->
232+
{r, vecpak_bin} = Photon.HTTP.read_body_all(state.socket, r)
233+
map = RDB.vecpak_decode(vecpak_bin)
234+
result = API.Proof.validators(map.key, map[:value])
235+
quick_reply(%{state|request: r}, result)
224236

225237
#r.method == "GET" ->
226238
# bin = build_dashboard(state)

ex/native/rdb/src/atoms.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ rustler::atoms! {
5353
missing_stem_other_stem,
5454

5555
tx_cost,
56+
tx_size,
5657

5758
direction,
5859
root,

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,60 @@ pub fn from_flat(coins: i128) -> f64 {
2222
(x * 1e9).round() / 1e9
2323
}
2424

25-
pub fn balance_burnt(env: &crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> i128 {
25+
pub fn balance_burnt(env: &mut crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> i128 {
2626
balance(env, &BURN_ADDRESS, symbol)
2727
}
2828

29-
pub fn balance(env: &crate::consensus::consensus_apply::ApplyEnv, address: &[u8], symbol: &[u8]) -> i128 {
29+
pub fn balance(env: &mut crate::consensus::consensus_apply::ApplyEnv, address: &[u8], symbol: &[u8]) -> i128 {
3030
match kv_get(env, &bcat(&[b"account:", address, b":balance:", symbol])) {
3131
Some(amount) => std::str::from_utf8(&amount).unwrap().parse::<i128>().unwrap_or_else(|_| panic_any("invalid_balance")),
3232
None => 0
3333
}
3434
}
3535

36-
pub fn mintable(env: &crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
36+
pub fn mintable(env: &mut crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
3737
match kv_get(env, &bcat(&[b"coin:", symbol, b":mintable"])).as_deref() {
3838
Some(b"true") => true,
3939
_ => false
4040
}
4141
}
4242

43-
pub fn pausable(env: &crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
43+
pub fn pausable(env: &mut crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
4444
match kv_get(env, &bcat(&[b"coin:", symbol, b":pausable"])).as_deref() {
4545
Some(b"true") => true,
4646
_ => false
4747
}
4848
}
4949

50-
pub fn paused(env: &crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
50+
pub fn paused(env: &mut crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
5151
match kv_get(env, &bcat(&[b"coin:", symbol, b":paused"])).as_deref() {
5252
Some(b"true") => pausable(env, symbol),
5353
_ => false
5454
}
5555
}
5656

57-
pub fn soulbound(env: &crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
57+
pub fn soulbound(env: &mut crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
5858
match kv_get(env, &bcat(&[b"coin:", symbol, b":soulbound"])).as_deref() {
5959
Some(b"true") => true,
6060
_ => false
6161
}
6262
}
6363

64-
pub fn total_supply(env: &crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> i128 {
64+
pub fn total_supply(env: &mut crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> i128 {
6565
match kv_get(env, &bcat(&[b"coin:", symbol, b":totalSupply"])) {
6666
Some(amount) => std::str::from_utf8(&amount).unwrap().parse::<i128>().unwrap_or_else(|_| panic_any("invalid_total_supply")),
6767
None => 0
6868
}
6969
}
7070

71-
pub fn exists(env: &crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
71+
pub fn exists(env: &mut crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8]) -> bool {
7272
match kv_get(env, &bcat(&[b"coin:", symbol, b":totalSupply"])) {
7373
Some(_) => true,
7474
None => false
7575
}
7676
}
7777

78-
pub fn has_permission(env: &crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8], signer: &[u8]) -> bool {
78+
pub fn has_permission(env: &mut crate::consensus::consensus_apply::ApplyEnv, symbol: &[u8], signer: &[u8]) -> bool {
7979
match kv_get(env, &bcat(&[b"coin:", symbol, b":permission"])) {
8080
None => false,
8181
Some(permission_list) => {
@@ -100,7 +100,7 @@ pub fn call_transfer(env: &mut crate::consensus::consensus_apply::ApplyEnv, args
100100
if receiver.len() != 48 { panic_any("invalid_receiver_pk") }
101101
if !(consensus::bls12_381::validate_public_key(receiver) || receiver == &BURN_ADDRESS) { panic_any("invalid_receiver_pk") }
102102
if amount <= 0 { panic_any("invalid_amount") }
103-
if amount > balance(env, env.caller_env.account_caller.as_slice(), &symbol) { panic_any("insufficient_funds") }
103+
if amount > balance(env, &env.caller_env.account_caller.clone(), &symbol) { panic_any("insufficient_funds") }
104104

105105
if paused(env, symbol) { panic_any("paused") }
106106
if soulbound(env, symbol) { panic_any("soulbound") }
@@ -159,7 +159,7 @@ pub fn call_mint(env: &mut crate::consensus::consensus_apply::ApplyEnv, args: Ve
159159
let receiver = args[2].as_slice();
160160
if receiver.len() != 48 { panic_any("invalid_receiver_pk") }
161161

162-
if !has_permission(env, &symbol, env.caller_env.account_caller.as_slice()) { panic_any("no_permissions") }
162+
if !has_permission(env, &symbol, &env.caller_env.account_caller.clone()) { panic_any("no_permissions") }
163163

164164
mint(env, symbol, amount, receiver);
165165
}
@@ -184,7 +184,7 @@ pub fn call_pause(env: &mut crate::consensus::consensus_apply::ApplyEnv, args: V
184184
if direction != b"true" && direction != b"false" { panic_any("invalid_direction") }
185185

186186
if !exists(env, &symbol) { panic_any("symbol_doesnt_exist") }
187-
if !has_permission(env, &symbol, env.caller_env.account_caller.as_slice()) { panic_any("no_permissions") }
187+
if !has_permission(env, &symbol, &env.caller_env.account_caller.clone()) { panic_any("no_permissions") }
188188
if !pausable(env, &symbol) { panic_any("not_pausable") }
189189

190190
kv_put(env, &bcat(&[b"coin:", &symbol, b":paused"]), &direction);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub fn call_submit_sol(env: &mut crate::consensus::consensus_apply::ApplyEnv, ar
213213
kv_increment(env, &bcat(&[b"bic:epoch:solutions_count:", usol.pk.as_slice()]), 1);
214214
}
215215

216-
pub fn kv_get_trainers(env: &crate::consensus::consensus_apply::ApplyEnv, height: u64) -> Vec<Vec<u8>> {
216+
pub fn kv_get_trainers(env: &mut crate::consensus::consensus_apply::ApplyEnv, height: u64) -> Vec<Vec<u8>> {
217217
let height_padded = format!("{:012}", height).into_bytes();
218218
match kv_get_prev_or_first(env, b"bic:epoch:validators:height:", &height_padded) {
219219
None => Vec::new(),
@@ -237,7 +237,7 @@ pub fn kv_get_trainers(env: &crate::consensus::consensus_apply::ApplyEnv, height
237237
}
238238
}
239239

240-
pub fn kv_get_trainers_removed(env: &crate::consensus::consensus_apply::ApplyEnv) -> Vec<Vec<u8>> {
240+
pub fn kv_get_trainers_removed(env: &mut crate::consensus::consensus_apply::ApplyEnv) -> Vec<Vec<u8>> {
241241
let trainers_start = kv_get_trainers(env, env.caller_env.entry_epoch * 100_000);
242242
let trainers_now = kv_get_trainers(env, env.caller_env.entry_height);
243243

0 commit comments

Comments
 (0)