1
1
use crate :: eth:: RLPBlock ;
2
- use crate :: hash:: hash_to_fields;
3
2
use crate :: transaction:: proof:: { IntermediateMPT , ProofType , RootMPTHeader , TransactionMPT } ;
4
- use crate :: transaction:: PACKED_HASH_LEN ;
5
- use crate :: ByteProofTuple ;
6
3
use crate :: {
7
4
eth:: { extract_child_hashes, BlockData } ,
8
5
utils:: keccak256,
@@ -16,7 +13,7 @@ use rlp::Encodable;
16
13
use std:: collections:: HashMap ;
17
14
18
15
#[ derive( Debug , Default ) ]
19
- pub ( crate ) enum TxFilter {
16
+ pub enum TxFilter {
20
17
// only prove tx that are less than this size in bytes
21
18
BySize ( usize ) ,
22
19
// only prove the tx which have these hashes
@@ -41,28 +38,29 @@ impl TxFilter {
41
38
}
42
39
}
43
40
}
44
- struct TxBlockProver {
45
- data : BlockData ,
46
- policy : TxFilter ,
41
+ pub struct TxBlockProver {
42
+ pub data : BlockData ,
43
+ pub policy : TxFilter ,
47
44
#[ cfg( test) ]
48
- nb_nodes : usize ,
45
+ pub nb_nodes : usize ,
49
46
}
50
47
51
- struct MPTNode {
52
- node_bytes : Vec < u8 > , // RLP byte representation of the node
53
- hash : Vec < u8 > , // its hash
48
+ #[ derive( Debug , PartialEq , Eq , Clone ) ]
49
+ pub struct MPTNode {
50
+ pub node_bytes : Vec < u8 > , // RLP byte representation of the node
51
+ pub hash : Vec < u8 > , // its hash
54
52
// ALL the children of this node if any (zero if leaf for example)
55
- total_nb_children : usize ,
53
+ pub total_nb_children : usize ,
56
54
// expected children hashes - will be filled in with only the hashes that we
57
55
// traverse in the proofs of all tx we are looking at. For the nodes that we don't
58
56
// traverse, their hash may be in children_hashes, and we'll only provide a "null" proof
59
57
// for them.
60
- exp_children : Vec < Vec < u8 > > ,
58
+ pub exp_children : Vec < Vec < u8 > > ,
61
59
// child i : (key, proof) - key needed locate where is the hash of the child in the node
62
- rcvd_children_proofs : Vec < ProverOutput > , // will be filled in
63
- parent_hash : Option < Vec < u8 > > , // indicator to go up one level when this node has been "proven"
60
+ pub rcvd_children_proofs : Vec < ProverOutput > , // will be filled in
61
+ pub parent_hash : Option < Vec < u8 > > , // indicator to go up one level when this node has been "proven"
64
62
/// Used for information about tx in the leaf node proving phase
65
- transaction : Option < Transaction > ,
63
+ pub transaction : Option < Transaction > ,
66
64
}
67
65
impl MPTNode {
68
66
fn is_provable ( & self ) -> bool {
@@ -93,7 +91,8 @@ impl MPTNode {
93
91
}
94
92
}
95
93
96
- struct ProverOutput {
94
+ #[ derive( Debug , PartialEq , Eq , Clone ) ]
95
+ pub struct ProverOutput {
97
96
parent_hash : Option < Vec < u8 > > ,
98
97
// hash of this node
99
98
hash : Vec < u8 > ,
@@ -116,6 +115,7 @@ impl TxBlockProver {
116
115
117
116
pub fn prove ( & mut self ) -> Result < Vec < u8 > > {
118
117
let ( mut trie, leaves_hashes) = self . init_proofs_trie ( ) ;
118
+
119
119
println ! (
120
120
"[+] Built internal trie with {} leaves, and {} nodes in total" ,
121
121
leaves_hashes. len( ) ,
@@ -204,7 +204,9 @@ impl TxBlockProver {
204
204
intermediate_node : node_bytes,
205
205
children_proofs : inner_proofs,
206
206
} ) ;
207
+
207
208
let proof = prover. compute_proof ( ) ?;
209
+
208
210
println ! (
209
211
"[+] OK Valid recursive proof for node hash {}" ,
210
212
hex:: encode( & node_hash)
@@ -232,10 +234,6 @@ impl TxBlockProver {
232
234
transaction,
233
235
} ) ;
234
236
let proof = prover. compute_proof ( ) ?;
235
- println ! (
236
- "[+] OK Valid proof for leaf - hash {}" ,
237
- hex:: encode( & leaf_hash)
238
- ) ;
239
237
Ok ( ProverOutput {
240
238
parent_hash,
241
239
proof,
@@ -246,7 +244,7 @@ impl TxBlockProver {
246
244
// Returns the hashmap filled with the trie info
247
245
// and returns the initial list of nodes's hash, which happen to be leaves, to prove
248
246
#[ allow( clippy:: type_complexity) ]
249
- fn init_proofs_trie ( & mut self ) -> ( HashMap < Vec < u8 > , MPTNode > , Vec < Vec < u8 > > ) {
247
+ pub fn init_proofs_trie ( & mut self ) -> ( HashMap < Vec < u8 > , MPTNode > , Vec < Vec < u8 > > ) {
250
248
// H(node) => { MPTNode() }
251
249
let mut tree = HashMap :: new ( ) ;
252
250
let mut leaves = Vec :: new ( ) ;
@@ -264,10 +262,12 @@ impl TxBlockProver {
264
262
let idx = txr. receipt ( ) . transaction_index ;
265
263
let key = idx. rlp_bytes ( ) . to_vec ( ) ;
266
264
let proof_bytes = self . data . tx_trie . get_proof ( & key) . unwrap ( ) ;
265
+
267
266
let mut child_hash = None ;
268
267
for ( i, node_bytes) in proof_bytes. iter ( ) . rev ( ) . enumerate ( ) {
269
268
let idx_in_path = proof_bytes. len ( ) - 1 - i;
270
269
let hash = keccak256 ( node_bytes) ;
270
+
271
271
if i == 0 {
272
272
leaves. push ( hash. clone ( ) ) ;
273
273
}
@@ -306,6 +306,7 @@ impl TxBlockProver {
306
306
}
307
307
}
308
308
309
+ #[ cfg( test) ]
309
310
mod test {
310
311
use std:: time:: Instant ;
311
312
0 commit comments