@@ -7,21 +7,26 @@ use crate::foldable::Fold;
77use crate :: foldable:: Foldable ;
88use crate :: foldable:: NodeFold ;
99use crate :: hash:: Hash ;
10+ use crate :: tree:: Tree ;
11+
12+ #[ derive( Debug , Clone , PartialEq ) ]
13+ pub struct MerkleTreeLeafData {
14+ pub hash : Hash ,
15+ pub access_info : bool ,
16+ pub data : Vec < u8 > ,
17+ }
18+
19+ #[ derive( Debug , Clone , PartialEq ) ]
20+ pub struct MerkleTreeNodeData {
21+ pub hash : Hash ,
22+ }
1023
1124/// A variable-width Merkle tree with access metadata for leaves.
1225///
1326/// Values of this type are produced by the proof-generating backend to capture
1427/// a snapshot of the machine state along with access information for leaves
1528/// which hold data that was used in a particular evaluation step.
16- #[ derive( Debug , Clone ) ]
17- pub enum MerkleTree {
18- Leaf {
19- hash : Hash ,
20- access_info : bool ,
21- data : Vec < u8 > ,
22- } ,
23- Node ( Hash , Vec < Self > ) ,
24- }
29+ pub type MerkleTree = Tree < MerkleTreeLeafData , MerkleTreeNodeData > ;
2530
2631impl MerkleTree {
2732 /// Returns the precalculated root hash of the node.
@@ -41,27 +46,37 @@ impl MerkleTree {
4146 /// ```
4247 pub fn root_hash ( & self ) -> Hash {
4348 match self {
44- Self :: Node ( hash, _) => * hash,
45- Self :: Leaf { hash, .. } => * hash,
49+ Self :: Node {
50+ data : MerkleTreeNodeData { hash } ,
51+ ..
52+ } => * hash,
53+ Self :: Leaf {
54+ data : MerkleTreeLeafData { hash, .. } ,
55+ } => * hash,
4656 }
4757 }
4858
4959 /// Creates a merkle tree which is a single leaf
5060 pub fn make_merkle_leaf ( data : Vec < u8 > , access_info : bool ) -> Self {
5161 let hash = Hash :: blake3_hash_bytes ( & data) ;
5262 MerkleTree :: Leaf {
53- hash,
54- access_info,
55- data,
63+ data : MerkleTreeLeafData {
64+ hash,
65+ access_info,
66+ data,
67+ } ,
5668 }
5769 }
5870
5971 /// Takes a list of children nodes and creates a
6072 /// new parent node on top of them.
6173 pub fn make_merkle_node ( children : Vec < Self > ) -> Self {
6274 let children_hashes = children. iter ( ) . map ( |t| t. root_hash ( ) ) ;
63- let node_hash = Hash :: combine ( children_hashes) ;
64- MerkleTree :: Node ( node_hash, children)
75+ let hash = Hash :: combine ( children_hashes) ;
76+ MerkleTree :: Node {
77+ data : MerkleTreeNodeData { hash } ,
78+ children,
79+ }
6580 }
6681
6782 /// Recomputes the hashes for the whole tree
@@ -73,8 +88,18 @@ impl MerkleTree {
7388
7489 while let Some ( node) = deque. pop_front ( ) {
7590 let is_valid_hash = match node {
76- Self :: Leaf { hash, data, .. } => & Hash :: blake3_hash_bytes ( data) == hash,
77- Self :: Node ( hash, children) => {
91+ Self :: Leaf {
92+ data :
93+ MerkleTreeLeafData {
94+ hash,
95+ data : node_data,
96+ ..
97+ } ,
98+ } => & Hash :: blake3_hash_bytes ( node_data) == hash,
99+ Self :: Node {
100+ data : MerkleTreeNodeData { hash } ,
101+ children,
102+ } => {
78103 let children_hashes: Vec < Hash > = children
79104 . iter ( )
80105 . map ( |child| {
0 commit comments