1- use std:: { array , collections:: BTreeMap , ops :: DerefMut } ;
1+ use std:: collections:: BTreeMap ;
22
33use alloy_primitives:: B256 ;
44use anyhow:: Result ;
@@ -7,53 +7,73 @@ use ssz::{Decode, Encode};
77
88use crate :: {
99 committer:: DEFAULT_COMMITER ,
10- constants:: VERKLE_NODE_WIDTH ,
1110 utils:: { b256_to_fr, fr_to_b256} ,
1211 Db , TrieKey , TrieValue ,
1312} ;
1413
15- use super :: { node:: NodeTrait , Node } ;
14+ use super :: { node:: NodeTrait , LeafNode , Node } ;
1615
1716pub struct BranchNode {
18- values : Box < [ Node ; VERKLE_NODE_WIDTH ] > ,
19- cp : Element ,
17+ values : BTreeMap < u8 , Node > ,
18+ commitment : Element ,
2019}
2120
2221impl BranchNode {
2322 pub fn new ( ) -> Self {
2423 Self {
25- values : array :: from_fn ( |_| Node :: Empty ) . into ( ) ,
26- cp : Element :: zero ( ) ,
24+ values : BTreeMap :: new ( ) ,
25+ commitment : Element :: zero ( ) ,
2726 }
2827 }
2928
30- pub fn set ( & mut self , index : usize , node : Node ) {
31- let node_at_index = & mut self . values [ index] ;
32- let pre_commitment = node_at_index. commit ( ) ;
33- * node_at_index = node;
34- let post_commitment = node_at_index. commit ( ) ;
35- self . cp += DEFAULT_COMMITER . scalar_mul ( index, post_commitment - pre_commitment) ;
29+ pub fn set ( & mut self , index : u8 , node : Node ) {
30+ let old_node = self . values . insert ( index, node) ;
31+ self . update_commitment (
32+ index,
33+ old_node
34+ . map ( |node| node. hash_commitment ( ) )
35+ . unwrap_or_default ( ) ,
36+ ) ;
3637 }
3738
38- pub ( super ) fn get_mut ( & mut self , index : usize ) -> & mut Node {
39- & mut self . values [ index]
39+ pub ( super ) fn get_mut ( & mut self , index : u8 ) -> Option < & mut Node > {
40+ self . values . get_mut ( & index)
4041 }
4142
4243 pub fn insert ( & mut self , depth : usize , key : TrieKey , value : TrieValue , db : & Db ) -> Result < ( ) > {
43- let index = key[ depth] as usize ;
44- let node = & mut self . values [ index] ;
45- let pre_commitment = node. commit ( ) ;
46- node. insert ( depth + 1 , key, value, db) ?;
47- let post_commitment = node. commit ( ) ;
48- self . cp += DEFAULT_COMMITER . scalar_mul ( index, post_commitment - pre_commitment) ;
44+ let index = key[ depth] ;
45+ let pre_commitment = self . get_child_commit ( index) ;
46+ match self . values . get_mut ( & index) {
47+ Some ( node) => {
48+ node. insert ( depth + 1 , key, value, db) ?;
49+ node. hash_commitment_mut ( ) ;
50+ }
51+ None => {
52+ self . values
53+ . insert ( index, Node :: Leaf ( LeafNode :: new_for_key_value ( & key, value) ) ) ;
54+ }
55+ } ;
56+ self . update_commitment ( index, pre_commitment) ;
4957 Ok ( ( ) )
5058 }
5159
60+ fn get_child_commit ( & mut self , index : u8 ) -> Fr {
61+ self . values
62+ . get_mut ( & index)
63+ . map ( |node| node. hash_commitment_mut ( ) )
64+ . unwrap_or_default ( )
65+ }
66+
67+ fn update_commitment ( & mut self , index : u8 , pre_commitment : Fr ) {
68+ let post_commitment = self . get_child_commit ( index) ;
69+ self . commitment += DEFAULT_COMMITER . scalar_mul ( index as usize , post_commitment - pre_commitment) ;
70+ }
71+
5272 pub fn write_and_commit ( & mut self , db : & mut Db ) -> Result < Fr > {
53- for node in self . values . deref_mut ( ) {
73+ for ( _ , node) in self . values . iter_mut ( ) {
5474 node. write_and_commit ( db) ?;
5575 }
56- Ok ( self . commit ( ) )
76+ Ok ( self . hash_commitment_mut ( ) )
5777 }
5878}
5979
@@ -65,7 +85,7 @@ impl Default for BranchNode {
6585
6686impl NodeTrait for BranchNode {
6787 fn hash_commitment ( & self ) -> Fr {
68- self . cp . map_to_scalar_field ( )
88+ self . commitment . map_to_scalar_field ( )
6989 }
7090}
7191
@@ -78,14 +98,7 @@ impl Encode for BranchNode {
7898 let commitments: BTreeMap < u8 , B256 > = self
7999 . values
80100 . iter ( )
81- . enumerate ( )
82- . filter_map ( |( index, node) | {
83- if node. is_empty ( ) {
84- None
85- } else {
86- Some ( ( index as u8 , fr_to_b256 ( & node. hash_commitment ( ) ) ) )
87- }
88- } )
101+ . map ( |( index, node) | ( * index, fr_to_b256 ( & node. hash_commitment ( ) ) ) )
89102 . collect ( ) ;
90103 commitments. ssz_append ( buf) ;
91104 }
@@ -106,22 +119,19 @@ impl Decode for BranchNode {
106119
107120 fn from_ssz_bytes ( bytes : & [ u8 ] ) -> Result < Self , ssz:: DecodeError > {
108121 let commitments = BTreeMap :: < u8 , B256 > :: from_ssz_bytes ( bytes) ?;
109- let commitments: BTreeMap < usize , Fr > = commitments
122+
123+ let values = commitments
110124 . iter ( )
111- . map ( |( index, commitment ) | ( * index as usize , b256_to_fr ( commitment ) ) )
125+ . map ( |( index, c ) | ( * index, Node :: Commitment ( b256_to_fr ( c ) ) ) )
112126 . collect ( ) ;
113127
114- let values = array :: from_fn ( |i| {
128+ let cp = DEFAULT_COMMITER . commit_sparse (
115129 commitments
116- . get ( & i)
117- . map ( |c| Node :: Commitment ( * c) )
118- . unwrap_or_else ( || Node :: Empty )
119- } ) ;
120- let cp = DEFAULT_COMMITER . commit_sparse ( commitments. into_iter ( ) . collect ( ) ) ;
121-
122- Ok ( Self {
123- values : values. into ( ) ,
124- cp,
125- } )
130+ . iter ( )
131+ . map ( |( index, commitment) | ( * index as usize , b256_to_fr ( commitment) ) )
132+ . collect ( ) ,
133+ ) ;
134+
135+ Ok ( Self { values, commitment : cp } )
126136 }
127137}
0 commit comments