pub struct CoinsViewCache<Block: BlockT, Client> { /* private fields */ }Expand description
In-memory UTXO cache with Substrate runtime backend.
Uses a dual-layer design to handle both blockchain UTXOs and mempool-created coins:
base_cache: LRU cache of UTXOs from the blockchain (cleared on block import)mempool_overlay: Coins created by pending transactions (preserved across blocks)mempool_spends: Outputs spent by pending transactions
Implementations§
Source§impl<Block, Client> CoinsViewCache<Block, Client>
impl<Block, Client> CoinsViewCache<Block, Client>
Sourcepub fn new(client: Arc<Client>, cache_size: u32) -> Self
pub fn new(client: Arc<Client>, cache_size: u32) -> Self
Create a new coins view cache.
§Arguments
client- Substrate client for runtime API accesscache_size- Maximum number of base layer entries to cache
Sourcepub fn get_coin(
&mut self,
outpoint: &COutPoint,
) -> Result<Option<PoolCoin>, MempoolError>
pub fn get_coin( &mut self, outpoint: &COutPoint, ) -> Result<Option<PoolCoin>, MempoolError>
Get coin with overlay priority.
Query order:
- Check if spent by mempool
- Check mempool overlay (created by pending txs)
- Check base cache
- Query runtime and cache result
Sourcepub fn ensure_coins(
&mut self,
outpoints: &[COutPoint],
) -> Result<(), MempoolError>
pub fn ensure_coins( &mut self, outpoints: &[COutPoint], ) -> Result<(), MempoolError>
Batch-prefetch coins (called at start of validation).
This is critical for performance: queries all needed coins in a single runtime call rather than making individual calls per input.
Should be called with all transaction inputs before validation begins.
Sourcepub fn add_mempool_coins(&mut self, tx: &Transaction)
pub fn add_mempool_coins(&mut self, tx: &Transaction)
Add coins from mempool transaction to overlay.
These coins are marked with MEMPOOL_HEIGHT and are preserved across block imports until the transaction is removed from the mempool.
Sourcepub fn spend_coin(&mut self, outpoint: &COutPoint)
pub fn spend_coin(&mut self, outpoint: &COutPoint)
Mark coin as spent by mempool transaction.
This creates an overlay that makes the coin appear spent even though it exists in the blockchain UTXO set.
Sourcepub fn remove_mempool_tx(&mut self, tx: &Transaction)
pub fn remove_mempool_tx(&mut self, tx: &Transaction)
Remove mempool transaction’s coins (on eviction/confirmation).
Cleans up both the coins created by this transaction and the spends it made from the overlay.
Sourcepub fn on_block_connected(&mut self, block_hash: Block::Hash)
pub fn on_block_connected(&mut self, block_hash: Block::Hash)
Update to new block tip.
CRITICAL: Only clears the base cache, preserving the mempool overlay. Mempool coins remain valid until their transactions are removed.
Sourcepub fn have_coin(&self, outpoint: &COutPoint) -> bool
pub fn have_coin(&self, outpoint: &COutPoint) -> bool
Check if coin exists (for quick lookups without fetching).
Sourcepub fn best_block(&self) -> Block::Hash
pub fn best_block(&self) -> Block::Hash
Get the current best block hash.
Sourcepub fn cache_stats(&self) -> CacheStats
pub fn cache_stats(&self) -> CacheStats
Get statistics about the cache.
Auto Trait Implementations§
impl<Block, Client> Freeze for CoinsViewCache<Block, Client>where
<Block as Block>::Hash: Freeze,
impl<Block, Client> RefUnwindSafe for CoinsViewCache<Block, Client>
impl<Block, Client> Send for CoinsViewCache<Block, Client>
impl<Block, Client> Sync for CoinsViewCache<Block, Client>
impl<Block, Client> Unpin for CoinsViewCache<Block, Client>
impl<Block, Client> UnwindSafe for CoinsViewCache<Block, Client>
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<T> CheckedConversion for T
impl<T> CheckedConversion for T
§fn checked_from<T>(t: T) -> Option<Self>where
Self: TryFrom<T>,
fn checked_from<T>(t: T) -> Option<Self>where
Self: TryFrom<T>,
§fn checked_into<T>(self) -> Option<T>where
Self: TryInto<T>,
fn checked_into<T>(self) -> Option<T>where
Self: TryInto<T>,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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§impl<Src, Dest> IntoTuple<Dest> for Srcwhere
Dest: FromTuple<Src>,
impl<Src, Dest> IntoTuple<Dest> for Srcwhere
Dest: FromTuple<Src>,
fn into_tuple(self) -> Dest
§impl<T, Outer> IsWrappedBy<Outer> for T
impl<T, Outer> IsWrappedBy<Outer> for T
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T. Read more§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from.§impl<T, S> UniqueSaturatedInto<T> for S
impl<T, S> UniqueSaturatedInto<T> for S
§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T.