Skip to content

Commit

Permalink
Improve timestamp handling (#335)
Browse files Browse the repository at this point in the history
* Add timestamp field

* Pass timestamp value

* Apply timestamp rule

* Add block time delta to timestamp

* Remove "genesis_timestamp" field

* Add timestamp to SoftBatchResponse

* Fix all tests

* Remove block timestamp delta field

* Use chrono to set current timestamp

* Set edition in rustfmt

* Fix tests

* Update docs

* Add soft confirmation rule enforce timestamp tests

* Fix RPC tests

* Remove set_timestamp
  • Loading branch information
rakanalh committed Apr 6, 2024
1 parent 33b5af7 commit 343a621
Show file tree
Hide file tree
Showing 44 changed files with 333 additions and 106 deletions.
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
11 changes: 7 additions & 4 deletions bin/citrea/src/test_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,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 @@ -167,6 +168,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,8 +317,7 @@ fn test_get_batches() {
#[test]
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":["74783120626f6479", "74783220626f6479"],"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":["74783120626f6479", "74783220626f6479"],"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 @@ -333,7 +334,7 @@ fn test_get_soft_batch() {
.map(|tx_receipt| tx_receipt.body_to_save.unwrap().encode_hex::<String>())
.collect::<Vec<String>>();
let expected = jsonrpc_result!(
{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": txs, "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": txs, "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0, "timestamp": 0}
);
regular_test_helper(payload, &expected);

Expand All @@ -352,7 +353,9 @@ fn test_get_soft_batch() {
.map(|tx_receipt| tx_receipt.body_to_save.unwrap().encode_hex::<String>())
.collect::<Vec<String>>();
let expected = jsonrpc_result!(
[{"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":["74783120626f6479", "74783220626f6479"],"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": txs, "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":["74783120626f6479", "74783220626f6479"],"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": txs, "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

0 comments on commit 343a621

Please sign in to comment.