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

refactor: move time management into its own component #385

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e-tests/test/evm-apis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("evm_increaseTime", function () {
const wallet = new Wallet(RichAccounts[0].PrivateKey, provider);
const userWallet = Wallet.createRandom().connect(provider);
let expectedTimestamp: number = await provider.send("config_getCurrentTimestamp", []);
expectedTimestamp += timeIncreaseInSeconds * 1000;
expectedTimestamp += timeIncreaseInSeconds;

// Act
await provider.send("evm_increaseTime", [timeIncreaseInSeconds]);
Expand Down
10 changes: 1 addition & 9 deletions src/node/config_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> Configurat
}

fn config_get_current_timestamp(&self) -> Result<u64> {
self.get_inner()
.read()
.map_err(|err| {
tracing::error!("failed acquiring lock: {:?}", err);
into_jsrpc_error(Web3Error::InternalError(anyhow::Error::msg(
"Failed to acquire read lock for inner node state.",
)))
})
.map(|reader| reader.current_timestamp)
Ok(self.time.last_timestamp())
}

fn config_set_show_calls(&self, value: String) -> Result<String> {
Expand Down
15 changes: 9 additions & 6 deletions src/node/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2954,7 +2954,7 @@ mod tests {
inner.current_batch = 1;
inner.current_miniblock = 1;
inner.current_miniblock_hash = H256::repeat_byte(0x1);
inner.current_timestamp = 1;
inner.time.set_last_timestamp(1);
inner
.filters
.add_block_filter()
Expand All @@ -2971,7 +2971,7 @@ mod tests {

let storage = inner.fork_storage.inner.read().unwrap();
let expected_snapshot = Snapshot {
current_timestamp: inner.current_timestamp,
current_timestamp: inner.time.last_timestamp(),
current_batch: inner.current_batch,
current_miniblock: inner.current_miniblock,
current_miniblock_hash: inner.current_miniblock_hash,
Expand Down Expand Up @@ -3060,7 +3060,7 @@ mod tests {
inner.current_batch = 1;
inner.current_miniblock = 1;
inner.current_miniblock_hash = H256::repeat_byte(0x1);
inner.current_timestamp = 1;
inner.time.set_last_timestamp(1);
inner
.filters
.add_block_filter()
Expand All @@ -3078,7 +3078,7 @@ mod tests {
let expected_snapshot = {
let storage = inner.fork_storage.inner.read().unwrap();
Snapshot {
current_timestamp: inner.current_timestamp,
current_timestamp: inner.time.last_timestamp(),
current_batch: inner.current_batch,
current_miniblock: inner.current_miniblock,
current_miniblock_hash: inner.current_miniblock_hash,
Expand Down Expand Up @@ -3113,7 +3113,7 @@ mod tests {
inner.current_batch = 2;
inner.current_miniblock = 2;
inner.current_miniblock_hash = H256::repeat_byte(0x2);
inner.current_timestamp = 2;
inner.time.set_last_timestamp(2);
inner
.filters
.add_pending_transaction_filter()
Expand All @@ -3134,7 +3134,10 @@ mod tests {
.expect("failed restoring snapshot");

let storage = inner.fork_storage.inner.read().unwrap();
assert_eq!(expected_snapshot.current_timestamp, inner.current_timestamp);
assert_eq!(
expected_snapshot.current_timestamp,
inner.time.last_timestamp()
);
assert_eq!(expected_snapshot.current_batch, inner.current_batch);
assert_eq!(expected_snapshot.current_miniblock, inner.current_miniblock);
assert_eq!(
Expand Down
29 changes: 16 additions & 13 deletions src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use zksync_types::{
use zksync_utils::{bytecode::hash_bytecode, h256_to_account_address, h256_to_u256, u256_to_h256};
use zksync_web3_decl::error::Web3Error;

use crate::node::time::TimestampManager;
use crate::{
bootloader_debug::{BootloaderDebug, BootloaderDebugTracer},
config::{
Expand Down Expand Up @@ -149,9 +150,8 @@ impl TransactionResult {
/// S - is the Source of the Fork.
#[derive(Clone)]
pub struct InMemoryNodeInner<S> {
/// The latest timestamp that was already generated.
/// Next block will be current_timestamp + 1
pub current_timestamp: u64,
/// Supplies timestamps that are unique across the system.
pub time: TimestampManager,
/// The latest batch number that was already generated.
/// Next block will be current_batch + 1
pub current_batch: u32,
Expand Down Expand Up @@ -223,7 +223,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
};

InMemoryNodeInner {
current_timestamp: f.block_timestamp,
time: TimestampManager::new(f.block_timestamp),
current_batch: f.l1_block.0,
current_miniblock: f.l2_miniblock,
current_miniblock_hash: f.l2_miniblock_hash,
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
let fee_input_provider =
TestNodeFeeInputProvider::default().with_overrides(gas_overrides);
InMemoryNodeInner {
current_timestamp: NON_FORK_FIRST_BLOCK_TIMESTAMP,
time: TimestampManager::new(NON_FORK_FIRST_BLOCK_TIMESTAMP),
current_batch: 0,
current_miniblock: 0,
current_miniblock_hash: block_hash,
Expand Down Expand Up @@ -304,15 +304,15 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {

let (last_l1_block_num, last_l1_block_ts) = load_last_l1_batch(storage.clone())
.map(|(num, ts)| (num as u32, ts))
.unwrap_or_else(|| (self.current_batch, self.current_timestamp));
.unwrap_or_else(|| (self.current_batch, self.time.last_timestamp()));
let last_l2_block = load_last_l2_block(&storage).unwrap_or_else(|| L2Block {
number: self.current_miniblock as u32,
hash: L2BlockHasher::legacy_hash(L2BlockNumber(self.current_miniblock as u32)),
timestamp: self.current_timestamp,
timestamp: self.time.last_timestamp(),
});
let latest_timestamp = std::cmp::max(
std::cmp::max(last_l1_block_ts, last_l2_block.timestamp),
self.current_timestamp,
self.time.last_timestamp(),
);

let block_ctx = BlockContext::from_current(
Expand Down Expand Up @@ -769,7 +769,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
.map_err(|err| format!("failed acquiring read lock on storage: {:?}", err))?;

Ok(Snapshot {
current_timestamp: self.current_timestamp,
current_timestamp: self.time.last_timestamp(),
current_batch: self.current_batch,
current_miniblock: self.current_miniblock,
current_miniblock_hash: self.current_miniblock_hash,
Expand All @@ -795,7 +795,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
.write()
.map_err(|err| format!("failed acquiring write lock on storage: {:?}", err))?;

self.current_timestamp = snapshot.current_timestamp;
self.time.set_last_timestamp(snapshot.current_timestamp);
self.current_batch = snapshot.current_batch;
self.current_miniblock = snapshot.current_miniblock;
self.current_miniblock_hash = snapshot.current_miniblock_hash;
Expand Down Expand Up @@ -850,6 +850,7 @@ pub struct InMemoryNode<S: Clone> {
/// Configuration option that survives reset.
#[allow(dead_code)]
pub(crate) system_contracts_options: system_contracts::Options,
pub(crate) time: TimestampManager,
}

fn contract_address_from_tx_result(execution_result: &VmExecutionResultAndLogs) -> Option<H160> {
Expand All @@ -876,10 +877,12 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
) -> Self {
let system_contracts_options = config.system_contracts_options;
let inner = InMemoryNodeInner::new(fork, observability, config, gas_overrides);
let time = inner.time.clone();
InMemoryNode {
inner: Arc::new(RwLock::new(inner)),
snapshots: Default::default(),
system_contracts_options,
time,
}
}

Expand Down Expand Up @@ -1694,7 +1697,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
}

inner.current_miniblock = inner.current_miniblock.saturating_add(1);
inner.current_timestamp = inner.current_timestamp.saturating_add(1);
let expected_timestamp = inner.time.next_timestamp();

let actual_l1_batch_number = block
.l1_batch_number
Expand All @@ -1715,10 +1718,10 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
);
}

if block.timestamp.as_u64() != inner.current_timestamp {
if block.timestamp.as_u64() != expected_timestamp {
panic!(
"expected next block to have timestamp {}, got {} | {i}",
inner.current_timestamp,
expected_timestamp,
block.timestamp.as_u64()
);
}
Expand Down
Loading
Loading