Skip to content

Commit

Permalink
rework TimeReader into a trait ReadTime
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov committed Jan 13, 2025
1 parent 07eb913 commit 3bd0e04
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 82 deletions.
6 changes: 3 additions & 3 deletions crates/core/src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::node::error::LoadStateError;
use crate::node::fee_model::TestNodeFeeInputProvider;
use crate::node::impersonate::{ImpersonationManager, ImpersonationState};
use crate::node::inner::blockchain::ReadBlockchain;
use crate::node::inner::time::TimeReader;
use crate::node::inner::time::ReadTime;
use crate::node::sealer::BlockSealerState;
use crate::node::state::VersionedState;
use crate::node::{BlockSealer, BlockSealerMode, NodeExecutor, TxPool};
Expand Down Expand Up @@ -247,7 +247,7 @@ pub struct InMemoryNode {
pub(crate) node_handle: NodeExecutorHandle,
/// List of snapshots of the [InMemoryNodeInner]. This is bounded at runtime by [MAX_SNAPSHOTS].
pub(crate) snapshots: Arc<RwLock<Vec<Snapshot>>>,
pub(crate) time: TimeReader,
pub(crate) time: Arc<dyn ReadTime>,
pub(crate) impersonation: ImpersonationManager,
/// An optional handle to the observability stack
pub(crate) observability: Option<Observability>,
Expand All @@ -263,7 +263,7 @@ impl InMemoryNode {
blockchain: Arc<dyn ReadBlockchain>,
node_handle: NodeExecutorHandle,
observability: Option<Observability>,
time: TimeReader,
time: Arc<dyn ReadTime>,
impersonation: ImpersonationManager,
pool: TxPool,
sealer_state: BlockSealerState,
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/node/inner/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::fork::ForkDetails;
use crate::filters::LogFilter;
use crate::node::time::TimeWriter;
use crate::node::time::{ReadTime, Time};
use crate::node::{compute_hash, create_genesis, create_genesis_from_json, TransactionResult};
use crate::utils::utc_datetime_from_epoch_ms;
use anvil_zksync_config::types::Genesis;
Expand Down Expand Up @@ -497,7 +497,7 @@ impl BlockchainState {
pub(super) fn last_env<S: ReadStorage>(
&self,
storage: &StoragePtr<S>,
time_writer: &TimeWriter,
time_writer: &Time,
) -> (L1BatchNumber, L2Block) {
// TODO: This whole logic seems off to me, reconsider if we need it at all.
// Specifically it is weird that we might not have our latest block in the storage.
Expand Down Expand Up @@ -553,7 +553,7 @@ impl BlockchainState {

pub(super) fn load_blocks(
&mut self,
time_writer: &mut TimeWriter,
time: &mut Time,
blocks: Vec<api::Block<api::TransactionVariant>>,
) {
tracing::trace!(
Expand Down Expand Up @@ -590,7 +590,7 @@ impl BlockchainState {
self.current_block = L2BlockNumber(latest_number as u32);
self.current_block_hash = latest_hash;
self.current_batch = L1BatchNumber(latest_batch_number);
time_writer.reset_to(latest_timestamp);
time.reset_to(latest_timestamp);
}

pub(super) fn load_transactions(&mut self, transactions: Vec<TransactionResult>) {
Expand Down
28 changes: 14 additions & 14 deletions crates/core/src/node/inner/in_memory_inner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::blockchain::{Blockchain, ReadBlockchain};
use super::fork::{ForkDetails, ForkStorage, SerializableStorage};
use super::time::TimeWriter;
use super::time::Time;
use crate::bootloader_debug::{BootloaderDebug, BootloaderDebugTracer};
use crate::console_log::ConsoleLogHandler;
use crate::deps::storage_view::StorageView;
Expand Down Expand Up @@ -65,7 +65,7 @@ use zksync_web3_decl::error::Web3Error;
pub struct InMemoryNodeInner {
/// Writeable blockchain state.
blockchain: Blockchain,
pub(super) time_writer: TimeWriter,
pub(super) time: Time,
/// The fee input provider.
pub fee_input_provider: TestNodeFeeInputProvider,
// Map from filter_id to the eth filter
Expand All @@ -88,7 +88,7 @@ impl InMemoryNodeInner {
#[allow(clippy::too_many_arguments)]
pub(super) fn new(
blockchain: Blockchain,
time_writer: TimeWriter,
time: Time,
fork_storage: ForkStorage,
fee_input_provider: TestNodeFeeInputProvider,
filters: Arc<RwLock<EthFilters>>,
Expand All @@ -98,7 +98,7 @@ impl InMemoryNodeInner {
) -> Self {
InMemoryNodeInner {
blockchain,
time_writer,
time,
fee_input_provider,
filters,
fork_storage,
Expand Down Expand Up @@ -138,14 +138,14 @@ impl InMemoryNodeInner {

let (last_l1_batch_number, last_l2_block) = self.blockchain.read().await.last_env(
&StorageView::new(&self.fork_storage).into_rc_ptr(),
&self.time_writer,
&self.time,
);

let block_ctx = BlockContext {
hash: H256::zero(),
batch: (last_l1_batch_number + 1).0,
miniblock: last_l2_block.number as u64 + 1,
timestamp: self.time_writer.peek_next_timestamp(),
timestamp: self.time.peek_next_timestamp(),
};

let fee_input = if let Some(fork) = &self
Expand Down Expand Up @@ -590,7 +590,7 @@ impl InMemoryNodeInner {
let (batch_env, mut block_ctx) = self.create_l1_batch_env().await;
// Advance clock as we are consuming next timestamp for this block
anyhow::ensure!(
self.time_writer.advance_timestamp() == block_ctx.timestamp,
self.time.advance_timestamp() == block_ctx.timestamp,
"advancing clock produced different timestamp than expected"
);

Expand Down Expand Up @@ -668,7 +668,7 @@ impl InMemoryNodeInner {
// we are adding one l2 block at the end of each batch (to handle things like remaining events etc).
// You can look at insert_fictive_l2_block function in VM to see how this fake block is inserted.
let parent_block_hash = block_ctx.hash;
let block_ctx = block_ctx.new_block(&mut self.time_writer);
let block_ctx = block_ctx.new_block(&mut self.time);
let hash = compute_hash(block_ctx.miniblock, []);

let virtual_block = create_block(
Expand Down Expand Up @@ -1126,7 +1126,7 @@ impl InMemoryNodeInner {
return Err(LoadStateError::EmptyState);
}

storage.load_blocks(&mut self.time_writer, state.blocks);
storage.load_blocks(&mut self.time, state.blocks);
storage.load_transactions(state.transactions);
self.fork_storage.load_state(state.fork_storage);

Expand Down Expand Up @@ -1238,7 +1238,7 @@ impl InMemoryNodeInner {
blockchain_storage,
));

self.time_writer.set_current_timestamp_unchecked(
self.time.set_current_timestamp_unchecked(
fork.as_ref()
.map(|f| f.block_timestamp)
.unwrap_or(NON_FORK_FIRST_BLOCK_TIMESTAMP),
Expand Down Expand Up @@ -1303,7 +1303,7 @@ pub struct BlockContext {

impl BlockContext {
/// Create the next batch instance that uses the same batch number, and has all other parameters incremented by `1`.
fn new_block(&self, time: &mut TimeWriter) -> BlockContext {
fn new_block(&self, time: &mut Time) -> BlockContext {
Self {
hash: H256::zero(),
batch: self.batch,
Expand Down Expand Up @@ -1753,7 +1753,7 @@ mod tests {
blockchain.current_block = L2BlockNumber(1);
blockchain.current_block_hash = H256::repeat_byte(0x1);
}
writer.time_writer.set_current_timestamp_unchecked(1);
writer.time.set_current_timestamp_unchecked(1);
writer
.filters
.write()
Expand Down Expand Up @@ -1862,7 +1862,7 @@ mod tests {
blockchain.current_block = L2BlockNumber(1);
blockchain.current_block_hash = H256::repeat_byte(0x1);
}
writer.time_writer.set_current_timestamp_unchecked(1);
writer.time.set_current_timestamp_unchecked(1);
writer
.filters
.write()
Expand Down Expand Up @@ -1924,7 +1924,7 @@ mod tests {
blockchain.current_block = L2BlockNumber(2);
blockchain.current_block_hash = H256::repeat_byte(0x2);
}
writer.time_writer.set_current_timestamp_unchecked(2);
writer.time.set_current_timestamp_unchecked(2);
writer
.filters
.write()
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/node/inner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use anvil_zksync_config::TestNodeConfig;
use blockchain::ReadBlockchain;
use fork::{ForkDetails, ForkStorage};
use std::sync::Arc;
use time::TimeReader;
use time::{ReadTime, Time};
use tokio::sync::RwLock;

impl InMemoryNodeInner {
Expand All @@ -42,9 +42,9 @@ impl InMemoryNodeInner {
Arc<RwLock<Self>>,
ForkStorage,
Arc<dyn ReadBlockchain>,
TimeReader,
Arc<dyn ReadTime>,
) {
let (time, time_writer) = TimeReader::new(
let time = Time::new(
fork.as_ref()
.map(|f| f.block_timestamp)
.unwrap_or(NON_FORK_FIRST_BLOCK_TIMESTAMP),
Expand All @@ -64,7 +64,7 @@ impl InMemoryNodeInner {

let node_inner = InMemoryNodeInner::new(
blockchain.clone(),
time_writer,
time.clone(),
fork_storage.clone(),
fee_input_provider.clone(),
filters,
Expand All @@ -77,7 +77,7 @@ impl InMemoryNodeInner {
Arc::new(RwLock::new(node_inner)),
fork_storage,
Arc::new(blockchain),
time,
Arc::new(time),
)
}
}
24 changes: 8 additions & 16 deletions crates/core/src/node/inner/node_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl NodeExecutor {
let mut node_inner = self.node_inner.write().await;

// Save old interval to restore later: it might get replaced with `interval` below
let old_interval = node_inner.time_writer.get_block_timestamp_interval();
let old_interval = node_inner.time.get_block_timestamp_interval();
let result = async {
let mut block_numbers = Vec::with_capacity(tx_batches.len());
// Processing the entire vector is essentially atomic here because `NodeExecutor` is
Expand All @@ -110,9 +110,7 @@ impl NodeExecutor {
// Enforce provided interval starting from the second block (i.e. first block should
// use the existing interval).
if i == 1 {
node_inner
.time_writer
.set_block_timestamp_interval(Some(interval));
node_inner.time.set_block_timestamp_interval(Some(interval));
}
let base_system_contracts = self
.system_contracts
Expand All @@ -125,9 +123,7 @@ impl NodeExecutor {
}
.await;
// Restore old interval
node_inner
.time_writer
.set_block_timestamp_interval(old_interval);
node_inner.time.set_block_timestamp_interval(old_interval);

// Reply to sender if we can, otherwise hold result for further processing
let result = if let Err(result) = reply.send(result) {
Expand All @@ -143,11 +139,7 @@ impl NodeExecutor {
}

async fn increase_time(&self, delta: u64, reply: oneshot::Sender<()>) {
self.node_inner
.write()
.await
.time_writer
.increase_time(delta);
self.node_inner.write().await.time.increase_time(delta);
// Reply to sender if we can
if reply.send(()).is_err() {
tracing::info!("failed to reply as receiver has been dropped");
Expand All @@ -163,7 +155,7 @@ impl NodeExecutor {
.node_inner
.write()
.await
.time_writer
.time
.enforce_next_timestamp(timestamp);
// Reply to sender if we can, otherwise hold result for further processing
let result = if let Err(result) = reply.send(result) {
Expand All @@ -183,7 +175,7 @@ impl NodeExecutor {
.node_inner
.write()
.await
.time_writer
.time
.set_current_timestamp_unchecked(timestamp);
// Reply to sender if we can
if reply.send(result).is_err() {
Expand All @@ -195,7 +187,7 @@ impl NodeExecutor {
self.node_inner
.write()
.await
.time_writer
.time
.set_block_timestamp_interval(Some(delta));
}

Expand All @@ -204,7 +196,7 @@ impl NodeExecutor {
.node_inner
.write()
.await
.time_writer
.time
.remove_block_timestamp_interval();
// Reply to sender if we can
if reply.send(result).is_err() {
Expand Down
Loading

0 comments on commit 3bd0e04

Please sign in to comment.