Skip to content

Commit

Permalink
Cleanup ledgerdb (#1623)
Browse files Browse the repository at this point in the history
  • Loading branch information
ercecan authored Dec 19, 2024
1 parent e8c5647 commit 2b51f7b
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 317 deletions.
15 changes: 7 additions & 8 deletions bin/citrea/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use jsonrpsee::RpcModule;
use sov_db::ledger_db::migrations::LedgerDBMigrator;
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
use sov_db::rocks_db_config::RocksdbConfig;
use sov_db::schema::types::BatchNumber;
use sov_db::schema::types::SoftConfirmationNumber;
use sov_modules_api::Spec;
use sov_modules_rollup_blueprint::RollupBlueprint;
use sov_modules_stf_blueprint::{Runtime as RuntimeTrait, StfBlueprint};
Expand Down Expand Up @@ -123,7 +123,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
.get_head_soft_confirmation()
.map_err(|e| anyhow!("Failed to get head soft confirmation: {}", e))?
.map(|(l2_height, _)| l2_height)
.unwrap_or(BatchNumber(0));
.unwrap_or(SoftConfirmationNumber(0));

let mut fork_manager = ForkManager::new(FORKS, current_l2_height.0);
fork_manager.register_handler(Box::new(ledger_db.clone()));
Expand Down Expand Up @@ -254,7 +254,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
.get_head_soft_confirmation()
.map_err(|e| anyhow!("Failed to get head soft confirmation: {}", e))?
.map(|(l2_height, _)| l2_height)
.unwrap_or(BatchNumber(0));
.unwrap_or(SoftConfirmationNumber(0));

let mut fork_manager = ForkManager::new(FORKS, current_l2_height.0);
fork_manager.register_handler(Box::new(ledger_db.clone()));
Expand Down Expand Up @@ -389,12 +389,11 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
let elfs_by_spec = self.get_batch_proof_elfs();

let current_l2_height = ledger_db
.get_head_soft_confirmation()
.get_head_soft_confirmation_height()
.map_err(|e| anyhow!("Failed to get head soft confirmation: {}", e))?
.map(|(l2_height, _)| l2_height)
.unwrap_or(BatchNumber(0));
.unwrap_or(0);

let mut fork_manager = ForkManager::new(FORKS, current_l2_height.0);
let mut fork_manager = ForkManager::new(FORKS, current_l2_height);
fork_manager.register_handler(Box::new(ledger_db.clone()));

let runner = CitreaBatchProver::new(
Expand Down Expand Up @@ -488,7 +487,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
.get_head_soft_confirmation()
.map_err(|e| anyhow!("Failed to get head soft confirmation: {}", e))?
.map(|(l2_height, _)| l2_height)
.unwrap_or(BatchNumber(0));
.unwrap_or(SoftConfirmationNumber(0));

let mut fork_manager = ForkManager::new(FORKS, current_l2_height.0);
fork_manager.register_handler(Box::new(ledger_db.clone()));
Expand Down
2 changes: 1 addition & 1 deletion bin/citrea/tests/bitcoin_e2e/batch_prover_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl TestCase for SkipPreprovenCommitmentsTest {

// Wait for the full node to see all process verify and store all batch proofs
full_node.wait_for_l1_height(finalized_height, None).await?;
let proofs = wait_for_zkproofs(full_node, finalized_height, None)
let proofs = wait_for_zkproofs(full_node, finalized_height, Some(Duration::from_secs(600)))
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion bin/citrea/tests/e2e/sequencer_behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ async fn test_gas_limit_too_high() {
assert_eq!(block_from_sequencer.header.hash, block.header.hash);

seq_test_client.send_publish_batch_request().await;
wait_for_l2_block(&full_node_test_client, 2, None).await;
wait_for_l2_block(&full_node_test_client, 2, Some(Duration::from_secs(60))).await;

let block = full_node_test_client
.eth_get_block_by_number(Some(BlockNumberOrTag::Latest))
Expand Down
23 changes: 11 additions & 12 deletions crates/batch-prover/src/da_block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rand::Rng;
use serde::de::DeserializeOwned;
use serde::Serialize;
use sov_db::ledger_db::BatchProverLedgerOps;
use sov_db::schema::types::{BatchNumber, SlotNumber};
use sov_db::schema::types::{SlotNumber, SoftConfirmationNumber};
use sov_modules_api::fork::fork_from_block_number;
use sov_modules_api::{DaSpec, StateDiff, Zkvm};
use sov_rollup_interface::da::{BlockHeaderTrait, SequencerCommitment};
Expand Down Expand Up @@ -366,9 +366,9 @@ pub(crate) async fn get_batch_proof_circuit_input_from_commitments<
let mut witnesses = vec![];
let start_l2 = sequencer_commitment.l2_start_block_number;
let end_l2 = sequencer_commitment.l2_end_block_number;
let soft_confirmations_in_commitment = match ledger_db
.get_soft_confirmation_range(&(BatchNumber(start_l2)..=BatchNumber(end_l2)))
{
let soft_confirmations_in_commitment = match ledger_db.get_soft_confirmation_range(
&(SoftConfirmationNumber(start_l2)..=SoftConfirmationNumber(end_l2)),
) {
Ok(soft_confirmations) => soft_confirmations,
Err(e) => {
return Err(anyhow!(
Expand Down Expand Up @@ -451,14 +451,13 @@ pub(crate) fn break_sequencer_commitments_into_groups<DB: BatchProverLedgerOps>(
for l2_height in
sequencer_commitment.l2_start_block_number..=sequencer_commitment.l2_end_block_number
{
let state_diff =
ledger_db
.get_l2_state_diff(BatchNumber(l2_height))?
.ok_or(anyhow!(
"Could not find state diff for L2 range {}-{}",
sequencer_commitment.l2_start_block_number,
sequencer_commitment.l2_end_block_number
))?;
let state_diff = ledger_db
.get_l2_state_diff(SoftConfirmationNumber(l2_height))?
.ok_or(anyhow!(
"Could not find state diff for L2 range {}-{}",
sequencer_commitment.l2_start_block_number,
sequencer_commitment.l2_end_block_number
))?;
sequencer_commitment_state_diff =
merge_state_diffs(sequencer_commitment_state_diff, state_diff);
}
Expand Down
9 changes: 6 additions & 3 deletions crates/batch-prover/src/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use citrea_primitives::forks::FORKS;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sov_db::ledger_db::BatchProverLedgerOps;
use sov_db::schema::types::{BatchNumber, StoredBatchProof, StoredBatchProofOutput};
use sov_db::schema::types::{SoftConfirmationNumber, StoredBatchProof, StoredBatchProofOutput};
use sov_modules_api::{BatchProofCircuitOutput, BlobReaderTrait, SlotData, SpecId, Zkvm};
use sov_rollup_interface::da::{BlockHeaderTrait, DaNamespace, DaSpec, SequencerCommitment};
use sov_rollup_interface::fork::fork_from_block_number;
Expand Down Expand Up @@ -165,7 +165,7 @@ where
.expect("There should be a state root");

let initial_batch_hash = ledger
.get_soft_confirmation_by_number(&BatchNumber(first_l2_height_of_l1))
.get_soft_confirmation_by_number(&SoftConfirmationNumber(first_l2_height_of_l1))
.map_err(|e| {
L1ProcessingError::Other(format!("Error getting initial batch hash: {:?}", e))
})?
Expand Down Expand Up @@ -390,7 +390,10 @@ pub(crate) fn save_commitments<DB>(
let l2_end_height = sequencer_commitment.l2_end_block_number;
for i in l2_start_height..=l2_end_height {
ledger_db
.put_soft_confirmation_status(BatchNumber(i), SoftConfirmationStatus::Proven)
.put_soft_confirmation_status(
SoftConfirmationNumber(i),
SoftConfirmationStatus::Proven,
)
.unwrap_or_else(|_| {
panic!(
"Failed to put soft confirmation status in the ledger db {}",
Expand Down
16 changes: 7 additions & 9 deletions crates/batch-prover/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use jsonrpsee::server::{BatchRequestConfig, ServerBuilder};
use jsonrpsee::RpcModule;
use sov_db::ledger_db::BatchProverLedgerOps;
use sov_db::schema::types::{BatchNumber, SlotNumber};
use sov_db::schema::types::{SlotNumber, SoftConfirmationNumber};
use sov_ledger_rpc::LedgerRpcClient;
use sov_modules_api::{Context, SignedSoftConfirmation, SlotData, Spec};
use sov_modules_stf_blueprint::{Runtime, StfBlueprint};
Expand Down Expand Up @@ -130,12 +130,8 @@ where
}
};

// Start the main rollup loop
let item_numbers = ledger_db.get_next_items_numbers();
let last_soft_confirmation_processed_before_shutdown =
item_numbers.soft_confirmation_number;
// Last L1/L2 height before shutdown.
let start_l2_height = last_soft_confirmation_processed_before_shutdown;
let start_l2_height = ledger_db.get_head_soft_confirmation_height()?.unwrap_or(0) + 1;

Ok(Self {
start_l2_height,
Expand Down Expand Up @@ -446,8 +442,10 @@ where
}

// Save state diff to ledger DB
self.ledger_db
.set_l2_state_diff(BatchNumber(l2_height), soft_confirmation_result.state_diff)?;
self.ledger_db.set_l2_state_diff(
SoftConfirmationNumber(l2_height),
soft_confirmation_result.state_diff,
)?;

// Save witnesses data to ledger db
self.ledger_db.set_l2_witness(
Expand All @@ -472,7 +470,7 @@ where

self.ledger_db.extend_l2_range_of_l1_slot(
SlotNumber(current_l1_block.header().height()),
BatchNumber(l2_height),
SoftConfirmationNumber(l2_height),
)?;

// Register this new block with the fork manager to active
Expand Down
9 changes: 5 additions & 4 deletions crates/common/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use jsonrpsee::types::error::{INTERNAL_ERROR_CODE, INTERNAL_ERROR_MSG};
use jsonrpsee::types::{ErrorObjectOwned, Request};
use jsonrpsee::{MethodResponse, RpcModule};
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
use sov_db::schema::types::BatchNumber;
use sov_db::schema::types::SoftConfirmationNumber;
use tower_http::cors::{Any, CorsLayer};

// Exit early if head_batch_num is below this threshold
Expand All @@ -33,7 +33,7 @@ pub fn register_healthcheck_rpc<T: Send + Sync + 'static>(
)
};

let Some((BatchNumber(head_batch_num), _)) = ledger_db
let Some((SoftConfirmationNumber(head_batch_num), _)) = ledger_db
.get_head_soft_confirmation()
.map_err(|err| error(&format!("Failed to get head soft batch: {}", err)))?
else {
Expand All @@ -47,7 +47,8 @@ pub fn register_healthcheck_rpc<T: Send + Sync + 'static>(

let soft_batches = ledger_db
.get_soft_confirmation_range(
&(BatchNumber(head_batch_num - 1)..=BatchNumber(head_batch_num)),
&(SoftConfirmationNumber(head_batch_num - 1)
..=SoftConfirmationNumber(head_batch_num)),
)
.map_err(|err| error(&format!("Failed to get soft batch range: {}", err)))?;

Expand All @@ -58,7 +59,7 @@ pub fn register_healthcheck_rpc<T: Send + Sync + 'static>(
.get_head_soft_confirmation()
.map_err(|err| error(&format!("Failed to get head soft batch: {}", err)))?
.unwrap();
if new_head_batch_num > BatchNumber(head_batch_num) {
if new_head_batch_num > SoftConfirmationNumber(head_batch_num) {
Ok::<(), ErrorObjectOwned>(())
} else {
Err(error("Block number is not increasing"))
Expand Down
10 changes: 6 additions & 4 deletions crates/common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};

use sov_db::ledger_db::SharedLedgerOps;
use sov_db::schema::types::BatchNumber;
use sov_db::schema::types::SoftConfirmationNumber;
use sov_modules_api::{Context, Spec};
use sov_rollup_interface::da::{DaSpec, SequencerCommitment};
use sov_rollup_interface::digest::Digest;
Expand Down Expand Up @@ -53,8 +53,9 @@ fn filter_out_commitments_by_status<DB: SharedLedgerOps>(
visited_l2_ranges.insert(current_range);

// Check if the commitment was previously finalized.
let Some(status) = ledger_db
.get_soft_confirmation_status(BatchNumber(sequencer_commitment.l2_end_block_number))?
let Some(status) = ledger_db.get_soft_confirmation_status(SoftConfirmationNumber(
sequencer_commitment.l2_end_block_number,
))?
else {
filtered.push(sequencer_commitment.clone());
continue;
Expand All @@ -76,7 +77,8 @@ pub fn check_l2_range_exists<DB: SharedLedgerOps>(
last_l2_height_of_l1: u64,
) -> bool {
if let Ok(range) = ledger_db.get_soft_confirmation_range(
&(BatchNumber(first_l2_height_of_l1)..=BatchNumber(last_l2_height_of_l1)),
&(SoftConfirmationNumber(first_l2_height_of_l1)
..=SoftConfirmationNumber(last_l2_height_of_l1)),
) {
if (range.len() as u64) >= (last_l2_height_of_l1 - first_l2_height_of_l1 + 1) {
return true;
Expand Down
18 changes: 11 additions & 7 deletions crates/fullnode/src/da_block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use serde::de::DeserializeOwned;
use serde::Serialize;
use sov_db::ledger_db::NodeLedgerOps;
use sov_db::schema::types::{
BatchNumber, SlotNumber, StoredBatchProofOutput, StoredSoftConfirmation,
SlotNumber, SoftConfirmationNumber, StoredBatchProofOutput, StoredSoftConfirmation,
};
use sov_modules_api::{Context, Zkvm};
use sov_rollup_interface::da::{BlockHeaderTrait, SequencerCommitment};
Expand Down Expand Up @@ -238,7 +238,7 @@ where
// and compare the root with the one from the ledger
let stored_soft_confirmations: Vec<StoredSoftConfirmation> =
self.ledger_db.get_soft_confirmation_range(
&(BatchNumber(start_l2_height)..=BatchNumber(end_l2_height)),
&(SoftConfirmationNumber(start_l2_height)..=SoftConfirmationNumber(end_l2_height)),
)?;

// Make sure that the number of stored soft confirmations is equal to the range's length.
Expand Down Expand Up @@ -279,11 +279,13 @@ where
)?;

for i in start_l2_height..=end_l2_height {
self.ledger_db
.put_soft_confirmation_status(BatchNumber(i), SoftConfirmationStatus::Finalized)?;
self.ledger_db.put_soft_confirmation_status(
SoftConfirmationNumber(i),
SoftConfirmationStatus::Finalized,
)?;
}
self.ledger_db
.set_last_commitment_l2_height(BatchNumber(end_l2_height))?;
.set_last_commitment_l2_height(SoftConfirmationNumber(end_l2_height))?;

Ok(())
}
Expand Down Expand Up @@ -410,8 +412,10 @@ where
let l2_start_height = commitment.l2_start_block_number;
let l2_end_height = commitment.l2_end_block_number;
for i in l2_start_height..=l2_end_height {
self.ledger_db
.put_soft_confirmation_status(BatchNumber(i), SoftConfirmationStatus::Proven)?;
self.ledger_db.put_soft_confirmation_status(
SoftConfirmationNumber(i),
SoftConfirmationStatus::Proven,
)?;
}
}
// store in ledger db
Expand Down
6 changes: 3 additions & 3 deletions crates/fullnode/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use jsonrpsee::server::{BatchRequestConfig, RpcServiceBuilder, ServerBuilder};
use jsonrpsee::RpcModule;
use sov_db::ledger_db::NodeLedgerOps;
use sov_db::schema::types::{BatchNumber, SlotNumber};
use sov_db::schema::types::{SlotNumber, SoftConfirmationNumber};
use sov_ledger_rpc::LedgerRpcClient;
use sov_modules_api::{Context, SignedSoftConfirmation, Spec};
use sov_modules_stf_blueprint::{Runtime, StfBlueprint};
Expand Down Expand Up @@ -125,7 +125,7 @@ where
}
};

let start_l2_height = ledger_db.get_next_items_numbers().soft_confirmation_number;
let start_l2_height = ledger_db.get_head_soft_confirmation_height()?.unwrap_or(0) + 1;

info!("Starting L2 height: {}", start_l2_height);

Expand Down Expand Up @@ -293,7 +293,7 @@ where

self.ledger_db.extend_l2_range_of_l1_slot(
SlotNumber(current_l1_block.header().height()),
BatchNumber(l2_height),
SoftConfirmationNumber(l2_height),
)?;

// Register this new block with the fork manager to active
Expand Down
14 changes: 7 additions & 7 deletions crates/sequencer/src/commitment/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use citrea_common::utils::merge_state_diffs;
use citrea_primitives::compression::compress_blob;
use citrea_primitives::MAX_TXBODY_SIZE;
use sov_db::ledger_db::SequencerLedgerOps;
use sov_db::schema::types::BatchNumber;
use sov_db::schema::types::SoftConfirmationNumber;
use sov_modules_api::StateDiff;
use tracing::{debug, warn};

Expand Down Expand Up @@ -46,14 +46,14 @@ where
let last_finalized_l2_height = self
.ledger_db
.get_last_commitment_l2_height()?
.unwrap_or(BatchNumber(0));
.unwrap_or(SoftConfirmationNumber(0));
let last_pending_l2_height = self
.ledger_db
.get_pending_commitments_l2_range()?
.iter()
.map(|(_, end)| *end)
.max()
.unwrap_or(BatchNumber(0));
.unwrap_or(SoftConfirmationNumber(0));
let last_committed_l2_height = cmp::max(last_finalized_l2_height, last_pending_l2_height);

// If block state diff is empty, it is certain that state diff threshold won't be exceeded.
Expand Down Expand Up @@ -97,7 +97,7 @@ where

fn check_min_soft_confirmations(
&self,
last_committed_l2_height: BatchNumber,
last_committed_l2_height: SoftConfirmationNumber,
current_l2_height: u64,
) -> Option<CommitmentInfo> {
// If the last commitment made is on par with the head
Expand All @@ -121,13 +121,13 @@ where

debug!("Enough soft confirmations to submit commitment");
Some(CommitmentInfo {
l2_height_range: BatchNumber(l2_start)..=BatchNumber(l2_end),
l2_height_range: SoftConfirmationNumber(l2_start)..=SoftConfirmationNumber(l2_end),
})
}

fn check_state_diff_threshold(
&self,
last_committed_l2_height: BatchNumber,
last_committed_l2_height: SoftConfirmationNumber,
current_l2_height: u64,
state_diff: &StateDiff,
) -> Option<CommitmentInfo> {
Expand Down Expand Up @@ -157,7 +157,7 @@ where

debug!("Enough state diff size to submit commitment");
Some(CommitmentInfo {
l2_height_range: BatchNumber(l2_start)..=BatchNumber(l2_end),
l2_height_range: SoftConfirmationNumber(l2_start)..=SoftConfirmationNumber(l2_end),
})
}

Expand Down
Loading

0 comments on commit 2b51f7b

Please sign in to comment.