Expose merkleroot(Vec<Sha256dHash>) publicly

In a project of mine I needed to check the merkle root before
moving some Vec<Transaction>s around, so need to be able to
calculate the merkle root on a Vec<Sha256dHash> directly.
This commit is contained in:
Matt Corallo 2018-06-01 18:46:10 -04:00
parent 881972b2a5
commit b78ab0f60b
1 changed files with 23 additions and 21 deletions

View File

@ -419,9 +419,8 @@ pub trait MerkleRoot {
fn merkle_root(&self) -> Sha256dHash; fn merkle_root(&self) -> Sha256dHash;
} }
impl<'a, T: BitcoinHash> MerkleRoot for &'a [T] { /// Calculates the merkle root of a list of txids hashes directly
fn merkle_root(&self) -> Sha256dHash { pub fn bitcoin_merkle_root(data: Vec<Sha256dHash>) -> Sha256dHash {
fn merkle_root(data: Vec<Sha256dHash>) -> Sha256dHash {
// Base case // Base case
if data.len() < 1 { if data.len() < 1 {
return Default::default(); return Default::default();
@ -439,9 +438,12 @@ impl<'a, T: BitcoinHash> MerkleRoot for &'a [T] {
data[idx2].consensus_encode(&mut encoder).unwrap(); data[idx2].consensus_encode(&mut encoder).unwrap();
next.push(encoder.into_inner().into_inner().bitcoin_hash()); next.push(encoder.into_inner().into_inner().bitcoin_hash());
} }
merkle_root(next) bitcoin_merkle_root(next)
} }
merkle_root(self.iter().map(|obj| obj.bitcoin_hash()).collect())
impl<'a, T: BitcoinHash> MerkleRoot for &'a [T] {
fn merkle_root(&self) -> Sha256dHash {
bitcoin_merkle_root(self.iter().map(|obj| obj.bitcoin_hash()).collect())
} }
} }