merkle-tree: Make instantiation a little less painful (#5037)

automerge
This commit is contained in:
Trent Nelson 2019-07-11 16:15:08 -06:00 committed by Grimes
parent 5b95685e12
commit 5698d48dc8
2 changed files with 4 additions and 3 deletions

View File

@ -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() {

View File

@ -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);
} }