@@ -21,19 +21,12 @@ impl<A: AccountsTreeLeave> AccountsTree<A> {
21
21
22
22
pub fn new ( env : Environment ) -> Self {
23
23
let db = env. open_database ( Self :: DB_NAME . to_string ( ) ) ;
24
- let tree = AccountsTree {
25
- db,
26
- _account : PhantomData ,
27
- } ;
24
+ let tree = AccountsTree { db, _account : PhantomData } ;
28
25
29
26
let mut txn = WriteTransaction :: new ( & env) ;
30
27
if tree. get_root ( & txn) . is_none ( ) {
31
28
let root = AddressNibbles :: empty ( ) ;
32
- txn. put_reserve (
33
- & tree. db ,
34
- & root,
35
- & AccountsTreeNode :: < A > :: new_branch ( root. clone ( ) , NO_CHILDREN ) ,
36
- ) ;
29
+ txn. put_reserve ( & tree. db , & root, & AccountsTreeNode :: < A > :: new_branch ( root. clone ( ) , NO_CHILDREN ) ) ;
37
30
}
38
31
txn. commit ( ) ;
39
32
tree
@@ -69,12 +62,11 @@ impl<A: AccountsTreeLeave> AccountsTree<A> {
69
62
txn. put_reserve ( & self . db , new_child. prefix ( ) , & new_child) ;
70
63
71
64
// Insert the new parent node.
72
- let new_parent =
73
- AccountsTreeNode :: < A > :: new_branch ( node_prefix. common_prefix ( & prefix) , NO_CHILDREN )
74
- . with_child ( & node_prefix, Blake2bHash :: default ( ) )
75
- . unwrap ( )
76
- . with_child ( new_child. prefix ( ) , Blake2bHash :: default ( ) )
77
- . unwrap ( ) ;
65
+ let new_parent = AccountsTreeNode :: < A > :: new_branch ( node_prefix. common_prefix ( & prefix) , NO_CHILDREN )
66
+ . with_child ( & node_prefix, Blake2bHash :: default ( ) )
67
+ . unwrap ( )
68
+ . with_child ( new_child. prefix ( ) , Blake2bHash :: default ( ) )
69
+ . unwrap ( ) ;
78
70
txn. put_reserve ( & self . db , new_parent. prefix ( ) , & new_parent) ;
79
71
80
72
return self . update_keys_batch ( txn, new_parent. prefix ( ) . clone ( ) , root_path) ;
@@ -112,20 +104,13 @@ impl<A: AccountsTreeLeave> AccountsTree<A> {
112
104
// If no matching child exists, add a new child account node to the current node.
113
105
let child = AccountsTreeNode :: < A > :: new_terminal ( prefix, account) ;
114
106
txn. put_reserve ( & self . db , child. prefix ( ) , & child) ;
115
- let node = node
116
- . with_child ( child. prefix ( ) , Blake2bHash :: default ( ) )
117
- . unwrap ( ) ;
107
+ let node = node. with_child ( child. prefix ( ) , Blake2bHash :: default ( ) ) . unwrap ( ) ;
118
108
txn. put_reserve ( & self . db , node. prefix ( ) , & node) ;
119
109
120
110
self . update_keys_batch ( txn, node_prefix, root_path)
121
111
}
122
112
123
- fn prune_batch (
124
- & self ,
125
- txn : & mut WriteTransaction ,
126
- prefix : AddressNibbles ,
127
- mut root_path : Vec < AccountsTreeNode < A > > ,
128
- ) {
113
+ fn prune_batch ( & self , txn : & mut WriteTransaction , prefix : AddressNibbles , mut root_path : Vec < AccountsTreeNode < A > > ) {
129
114
// Walk along the rootPath towards the root node starting with the
130
115
// immediate predecessor of the node specified by 'prefix'.
131
116
let mut tmp_prefix = prefix;
@@ -153,20 +138,13 @@ impl<A: AccountsTreeLeave> AccountsTree<A> {
153
138
}
154
139
}
155
140
156
- fn update_keys_batch (
157
- & self ,
158
- txn : & mut WriteTransaction ,
159
- prefix : AddressNibbles ,
160
- mut root_path : Vec < AccountsTreeNode < A > > ,
161
- ) {
141
+ fn update_keys_batch ( & self , txn : & mut WriteTransaction , prefix : AddressNibbles , mut root_path : Vec < AccountsTreeNode < A > > ) {
162
142
// Walk along the rootPath towards the root node starting with the
163
143
// immediate predecessor of the node specified by 'prefix'.
164
144
let mut tmp_prefix = & prefix;
165
145
let mut node;
166
146
while let Some ( path_node) = root_path. pop ( ) {
167
- node = path_node
168
- . with_child ( tmp_prefix, Blake2bHash :: default ( ) )
169
- . unwrap ( ) ;
147
+ node = path_node. with_child ( tmp_prefix, Blake2bHash :: default ( ) ) . unwrap ( ) ;
170
148
txn. put_reserve ( & self . db , node. prefix ( ) , & node) ;
171
149
tmp_prefix = node. prefix ( ) ;
172
150
}
@@ -206,13 +184,7 @@ impl<A: AccountsTreeLeave> AccountsTree<A> {
206
184
AccountsProof :: new ( nodes)
207
185
}
208
186
209
- fn get_accounts_proof_rec (
210
- & self ,
211
- txn : & Transaction ,
212
- node : & AccountsTreeNode < A > ,
213
- prefixes : & [ AddressNibbles ] ,
214
- nodes : & mut Vec < AccountsTreeNode < A > > ,
215
- ) -> bool {
187
+ fn get_accounts_proof_rec ( & self , txn : & Transaction , node : & AccountsTreeNode < A > , prefixes : & [ AddressNibbles ] , nodes : & mut Vec < AccountsTreeNode < A > > ) -> bool {
216
188
// For each prefix, descend the tree individually.
217
189
let mut include_node = false ;
218
190
let mut i = 0 ;
@@ -248,8 +220,7 @@ impl<A: AccountsTreeLeave> AccountsTree<A> {
248
220
// we continue from there in the next iteration.
249
221
i = j;
250
222
}
251
- include_node = self . get_accounts_proof_rec ( txn, & child_node, & sub_prefixes, nodes)
252
- || include_node;
223
+ include_node = self . get_accounts_proof_rec ( txn, & child_node, & sub_prefixes, nodes) || include_node;
253
224
} else {
254
225
// No child node exists with the requested prefix. Include the current node to prove the absence of the requested account.
255
226
include_node = true ;
@@ -267,40 +238,24 @@ impl<A: AccountsTreeLeave> AccountsTree<A> {
267
238
}
268
239
269
240
pub fn get ( & self , txn : & Transaction , address : & Address ) -> Option < A > {
270
- if let AccountsTreeNode :: TerminalNode { account, .. } =
271
- txn. get ( & self . db , & AddressNibbles :: from ( address) ) ?
272
- {
241
+ if let AccountsTreeNode :: TerminalNode { account, .. } = txn. get ( & self . db , & AddressNibbles :: from ( address) ) ? {
273
242
return Some ( account) ;
274
243
}
275
244
None
276
245
}
277
246
278
- pub ( crate ) fn get_chunk (
279
- & self ,
280
- txn : & Transaction ,
281
- start : & str ,
282
- size : usize ,
283
- ) -> Option < AccountsTreeChunk < A > > {
284
- let mut chunk =
285
- self . get_terminal_nodes ( txn, & AddressNibbles :: from_str ( start) . ok ( ) ?, size) ?;
247
+ pub ( crate ) fn get_chunk ( & self , txn : & Transaction , start : & str , size : usize ) -> Option < AccountsTreeChunk < A > > {
248
+ let mut chunk = self . get_terminal_nodes ( txn, & AddressNibbles :: from_str ( start) . ok ( ) ?, size) ?;
286
249
let last_node = chunk. pop ( ) ;
287
250
let proof = if let Some ( node) = last_node {
288
251
self . get_accounts_proof ( txn, & [ node. prefix ( ) . to_address ( ) ?] )
289
252
} else {
290
- self . get_accounts_proof (
291
- txn,
292
- & [ Address :: from_str ( "ffffffffffffffffffffffffffffffffffffffff" ) . ok ( ) ?] ,
293
- )
253
+ self . get_accounts_proof ( txn, & [ Address :: from_str ( "ffffffffffffffffffffffffffffffffffffffff" ) . ok ( ) ?] )
294
254
} ;
295
255
Some ( AccountsTreeChunk :: new ( chunk, proof) )
296
256
}
297
257
298
- pub ( crate ) fn get_terminal_nodes (
299
- & self ,
300
- txn : & Transaction ,
301
- start : & AddressNibbles ,
302
- size : usize ,
303
- ) -> Option < Vec < AccountsTreeNode < A > > > {
258
+ pub ( crate ) fn get_terminal_nodes ( & self , txn : & Transaction , start : & AddressNibbles , size : usize ) -> Option < Vec < AccountsTreeNode < A > > > {
304
259
let mut vec = Vec :: new ( ) ;
305
260
let mut stack = Vec :: new ( ) ;
306
261
stack. push ( self . get_root ( txn) ?) ;
@@ -348,18 +303,15 @@ mod tests {
348
303
349
304
#[ test]
350
305
fn it_can_create_valid_chunk ( ) {
351
- let address1 =
352
- Address :: from ( & hex:: decode ( "0000000000000000000000000000000000000000" ) . unwrap ( ) [ ..] ) ;
306
+ let address1 = Address :: from ( & hex:: decode ( "0000000000000000000000000000000000000000" ) . unwrap ( ) [ ..] ) ;
353
307
let account1 = Account :: Basic ( account:: BasicAccount {
354
308
balance : Coin :: try_from ( 5 ) . unwrap ( ) ,
355
309
} ) ;
356
- let address2 =
357
- Address :: from ( & hex:: decode ( "1000000000000000000000000000000000000000" ) . unwrap ( ) [ ..] ) ;
310
+ let address2 = Address :: from ( & hex:: decode ( "1000000000000000000000000000000000000000" ) . unwrap ( ) [ ..] ) ;
358
311
let account2 = Account :: Basic ( account:: BasicAccount {
359
312
balance : Coin :: try_from ( 55 ) . unwrap ( ) ,
360
313
} ) ;
361
- let address3 =
362
- Address :: from ( & hex:: decode ( "1200000000000000000000000000000000000000" ) . unwrap ( ) [ ..] ) ;
314
+ let address3 = Address :: from ( & hex:: decode ( "1200000000000000000000000000000000000000" ) . unwrap ( ) [ ..] ) ;
363
315
let account3 = Account :: Basic ( account:: BasicAccount {
364
316
balance : Coin :: try_from ( 55555555 ) . unwrap ( ) ,
365
317
} ) ;
0 commit comments