Merge pull request #415 from shesek/merkleblock-txids

Allow to construct MerkleBlock using the header and txids
This commit is contained in:
Elichai Turkel 2020-05-24 16:32:17 +03:00 committed by GitHub
commit f4e26caa94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 9 deletions

View File

@ -431,18 +431,31 @@ impl MerkleBlock {
/// assert_eq!(txid, matches[0]);
/// ```
pub fn from_block(block: &Block, match_txids: &HashSet<Txid>) -> Self {
let header = block.header;
let mut matches: Vec<bool> = Vec::with_capacity(block.txdata.len());
let mut hashes: Vec<Txid> = Vec::with_capacity(block.txdata.len());
for hash in block.txdata.iter().map(Transaction::txid) {
matches.push(match_txids.contains(&hash));
hashes.push(hash);
let block_txids: Vec<_> = block.txdata.iter().map(Transaction::txid).collect();
Self::from_header_txids(&block.header, &block_txids, match_txids)
}
let pmt = PartialMerkleTree::from_txids(&hashes, &matches);
MerkleBlock { header, txn: pmt }
/// Create a MerkleBlock from the block's header and txids, that should contain proofs for match_txids.
///
/// The `header` is the block header, `block_txids` is the full list of txids included in the block and
/// `match_txids` is a set containing the transaction ids that should be included in the partial merkle tree.
/// ```
pub fn from_header_txids(
header: &BlockHeader,
block_txids: &[Txid],
match_txids: &HashSet<Txid>,
) -> Self {
let matches: Vec<bool> = block_txids
.into_iter()
.map(|txid| match_txids.contains(txid))
.collect();
let pmt = PartialMerkleTree::from_txids(&block_txids, &matches);
MerkleBlock {
header: *header,
txn: pmt,
}
}
/// Extract the matching txid's represented by this partial merkle tree