Using ChainState as Chain Backend
Recall that
UtreexoNodeis the high-level component of Floresta that will interact with the P2P network. It is made of itsContextand aNodeCommonthat holds aChainbackend (for validation and state tracking). This and the following chapters will focus solely on the Floresta chain backend.
In this chapter we will learn about ChainState, a type that implements UpdatableChainstate + BlockchainInterface, so we can use it as Chain backend.
ChainState is the default blockchain backend provided by Floresta, with all its logic encapsulated within the floresta-chain library.
The following associations clarify the module structure:
UtreexoNodedeclaration and logic resides infloresta-wireChainStatedeclaration and logic resides infloresta-chain
ChainState Structure
The job of
ChainStateis to validate blocks, update the state and store it.
For a node to keep track of the chain state effectively, it is important to ensure the state is persisted to some sort of storage system. This way the node progress is saved, and we can recover it after the device is turned off.
That's why ChainState uses a generic PersistedState type, bounded by the ChainStore trait, which defines how we interact with our persistent state database.

Figure 2: Diagram of the ChainState type.
Filename: pruned_utreexo/chain_state.rs
// Path: floresta-chain/src/pruned_utreexo/chain_state.rs
pub struct ChainState<PersistedState: ChainStore> {
inner: RwLock<ChainStateInner<PersistedState>>,
}
// Path: floresta-chain/src/pruned_utreexo/chain_state.rs
impl<PersistedState: ChainStore> BlockchainInterface for ChainState<PersistedState> {
// Implementation of BlockchainInterface for ChainState
// Path: floresta-chain/src/pruned_utreexo/chain_state.rs
impl<PersistedState: ChainStore> UpdatableChainstate for ChainState<PersistedState> {
// Implementation of UpdatableChainstate for ChainState
As Floresta is currently only pruned, the expected database primarily consists of the block header chain and the utreexo accumulator; blocks themselves are not stored.
The default implementation of ChainStore is FlatChainStore, but we also provide a simpler KvChainStore implementation. This means that developers may:
- Implement custom
UpdatableChainstate + BlockchainInterfacetypes for use asChainbackends. - Use the provided
ChainStatebackend:- With their own
ChainStoreimplementation. - Or the provided
FlatChainStoreorKvChainStoreimplementations.
- With their own
Next, let’s build the ChainState struct step by step!