Skip to main content

ChainState

Struct ChainState 

Source
pub struct ChainState { /* private fields */ }
Expand description

The chain state manager.

Owns the block index (header tree), UTXO set, and the undo data needed to disconnect blocks during a reorg.

Implementations§

Source§

impl ChainState

Source

pub fn new( genesis: Block, params: ConsensusParams, ) -> Result<Self, ChainStateError>

Create a new chain state, initialised with a genesis block.

The genesis block is connected immediately (its coinbase outputs are added to the UTXO set).

Source

pub fn set_verify_scripts(&mut self, verify: bool)

Enable or disable script verification (useful for fast-sync / IBD).

Source

pub fn process_block( &mut self, block: Block, ) -> Result<ProcessBlockResult, ChainStateError>

Process a new block. This is the main entry point.

Determines whether the block extends the tip, causes a reorg, or sits on a side chain, and acts accordingly.

Source

pub fn tip(&self) -> BlockHash

Current active chain tip hash.

Source

pub fn tip_height(&self) -> u32

Current active chain tip height.

Source

pub fn index(&self) -> &BlockIndex

Reference to the block index.

Source

pub fn index_mut(&mut self) -> &mut BlockIndex

Mutable reference to the block index.

Source

pub fn utxo_set(&self) -> &MemoryUtxoSet

Reference to the UTXO set.

Source

pub fn get_block(&self, hash: &BlockHash) -> Option<&Block>

Get a stored block by hash.

Source

pub fn params(&self) -> &ConsensusParams

Get the consensus parameters.

Source

pub fn block_count(&self) -> usize

Number of blocks in the block store.

Source

pub fn utxo_count(&self) -> usize

Number of UTXOs in the active set.

Source

pub fn has_utxo(&self, outpoint: &OutPoint) -> bool

Check if a specific UTXO exists in the active set.

Source

pub fn get_block_hash_at_height(&self, height: u32) -> Option<BlockHash>

Get the block hash at a given height on the active chain.

Source

pub async fn flush_to_store( &self, store: &dyn ChainStateStore, ) -> Result<(), Box<dyn Error + Send + Sync>>

Flush the current UTXO set and chain tip to persistent storage.

This writes a full snapshot of all UTXOs created/spent since the last flush, plus the chain tip. Call this after connecting a batch of blocks (e.g. at the end of IBD or periodically during normal operation).

Source

pub async fn flush_block_delta( &self, result: &BlockConnectResult, store: &dyn ChainStateStore, ) -> Result<(), Box<dyn Error + Send + Sync>>

Flush only the UTXO changes from a single block connect/disconnect.

More efficient than a full flush — writes only the delta.

Source

pub async fn load_tip_from_store( store: &dyn ChainStateStore, ) -> Result<Option<(BlockHash, u32)>, Box<dyn Error + Send + Sync>>

Load chain tip from a persistent store and verify consistency.

Returns the stored (tip_hash, tip_height), or None if the store is empty.

Auto Trait Implementations§

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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