Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: trait-based storage API #12616

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

feat: trait-based storage API #12616

wants to merge 4 commits into from

Conversation

klkvr
Copy link
Collaborator

@klkvr klkvr commented Nov 17, 2024

ref #12157

This PR introduces a way to customize how DatabaseProvider writes primitive types to storage. This is done by introducing the following traits:

/// Trait that implements how block bodies are written to the storage.
///
/// Note: Within the current abstraction, this should only write to tables unrelated to
/// transactions. Writing of transactions is handled separately.
#[auto_impl::auto_impl(&, Arc)]
pub trait BlockBodyWriter<Provider, Body: BlockBody> {
/// Writes a set of block bodies to the storage.
fn write_block_bodies(
&self,
provider: &Provider,
bodies: Vec<(BlockNumber, Option<Body>)>,
) -> ProviderResult<()>;
}
/// Trait that implements how chain-specific types are written to the storage.
pub trait ChainStorageWriter<Provider, Primitives: FullNodePrimitives>:
BlockBodyWriter<Provider, <Primitives::Block as Block>::Body>
{
}
impl<T, Provider, Primitives: FullNodePrimitives> ChainStorageWriter<Provider, Primitives> for T where
T: BlockBodyWriter<Provider, <Primitives::Block as Block>::Body>
{
}

Those are generic over provider and primitive types and allow to define type that given a specifc provider implementation knows how to use it to write given object to database.

Additionaly we define the actual ChainStorage trait which is responsible for giving DatabaseProvider the storage implementations:

/// Trait that provides access to implementations of [`ChainStorage`]
pub trait ChainStorage<Primitives: FullNodePrimitives>: Send + Sync {
/// Provides access to the chain writer.
fn writer<TX, Types>(&self) -> impl ChainStorageWriter<DatabaseProvider<TX, Types>, Primitives>
where
TX: DbTxMut + DbTx + 'static,
Types: NodeTypes<Primitives = Primitives>;
}

This trait is required due to DatabaseProvider being generic over TX and Types and thus not allowing us to express requirements through just a single bound like Storage: ChainStorage<DatabaseProvider<???, N>, N::Primitives>

klkvr and others added 2 commits November 18, 2024 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant