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
impl ChainState
Sourcepub fn new(
genesis: Block,
params: ConsensusParams,
) -> Result<Self, ChainStateError>
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).
Sourcepub fn set_verify_scripts(&mut self, verify: bool)
pub fn set_verify_scripts(&mut self, verify: bool)
Enable or disable script verification (useful for fast-sync / IBD).
Sourcepub fn process_block(
&mut self,
block: Block,
) -> Result<ProcessBlockResult, ChainStateError>
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.
Sourcepub fn tip_height(&self) -> u32
pub fn tip_height(&self) -> u32
Current active chain tip height.
Sourcepub fn index(&self) -> &BlockIndex
pub fn index(&self) -> &BlockIndex
Reference to the block index.
Sourcepub fn index_mut(&mut self) -> &mut BlockIndex
pub fn index_mut(&mut self) -> &mut BlockIndex
Mutable reference to the block index.
Sourcepub fn utxo_set(&self) -> &MemoryUtxoSet
pub fn utxo_set(&self) -> &MemoryUtxoSet
Reference to the UTXO set.
Sourcepub fn params(&self) -> &ConsensusParams
pub fn params(&self) -> &ConsensusParams
Get the consensus parameters.
Sourcepub fn block_count(&self) -> usize
pub fn block_count(&self) -> usize
Number of blocks in the block store.
Sourcepub fn utxo_count(&self) -> usize
pub fn utxo_count(&self) -> usize
Number of UTXOs in the active set.
Sourcepub fn has_utxo(&self, outpoint: &OutPoint) -> bool
pub fn has_utxo(&self, outpoint: &OutPoint) -> bool
Check if a specific UTXO exists in the active set.
Sourcepub fn get_block_hash_at_height(&self, height: u32) -> Option<BlockHash>
pub fn get_block_hash_at_height(&self, height: u32) -> Option<BlockHash>
Get the block hash at a given height on the active chain.
Sourcepub async fn flush_to_store(
&self,
store: &dyn ChainStateStore,
) -> Result<(), Box<dyn Error + Send + Sync>>
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).
Sourcepub async fn flush_block_delta(
&self,
result: &BlockConnectResult,
store: &dyn ChainStateStore,
) -> Result<(), Box<dyn Error + Send + Sync>>
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.