pub trait ChainStore {
type Error: DatabaseError;
// Required methods
fn save_roots_for_block(
&mut self,
roots: Vec<u8>,
height: u32,
) -> Result<(), Self::Error>;
fn load_roots_for_block(
&mut self,
height: u32,
) -> Result<Option<Vec<u8>>, Self::Error>;
fn load_height(&self) -> Result<Option<BestChain>, Self::Error>;
fn save_height(&mut self, height: &BestChain) -> Result<(), Self::Error>;
fn get_header(
&self,
block_hash: &BlockHash,
) -> Result<Option<DiskBlockHeader>, Self::Error>;
fn get_header_by_height(
&self,
height: u32,
) -> Result<Option<DiskBlockHeader>, Self::Error>;
fn save_header(
&mut self,
header: &DiskBlockHeader,
) -> Result<(), Self::Error>;
fn get_block_hash(
&self,
height: u32,
) -> Result<Option<BlockHash>, Self::Error>;
fn flush(&mut self) -> Result<(), Self::Error>;
fn update_block_index(
&mut self,
height: u32,
hash: BlockHash,
) -> Result<(), Self::Error>;
fn check_integrity(&self) -> Result<(), Self::Error>;
}Expand description
This trait is defining how we interact with our chain database. This definitions
will be used by the ChainState to save and retrieve data about the blockchain, likely
on disk.
Right now, you can use the KvChainStore in your code, it implements this trait and
uses a key-value store to save data.
The DatabaseError is a simple trait that can be implemented by any error type that
implements std::error::Error and std::fmt::Display. This is useful to abstract
the database implementation from the blockchain.
See the documentation of DatabaseError for more info.
Required Associated Types§
type Error: DatabaseError
Required Methods§
Sourcefn save_roots_for_block(
&mut self,
roots: Vec<u8>,
height: u32,
) -> Result<(), Self::Error>
fn save_roots_for_block( &mut self, roots: Vec<u8>, height: u32, ) -> Result<(), Self::Error>
Saves the accumulator state for a given block height.
Sourcefn load_roots_for_block(
&mut self,
height: u32,
) -> Result<Option<Vec<u8>>, Self::Error>
fn load_roots_for_block( &mut self, height: u32, ) -> Result<Option<Vec<u8>>, Self::Error>
Loads the state of our accumulator for a given block height.
This is the state of the resulting accumulator after we process the block at height. If you
need the accumulator used to validate a block at height n, you should get the accumulator
from block n - 1.
Sourcefn save_height(&mut self, height: &BestChain) -> Result<(), Self::Error>
fn save_height(&mut self, height: &BestChain) -> Result<(), Self::Error>
Saves the blockchain height.
Sourcefn get_header(
&self,
block_hash: &BlockHash,
) -> Result<Option<DiskBlockHeader>, Self::Error>
fn get_header( &self, block_hash: &BlockHash, ) -> Result<Option<DiskBlockHeader>, Self::Error>
Get a block header from our database. See DiskBlockHeader for more info about the data we save.
Sourcefn get_header_by_height(
&self,
height: u32,
) -> Result<Option<DiskBlockHeader>, Self::Error>
fn get_header_by_height( &self, height: u32, ) -> Result<Option<DiskBlockHeader>, Self::Error>
Get a block header by its height in our database.
Sourcefn save_header(&mut self, header: &DiskBlockHeader) -> Result<(), Self::Error>
fn save_header(&mut self, header: &DiskBlockHeader) -> Result<(), Self::Error>
Saves a block header to our database. See DiskBlockHeader for more info about the data we save.
Sourcefn get_block_hash(&self, height: u32) -> Result<Option<BlockHash>, Self::Error>
fn get_block_hash(&self, height: u32) -> Result<Option<BlockHash>, Self::Error>
Returns the block hash for a given height.
Sourcefn flush(&mut self) -> Result<(), Self::Error>
fn flush(&mut self) -> Result<(), Self::Error>
Flushes write buffers to disk, this is called periodically by the ChainState, so in case of a crash, we don’t lose too much data. If the database doesn’t support write buffers, this method can be a no-op.
Sourcefn update_block_index(
&mut self,
height: u32,
hash: BlockHash,
) -> Result<(), Self::Error>
fn update_block_index( &mut self, height: u32, hash: BlockHash, ) -> Result<(), Self::Error>
Associates a block hash with a given height, so we can retrieve it later.
Sourcefn check_integrity(&self) -> Result<(), Self::Error>
fn check_integrity(&self) -> Result<(), Self::Error>
Checks if our database didn’t get corrupted, and if it has, it returns an error.
If you’re using a database that already checks for integrity by itself, this can safely be a no-op.
Implementors§
Source§impl ChainStore for FlatChainStore
Available on crate feature flat-chainstore only.
impl ChainStore for FlatChainStore
flat-chainstore only.type Error = FlatChainstoreError
Source§impl ChainStore for KvChainStore<'_>
Available on crate feature kv-chainstore only.
impl ChainStore for KvChainStore<'_>
kv-chainstore only.