@@ -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
0 commit comments