Skip to content

Commit

Permalink
LedgerDB namespacing (#917)
Browse files Browse the repository at this point in the history
* Split up LedgerDB by Ops type

* Propagate errors

* Cleanup generic

* Lint

* Make lint

* Fix tests
  • Loading branch information
jfldde authored Jul 30, 2024
1 parent f332d3e commit 49d2f47
Show file tree
Hide file tree
Showing 14 changed files with 576 additions and 357 deletions.
1 change: 1 addition & 0 deletions bin/citrea/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use citrea_fullnode::{CitreaFullnode, FullNode};
use citrea_prover::{CitreaProver, Prover};
use citrea_sequencer::{CitreaSequencer, Sequencer, SequencerConfig};
pub use mock::*;
use sov_db::ledger_db::SharedLedgerOps;
use sov_modules_api::storage::HierarchicalStorageManager;
use sov_modules_api::Spec;
use sov_modules_rollup_blueprint::RollupBlueprint;
Expand Down
2 changes: 1 addition & 1 deletion bin/citrea/src/test_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use proptest::{prop_compose, proptest};
use reqwest::header::CONTENT_TYPE;
use serde_json::json;
use sha2::Digest;
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_db::ledger_db::{LedgerDB, SequencerLedgerOps, SharedLedgerOps, SlotCommit};
use sov_mock_da::MockDaSpec;
#[cfg(test)]
use sov_mock_da::{MockBlock, MockBlockHeader, MockHash};
Expand Down
2 changes: 2 additions & 0 deletions crates/fullnode/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::net::SocketAddr;

pub use runner::*;
use sov_db::ledger_db::LedgerDB;
use sov_modules_rollup_blueprint::RollupBlueprint;
use sov_modules_stf_blueprint::StfBlueprint;
use tokio::sync::oneshot;
Expand All @@ -18,6 +19,7 @@ pub struct FullNode<S: RollupBlueprint> {
S::DaService,
S::Vm,
S::NativeContext,
LedgerDB,
>,
/// Rpc methods for the rollup.
pub rpc_methods: jsonrpsee::RpcModule<()>,
Expand Down
13 changes: 7 additions & 6 deletions crates/fullnode/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use jsonrpsee::RpcModule;
use rs_merkle::algorithms::Sha256;
use rs_merkle::MerkleTree;
use sequencer_client::{GetSoftBatchResponse, SequencerClient};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_db::ledger_db::{NodeLedgerOps, SlotCommit};
use sov_db::schema::types::{BatchNumber, SlotNumber, StoredSoftBatch, StoredStateTransition};
use sov_modules_api::Context;
use sov_modules_stf_blueprint::StfBlueprintTrait;
Expand All @@ -36,22 +36,22 @@ use tracing::{debug, error, info, instrument, warn};
type StateRoot<ST, Vm, Da> = <ST as StateTransitionFunction<Vm, Da>>::StateRoot;

/// Citrea's own STF runner implementation.
pub struct CitreaFullnode<Stf, Sm, Da, Vm, C>
pub struct CitreaFullnode<Stf, Sm, Da, Vm, C, DB>
where
Da: DaService,
Vm: ZkvmHost + Zkvm,
Sm: HierarchicalStorageManager<Da::Spec>,
Stf: StateTransitionFunction<Vm, Da::Spec, Condition = <Da::Spec as DaSpec>::ValidityCondition>
+ StfBlueprintTrait<C, Da::Spec, Vm>,
C: Context,
DB: NodeLedgerOps,
{
start_l2_height: u64,
start_l1_height: u64,
da_service: Da,
stf: Stf,
storage_manager: Sm,
/// made pub so that sequencer can clone it
pub ledger_db: LedgerDB,
ledger_db: DB,
state_root: StateRoot<Stf, Vm, Da::Spec>,
batch_hash: SoftConfirmationHash,
rpc_config: RpcConfig,
Expand All @@ -68,7 +68,7 @@ where
soft_confirmation_tx: broadcast::Sender<u64>,
}

impl<Stf, Sm, Da, Vm, C> CitreaFullnode<Stf, Sm, Da, Vm, C>
impl<Stf, Sm, Da, Vm, C, DB> CitreaFullnode<Stf, Sm, Da, Vm, C, DB>
where
Da: DaService<Error = anyhow::Error> + Clone + Send + Sync + 'static,
Vm: ZkvmHost + Zkvm,
Expand All @@ -81,6 +81,7 @@ where
ChangeSet = Sm::NativeChangeSet,
> + StfBlueprintTrait<C, Da::Spec, Vm>,
C: Context,
DB: NodeLedgerOps,
{
/// Creates a new `StateTransitionRunner`.
///
Expand All @@ -93,7 +94,7 @@ where
public_keys: RollupPublicKeys,
rpc_config: RpcConfig,
da_service: Da,
ledger_db: LedgerDB,
ledger_db: DB,
stf: Stf,
mut storage_manager: Sm,
init_variant: InitVariant<Stf, Vm, Da::Spec>,
Expand Down
1 change: 1 addition & 0 deletions crates/fullnode/tests/runner_initialization_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn initialize_runner(
MockDaService,
MockZkvm<MockValidityCond>,
sov_modules_api::default_context::DefaultContext,
LedgerDB,
> {
let da_storage_path = storage_path.join("da").to_path_buf();
let rollup_storage_path = storage_path.join("rollup").to_path_buf();
Expand Down
4 changes: 2 additions & 2 deletions crates/fullnode/tests/runner_reorg_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sov_stf_runner::{
mod hash_stf;

use hash_stf::{get_result_from_blocks, HashStf, Q, S};
use sov_db::ledger_db::LedgerDB;
use sov_db::ledger_db::{LedgerDB, NodeLedgerOps};
use sov_mock_zkvm::MockCodeCommitment;
use sov_prover_storage_manager::ProverStorageManager;
use sov_rollup_interface::services::da::DaService;
Expand Down Expand Up @@ -159,7 +159,7 @@ async fn runner_execution(
};
let storage_manager = ProverStorageManager::new(storage_config).unwrap();

let mut runner: CitreaFullnode<_, _, _, _, DefaultContext> = CitreaFullnode::new(
let mut runner: CitreaFullnode<_, _, _, _, DefaultContext, _> = CitreaFullnode::new(
rollup_config.runner.unwrap(),
rollup_config.public_keys,
rollup_config.rpc,
Expand Down
2 changes: 2 additions & 0 deletions crates/prover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::net::SocketAddr;

use sov_db::ledger_db::LedgerDB;
use sov_modules_rollup_blueprint::RollupBlueprint;
use sov_modules_stf_blueprint::StfBlueprint;
use tokio::sync::oneshot;
Expand All @@ -20,6 +21,7 @@ pub struct Prover<S: RollupBlueprint> {
S::Vm,
StfBlueprint<S::NativeContext, S::DaSpec, S::Vm, S::NativeRuntime>,
S::ProverService,
LedgerDB,
>,
/// Rpc methods for the rollup.
pub rpc_methods: jsonrpsee::RpcModule<()>,
Expand Down
13 changes: 7 additions & 6 deletions crates/prover/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use jsonrpsee::RpcModule;
use rand::Rng;
use sequencer_client::{GetSoftBatchResponse, SequencerClient};
use shared_backup_db::{DbPoolError, PostgresConnector, ProofType};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_db::ledger_db::{ProverLedgerOps, SlotCommit};
use sov_db::schema::types::{BatchNumber, SlotNumber, StoredStateTransition};
use sov_modules_api::storage::HierarchicalStorageManager;
use sov_modules_api::{BlobReaderTrait, Context, SignedSoftConfirmationBatch, SlotData};
Expand All @@ -42,7 +42,7 @@ type CommitmentStateTransitionData<Stf, Vm, Da> = (
VecDeque<Vec<<<Da as DaService>::Spec as DaSpec>::BlockHeader>>,
);

pub struct CitreaProver<C, Da, Sm, Vm, Stf, Ps>
pub struct CitreaProver<C, Da, Sm, Vm, Stf, Ps, DB>
where
C: Context,
Da: DaService,
Expand All @@ -52,13 +52,13 @@ where
+ StfBlueprintTrait<C, Da::Spec, Vm>,

Ps: ProverService<Vm>,
DB: ProverLedgerOps + Clone,
{
start_l2_height: u64,
da_service: Da,
stf: Stf,
storage_manager: Sm,
/// made pub so that sequencer can clone it
pub ledger_db: LedgerDB,
ledger_db: DB,
state_root: StateRoot<Stf, Vm, Da::Spec>,
batch_hash: SoftConfirmationHash,
rpc_config: RpcConfig,
Expand All @@ -75,7 +75,7 @@ where
soft_confirmation_tx: broadcast::Sender<u64>,
}

impl<C, Da, Sm, Vm, Stf, Ps> CitreaProver<C, Da, Sm, Vm, Stf, Ps>
impl<C, Da, Sm, Vm, Stf, Ps, DB> CitreaProver<C, Da, Sm, Vm, Stf, Ps, DB>
where
C: Context,
Da: DaService<Error = anyhow::Error> + Clone + Send + Sync + 'static,
Expand All @@ -89,6 +89,7 @@ where
ChangeSet = Sm::NativeChangeSet,
> + StfBlueprintTrait<C, Da::Spec, Vm>,
Ps: ProverService<Vm, StateRoot = Stf::StateRoot, Witness = Stf::Witness, DaService = Da>,
DB: ProverLedgerOps + Clone,
{
/// Creates a new `StateTransitionRunner`.
///
Expand All @@ -101,7 +102,7 @@ where
public_keys: RollupPublicKeys,
rpc_config: RpcConfig,
da_service: Da,
ledger_db: LedgerDB,
ledger_db: DB,
stf: Stf,
mut storage_manager: Sm,
init_variant: InitVariant<Stf, Vm, Da::Spec>,
Expand Down
6 changes: 3 additions & 3 deletions crates/sequencer/src/commitment_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::RangeInclusive;
use anyhow::anyhow;
use rs_merkle::algorithms::Sha256;
use rs_merkle::MerkleTree;
use sov_db::ledger_db::LedgerDB;
use sov_db::ledger_db::SequencerLedgerOps;
use sov_db::schema::types::BatchNumber;
use sov_rollup_interface::da::SequencerCommitment;
use tracing::{debug, instrument};
Expand All @@ -19,8 +19,8 @@ pub struct CommitmentInfo {
/// Returns none if the commitable L2 block range is shorter than `min_soft_confirmations_per_commitment`
/// Returns `CommitmentInfo` if the sequencer should commit
#[instrument(level = "debug", skip_all, fields(prev_l1_height), err)]
pub fn get_commitment_info(
ledger_db: &LedgerDB,
pub fn get_commitment_info<T: SequencerLedgerOps>(
ledger_db: &T,
min_soft_confirmations_per_commitment: u64,
state_diff_threshold_reached: bool,
) -> anyhow::Result<Option<CommitmentInfo>> {
Expand Down
2 changes: 2 additions & 0 deletions crates/sequencer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::net::SocketAddr;

pub use config::{SequencerConfig, SequencerMempoolConfig};
pub use sequencer::CitreaSequencer;
use sov_db::ledger_db::LedgerDB;
use sov_modules_rollup_blueprint::RollupBlueprint;
use sov_modules_stf_blueprint::StfBlueprint;
use tokio::sync::oneshot;
Expand All @@ -26,6 +27,7 @@ pub struct Sequencer<S: RollupBlueprint> {
S::StorageManager,
S::Vm,
StfBlueprint<S::NativeContext, S::DaSpec, S::Vm, S::NativeRuntime>,
LedgerDB,
>,
/// Rpc methods for the rollup.
pub rpc_methods: jsonrpsee::RpcModule<()>,
Expand Down
12 changes: 7 additions & 5 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use shared_backup_db::{CommitmentStatus, PostgresConnector};
use soft_confirmation_rule_enforcer::SoftConfirmationRuleEnforcer;
use sov_accounts::Accounts;
use sov_accounts::Response::{AccountEmpty, AccountExists};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_db::ledger_db::{SequencerLedgerOps, SlotCommit};
use sov_db::schema::types::{BatchNumber, SlotNumber};
use sov_modules_api::hooks::HookSoftConfirmationInfo;
use sov_modules_api::transaction::Transaction;
Expand Down Expand Up @@ -64,14 +64,15 @@ type StateRoot<ST, Vm, Da> = <ST as StateTransitionFunction<Vm, Da>>::StateRoot;
/// Contains previous height, latest finalized block and fee rate.
type L1Data<Da> = (<Da as DaService>::FilteredBlock, u128);

pub struct CitreaSequencer<C, Da, Sm, Vm, Stf>
pub struct CitreaSequencer<C, Da, Sm, Vm, Stf, DB>
where
C: Context,
Da: DaService,
Sm: HierarchicalStorageManager<Da::Spec>,
Vm: ZkvmHost,
Stf: StateTransitionFunction<Vm, Da::Spec, Condition = <Da::Spec as DaSpec>::ValidityCondition>
+ StfBlueprintTrait<C, Da::Spec, Vm>,
DB: SequencerLedgerOps + Send + Clone + 'static,
{
da_service: Da,
mempool: Arc<CitreaMempool<C>>,
Expand All @@ -80,7 +81,7 @@ where
l2_force_block_rx: UnboundedReceiver<()>,
db_provider: DbProvider<C>,
storage: C::Storage,
ledger_db: LedgerDB,
ledger_db: DB,
config: SequencerConfig,
stf: Stf,
deposit_mempool: Arc<Mutex<DepositDataMempool>>,
Expand All @@ -99,7 +100,7 @@ enum L2BlockMode {
NotEmpty,
}

impl<C, Da, Sm, Vm, Stf> CitreaSequencer<C, Da, Sm, Vm, Stf>
impl<C, Da, Sm, Vm, Stf, DB> CitreaSequencer<C, Da, Sm, Vm, Stf, DB>
where
C: Context,
Da: DaService + Clone,
Expand All @@ -112,6 +113,7 @@ where
PreState = Sm::NativeStorage,
ChangeSet = Sm::NativeChangeSet,
> + StfBlueprintTrait<C, Da::Spec, Vm>,
DB: SequencerLedgerOps + Send + Clone + 'static,
{
#[allow(clippy::too_many_arguments)]
pub fn new(
Expand All @@ -122,7 +124,7 @@ where
mut storage_manager: Sm,
init_variant: InitVariant<Stf, Vm, Da::Spec>,
public_keys: RollupPublicKeys,
ledger_db: LedgerDB,
ledger_db: DB,
rpc_config: RpcConfig,
soft_confirmation_tx: broadcast::Sender<u64>,
) -> anyhow::Result<Self> {
Expand Down
Loading

0 comments on commit 49d2f47

Please sign in to comment.