[][src]Struct bitcoin::util::merkleblock::PartialMerkleTree

pub struct PartialMerkleTree { /* fields omitted */ }

Data structure that represents a partial merkle tree.

It represents a subset of the txid's of a known block, in a way that allows recovery of the list of txid's and the merkle root, in an authenticated way.

The encoding works as follows: we traverse the tree in depth-first order, storing a bit for each traversed node, signifying whether the node is the parent of at least one matched leaf txid (or a matched txid itself). In case we are at the leaf level, or this bit is 0, its merkle node hash is stored, and its children are not explored further. Otherwise, no hash is stored, but we recurse into both (or the only) child branch. During decoding, the same depth-first traversal is performed, consuming bits and hashes as they written during encoding.

The serialization is fixed and provides a hard guarantee about the encoded size:

SIZE <= 10 + ceil(32.25*N)

Where N represents the number of leaf nodes of the partial tree. N itself is bounded by:

N <= total_transactions N <= 1 + matched_transactions*tree_height

The serialization format:

Methods

impl PartialMerkleTree[src]

pub fn from_txids(txids: &[Txid], matches: &[bool]) -> Self[src]

Construct a partial merkle tree The txids are the transaction hashes of the block and the matches is the contains flags wherever a tx hash should be included in the proof.

Panics when txids is empty or when matches has a different length

Examples

use bitcoin::hash_types::Txid;
use bitcoin::hashes::hex::FromHex;
use bitcoin::util::merkleblock::PartialMerkleTree;

// Block 80000
let txids: Vec<Txid> = [
    "c06fbab289f723c6261d3030ddb6be121f7d2508d77862bb1e484f5cd7f92b25",
    "5a4ebf66822b0b2d56bd9dc64ece0bc38ee7844a23ff1d7320a88c5fdb2ad3e2",
]
.iter()
.map(|hex| Txid::from_hex(hex).unwrap())
.collect();

// Select the second transaction
let matches = vec![false, true];
let tree = PartialMerkleTree::from_txids(&txids, &matches);
assert!(tree.extract_matches(&mut vec![], &mut vec![]).is_ok());

pub fn extract_matches(
    &self,
    matches: &mut Vec<Txid>,
    indexes: &mut Vec<u32>
) -> Result<TxMerkleNode, MerkleBlockError>
[src]

Extract the matching txid's represented by this partial merkle tree and their respective indices within the partial tree. returns the merkle root, or error in case of failure

Trait Implementations

impl Encodable for PartialMerkleTree[src]

impl Decodable for PartialMerkleTree[src]

impl Clone for PartialMerkleTree[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl PartialEq<PartialMerkleTree> for PartialMerkleTree[src]

impl Eq for PartialMerkleTree[src]

impl Debug for PartialMerkleTree[src]

Auto Trait Implementations

impl Unpin for PartialMerkleTree

impl Sync for PartialMerkleTree

impl Send for PartialMerkleTree

impl UnwindSafe for PartialMerkleTree

impl RefUnwindSafe for PartialMerkleTree

Blanket Implementations

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]