merkle-tree: Make instantiation a little less painful (#5037)
automerge
This commit is contained in:
parent
5b95685e12
commit
5698d48dc8
|
@ -175,7 +175,7 @@ pub fn hash_transactions(transactions: &[Transaction]) -> Hash {
|
||||||
// a hash of a slice of transactions only needs to hash the signatures
|
// a hash of a slice of transactions only needs to hash the signatures
|
||||||
let signatures: Vec<_> = transactions
|
let signatures: Vec<_> = transactions
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|tx| tx.signatures.iter().map(|sig| sig.as_ref()))
|
.flat_map(|tx| tx.signatures.iter())
|
||||||
.collect();
|
.collect();
|
||||||
let merkle_tree = MerkleTree::new(&signatures);
|
let merkle_tree = MerkleTree::new(&signatures);
|
||||||
if let Some(root_hash) = merkle_tree.get_root() {
|
if let Some(root_hash) = merkle_tree.get_root() {
|
||||||
|
|
|
@ -84,7 +84,7 @@ impl MerkleTree {
|
||||||
capacity
|
capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(items: &[&[u8]]) -> Self {
|
pub fn new<T: AsRef<[u8]>>(items: &[T]) -> Self {
|
||||||
let cap = MerkleTree::calculate_vec_capacity(items.len());
|
let cap = MerkleTree::calculate_vec_capacity(items.len());
|
||||||
let mut mt = MerkleTree {
|
let mut mt = MerkleTree {
|
||||||
leaf_count: items.len(),
|
leaf_count: items.len(),
|
||||||
|
@ -92,6 +92,7 @@ impl MerkleTree {
|
||||||
};
|
};
|
||||||
|
|
||||||
for item in items {
|
for item in items {
|
||||||
|
let item = item.as_ref();
|
||||||
let hash = hash_leaf!(item);
|
let hash = hash_leaf!(item);
|
||||||
mt.nodes.push(hash);
|
mt.nodes.push(hash);
|
||||||
}
|
}
|
||||||
|
@ -178,7 +179,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tree_from_empty() {
|
fn test_tree_from_empty() {
|
||||||
let mt = MerkleTree::new(&[]);
|
let mt = MerkleTree::new::<[u8; 0]>(&[]);
|
||||||
assert_eq!(mt.get_root(), None);
|
assert_eq!(mt.get_root(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue