@@ -58,6 +58,18 @@ impl<H: Hasher> MerklePath<H> {
5858 pub fn new ( path : Vec < H :: Hash > ) -> Self {
5959 Self ( path)
6060 }
61+
62+ pub fn serialize ( & self ) -> Option < Vec < u8 > > {
63+ let mut serialized = vec ! [ ] ;
64+ let proof_size: u8 = self . 0 . len ( ) . try_into ( ) . ok ( ) ?;
65+ serialized. extend_from_slice ( & proof_size. to_be_bytes ( ) ) ;
66+
67+ for node in self . 0 . iter ( ) {
68+ serialized. extend_from_slice ( node. as_ref ( ) ) ;
69+ }
70+
71+ Some ( serialized)
72+ }
6173}
6274
6375/// A MerkleAccumulator maintains a Merkle Tree.
@@ -86,11 +98,12 @@ pub struct MerkleAccumulator<H: Hasher = Keccak256> {
8698// TODO: This code does not belong to MerkleAccumulator, we should be using the wire data types in
8799// calling code to wrap this value.
88100impl < ' a , H : Hasher + ' a > MerkleAccumulator < H > {
89- pub fn serialize ( & self , storage : u32 ) -> Vec < u8 > {
101+ pub fn serialize ( & self , slot : u64 , ring_size : u32 ) -> Vec < u8 > {
90102 let mut serialized = vec ! [ ] ;
91103 serialized. extend_from_slice ( 0x41555756u32 . to_be_bytes ( ) . as_ref ( ) ) ;
92104 serialized. extend_from_slice ( 0u8 . to_be_bytes ( ) . as_ref ( ) ) ;
93- serialized. extend_from_slice ( storage. to_be_bytes ( ) . as_ref ( ) ) ;
105+ serialized. extend_from_slice ( slot. to_be_bytes ( ) . as_ref ( ) ) ;
106+ serialized. extend_from_slice ( ring_size. to_be_bytes ( ) . as_ref ( ) ) ;
94107 serialized. extend_from_slice ( self . root . as_ref ( ) ) ;
95108 serialized
96109 }
@@ -146,7 +159,7 @@ impl<H: Hasher> MerkleAccumulator<H> {
146159 // Filling the leaf hashes
147160 for i in 0 ..( 1 << depth) {
148161 if i < items. len ( ) {
149- tree[ ( 1 << depth) + i] = hash_leaf :: < H > ( items[ i] . as_ref ( ) ) ;
162+ tree[ ( 1 << depth) + i] = hash_leaf :: < H > ( items[ i] ) ;
150163 } else {
151164 tree[ ( 1 << depth) + i] = hash_null :: < H > ( ) ;
152165 }
@@ -171,7 +184,7 @@ impl<H: Hasher> MerkleAccumulator<H> {
171184 fn find_path ( & self , mut index : usize ) -> MerklePath < H > {
172185 let mut path = Vec :: new ( ) ;
173186 while index > 1 {
174- path. push ( self . nodes [ index ^ 1 ] . clone ( ) ) ;
187+ path. push ( self . nodes [ index ^ 1 ] ) ;
175188 index /= 2 ;
176189 }
177190 MerklePath :: new ( path)
@@ -369,7 +382,7 @@ mod test {
369382 // Accumulate into a 2 level tree.
370383 let accumulator = MerkleAccumulator :: < Keccak256 > :: from_set ( set. into_iter ( ) ) . unwrap ( ) ;
371384 let proof = accumulator. prove ( & item_a) . unwrap ( ) ;
372- assert ! ( accumulator. check( proof. clone ( ) , & item_a) ) ;
385+ assert ! ( accumulator. check( proof, & item_a) ) ;
373386
374387 // We now have a 2 level tree with 4 nodes:
375388 //
@@ -399,10 +412,10 @@ mod test {
399412 let faulty_accumulator = MerkleAccumulator :: < Keccak256 > {
400413 root : accumulator. root ,
401414 nodes : vec ! [
402- accumulator. nodes[ 0 ] . clone ( ) ,
403- accumulator. nodes[ 1 ] . clone ( ) , // Root Stays the Same
404- accumulator. nodes[ 2 ] . clone ( ) , // Left node hash becomes a leaf.
405- accumulator. nodes[ 3 ] . clone ( ) , // Right node hash becomes a leaf.
415+ accumulator. nodes[ 0 ] ,
416+ accumulator. nodes[ 1 ] , // Root Stays the Same
417+ accumulator. nodes[ 2 ] , // Left node hash becomes a leaf.
418+ accumulator. nodes[ 3 ] , // Right node hash becomes a leaf.
406419 ] ,
407420 } ;
408421
@@ -415,13 +428,13 @@ mod test {
415428 . concat ( ) ;
416429
417430 // Confirm our combined hash existed as a node pair in the original tree.
418- assert_eq ! ( hash_leaf:: <Keccak256 >( & fake_leaf_A) , accumulator. nodes[ 2 ] ) ;
431+ assert_eq ! ( hash_leaf:: <Keccak256 >( fake_leaf_A) , accumulator. nodes[ 2 ] ) ;
419432
420433 // Now we can try and prove leaf membership in the faulty accumulator. NOTE: this should
421434 // fail but to confirm that the test is actually correct you can remove the PREFIXES from
422435 // the hash functions and this test will erroneously pass.
423- let proof = faulty_accumulator. prove ( & fake_leaf_A) . unwrap ( ) ;
424- assert ! ( faulty_accumulator. check( proof, & fake_leaf_A) ) ;
436+ let proof = faulty_accumulator. prove ( fake_leaf_A) . unwrap ( ) ;
437+ assert ! ( faulty_accumulator. check( proof, fake_leaf_A) ) ;
425438 }
426439
427440 proptest ! {
0 commit comments