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
4 changes: 1 addition & 3 deletions bin/test-data/genesis/demo-tests/bitcoin-regtest/evm.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"genesis_timestamp": 1705957200,
"block_timestamp_delta": 2,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
}
}
}
2 changes: 0 additions & 2 deletions bin/test-data/genesis/demo-tests/celestia/evm.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"genesis_timestamp": 1705957200,
"block_timestamp_delta": 2,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
Expand Down
4 changes: 1 addition & 3 deletions bin/test-data/genesis/demo-tests/mock/evm.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"genesis_timestamp": 1705957200,
"block_timestamp_delta": 2,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"genesis_timestamp": 0,
"block_timestamp_delta": 1,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
}
}
}
4 changes: 1 addition & 3 deletions bin/test-data/genesis/integration-tests/evm.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
"coinbase": "0x0000000000000000000000000000000000000000",
"starting_base_fee": 1000000000,
"block_gas_limit": 30000000,
"genesis_timestamp": 1705957200,
"block_timestamp_delta": 2,
"base_fee_params": {
"max_change_denominator": 8,
"elasticity_multiplier": 2
}
}
}
1 change: 1 addition & 0 deletions crates/citrea-stf/src/hooks_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl<C: Context, Da: DaSpec> ApplySoftConfirmationHooks<Da> for Runtime<C, Da> {
soft_batch.da_slot_txs_commitment(),
&soft_batch.pre_state_root(),
soft_batch.l1_fee_rate(),
soft_batch.timestamp(),
working_set,
);

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
15 changes: 1 addition & 14 deletions crates/evm/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ pub struct EvmConfig {
pub starting_base_fee: u64,
/// Gas limit for single block
pub block_gas_limit: u64,
/// Genesis timestamp.
pub genesis_timestamp: 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 @@ -83,8 +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(),
genesis_timestamp: 0,
base_fee_params: reth_primitives::BaseFeeParams::ethereum(),
}
}
Expand Down Expand Up @@ -144,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 All @@ -164,7 +157,7 @@ impl<C: sov_modules_api::Context> Evm<C> {
number: 0,
gas_limit: config.block_gas_limit,
gas_used: 0,
timestamp: config.genesis_timestamp,
timestamp: 0,
mix_hash: B256::default(),
nonce: 0,
base_fee_per_gas: Some(config.starting_base_fee),
Expand Down Expand Up @@ -219,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 @@ -241,8 +233,6 @@ mod tests {
"coinbase":"0x0000000000000000000000000000000000000000",
"starting_base_fee":1000000000,
"block_gas_limit":30000000,
"genesis_timestamp":0,
"block_timestamp_delta":1,
"base_fee_params":{
"max_change_denominator":8,
"elasticity_multiplier":2
Expand Down Expand Up @@ -275,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 @@ -303,8 +292,6 @@ mod tests {
"coinbase":"0x0000000000000000000000000000000000000000",
"starting_base_fee":1000000000,
"block_gas_limit":30000000,
"genesis_timestamp":0,
"block_timestamp_delta":1,
"base_fee_params":{
"max_change_denominator":8,
"elasticity_multiplier":2
Expand Down
3 changes: 2 additions & 1 deletion crates/evm/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ where
_da_slot_txs_commitment: [u8; 32],
pre_state_root: &[u8],
l1_fee_rate: u64,
timestamp: u64,
working_set: &mut WorkingSet<C>,
) {
let mut parent_block = self
Expand Down Expand Up @@ -45,7 +46,7 @@ where
let new_pending_env = BlockEnv {
number: parent_block.header.number + 1,
coinbase: cfg.coinbase,
timestamp: parent_block.header.timestamp + cfg.block_timestamp_delta,
timestamp,
prevrandao: da_slot_hash.into(),
basefee: parent_block
.header
Expand Down
18 changes: 17 additions & 1 deletion crates/evm/src/tests/call_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn call_multiple_test() {
[42u8; 32],
&[10u8; 32],
l1_fee_rate,
0,
&mut working_set,
);

Expand Down Expand Up @@ -160,6 +161,7 @@ fn call_test() {
[42u8; 32],
&[10u8; 32],
l1_fee_rate,
0,
&mut working_set,
);

Expand Down Expand Up @@ -230,7 +232,14 @@ fn failed_transaction_test() {
let working_set = &mut working_set;
let l1_fee_rate = 0;

evm.begin_soft_confirmation_hook([5u8; 32], [42u8; 32], &[10u8; 32], l1_fee_rate, working_set);
evm.begin_soft_confirmation_hook(
[5u8; 32],
[42u8; 32],
&[10u8; 32],
l1_fee_rate,
0,
working_set,
);
{
let sender_address = generate_address::<C>("sender");
let sequencer_address = generate_address::<C>("sequencer");
Expand Down Expand Up @@ -284,6 +293,7 @@ fn self_destruct_test() {
[42u8; 32],
&[10u8; 32],
l1_fee_rate,
0,
&mut working_set,
);
{
Expand Down Expand Up @@ -338,6 +348,7 @@ fn self_destruct_test() {
[42u8; 32],
&[99u8; 32],
l1_fee_rate,
0,
&mut working_set,
);
{
Expand Down Expand Up @@ -413,6 +424,7 @@ fn test_block_hash_in_evm() {
[42u8; 32],
&[10u8; 32],
l1_fee_rate,
0,
&mut working_set,
);
{
Expand Down Expand Up @@ -442,6 +454,7 @@ fn test_block_hash_in_evm() {
[42u8; 32],
&[99u8; 32],
l1_fee_rate,
0,
&mut working_set,
);
evm.end_soft_confirmation_hook(&mut working_set);
Expand Down Expand Up @@ -523,6 +536,7 @@ fn test_block_gas_limit() {
[42u8; 32],
&[10u8; 32],
l1_fee_rate,
0,
&mut working_set,
);
{
Expand Down Expand Up @@ -788,6 +802,7 @@ fn test_l1_fee_success() {
[42u8; 32],
&[10u8; 32],
l1_fee_rate,
0,
&mut working_set,
);
{
Expand Down Expand Up @@ -861,6 +876,7 @@ fn test_l1_fee_not_enough_funds() {
[42u8; 32],
&[10u8; 32],
l1_fee_rate,
0,
&mut working_set,
);
{
Expand Down
Loading
Loading