31
31
//! efficient by removing the need to track which side each intermediate hash is concatenated on.
32
32
//!
33
33
//! If the number of leaves is not even, last leaf (hash of) is promoted to the upper layer.
34
+ #[ cfg( not( feature = "std" ) ) ]
35
+ extern crate alloc;
36
+ #[ cfg( not( feature = "std" ) ) ]
37
+ use alloc:: vec;
38
+ #[ cfg( not( feature = "std" ) ) ]
39
+ use alloc:: vec:: Vec ;
34
40
35
- pub use sp_runtime:: traits:: Keccak256 ;
36
- use sp_runtime:: { app_crypto:: sp_core, sp_std, traits:: Hash as HashT } ;
37
- use sp_std:: { vec, vec:: Vec } ;
38
-
39
- use beefy_primitives:: mmr:: { BeefyAuthoritySet , BeefyNextAuthoritySet } ;
41
+ use hash_db:: Hasher ;
40
42
41
43
/// Construct a root hash of a Binary Merkle Tree created from given leaves.
42
44
///
43
45
/// See crate-level docs for details about Merkle Tree construction.
44
46
///
45
47
/// In case an empty list of leaves is passed the function returns a 0-filled hash.
46
- pub fn merkle_root < H , I > ( leaves : I ) -> H :: Output
48
+ pub fn merkle_root < H , I > ( leaves : I ) -> H :: Out
47
49
where
48
- H : HashT ,
49
- H :: Output : Default + AsRef < [ u8 ] > + PartialOrd ,
50
+ H : Hasher ,
51
+ H :: Out : Default + AsRef < [ u8 ] > + PartialOrd ,
50
52
I : IntoIterator ,
51
53
I :: Item : AsRef < [ u8 ] > ,
52
54
{
53
- let iter = leaves. into_iter ( ) . map ( |l| <H as HashT >:: hash ( l. as_ref ( ) ) ) ;
55
+ let iter = leaves. into_iter ( ) . map ( |l| <H as Hasher >:: hash ( l. as_ref ( ) ) ) ;
54
56
merkelize :: < H , _ , _ > ( iter, & mut ( ) ) . into ( )
55
57
}
56
58
57
- fn merkelize < H , V , I > ( leaves : I , visitor : & mut V ) -> H :: Output
59
+ fn merkelize < H , V , I > ( leaves : I , visitor : & mut V ) -> H :: Out
58
60
where
59
- H : HashT ,
60
- H :: Output : Default + AsRef < [ u8 ] > + PartialOrd ,
61
- V : Visitor < H :: Output > ,
62
- I : Iterator < Item = H :: Output > ,
61
+ H : Hasher ,
62
+ H :: Out : Default + AsRef < [ u8 ] > + PartialOrd ,
63
+ V : Visitor < H :: Out > ,
64
+ I : Iterator < Item = H :: Out > ,
63
65
{
64
66
let upper = Vec :: with_capacity ( ( leaves. size_hint ( ) . 1 . unwrap_or ( 0 ) . saturating_add ( 1 ) ) / 2 ) ;
65
67
let mut next = match merkelize_row :: < H , _ , _ > ( leaves, upper, visitor) {
66
68
Ok ( root) => return root,
67
- Err ( next) if next. is_empty ( ) => return H :: Output :: default ( ) ,
69
+ Err ( next) if next. is_empty ( ) => return H :: Out :: default ( ) ,
68
70
Err ( next) => next,
69
71
} ;
70
72
@@ -139,17 +141,17 @@ impl<T> Visitor<T> for () {
139
141
/// # Panic
140
142
///
141
143
/// The function will panic if given `leaf_index` is greater than the number of leaves.
142
- pub fn merkle_proof < H , I , T > ( leaves : I , leaf_index : usize ) -> MerkleProof < H :: Output , T >
144
+ pub fn merkle_proof < H , I , T > ( leaves : I , leaf_index : usize ) -> MerkleProof < H :: Out , T >
143
145
where
144
- H : HashT ,
145
- H :: Output : Default + Copy + AsRef < [ u8 ] > + PartialOrd ,
146
+ H : Hasher ,
147
+ H :: Out : Default + Copy + AsRef < [ u8 ] > + PartialOrd ,
146
148
I : IntoIterator < Item = T > ,
147
149
I :: IntoIter : ExactSizeIterator ,
148
150
T : AsRef < [ u8 ] > ,
149
151
{
150
152
let mut leaf = None ;
151
153
let iter = leaves. into_iter ( ) . enumerate ( ) . map ( |( idx, l) | {
152
- let hash = <H as HashT >:: hash ( l. as_ref ( ) ) ;
154
+ let hash = <H as Hasher >:: hash ( l. as_ref ( ) ) ;
153
155
if idx == leaf_index {
154
156
leaf = Some ( l) ;
155
157
}
@@ -234,28 +236,28 @@ impl<'a, H, T: AsRef<[u8]>> From<&'a T> for Leaf<'a, H> {
234
236
///
235
237
/// The proof must not contain the root hash.
236
238
pub fn verify_proof < ' a , H , P , L > (
237
- root : & ' a H :: Output ,
239
+ root : & ' a H :: Out ,
238
240
proof : P ,
239
241
number_of_leaves : usize ,
240
242
leaf_index : usize ,
241
243
leaf : L ,
242
244
) -> bool
243
245
where
244
- H : HashT ,
245
- H :: Output : PartialEq + AsRef < [ u8 ] > + PartialOrd ,
246
- P : IntoIterator < Item = H :: Output > ,
247
- L : Into < Leaf < ' a , H :: Output > > ,
246
+ H : Hasher ,
247
+ H :: Out : PartialEq + AsRef < [ u8 ] > + PartialOrd ,
248
+ P : IntoIterator < Item = H :: Out > ,
249
+ L : Into < Leaf < ' a , H :: Out > > ,
248
250
{
249
251
if leaf_index >= number_of_leaves {
250
252
return false
251
253
}
252
254
253
255
let leaf_hash = match leaf. into ( ) {
254
- Leaf :: Value ( content) => <H as HashT >:: hash ( content) ,
256
+ Leaf :: Value ( content) => <H as Hasher >:: hash ( content) ,
255
257
Leaf :: Hash ( hash) => hash,
256
258
} ;
257
259
258
- let hash_len = <H as sp_core :: Hasher >:: LENGTH ;
260
+ let hash_len = <H as Hasher >:: LENGTH ;
259
261
let mut combined = vec ! [ 0_u8 ; hash_len * 2 ] ;
260
262
let computed = proof. into_iter ( ) . fold ( leaf_hash, |a, b| {
261
263
if a < b {
@@ -265,7 +267,7 @@ where
265
267
combined[ ..hash_len] . copy_from_slice ( & b. as_ref ( ) ) ;
266
268
combined[ hash_len..] . copy_from_slice ( & a. as_ref ( ) ) ;
267
269
}
268
- let hash = <H as HashT >:: hash ( & combined) ;
270
+ let hash = <H as Hasher >:: hash ( & combined) ;
269
271
#[ cfg( feature = "debug" ) ]
270
272
log:: debug!(
271
273
"[verify_proof]: (a, b) {:?}, {:?} => {:?} ({:?}) hash" ,
@@ -287,20 +289,20 @@ where
287
289
/// empty iterator) an `Err` with the inner nodes of upper layer is returned.
288
290
fn merkelize_row < H , V , I > (
289
291
mut iter : I ,
290
- mut next : Vec < H :: Output > ,
292
+ mut next : Vec < H :: Out > ,
291
293
visitor : & mut V ,
292
- ) -> Result < H :: Output , Vec < H :: Output > >
294
+ ) -> Result < H :: Out , Vec < H :: Out > >
293
295
where
294
- H : HashT ,
295
- H :: Output : AsRef < [ u8 ] > + PartialOrd ,
296
- V : Visitor < H :: Output > ,
297
- I : Iterator < Item = H :: Output > ,
296
+ H : Hasher ,
297
+ H :: Out : AsRef < [ u8 ] > + PartialOrd ,
298
+ V : Visitor < H :: Out > ,
299
+ I : Iterator < Item = H :: Out > ,
298
300
{
299
301
#[ cfg( feature = "debug" ) ]
300
302
log:: debug!( "[merkelize_row]" ) ;
301
303
next. clear ( ) ;
302
304
303
- let hash_len = <H as sp_core :: Hasher >:: LENGTH ;
305
+ let hash_len = <H as Hasher >:: LENGTH ;
304
306
let mut index = 0 ;
305
307
let mut combined = vec ! [ 0_u8 ; hash_len * 2 ] ;
306
308
loop {
@@ -326,7 +328,7 @@ where
326
328
combined[ hash_len..] . copy_from_slice ( a. as_ref ( ) ) ;
327
329
}
328
330
329
- next. push ( <H as HashT >:: hash ( & combined) ) ;
331
+ next. push ( <H as Hasher >:: hash ( & combined) ) ;
330
332
} ,
331
333
// Odd number of items. Promote the item to the upper layer.
332
334
( Some ( a) , None ) if !next. is_empty ( ) => {
@@ -347,24 +349,11 @@ where
347
349
}
348
350
}
349
351
350
- sp_api:: decl_runtime_apis! {
351
- /// API useful for BEEFY light clients.
352
- pub trait BeefyMmrApi <H >
353
- where
354
- BeefyAuthoritySet <H >: sp_api:: Decode ,
355
- {
356
- /// Return the currently active BEEFY authority set proof.
357
- fn authority_set_proof( ) -> BeefyAuthoritySet <H >;
358
-
359
- /// Return the next/queued BEEFY authority set proof.
360
- fn next_authority_set_proof( ) -> BeefyNextAuthoritySet <H >;
361
- }
362
- }
363
-
364
352
#[ cfg( test) ]
365
353
mod tests {
366
354
use super :: * ;
367
- use crate :: sp_core:: H256 ;
355
+ use sp_core:: H256 ;
356
+ use sp_runtime:: traits:: Keccak256 ;
368
357
369
358
#[ test]
370
359
fn should_generate_empty_root ( ) {
0 commit comments