Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve timestamp handling #335

Merged
merged 16 commits into from
Apr 4, 2024
Merged
6 changes: 4 additions & 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ bincode = "1.3.3"
bcs = "0.1.6"
byteorder = { version = "1.5.0", default-features = false }
bytes = { version = "1.2.1", default-features = false }
chrono = { version = "0.4.37", default-features = false }
digest = { version = "0.10.6", default-features = false, features = ["alloc"] }
rs_merkle = "1.4.2"
futures = "0.3"
Expand Down
10 changes: 7 additions & 3 deletions bin/citrea/src/test_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ fn regular_test_helper(payload: serde_json::Value, expected: &serde_json::Value)
phantom_data: PhantomData,
pub_key: vec![],
l1_fee_rate: 0,
timestamp: 0,
},
SoftBatchReceipt {
da_slot_height: 1,
Expand All @@ -166,6 +167,7 @@ fn regular_test_helper(payload: serde_json::Value, expected: &serde_json::Value)
phantom_data: PhantomData,
pub_key: vec![],
l1_fee_rate: 0,
timestamp: 0,
},
];

Expand Down Expand Up @@ -315,7 +317,7 @@ fn test_get_batches() {
fn test_get_soft_batch() {
// Get the first soft batch by number
let payload = jsonrpc_req!("ledger_getSoftBatchByNumber", [1]);
let expected = jsonrpc_result!({"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":[[116,120,49,32,98,111,100,121],[116,120,50,32,98,111,100,121]],"pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0});
let expected = jsonrpc_result!({"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":[[116,120,49,32,98,111,100,121],[116,120,50,32,98,111,100,121]],"pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0, "timestamp": 0});
regular_test_helper(payload, &expected);

// Get the first soft batch by hash
Expand All @@ -328,7 +330,7 @@ fn test_get_soft_batch() {
// Get the second soft batch by number
let payload = jsonrpc_req!("ledger_getSoftBatchByNumber", [2]);
let expected = jsonrpc_result!(
{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": batch2_tx_receipts().into_iter().map(|tx_receipt| tx_receipt.body_to_save.unwrap()).collect::<Vec<_>>(), "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0}
{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": batch2_tx_receipts().into_iter().map(|tx_receipt| tx_receipt.body_to_save.unwrap()).collect::<Vec<_>>(), "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0, "timestamp": 0}
);
regular_test_helper(payload, &expected);

Expand All @@ -342,7 +344,9 @@ fn test_get_soft_batch() {
// Get range of soft batches
let payload = jsonrpc_req!("ledger_getSoftBatchRange", [1, 2]);
let expected = jsonrpc_result!(
[{"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":[[116,120,49,32,98,111,100,121],[116,120,50,32,98,111,100,121]],"pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0},{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": batch2_tx_receipts().into_iter().map(|tx_receipt| tx_receipt.body_to_save.unwrap()).collect::<Vec<_>>(), "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0}
[
{"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":[[116,120,49,32,98,111,100,121],[116,120,50,32,98,111,100,121]],"pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0, "timestamp": 0},
{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": batch2_tx_receipts().into_iter().map(|tx_receipt| tx_receipt.body_to_save.unwrap()).collect::<Vec<_>>(), "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0, "timestamp": 0}
]
);
regular_test_helper(payload, &expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"block_timestamp_delta": 2,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
Expand Down
1 change: 0 additions & 1 deletion bin/test-data/genesis/demo-tests/celestia/evm.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"block_timestamp_delta": 2,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
Expand Down
1 change: 0 additions & 1 deletion bin/test-data/genesis/demo-tests/mock/evm.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"block_timestamp_delta": 2,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"block_timestamp_delta": 1,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
Expand Down
1 change: 0 additions & 1 deletion bin/test-data/genesis/integration-tests/evm.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"block_timestamp_delta": 2,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
Expand Down
4 changes: 0 additions & 4 deletions crates/evm/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ pub struct EvmChainConfig {
/// Gas limit for single block
pub block_gas_limit: u64,

/// Delta to add to parent block timestamp
pub block_timestamp_delta: u64,

/// Base fee params.
pub base_fee_params: BaseFeeParams,
}
Expand All @@ -115,7 +112,6 @@ impl Default for EvmChainConfig {
spec: vec![(0, SpecId::SHANGHAI)],
coinbase: Address::ZERO,
block_gas_limit: reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT,
block_timestamp_delta: 2,
base_fee_params: BaseFeeParams::ethereum(),
}
}
Expand Down
8 changes: 0 additions & 8 deletions crates/evm/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ pub struct EvmConfig {
pub starting_base_fee: u64,
/// Gas limit for single block
pub block_gas_limit: u64,
/// Delta to add to parent block timestamp,
pub block_timestamp_delta: u64,
/// Base fee params.
pub base_fee_params: reth_primitives::BaseFeeParams,
}
Expand All @@ -81,7 +79,6 @@ impl Default for EvmConfig {
coinbase: Address::ZERO,
starting_base_fee: reth_primitives::constants::EIP1559_INITIAL_BASE_FEE,
block_gas_limit: reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT,
block_timestamp_delta: reth_primitives::constants::SLOT_DURATION.as_secs(),
base_fee_params: reth_primitives::BaseFeeParams::ethereum(),
}
}
Expand Down Expand Up @@ -141,7 +138,6 @@ impl<C: sov_modules_api::Context> Evm<C> {
spec,
coinbase: config.coinbase,
block_gas_limit: config.block_gas_limit,
block_timestamp_delta: config.block_timestamp_delta,
base_fee_params: config.base_fee_params,
};

Expand Down Expand Up @@ -216,7 +212,6 @@ mod tests {
chain_id: 1,
limit_contract_code_size: None,
spec: vec![(0, SpecId::SHANGHAI)].into_iter().collect(),
block_timestamp_delta: 1u64,
..Default::default()
};

Expand All @@ -238,7 +233,6 @@ mod tests {
"coinbase":"0x0000000000000000000000000000000000000000",
"starting_base_fee":1000000000,
"block_gas_limit":30000000,
"block_timestamp_delta":1,
"base_fee_params":{
"max_change_denominator":8,
"elasticity_multiplier":2
Expand Down Expand Up @@ -271,7 +265,6 @@ mod tests {
chain_id: 1,
limit_contract_code_size: None,
spec: vec![(0, SpecId::SHANGHAI)].into_iter().collect(),
block_timestamp_delta: 1u64,
..Default::default()
};

Expand Down Expand Up @@ -299,7 +292,6 @@ mod tests {
"coinbase":"0x0000000000000000000000000000000000000000",
"starting_base_fee":1000000000,
"block_gas_limit":30000000,
"block_timestamp_delta": 1,
"base_fee_params":{
"max_change_denominator":8,
"elasticity_multiplier":2
Expand Down
6 changes: 2 additions & 4 deletions crates/evm/src/tests/genesis_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ lazy_static! {
.collect(),
chain_id: 1000,
block_gas_limit: reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT,
block_timestamp_delta: 2,
coinbase: Address::from([3u8; 20]),
limit_contract_code_size: Some(5000),
starting_base_fee: 1000000000,
base_fee_params: BaseFeeParams::ethereum(),
};

pub(crate) static ref GENESIS_HASH: B256 = B256::from(hex!(
"4e9bf2c5ace397aee0e37c4614566b947773f7023cdb05010349419cacbd2b44"
"5aa8e649f234ae2128a8eb8536f557fafd7b6a46f12cba8d89a73eb74ee0f0e3"
));
pub(crate) static ref GENESIS_STATE_ROOT: B256 = B256::from(hex!(
"bd86fd85964d4aac6a0bdd77011a791522ca71e23895bd1b3f7f2a1803cb4902"
"62dcfcbbc789410f13b8ec77a27fa8fc1c804c5cedbf2c2498b886b7c5a2f35b"
));
pub(crate) static ref GENESIS_DA_TXS_COMMITMENT: B256 = B256::from(hex!(
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
Expand Down Expand Up @@ -149,7 +148,6 @@ fn genesis_cfg() {
spec: vec![(0, SpecId::BERLIN), (1, SpecId::SHANGHAI)],
chain_id: 1000,
block_gas_limit: reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT,
block_timestamp_delta: 2,
coinbase: Address::from([3u8; 20]),
limit_contract_code_size: Some(5000),
base_fee_params: BaseFeeParams::ethereum(),
Expand Down
8 changes: 4 additions & 4 deletions crates/evm/src/tests/hooks_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn begin_soft_confirmation_hook_creates_pending_block() {
BlockEnv {
number: 2,
coinbase: *BENEFICIARY,
timestamp: 50 + TEST_CONFIG.block_timestamp_delta * 2,
timestamp: 54,
prevrandao: *DA_ROOT_HASH,
basefee: 765625000,
gas_limit: TEST_CONFIG.block_gas_limit,
Expand Down Expand Up @@ -85,7 +85,7 @@ fn end_soft_confirmation_hook_sets_head() {
Block {
header: Header {
parent_hash: B256::from(hex!(
"e027fa45a5142bca92c6861cec6da54a0ce6c615fd8b4c488a9a8a7523bd4b81"
"fc47267bb1f23e28564d539ad13370a335660149b44ca2b76c1787563781f69a"
)),

ommers_hash: EMPTY_OMMER_ROOT_HASH,
Expand All @@ -103,7 +103,7 @@ fn end_soft_confirmation_hook_sets_head() {
number: 2,
gas_limit: TEST_CONFIG.block_gas_limit,
gas_used: 200u64,
timestamp: 50 + TEST_CONFIG.block_timestamp_delta * 2,
timestamp: 54,
mix_hash: *DA_ROOT_HASH,
nonce: 0,
base_fee_per_gas: Some(765625000),
Expand Down Expand Up @@ -296,7 +296,7 @@ fn finalize_hook_creates_final_block() {
parent_beacon_block_root: None,
},
B256::from(hex!(
"a0a49c63fcbf3f89f4a54d7f34ce0212135d0a655db76bf7d9e95be512ba02de"
"58ba97552f2687271aeb249b540f35f50b176df8087a930340fff7a9be2c34b5"
))
),
l1_fee_rate: 0,
Expand Down
13 changes: 6 additions & 7 deletions crates/sequencer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ publish = false
resolver = "2"

[dependencies]
bitcoin-da = { path = "../bitcoin-da" }
anyhow = { workspace = true }
tracing = { workspace = true }
jsonrpsee = { workspace = true, features = ["http-client", "server"] }

futures = { workspace = true }

bitcoin-da = { path = "../bitcoin-da" }
borsh = { workspace = true }
chrono = { workspace = true }
digest = { workspace = true }
futures = { workspace = true }
jsonrpsee = { workspace = true, features = ["http-client", "server"] }
rs_merkle = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
rs_merkle = { workspace = true }
tracing = { workspace = true }

reth-primitives = { workspace = true }
reth-rpc-types = { workspace = true }
Expand Down
26 changes: 4 additions & 22 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ where
da_block: <Da as DaService>::FilteredBlock,
l1_fee_rate: u64,
rlp_txs: Vec<RlpEvmTransaction>,
block_timestamp_delta: u64,
) -> Result<(), anyhow::Error> {
debug!(
"Sequencer: publishing block with {} transactions",
Expand All @@ -204,13 +203,7 @@ where
"Sequencer: L1 height mismatch, expected {da_height} (or {da_height}-1), got {l1_height}",
);

let timestamp = self
.db_provider
.latest_header()
.expect("Failed to get latest header")
.map(|header| header.timestamp)
.expect("Failed to get next block timestamp")
+ block_timestamp_delta;
let timestamp = chrono::Local::now().timestamp() as u64;

let batch_info = HookSoftConfirmationInfo {
da_slot_height: da_block.header().height(),
Expand Down Expand Up @@ -429,13 +422,7 @@ where
);
let da_block =
self.da_service.get_block_at(skipped_height).await.unwrap();
self.produce_l2_block(
da_block,
l1_fee_rate,
vec![],
cfg.block_timestamp_delta,
)
.await?;
self.produce_l2_block(da_block, l1_fee_rate, vec![]).await?;
}
}
let prev_l1_height = last_finalized_height - 1;
Expand Down Expand Up @@ -514,13 +501,8 @@ where
.await
.unwrap();

self.produce_l2_block(
last_finalized_block,
l1_fee_rate,
rlp_txs,
cfg.block_timestamp_delta,
)
.await?;
self.produce_l2_block(last_finalized_block, l1_fee_rate, rlp_txs)
.await?;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/soft-confirmation-rule-enforcer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ lazy_static = "1.4.0"
sov-mock-da = { path = "../sovereign-sdk/adapters/mock-da", features = [
"native",
] }
chrono = { workspace = true, default-features = true }


[features]
Expand Down
3 changes: 2 additions & 1 deletion crates/soft-confirmation-rule-enforcer/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ where
Ok(())
}

/// Checks that the current block's timestamp is greater than the last block's timestamp.
/// Checks that the current block's timestamp.
/// This is to make sure that the set timestamp is greater than the last block's timestamp.
fn apply_timestamp_rule(
&self,
soft_batch: &mut HookSoftConfirmationInfo,
Expand Down
6 changes: 6 additions & 0 deletions crates/soft-confirmation-rule-enforcer/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ impl<C: Context, Da: DaSpec> SoftConfirmationRuleEnforcer<C, Da> {
pub fn get_last_l1_fee_rate(&self, working_set: &mut WorkingSet<C>) -> RpcResult<u64> {
Ok(self.last_l1_fee_rate.get(working_set).unwrap_or(0))
}
#[rpc_method(name = "getLatestBlockTimestamp")]
/// Get the latest block's timestamp.
/// 0 at genesis.
pub fn get_last_timestamp(&self, working_set: &mut WorkingSet<C>) -> RpcResult<u64> {
Ok(self.last_timestamp.get(working_set).unwrap_or(0))
}
}
Loading
Loading