pub struct UtxoMerkleTree { /* private fields */ }Expand description
UTXO Merkle Tree
Provides incremental updates for UTXO set with Merkle tree commitments. Wraps sparse-merkle-tree to provide UTXO-specific operations.
Implementations§
Source§impl UtxoMerkleTree
impl UtxoMerkleTree
Sourcepub fn new() -> UtxoCommitmentResult<Self>
pub fn new() -> UtxoCommitmentResult<Self>
Create a new empty UTXO Merkle tree
Sourcepub fn insert(
&mut self,
outpoint: OutPoint,
utxo: UTXO,
) -> UtxoCommitmentResult<Hash>
pub fn insert( &mut self, outpoint: OutPoint, utxo: UTXO, ) -> UtxoCommitmentResult<Hash>
Insert a UTXO into the tree
Sourcepub fn remove(
&mut self,
outpoint: &OutPoint,
utxo: &UTXO,
) -> UtxoCommitmentResult<Hash>
pub fn remove( &mut self, outpoint: &OutPoint, utxo: &UTXO, ) -> UtxoCommitmentResult<Hash>
Remove a UTXO from the tree (by updating with zero value)
Sourcepub fn get(&self, outpoint: &OutPoint) -> UtxoCommitmentResult<Option<UTXO>>
pub fn get(&self, outpoint: &OutPoint) -> UtxoCommitmentResult<Option<UTXO>>
Get a UTXO from the tree
Sourcepub fn generate_commitment(
&self,
block_hash: Hash,
block_height: Natural,
) -> UtxoCommitment
pub fn generate_commitment( &self, block_hash: Hash, block_height: Natural, ) -> UtxoCommitment
Generate a UTXO commitment
Sourcepub fn total_supply(&self) -> u64
pub fn total_supply(&self) -> u64
Get total supply
Sourcepub fn utxo_count(&self) -> u64
pub fn utxo_count(&self) -> u64
Get UTXO count
Sourcepub fn generate_proof(
&self,
outpoint: &OutPoint,
) -> UtxoCommitmentResult<MerkleProof>
pub fn generate_proof( &self, outpoint: &OutPoint, ) -> UtxoCommitmentResult<MerkleProof>
Generate a Merkle proof for a specific UTXO
Returns a proof that can be used to verify the UTXO exists in the tree.
Sourcepub fn serialize_proof_for_wire(
proof: MerkleProof,
) -> UtxoCommitmentResult<Vec<u8>>
pub fn serialize_proof_for_wire( proof: MerkleProof, ) -> UtxoCommitmentResult<Vec<u8>>
Serialize a Merkle proof to bytes for wire transmission.
Call this from network handlers (e.g. blvm-node) to avoid serde trait resolution issues when multiple serde versions exist in the dependency tree.
Sourcepub fn deserialize_proof_from_wire(
bytes: &[u8],
) -> UtxoCommitmentResult<MerkleProof>
pub fn deserialize_proof_from_wire( bytes: &[u8], ) -> UtxoCommitmentResult<MerkleProof>
Deserialize a Merkle proof from bytes (inverse of serialize_proof_for_wire).
Sourcepub fn verify_commitment_supply(
&self,
commitment: &UtxoCommitment,
) -> UtxoCommitmentResult<bool>
pub fn verify_commitment_supply( &self, commitment: &UtxoCommitment, ) -> UtxoCommitmentResult<bool>
Verify a UTXO commitment matches expected supply
Compares the total supply in the commitment against the expected Bitcoin supply at the given block height.
Sourcepub fn from_utxo_set(utxo_set: &UtxoSet) -> UtxoCommitmentResult<Self>
pub fn from_utxo_set(utxo_set: &UtxoSet) -> UtxoCommitmentResult<Self>
Rebuild tree from UtxoSet
Used after connect_block() to update the Merkle tree with the validated UTXO set. This rebuilds the entire tree.
Sourcepub fn update_from_utxo_set(
&mut self,
new_utxo_set: &UtxoSet,
old_utxo_set: &UtxoSet,
) -> UtxoCommitmentResult<Hash>
pub fn update_from_utxo_set( &mut self, new_utxo_set: &UtxoSet, old_utxo_set: &UtxoSet, ) -> UtxoCommitmentResult<Hash>
Update tree from UtxoSet (incremental update)
Compares current tree state with new UtxoSet and applies only the differences. More efficient than full rebuild.
Note: This function requires knowing the previous UtxoSet to
efficiently detect removals. If the previous set is not available,
use from_utxo_set() to rebuild the tree.
§Arguments
new_utxo_set- The new UTXO set (from connect_block)old_utxo_set- The previous UTXO set (for detecting removals)
Sourcepub fn to_utxo_set(&self) -> UtxoCommitmentResult<UtxoSet>
pub fn to_utxo_set(&self) -> UtxoCommitmentResult<UtxoSet>
Convert UtxoMerkleTree to UtxoSet
Iterates through the tree and builds a UtxoSet. Note: This is expensive as sparse merkle trees don’t support efficient iteration. Use only when necessary.
Sourcepub fn verify_commitment_root(&self, commitment: &UtxoCommitment) -> bool
pub fn verify_commitment_root(&self, commitment: &UtxoCommitment) -> bool
Verify a commitment’s Merkle root matches the tree’s root
Sourcepub fn verify_utxo_proof(
commitment: &UtxoCommitment,
outpoint: &OutPoint,
utxo: &UTXO,
proof: MerkleProof,
) -> UtxoCommitmentResult<bool>
pub fn verify_utxo_proof( commitment: &UtxoCommitment, outpoint: &OutPoint, utxo: &UTXO, proof: MerkleProof, ) -> UtxoCommitmentResult<bool>
Verify a UTXO Merkle proof against a commitment’s root
This is a static/associated function - it doesn’t need a tree instance, only the commitment’s merkle root for verification.
This function cryptographically verifies that a UTXO exists in the commitment’s UTXO set without requiring the full tree.
§Arguments
commitment- The UTXO commitment containing the merkle rootoutpoint- The outpoint to verifyutxo- The UTXO data to verifyproof- The Merkle proof (takes ownership)
§Returns
Ok(true) if proof is valid, Ok(false) or Err if invalid
Trait Implementations§
Source§impl Default for UtxoMerkleTree
Available on crate feature utxo-commitments only.
impl Default for UtxoMerkleTree
utxo-commitments only.Source§fn default() -> Self
fn default() -> Self
Prefer UtxoMerkleTree::new in code that can handle allocation failure.
Default panics if the underlying sparse Merkle tree cannot be constructed (e.g. severe
memory pressure). This matches “default must be infallible” call sites but is not ideal for
untrusted or resource-constrained environments.
Auto Trait Implementations§
impl Freeze for UtxoMerkleTree
impl RefUnwindSafe for UtxoMerkleTree
impl Send for UtxoMerkleTree
impl Sync for UtxoMerkleTree
impl Unpin for UtxoMerkleTree
impl UnsafeUnpin for UtxoMerkleTree
impl UnwindSafe for UtxoMerkleTree
Blanket Implementations§
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more