Skip to content

Commit

Permalink
add parameters to the ConsistencyChecker structure itself, rather tha…
Browse files Browse the repository at this point in the history
…n to the run() method (#152)
  • Loading branch information
ilitteri authored Feb 19, 2024
1 parent 776f935 commit 7f52a8d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
9 changes: 4 additions & 5 deletions core/bin/external_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ async fn init_tasks(
.context("failed initializing metadata calculator")?;
healthchecks.push(Box::new(metadata_calculator.tree_health_check()));

let l1_batch_commit_data_generator = Arc::new(RollupModeL1BatchCommitDataGenerator {});

let consistency_checker = ConsistencyChecker::new(
&config
.required
Expand All @@ -249,6 +251,7 @@ async fn init_tasks(
.build()
.await
.context("failed to build connection pool for ConsistencyChecker")?,
l1_batch_commit_data_generator,
);

let batch_status_updater = BatchStatusUpdater::new(
Expand All @@ -268,11 +271,7 @@ async fn init_tasks(
.context("failed to build a tree_pool")?;
let tree_handle = task::spawn(metadata_calculator.run(tree_pool, tree_stop_receiver));

let l1_batch_commit_data_generator = Arc::new(RollupModeL1BatchCommitDataGenerator {});

let consistency_checker_handle = tokio::spawn(
consistency_checker.run(stop_receiver.clone(), l1_batch_commit_data_generator),
);
let consistency_checker_handle = tokio::spawn(consistency_checker.run(stop_receiver.clone()));

let updater_handle = task::spawn(batch_status_updater.run(stop_receiver.clone()));
let fee_address_migration_handle =
Expand Down
17 changes: 10 additions & 7 deletions core/lib/zksync_core/src/consistency_checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,18 @@ pub struct ConsistencyChecker {
l1_batch_updater: Box<dyn UpdateCheckedBatch>,
l1_data_mismatch_behavior: L1DataMismatchBehavior,
pool: ConnectionPool,
l1_batch_commit_data_generator: Arc<dyn L1BatchCommitDataGenerator>,
}

impl ConsistencyChecker {
const DEFAULT_SLEEP_INTERVAL: Duration = Duration::from_secs(5);

pub fn new(web3_url: &str, max_batches_to_recheck: u32, pool: ConnectionPool) -> Self {
pub fn new(
web3_url: &str,
max_batches_to_recheck: u32,
pool: ConnectionPool,
l1_batch_commit_data_generator: Arc<dyn L1BatchCommitDataGenerator>,
) -> Self {
let web3 = QueryClient::new(web3_url).unwrap();
Self {
contract: zksync_contracts::zksync_contract(),
Expand All @@ -150,6 +156,7 @@ impl ConsistencyChecker {
l1_batch_updater: Box::new(()),
l1_data_mismatch_behavior: L1DataMismatchBehavior::Log,
pool,
l1_batch_commit_data_generator,
}
}

Expand Down Expand Up @@ -252,11 +259,7 @@ impl ConsistencyChecker {
.await?)
}

pub async fn run(
mut self,
mut stop_receiver: watch::Receiver<bool>,
l1_batch_commit_data_generator: Arc<dyn L1BatchCommitDataGenerator>,
) -> anyhow::Result<()> {
pub async fn run(mut self, mut stop_receiver: watch::Receiver<bool>) -> anyhow::Result<()> {
// It doesn't make sense to start the checker until we have at least one L1 batch with metadata.
let earliest_l1_batch_number =
wait_for_l1_batch_with_metadata(&self.pool, self.sleep_interval, &mut stop_receiver)
Expand Down Expand Up @@ -297,7 +300,7 @@ impl ConsistencyChecker {
let Some(local) = LocalL1BatchCommitData::new(
&mut storage,
batch_number,
l1_batch_commit_data_generator.clone(),
self.l1_batch_commit_data_generator.clone(),
)
.await?
else {
Expand Down
2 changes: 2 additions & 0 deletions core/lib/zksync_core/src/consistency_checker/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub(crate) fn build_commit_tx_input_data(
pub(crate) fn create_mock_checker(
client: MockEthereum,
pool: ConnectionPool,
l1_batch_commit_data_generator: Arc<dyn L1BatchCommitDataGenerator>,
) -> ConsistencyChecker {
ConsistencyChecker {
contract: zksync_contracts::zksync_contract(),
Expand All @@ -77,6 +78,7 @@ pub(crate) fn create_mock_checker(
l1_batch_updater: Box::new(()),
l1_data_mismatch_behavior: L1DataMismatchBehavior::Bail,
pool,
l1_batch_commit_data_generator,
}
}

Expand Down
26 changes: 11 additions & 15 deletions core/lib/zksync_core/src/consistency_checker/tests/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ pub(crate) async fn normal_checker_function(
let (l1_batch_updates_sender, mut l1_batch_updates_receiver) = mpsc::unbounded_channel();
let checker = ConsistencyChecker {
l1_batch_updater: Box::new(l1_batch_updates_sender),
..create_mock_checker(client, pool.clone())
..create_mock_checker(client, pool.clone(), l1_batch_commit_data_generator)
};

let (stop_sender, stop_receiver) = watch::channel(false);
let checker_task =
tokio::spawn(checker.run(stop_receiver, l1_batch_commit_data_generator.clone()));
let checker_task = tokio::spawn(checker.run(stop_receiver));

// Add new batches to the storage.
for save_action in save_actions_mapper(&l1_batches) {
Expand Down Expand Up @@ -153,11 +152,11 @@ pub(crate) async fn checker_processes_pre_boojum_batches(
let (l1_batch_updates_sender, mut l1_batch_updates_receiver) = mpsc::unbounded_channel();
let checker = ConsistencyChecker {
l1_batch_updater: Box::new(l1_batch_updates_sender),
..create_mock_checker(client, pool.clone())
..create_mock_checker(client, pool.clone(), l1_batch_commit_data_generator)
};

let (stop_sender, stop_receiver) = watch::channel(false);
let checker_task = tokio::spawn(checker.run(stop_receiver, l1_batch_commit_data_generator));
let checker_task = tokio::spawn(checker.run(stop_receiver));

// Add new batches to the storage.
for save_action in save_actions_mapper(&l1_batches) {
Expand Down Expand Up @@ -228,10 +227,10 @@ pub async fn checker_functions_after_snapshot_recovery(
let (l1_batch_updates_sender, mut l1_batch_updates_receiver) = mpsc::unbounded_channel();
let checker = ConsistencyChecker {
l1_batch_updater: Box::new(l1_batch_updates_sender),
..create_mock_checker(client, pool.clone())
..create_mock_checker(client, pool.clone(), l1_batch_commit_data_generator)
};
let (stop_sender, stop_receiver) = watch::channel(false);
let checker_task = tokio::spawn(checker.run(stop_receiver, l1_batch_commit_data_generator));
let checker_task = tokio::spawn(checker.run(stop_receiver));

if delay_batch_insertion {
tokio::time::sleep(Duration::from_millis(10)).await;
Expand Down Expand Up @@ -287,14 +286,11 @@ pub(crate) async fn checker_detects_incorrect_tx_data(
}
drop(storage);

let checker = create_mock_checker(client, pool);
let checker = create_mock_checker(client, pool, l1_batch_commit_data_generator);
let (_stop_sender, stop_receiver) = watch::channel(false);
// The checker must stop with an error.
tokio::time::timeout(
Duration::from_secs(30),
checker.run(stop_receiver, l1_batch_commit_data_generator),
)
.await
.expect("Timed out waiting for checker to stop")
.unwrap_err();
tokio::time::timeout(Duration::from_secs(30), checker.run(stop_receiver))
.await
.expect("Timed out waiting for checker to stop")
.unwrap_err();
}

0 comments on commit 7f52a8d

Please sign in to comment.