pub struct ChainState<PersistedState: ChainStore> { /* private fields */ }Expand description
The high-level chain backend managing the blockchain state.
ChainState is responsible for:
- Keeping track of the chain state with the help of a
ChainStorefor persisted storage. - Correctly updating the state using consensus functions.
- Interfacing with other components and providing data about the current view of the chain.
Implementations§
Source§impl<PersistedState: ChainStore> ChainState<PersistedState>
impl<PersistedState: ChainStore> ChainState<PersistedState>
Sourcepub fn push_headers(
&self,
headers: Vec<BlockHeader>,
height: u32,
) -> Result<(), BlockchainError>
pub fn push_headers( &self, headers: Vec<BlockHeader>, height: u32, ) -> Result<(), BlockchainError>
Just adds headers to the chainstate, without validating them.
pub fn new( chainstore: PersistedState, network: Network, assume_valid: AssumeValidArg, ) -> ChainState<PersistedState>
pub fn load_chain_state( chainstore: PersistedState, network: Network, assume_valid: AssumeValidArg, ) -> Result<ChainState<PersistedState>, BlockchainError>
pub fn acc(&self) -> Stump
pub fn get_bip34_height(&self, block: &Block) -> Option<u32>
Sourcepub fn validate_block_no_acc(
&self,
block: &Block,
height: u32,
inputs: HashMap<OutPoint, UtxoData>,
) -> Result<(), BlockchainError>
pub fn validate_block_no_acc( &self, block: &Block, height: u32, inputs: HashMap<OutPoint, UtxoData>, ) -> Result<(), BlockchainError>
Validates the block without checking whether the inputs are present in the UTXO set. This function contains the core validation logic.
The methods BlockchainInterface::validate_block and UpdatableChainstate::connect_block
call this and additionally verify the inclusion proof (i.e., they perform full validation).
Trait Implementations§
Source§impl<PersistedState: ChainStore> BlockchainInterface for ChainState<PersistedState>
impl<PersistedState: ChainStore> BlockchainInterface for ChainState<PersistedState>
type Error = BlockchainError
fn get_params(&self) -> Params
fn acc(&self) -> Stump
fn get_fork_point(&self, block: BlockHash) -> Result<BlockHash, Self::Error>
fn update_acc( &self, acc: Stump, block: UtreexoBlock, height: u32, proof: Proof, del_hashes: Vec<Hash>, ) -> Result<Stump, Self::Error>
fn get_chain_tips(&self) -> Result<Vec<BlockHash>, Self::Error>
fn validate_block( &self, block: &Block, proof: Proof, inputs: HashMap<OutPoint, UtxoData>, del_hashes: Vec<Hash>, acc: Stump, ) -> Result<(), Self::Error>
Source§fn get_block_locator_for_tip(
&self,
tip: BlockHash,
) -> Result<Vec<BlockHash>, BlockchainError>
fn get_block_locator_for_tip( &self, tip: BlockHash, ) -> Result<Vec<BlockHash>, BlockchainError>
Returns a block locator from a given tip Read more
Source§fn get_block_height(&self, hash: &BlockHash) -> Result<Option<u32>, Self::Error>
fn get_block_height(&self, hash: &BlockHash) -> Result<Option<u32>, Self::Error>
Returns the height of a block, given it’s hash
Source§fn get_block_hash(&self, height: u32) -> Result<BlockHash, Self::Error>
fn get_block_hash(&self, height: u32) -> Result<BlockHash, Self::Error>
Returns the block with a given height in our current tip.
Source§fn get_tx(&self, _txid: &Txid) -> Result<Option<Transaction>, Self::Error>
fn get_tx(&self, _txid: &Txid) -> Result<Option<Transaction>, Self::Error>
Returns a bitcoin [Transaction] given it’s txid.
Source§fn broadcast(&self, tx: &Transaction) -> Result<(), Self::Error>
fn broadcast(&self, tx: &Transaction) -> Result<(), Self::Error>
Broadcasts a transaction to the network.
Source§fn estimate_fee(&self, target: usize) -> Result<f64, Self::Error>
fn estimate_fee(&self, target: usize) -> Result<f64, Self::Error>
Returns fee estimation for inclusion in
target blocks.Source§fn get_block(&self, _hash: &BlockHash) -> Result<Block, Self::Error>
fn get_block(&self, _hash: &BlockHash) -> Result<Block, Self::Error>
Returns a block with a given
hash if any.Source§fn get_best_block(&self) -> Result<(u32, BlockHash), Self::Error>
fn get_best_block(&self) -> Result<(u32, BlockHash), Self::Error>
Returns the best known block
Source§fn get_block_header(&self, hash: &BlockHash) -> Result<Header, Self::Error>
fn get_block_header(&self, hash: &BlockHash) -> Result<Header, Self::Error>
Returns associated header for block with
hashSource§fn subscribe(&self, tx: Arc<dyn BlockConsumer>)
fn subscribe(&self, tx: Arc<dyn BlockConsumer>)
Register for receiving notifications for some event. Right now it only works for
new blocks, but may work with transactions in the future too.
if a module performs some heavy-lifting on the block’s data, it should pass in a
vector or a channel where data can be transferred to the atual worker, otherwise
chainstate will be stuck for as long as you have work to do.
Source§fn get_block_locator(&self) -> Result<Vec<BlockHash>, BlockchainError>
fn get_block_locator(&self) -> Result<Vec<BlockHash>, BlockchainError>
Returns a block locator
Source§fn get_validation_index(&self) -> Result<u32, Self::Error>
fn get_validation_index(&self) -> Result<u32, Self::Error>
Returns the last block we validated
Source§fn is_coinbase_mature(
&self,
height: u32,
block: BlockHash,
) -> Result<bool, Self::Error>
fn is_coinbase_mature( &self, height: u32, block: BlockHash, ) -> Result<bool, Self::Error>
Checks if a coinbase is mature
Source§fn get_unbroadcasted(&self) -> Vec<Transaction>
fn get_unbroadcasted(&self) -> Vec<Transaction>
Returns the list of unbroadcasted transactions.
Source§impl<T: ChainStore> TryFrom<ChainStateBuilder<T>> for ChainState<T>
impl<T: ChainStore> TryFrom<ChainStateBuilder<T>> for ChainState<T>
Source§type Error = BlockchainBuilderError
type Error = BlockchainBuilderError
The type returned in the event of a conversion error.
Source§impl<PersistedState: ChainStore> UpdatableChainstate for ChainState<PersistedState>
impl<PersistedState: ChainStore> UpdatableChainstate for ChainState<PersistedState>
fn switch_chain(&self, new_tip: BlockHash) -> Result<(), BlockchainError>
Source§fn mark_block_as_valid(&self, block: BlockHash) -> Result<(), BlockchainError>
fn mark_block_as_valid(&self, block: BlockHash) -> Result<(), BlockchainError>
Marks one block as being fully validated, this overrides a block that was explicitly
marked as invalid.
Source§fn mark_chain_as_assumed(
&self,
acc: Stump,
assumed_hash: BlockHash,
) -> Result<bool, BlockchainError>
fn mark_chain_as_assumed( &self, acc: Stump, assumed_hash: BlockHash, ) -> Result<bool, BlockchainError>
Marks a chain as fully-valid Read more
Source§fn invalidate_block(&self, block: BlockHash) -> Result<(), BlockchainError>
fn invalidate_block(&self, block: BlockHash) -> Result<(), BlockchainError>
Tells this blockchain to consider this block invalid, and not build on top of it
Source§fn toggle_ibd(&self, is_ibd: bool)
fn toggle_ibd(&self, is_ibd: bool)
Toggle IBD on/off
Source§fn connect_block(
&self,
block: &Block,
proof: Proof,
inputs: HashMap<OutPoint, UtxoData>,
del_hashes: Vec<Hash>,
) -> Result<u32, BlockchainError>
fn connect_block( &self, block: &Block, proof: Proof, inputs: HashMap<OutPoint, UtxoData>, del_hashes: Vec<Hash>, ) -> Result<u32, BlockchainError>
This is one of the most important methods for a ChainState,
it gets a block and some utreexo data, validates this block and
connects to our chain of blocks. This function is meant to be atomic
and prone of running in parallel.
Source§fn handle_transaction(&self) -> Result<(), BlockchainError>
fn handle_transaction(&self) -> Result<(), BlockchainError>
Not used for now, but in a future blockchain with mempool, we can process transactions
that are not in a block yet.
Source§fn flush(&self) -> Result<(), BlockchainError>
fn flush(&self) -> Result<(), BlockchainError>
Persists our data. Should be invoked periodically.
Source§fn accept_header(&self, header: BlockHeader) -> Result<(), BlockchainError>
fn accept_header(&self, header: BlockHeader) -> Result<(), BlockchainError>
Accepts a new header to our chain. This method is called before connect_block, and
makes some basic checks on a header and saves it on disk. We only accept a block as
valid after calling connect_block. Read more
Source§fn get_root_hashes(&self) -> Vec<BitcoinNodeHash>
fn get_root_hashes(&self) -> Vec<BitcoinNodeHash>
Returns the root hashes of our utreexo forest
Source§fn get_partial_chain(
&self,
initial_height: u32,
final_height: u32,
acc: Stump,
) -> Result<PartialChainState, BlockchainError>
fn get_partial_chain( &self, initial_height: u32, final_height: u32, acc: Stump, ) -> Result<PartialChainState, BlockchainError>
Returns a partial chainstate from a range of blocks. Read more
Auto Trait Implementations§
impl<PersistedState> !Freeze for ChainState<PersistedState>
impl<PersistedState> !RefUnwindSafe for ChainState<PersistedState>
impl<PersistedState> Send for ChainState<PersistedState>where
PersistedState: Send,
impl<PersistedState> Sync for ChainState<PersistedState>
impl<PersistedState> Unpin for ChainState<PersistedState>where
PersistedState: Unpin,
impl<PersistedState> !UnwindSafe for ChainState<PersistedState>
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
Mutably borrows from an owned value. Read more
§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>
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 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>
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