Skip to content

Commit

Permalink
Commit when diff size > 300KB (#854)
Browse files Browse the repository at this point in the history
* Introduce StateDiff tables

* Submit commitment when state diff > 300Kb

* Add table to list

* Add comments

* Pick a better name for constant

* Collect diff in prover storage

* Add flag to force commitment

* Fix state diff calculation

* Add test

* Fix clippy

* Merge diffs

* Deploy contract to generate large diffs

* Fix waits

* Fix clippy issues

* Use a different contract

* Add disabled logging lines

* Commit / reset logic

* Disable transport logs

* Send transactions and fill diff

* Use clone_from as clippy suggests

* Remove InfiniteLoopContract import
  • Loading branch information
rakanalh authored Jul 5, 2024
1 parent 5ca7db1 commit 478e470
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 48 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/citrea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn initialize_logging(level: Level) {
level.as_str().to_owned(),
"jmt=info".to_owned(),
"hyper=info".to_owned(),
"alloy_transport_http=info".to_owned(),
// Limit output as much as possible, use WARN.
"risc0_zkvm=warn".to_owned(),
"guest_execution=info".to_owned(),
Expand Down
91 changes: 91 additions & 0 deletions bin/citrea/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ async fn initialize_test(

#[tokio::test(flavor = "multi_thread")]
async fn test_soft_batch_save() -> Result<(), anyhow::Error> {
// citrea::initialize_logging(tracing::Level::DEBUG);

let storage_dir = tempdir_with_children(&["DA", "sequencer", "full-node"]);
let da_db_dir = storage_dir.path().join("DA").to_path_buf();
let sequencer_db_dir = storage_dir.path().join("sequencer").to_path_buf();
Expand Down Expand Up @@ -3296,3 +3298,92 @@ async fn test_full_node_sync_status() {
seq_task.abort();
full_node_task.abort();
}

#[tokio::test(flavor = "multi_thread")]
async fn test_sequencer_commitment_threshold() {
// citrea::initialize_logging(tracing::Level::DEBUG);

let storage_dir = tempdir_with_children(&["DA", "sequencer", "full-node"]);
let da_db_dir = storage_dir.path().join("DA").to_path_buf();
let sequencer_db_dir = storage_dir.path().join("sequencer").to_path_buf();

let psql_db_name = "test_sequencer_commitment_threshold".to_owned();

let db_test_client = PostgresConnector::new_test_client(psql_db_name.clone())
.await
.unwrap();

let mut sequencer_config = create_default_sequencer_config(4, Some(true), 10);

sequencer_config.db_config = Some(SharedBackupDbConfig::default().set_db_name(psql_db_name));
sequencer_config.mempool_conf = SequencerMempoolConfig {
max_account_slots: 1000,
..Default::default()
};

let (seq_port_tx, seq_port_rx) = tokio::sync::oneshot::channel();

let da_db_dir_cloned = da_db_dir.clone();
let seq_task = tokio::spawn(async {
start_rollup(
seq_port_tx,
GenesisPaths::from_dir(TEST_DATA_GENESIS_PATH),
None,
NodeMode::SequencerNode,
sequencer_db_dir,
da_db_dir_cloned,
1_000_000, // Put a large number for commitment threshold
true,
None,
Some(sequencer_config),
Some(true),
DEFAULT_DEPOSIT_MEMPOOL_FETCH_LIMIT,
)
.await;
});

let seq_port = seq_port_rx.await.unwrap();

let seq_test_client = init_test_rollup(seq_port).await;

seq_test_client.send_publish_batch_request().await;

for _ in 0..10 {
for _ in 0..100 {
let address = Address::random();
let _pending = seq_test_client
.send_eth(address, None, None, None, 1u128)
.await
.unwrap();
}
seq_test_client.send_publish_batch_request().await;
}

wait_for_l2_block(&seq_test_client, 11, Some(Duration::from_secs(60))).await;

// At block 725, the state diff should be large enough to trigger a commitment.
wait_for_postgres_commitment(&db_test_client, 1, Some(Duration::from_secs(60))).await;
let commitments = db_test_client.get_all_commitments().await.unwrap();
assert_eq!(commitments.len(), 1);

for _ in 0..10 {
for _ in 0..100 {
let address = Address::random();
let _pending = seq_test_client
.send_eth(address, None, None, None, 1u128)
.await
.unwrap();
}
seq_test_client.send_publish_batch_request().await;
}

wait_for_l2_block(&seq_test_client, 21, Some(Duration::from_secs(60))).await;

// At block 1450, the state diff should be large enough to trigger a commitment.
// But the 50 remaining blocks state diff should NOT trigger a third.
wait_for_postgres_commitment(&db_test_client, 2, Some(Duration::from_secs(60))).await;
let commitments = db_test_client.get_all_commitments().await.unwrap();
assert_eq!(commitments.len(), 2);

seq_task.abort();
}
39 changes: 18 additions & 21 deletions crates/sequencer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ readme = "README.md"
resolver = "2"

[dependencies]
# 3rd-party deps
alloy-rlp = { workspace = true }
alloy-sol-types = { workspace = true }

anyhow = { workspace = true }
bincode = { workspace = true }
borsh = { workspace = true }
chrono = { workspace = true }
deadpool-postgres = { workspace = true }
digest = { workspace = true }
futures = { workspace = true }
hex = { workspace = true }
hyper = { workspace = true }
jsonrpsee = { workspace = true, features = ["http-client", "server"] }
rs_merkle = { workspace = true }
schnellru = "0.2.1"
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
tower = { workspace = true }
tower-http = { workspace = true }
tracing = { workspace = true }

reth-db = { workspace = true }
Expand All @@ -36,33 +44,22 @@ reth-rpc-types-compat = { workspace = true }
reth-tasks = { workspace = true }
reth-transaction-pool = { workspace = true }
reth-trie = { workspace = true }

revm = { workspace = true }

deadpool-postgres = { workspace = true }
hyper = { workspace = true }
schnellru = "0.2.1"
tokio = { workspace = true }
tower = { workspace = true }
tower-http = { workspace = true }

citrea-evm = { path = "../evm", features = ["native"] }
sov-db = { path = "../sovereign-sdk/full-node/db/sov-db" }
sov-rollup-interface = { path = "../sovereign-sdk/rollup-interface", features = ["native"] }

sov-stf-runner = { path = "../sovereign-sdk/full-node/sov-stf-runner" }

sov-modules-rollup-blueprint = { path = "../sovereign-sdk/module-system/sov-modules-rollup-blueprint" }
sov-modules-stf-blueprint = { path = "../sovereign-sdk/module-system/sov-modules-stf-blueprint" }

citrea-stf = { path = "../citrea-stf", features = ["native"] }
# Sovereign SDK deps
soft-confirmation-rule-enforcer = { path = "../soft-confirmation-rule-enforcer", features = ["native"] }
sov-accounts = { path = "../sovereign-sdk/module-system/module-implementations/sov-accounts", default-features = false }
sov-db = { path = "../sovereign-sdk/full-node/db/sov-db" }
sov-modules-api = { path = "../sovereign-sdk/module-system/sov-modules-api", default-features = false }
sov-modules-rollup-blueprint = { path = "../sovereign-sdk/module-system/sov-modules-rollup-blueprint" }
sov-modules-stf-blueprint = { path = "../sovereign-sdk/module-system/sov-modules-stf-blueprint" }
sov-rollup-interface = { path = "../sovereign-sdk/rollup-interface", features = ["native"] }
sov-state = { path = "../sovereign-sdk/module-system/sov-state" }
sov-stf-runner = { path = "../sovereign-sdk/full-node/sov-stf-runner" }

hex = { workspace = true }

# Citrea Deps
citrea-evm = { path = "../evm", features = ["native"] }
citrea-stf = { path = "../citrea-stf", features = ["native"] }
shared-backup-db = { path = "../shared-backup-db" }

[dev-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions crates/sequencer/src/commitment_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn get_commitment_info(
ledger_db: &LedgerDB,
min_soft_confirmations_per_commitment: u64,
prev_l1_height: u64,
state_diff_threshold_reached: bool,
) -> anyhow::Result<Option<CommitmentInfo>> {
// first get when the last merkle root of soft confirmations was submitted
let last_commitment_l1_height = ledger_db
Expand Down Expand Up @@ -98,8 +99,9 @@ pub fn get_commitment_info(
debug!("L2 range to submit: {:?}", l2_range_to_submit);
debug!("L1 height range: {:?}", l1_height_range);

if (l2_range_to_submit.1 .0 + 1)
< min_soft_confirmations_per_commitment + l2_range_to_submit.0 .0
if !state_diff_threshold_reached
&& (l2_range_to_submit.1 .0 + 1)
< min_soft_confirmations_per_commitment + l2_range_to_submit.0 .0
{
return Ok(None);
}
Expand Down
Loading

0 comments on commit 478e470

Please sign in to comment.