pub trait UpdatableChainstate {
// Required methods
fn connect_block(
&self,
block: &Block,
proof: Proof,
inputs: HashMap<OutPoint, TxOut>,
del_hashes: Vec<Hash>
) -> Result<u32, BlockchainError>;
fn switch_chain(&self, new_tip: BlockHash) -> Result<(), BlockchainError>;
fn accept_header(&self, header: BlockHeader) -> Result<(), BlockchainError>;
fn handle_transaction(&self) -> Result<(), BlockchainError>;
fn flush(&self) -> Result<(), BlockchainError>;
fn toggle_ibd(&self, is_ibd: bool);
fn invalidate_block(&self, block: BlockHash) -> Result<(), BlockchainError>;
fn mark_block_as_valid(
&self,
block: BlockHash
) -> Result<(), BlockchainError>;
fn process_rescan_block(&self, block: &Block) -> Result<(), BlockchainError>;
fn get_root_hashes(&self) -> Vec<NodeHash>;
fn get_partial_chain(
&self,
initial_height: u32,
final_height: u32,
acc: Stump
) -> Result<PartialChainState, BlockchainError>;
fn mark_chain_as_assumed(
&self,
acc: Stump,
tip: BlockHash
) -> Result<bool, BlockchainError>;
}
Expand description
UpdatableChainstate is a contract that a is expected from a chainstate implementation, that wishes to be updated. Using those methods, a backend like the p2p-node, can notify new blocks and transactions to a chainstate, allowing it to update it’s state.
Required Methods§
sourcefn connect_block(
&self,
block: &Block,
proof: Proof,
inputs: HashMap<OutPoint, TxOut>,
del_hashes: Vec<Hash>
) -> Result<u32, BlockchainError>
fn connect_block( &self, block: &Block, proof: Proof, inputs: HashMap<OutPoint, TxOut>, 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.
fn switch_chain(&self, new_tip: BlockHash) -> Result<(), BlockchainError>
sourcefn 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.
This function returns whether this block is on our best-known chain, or in a fork
sourcefn 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.
sourcefn flush(&self) -> Result<(), BlockchainError>
fn flush(&self) -> Result<(), BlockchainError>
Persists our data. Should be invoked periodically.
sourcefn toggle_ibd(&self, is_ibd: bool)
fn toggle_ibd(&self, is_ibd: bool)
Toggle ibd on/off
sourcefn 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
sourcefn 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.
sourcefn process_rescan_block(&self, block: &Block) -> Result<(), BlockchainError>
fn process_rescan_block(&self, block: &Block) -> Result<(), BlockchainError>
Gives a requested block for rescan
sourcefn get_root_hashes(&self) -> Vec<NodeHash>
fn get_root_hashes(&self) -> Vec<NodeHash>
Returns the root hashes of our utreexo forest
sourcefn 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.
PartialChainState is a simplified version of ChainState
that is used during IBD.
It doesn’t suport reorgs, only hold headers for a subset of blocks and isn’t Sync.
The idea here is that you take a OS thread or some async task that will drive one
PartialChainState to completion by downloading blocks inside that chainstate’s range.
If all goes right, it’ll end without error, and you should mark blocks in this range as
valid.
Since this chainstate may start from a height with an existing UTXO set, you need to provide a Stump for that block.
sourcefn mark_chain_as_assumed(
&self,
acc: Stump,
tip: BlockHash
) -> Result<bool, BlockchainError>
fn mark_chain_as_assumed( &self, acc: Stump, tip: BlockHash ) -> Result<bool, BlockchainError>
Marks a chain as fully-valid
This mimics the behavour of checking every block before this block, and continues from this point