Struct PartialMerkleTree
pub struct PartialMerkleTree { /* private fields */ }
Expand description
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:
- uint32 total_transactions (4 bytes)
- varint number of hashes (1-3 bytes)
- uint256[] hashes in depth-first order (<= 32*N bytes)
- varint number of bytes of flag bits (1-3 bytes)
- byte[] flag bits, packed per 8 in a byte, least significant bit first (<= 2*N-1 bits) The size constraints follow from this.
Implementations§
§impl PartialMerkleTree
impl PartialMerkleTree
pub fn num_transactions(&self) -> u32
pub fn num_transactions(&self) -> u32
Returns the total number of transactions in the block.
pub fn bits(&self) -> &Vec<bool>
pub fn bits(&self) -> &Vec<bool>
Returns the node-is-parent-of-matched-txid bits of the partial merkle tree.
pub fn hashes(&self) -> &Vec<TxMerkleNode>
pub fn hashes(&self) -> &Vec<TxMerkleNode>
Returns the transaction ids and internal hashes of the partial merkle tree.
pub fn from_txids(txids: &[Txid], matches: &[bool]) -> PartialMerkleTree
pub fn from_txids(txids: &[Txid], matches: &[bool]) -> PartialMerkleTree
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>
pub fn extract_matches( &self, matches: &mut Vec<Txid>, indexes: &mut Vec<u32>, ) -> Result<TxMerkleNode, MerkleBlockError>
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 Clone for PartialMerkleTree
impl Clone for PartialMerkleTree
§fn clone(&self) -> PartialMerkleTree
fn clone(&self) -> PartialMerkleTree
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl Debug for PartialMerkleTree
impl Debug for PartialMerkleTree
§impl Decodable for PartialMerkleTree
impl Decodable for PartialMerkleTree
§fn consensus_decode<R>(r: &mut R) -> Result<PartialMerkleTree, Error>
fn consensus_decode<R>(r: &mut R) -> Result<PartialMerkleTree, Error>
§impl Encodable for PartialMerkleTree
impl Encodable for PartialMerkleTree
§impl PartialEq for PartialMerkleTree
impl PartialEq for PartialMerkleTree
impl Eq for PartialMerkleTree
impl StructuralPartialEq for PartialMerkleTree
Auto Trait Implementations§
impl Freeze for PartialMerkleTree
impl RefUnwindSafe for PartialMerkleTree
impl Send for PartialMerkleTree
impl Sync for PartialMerkleTree
impl Unpin for PartialMerkleTree
impl UnwindSafe for PartialMerkleTree
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request