Skip to content

Commit

Permalink
db: added accessors to Database trait and added CommonDatabase type
Browse files Browse the repository at this point in the history
  • Loading branch information
delbonis committed May 29, 2024
1 parent 0f7ca6a commit 257c55c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
73 changes: 73 additions & 0 deletions crates/db/src/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::sync::Arc;

use super::traits::*;

/// Shim database type that assumes that all the database impls are wrapped in
/// `Arc`s and that the provider and stores are actually the same types. We
/// might actually use this in practice, it's just for testing.
pub struct CommonDatabase<L1, L2, S, C> {
l1db: Arc<L1>,
l2db: Arc<L2>,
sedb: Arc<S>,
csdb: Arc<C>,
}

impl<L1, L2, S, C> CommonDatabase<L1, L2, S, C> {
pub fn new(l1db: Arc<L1>, l2db: Arc<L2>, sedb: Arc<S>, csdb: Arc<C>) -> Self {
Self {
l1db,
l2db,
sedb,
csdb,
}
}
}

impl<
L1: L1DataStore + L1DataProvider,
L2: L2DataStore + L2DataProvider,
S: SyncEventStore + SyncEventProvider,
C: ConsensusStateStore + ConsensusStateProvider,
> Database for CommonDatabase<L1, L2, S, C>
{
type L1Store = L1;
type L1Prov = L1;
type L2Store = L2;
type L2Prov = L2;
type SeStore = S;
type SeProv = S;
type CsStore = C;
type CsProv = C;

fn l1_store(&self) -> &Arc<Self::L1Store> {
&self.l1db
}

fn l1_provider(&self) -> &Arc<Self::L1Prov> {
&self.l1db
}

fn l2_store(&self) -> &Arc<Self::L2Store> {
&self.l2db
}

fn l2_provider(&self) -> &Arc<Self::L2Prov> {
&self.l2db
}

fn sync_event_store(&self) -> &Arc<Self::SeStore> {
&self.sedb
}

fn sync_event_provider(&self) -> &Arc<Self::SeProv> {
&self.sedb
}

fn consensus_state_store(&self) -> &Arc<Self::CsStore> {
&self.csdb
}

fn consensus_state_provider(&self) -> &Arc<Self::CsProv> {
&self.csdb
}
}
2 changes: 2 additions & 0 deletions crates/db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Database abstraction layers, building on what Reth supports.
pub mod database;

pub mod errors;
pub mod traits;
11 changes: 10 additions & 1 deletion crates/db/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Trait definitions for low level database interfaces. This borrows some of
//! its naming conventions from reth.
use std::sync::Arc;

use alpen_vertex_mmr::CompactMmr;
use alpen_vertex_primitives::{l1::*, prelude::*};
use alpen_vertex_state::block::{L2Block, L2BlockId};
Expand All @@ -23,7 +25,14 @@ pub trait Database {
type CsStore: ConsensusStateStore;
type CsProv: ConsensusStateProvider;

// TODO accessors as needed
fn l1_store(&self) -> &Arc<Self::L1Store>;
fn l1_provider(&self) -> &Arc<Self::L1Prov>;
fn l2_store(&self) -> &Arc<Self::L2Store>;
fn l2_provider(&self) -> &Arc<Self::L2Prov>;
fn sync_event_store(&self) -> &Arc<Self::SeStore>;
fn sync_event_provider(&self) -> &Arc<Self::SeProv>;
fn consensus_state_store(&self) -> &Arc<Self::CsStore>;
fn consensus_state_provider(&self) -> &Arc<Self::CsProv>;
}

/// Storage interface to control our view of L1 data.
Expand Down

0 comments on commit 257c55c

Please sign in to comment.