Trait ChainStore

Source
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

A trait defining methods for interacting with our chain database. These methods will be used by the ChainState to save and retrieve data about the blockchain, likely on disk.

This trait requires an associated error type that implements DatabaseError; a marker trait satisfied by any T: Display + Error. This is useful to abstract the database implementation from the blockchain.

Required Associated Types§

Required Methods§

Source

fn save_roots_for_block( &mut self, roots: Vec<u8>, height: u32, ) -> Result<(), Self::Error>

Saves the accumulator state for a given block height.

Source

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.

Source

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

Loads the blockchain height

Source

fn save_height(&mut 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 get_header_by_height( &self, height: u32, ) -> Result<Option<DiskBlockHeader>, Self::Error>

Get a block header by its height in our database.

Source

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.

Source

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

Returns the block hash for a given height.

Source

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.

Source

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.

Source

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§