Skip to content

Commit 3601ed6

Browse files
committed
epoch 490; switch to bintreev2
1 parent 387afa7 commit 3601ed6

7 files changed

Lines changed: 324 additions & 41 deletions

File tree

ex/lib/consensus/models/entry.ex

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ defmodule Entry do
128128
if !is_list(e.txs), do: throw(%{error: :txs_not_list})
129129
if length(e.txs) > 100, do: throw(%{error: :TEMPORARY_txs_only_100_per_entry})
130130

131+
if !!Application.fetch_env!(:ama, :testnet) or eh.height >= RDBProtocol.forkheight() do
132+
if !is_binary(eh.root_tx), do: throw(%{error: :root_tx_not_binary})
133+
if byte_size(eh.root_tx) != 32, do: throw(%{error: :root_tx_not_256_bits})
134+
if eh.root_tx != root_tx2(Enum.map(e.txs, & &1.hash)), do: throw(%{error: :root_tx_invalid})
135+
136+
if !is_binary(eh.root_validator), do: throw(%{error: :root_validator_not_binary})
137+
if byte_size(eh.root_validator) != 32, do: throw(%{error: :root_validator_not_256_bits})
138+
validators = DB.Chain.validators_for_height(eh.height)
139+
validators_last_change_height = DB.Chain.validators_last_change_height(eh.height)
140+
if eh.root_validator != root_validator2(validators, validators_last_change_height), do: throw(%{error: :root_validator_invalid})
141+
else
131142
if !is_binary(eh.root_tx), do: throw(%{error: :root_tx_not_binary})
132143
if byte_size(eh.root_tx) != 32, do: throw(%{error: :root_tx_not_256_bits})
133144
if eh.root_tx != root_tx(Enum.map(e.txs, & &1.hash)), do: throw(%{error: :root_tx_invalid})
@@ -137,6 +148,7 @@ defmodule Entry do
137148
validators = DB.Chain.validators_for_height(eh.height)
138149
validators_last_change_height = DB.Chain.validators_last_change_height(eh.height)
139150
if eh.root_validator != root_validator(validators, validators_last_change_height), do: throw(%{error: :root_validator_invalid})
151+
end
140152

141153
is_special_meeting_block = !!e[:mask]
142154
steam = Task.async_stream(e.txs, fn txu ->
@@ -218,6 +230,22 @@ defmodule Entry do
218230
validators = DB.Chain.validators_for_height(next_height)
219231
validators_last_change_height = DB.Chain.validators_last_change_height(next_height)
220232

233+
if !!Application.fetch_env!(:ama, :testnet) or next_height >= RDBProtocol.forkheight() do
234+
%{
235+
header: %{
236+
slot: cur_entry.header.slot + 1,
237+
height: next_height,
238+
prev_slot: cur_entry.header.slot,
239+
prev_hash: cur_entry.hash,
240+
dr: dr,
241+
vr: vr,
242+
signer: pk,
243+
root_tx: root_tx2(Enum.map(txus, & &1.hash)),
244+
root_validator: root_validator2(validators, validators_last_change_height)
245+
},
246+
txs: txus
247+
}
248+
else
221249
%{
222250
header: %{
223251
slot: cur_entry.header.slot + 1,
@@ -232,6 +260,7 @@ defmodule Entry do
232260
},
233261
txs: txus
234262
}
263+
end
235264
end
236265

237266
def sign(seed, entry) do
@@ -263,6 +292,16 @@ defmodule Entry do
263292
by_index_hash ++ [{"count", "#{length(hashes)}"}]
264293
end
265294

295+
def root_tx2(hashes) do
296+
RDB.bintree_root2(root_tx_build2(hashes))
297+
end
298+
def root_tx_build2(hashes) do
299+
by_index_hash = Enum.flat_map(Enum.with_index(hashes), fn{hash, index}->
300+
[{nil, hash, "#{index}"}]
301+
end)
302+
by_index_hash ++ [{nil, "count", "#{length(hashes)}"}]
303+
end
304+
266305
def root_validator(validator_pks, last_change_height) do
267306
RDB.bintree_root(root_validator_build(validator_pks, last_change_height))
268307
end
@@ -274,6 +313,17 @@ defmodule Entry do
274313
kvs ++ [{"hash", :crypto.hash(:sha256, Enum.join(validator_pks))}, {"last_change_height", "#{last_change_height}"}]
275314
end
276315

316+
def root_validator2(validator_pks, last_change_height) do
317+
RDB.bintree_root2(root_validator_build2(validator_pks, last_change_height))
318+
end
319+
def root_validator_build2(validator_pks, last_change_height) do
320+
by_index_hash = Enum.flat_map(Enum.with_index(validator_pks), fn{hash, index}->
321+
[{nil, hash, "#{index}"}]
322+
end)
323+
kvs = by_index_hash ++ [{nil, "count", "#{length(validator_pks)}"}]
324+
kvs ++ [{nil, "hash", :crypto.hash(:sha256, Enum.join(validator_pks))}, {nil, "last_change_height", "#{last_change_height}"}]
325+
end
326+
277327
def root_block() do
278328
#TODO for future
279329
#proof of inclusion for previous blocks
@@ -282,8 +332,11 @@ defmodule Entry do
282332
def proof_tx_included(entry_hash, tx_hash) do
283333
entry = DB.Entry.by_hash(entry_hash)
284334
tx_hashes = Enum.map(entry.txs, & &1.hash)
285-
kvs = root_tx_build(tx_hashes)
286-
RDB.bintree_root_prove(kvs, tx_hash)
335+
kvs = root_tx_build2(tx_hashes)
336+
RDB.bintree_root_prove2(kvs, nil, tx_hash)
337+
338+
#p = Entry.proof_tx_included Base58.decode("BzF3P2a5gMpQ2FwAuiftHC85BnDfxcmYXPfW95BmNnXU"), Base58.decode("3ySbVoruCQsGb9K1XkRbQdm5WSuxnEoVCugAUj6qouS6")
339+
#RDB.bintree_root_verify2(p, nil, Base58.decode("3ySbVoruCQsGb9K1XkRbQdm5WSuxnEoVCugAUj6qouS6"), "0")
287340
end
288341

289342
def proof_validators(entry_hash) do

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
use crate::consensus::bic::coin;
22
use crate::consensus::consensus_kv;
33

4-
pub const FORKHEIGHT: u64 = 435_00000;
5-
//pub const FORKHEIGHT: u64 = 0;
4+
pub const FORKHEIGHT: u64 = 490_00000;
5+
//pub const FORKHEIGHT_TESTNET: u64 = 75_00000;
6+
pub const FORKHEIGHT_TESTNET: u64 = 100;
7+
8+
pub fn forkheight(env: &crate::consensus::consensus_apply::ApplyEnv) -> u64 {
9+
if env.testnet {
10+
FORKHEIGHT_TESTNET
11+
} else {
12+
FORKHEIGHT
13+
}
14+
}
615

716
pub const AMA_1_DOLLAR: i128 = 1_000_000_000;
817
pub const AMA_10_CENT: i128 = 100_000_000;
@@ -25,15 +34,15 @@ pub const COST_PER_DB_WRITE_BYTE: i128 = 250 * 10;
2534
pub const COST_PER_DB_WRITE_BYTE2: i128 = 250;
2635

2736
pub fn cost_db_read_byte(env: &crate::consensus::consensus_apply::ApplyEnv) -> i128 {
28-
if env.testnet {
37+
if env.caller_env.entry_height >= forkheight(env) {
2938
COST_PER_DB_READ_BYTE2
3039
} else {
3140
COST_PER_DB_READ_BYTE
3241
}
3342
}
3443

3544
pub fn cost_db_write_byte(env: &crate::consensus::consensus_apply::ApplyEnv) -> i128 {
36-
if env.testnet {
45+
if env.caller_env.entry_height >= forkheight(env) {
3746
COST_PER_DB_WRITE_BYTE2
3847
} else {
3948
COST_PER_DB_WRITE_BYTE
@@ -61,15 +70,6 @@ pub const WASM_MAX_GLOBALS: u32 = 100;
6170
pub const WASM_MAX_EXPORTS: u32 = 50;
6271
pub const WASM_MAX_IMPORTS: u32 = 50;
6372

64-
#[derive(Clone, Debug)]
65-
pub struct ExecutionReceipt {
66-
pub txid: Vec<u8>,
67-
pub success: bool,
68-
pub result: Vec<u8>,
69-
pub exec_used: Vec<u8>,
70-
pub logs: Vec<Vec<u8>>,
71-
}
72-
7373
pub fn pay_cost(env: &mut crate::consensus::consensus_apply::ApplyEnv, cost: i128) {
7474
consensus_kv::kv_increment(env, &crate::bcat(&[b"account:", &env.caller_env.account_origin, b":balance:AMA"]), -cost);
7575
// Increment validator / burn

0 commit comments

Comments
 (0)