Struct CoinsViewCache

Source
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>
where Block: BlockT, Client: ProvideRuntimeApi<Block> + HeaderBackend<Block> + Send + Sync, Client::Api: SubcoinApi<Block>,

Source

pub fn new(client: Arc<Client>, cache_size: u32) -> Self

Create a new coins view cache.

§Arguments
  • client - Substrate client for runtime API access
  • cache_size - Maximum number of base layer entries to cache
Source

pub fn get_coin( &mut self, outpoint: &COutPoint, ) -> Result<Option<PoolCoin>, MempoolError>

Get coin with overlay priority.

Query order:

  1. Check if spent by mempool
  2. Check mempool overlay (created by pending txs)
  3. Check base cache
  4. Query runtime and cache result
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn have_coin(&self, outpoint: &COutPoint) -> bool

Check if coin exists (for quick lookups without fetching).

Source

pub fn best_block(&self) -> Block::Hash

Get the current best block hash.

Source

pub fn client(&self) -> &Arc<Client>

Get reference to the client.

Source

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>
where <Block as Block>::Hash: RefUnwindSafe, Block: RefUnwindSafe, Client: RefUnwindSafe,

§

impl<Block, Client> Send for CoinsViewCache<Block, Client>
where Client: Sync + Send,

§

impl<Block, Client> Sync for CoinsViewCache<Block, Client>
where Client: Sync + Send,

§

impl<Block, Client> Unpin for CoinsViewCache<Block, Client>
where <Block as Block>::Hash: Unpin, Block: Unpin,

§

impl<Block, Client> UnwindSafe for CoinsViewCache<Block, Client>
where <Block as Block>::Hash: UnwindSafe, Client: RefUnwindSafe, Block: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CheckedConversion for T

§

fn checked_from<T>(t: T) -> Option<Self>
where Self: TryFrom<T>,

Convert from a value of T into an equivalent instance of Option<Self>. Read more
§

fn checked_into<T>(self) -> Option<T>
where Self: TryInto<T>,

Consume self to return Some equivalent value of Option<T>. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 Src
where Dest: FromTuple<Src>,

§

fn into_tuple(self) -> Dest

§

impl<T, Outer> IsWrappedBy<Outer> for T
where Outer: AsRef<T> + AsMut<T> + From<T>, T: From<Outer>,

§

fn from_ref(outer: &Outer) -> &T

Get a reference to the inner from the outer.

§

fn from_mut(outer: &mut Outer) -> &mut T

Get a mutable reference to the inner from the outer.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T> SaturatedConversion for T

§

fn saturated_from<T>(t: T) -> Self
where Self: UniqueSaturatedFrom<T>,

Convert from a value of T into an equivalent instance of Self. Read more
§

fn saturated_into<T>(self) -> T
where Self: UniqueSaturatedInto<T>,

Consume self to return an equivalent value of T. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<S, T> UncheckedInto<T> for S
where T: UncheckedFrom<S>,

§

fn unchecked_into(self) -> T

The counterpart to unchecked_from.
§

impl<T, S> UniqueSaturatedInto<T> for S
where T: Bounded, S: TryInto<T>,

§

fn unique_saturated_into(self) -> T

Consume self to return an equivalent value of T.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> JsonSchemaMaybe for T

§

impl<T> MaybeRefUnwindSafe for T
where T: RefUnwindSafe,