Skip to content

Commit

Permalink
Fix intersecting l2 commitment blocks (#853)
Browse files Browse the repository at this point in the history
* Fix compare_commitments_from_db

* Replace unwrap with ?

* Lint

* Use last batch

* Rename to sync_commitments_from_db

* Use get_soft_batch_by_number
  • Loading branch information
yaziciahmet authored Jul 4, 2024
1 parent 8f99def commit 8672a72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
6 changes: 2 additions & 4 deletions bin/citrea/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ async fn sequencer_crash_and_replace_full_node() -> Result<(), anyhow::Error> {

sequencer_config.db_config = Some(SharedBackupDbConfig::default().set_db_name(psql_db_name));

let da_service = MockDaService::with_finality(MockAddress::from([0; 32]), 2, &da_db_dir);
let da_service = MockDaService::with_finality(MockAddress::from([0; 32]), 0, &da_db_dir);
da_service.publish_test_block().await.unwrap();

let (seq_port_tx, seq_port_rx) = tokio::sync::oneshot::channel();
Expand Down Expand Up @@ -2067,9 +2067,7 @@ async fn sequencer_crash_and_replace_full_node() -> Result<(), anyhow::Error> {
assert_eq!(commitments.len(), 2);
assert_eq!(commitments[0].l2_start_height, 1);
assert_eq!(commitments[0].l2_end_height, 4);
// TODO: This is a bug that should be checked.
// The second commitment L2 start height should be 5
assert_eq!(commitments[1].l2_start_height, 1);
assert_eq!(commitments[1].l2_start_height, 5);
assert_eq!(commitments[1].l2_end_height, 9);

seq_task.abort();
Expand Down
36 changes: 22 additions & 14 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use sov_modules_api::{
};
use sov_modules_stf_blueprint::StfBlueprintTrait;
use sov_rollup_interface::da::{BlockHeaderTrait, DaData, DaSpec};
use sov_rollup_interface::rpc::LedgerRpcProvider;
use sov_rollup_interface::services::da::{BlobWithNotifier, DaService};
use sov_rollup_interface::stf::{SoftBatchReceipt, StateTransitionFunction};
use sov_rollup_interface::storage::HierarchicalStorageManager;
Expand Down Expand Up @@ -675,7 +676,7 @@ where
if let Some(db_config) = self.config.db_config.clone() {
pg_pool = match PostgresConnector::new(db_config).await {
Ok(pg_connector) => {
match self.compare_commitments_from_db(pg_connector.clone()).await {
match self.sync_commitments_from_db(pg_connector.clone()).await {
Ok(()) => debug!("Sequencer: Commitments are in sync"),
Err(e) => {
warn!("Sequencer: Offchain db error: {:?}", e);
Expand Down Expand Up @@ -1012,26 +1013,33 @@ where
Ok(())
}

pub async fn compare_commitments_from_db(
pub async fn sync_commitments_from_db(
&self,
pg_connector: PostgresConnector,
) -> Result<(), anyhow::Error> {
let db_commitment = match pg_connector.get_last_commitment().await? {
Some(comm) => comm,
// ignore if postgres is out of sync
None => return Ok(()),
};
let ledger_commitment_l2_height = self
.ledger_db
.get_last_sequencer_commitment_l2_height()?
.ok_or(anyhow!("No commitment exists"))?;

let db_commitment = pg_connector.get_last_commitment().await?;
// check if last commitment in db matches sequencer's last commitment
if let Some(db_commitment) = db_commitment {
// this means that the last commitment in the db is not the same as the sequencer's last commitment
if db_commitment.l2_start_height > ledger_commitment_l2_height.0 {
self.ledger_db
.set_last_sequencer_commitment_l2_height(BatchNumber(
db_commitment.l2_end_height,
))?
}
.unwrap_or_default();
if ledger_commitment_l2_height.0 >= db_commitment.l2_end_height {
return Ok(());
}

self.ledger_db
.set_last_sequencer_commitment_l2_height(BatchNumber(db_commitment.l2_end_height))?;

let l2_end_batch = self
.ledger_db
.get_soft_batch_by_number::<()>(db_commitment.l2_end_height)?
.unwrap();
self.ledger_db
.set_last_sequencer_commitment_l1_height(SlotNumber(l2_end_batch.da_slot_height))?;

Ok(())
}

Expand Down

0 comments on commit 8672a72

Please sign in to comment.