pub trait ChainStore {
    type Error: DatabaseError;

    // Required methods
    fn save_roots(&self, roots: Vec<u8>) -> Result<(), Self::Error>;
    fn load_roots(&self) -> Result<Option<Vec<u8>>, Self::Error>;
    fn load_height(&self) -> Result<Option<BestChain>, Self::Error>;
    fn save_height(&self, height: &BestChain) -> Result<(), Self::Error>;
    fn get_header(
        &self,
        block_hash: &BlockHash
    ) -> Result<Option<DiskBlockHeader>, Self::Error>;
    fn save_header(&self, header: &DiskBlockHeader) -> Result<(), Self::Error>;
    fn get_block_hash(
        &self,
        height: u32
    ) -> Result<Option<BlockHash>, Self::Error>;
    fn flush(&self) -> Result<(), Self::Error>;
    fn update_block_index(
        &self,
        height: u32,
        hash: BlockHash
    ) -> Result<(), Self::Error>;
}
Expand description

ChainStore is a trait 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§

Required Methods§

source

fn save_roots(&self, roots: Vec<u8>) -> Result<(), Self::Error>

Saves the current state of our accumulator.

source

fn load_roots(&self) -> Result<Option<Vec<u8>>, Self::Error>

Loads the state of our accumulator.

source

fn load_height(&self) -> Result<Option<BestChain>, Self::Error>

Loads the blockchain height

source

fn save_height(&self, height: &BestChain) -> Result<(), Self::Error>

Saves the blockchain height.

source

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.

source

fn save_header(&self, header: &DiskBlockHeader) -> Result<(), Self::Error>

Saves a block header to our database. See DiskBlockHeader for more info about the data we save.

source

fn get_block_hash(&self, height: u32) -> Result<Option<BlockHash>, Self::Error>

Returns the block hash for a given height.

source

fn flush(&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.

source

fn update_block_index( &self, height: u32, hash: BlockHash ) -> Result<(), Self::Error>

Associates a block hash with a given height, so we can retrieve it later.

Implementors§