Skip to content

Commit 7994cfa

Browse files
authored
fix: do not panic if proof is null (#533)
# What ❔ do not panic if proof is null, wait until it's ready instead ## Why ❔ proofs are calculated asynchronously with v26 upgrade, so it can be that proof is not ready for some short period after withdrawal's block is finalized ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `cargo fmt`.
1 parent bd713eb commit 7994cfa

File tree

11 files changed

+56
-14
lines changed

11 files changed

+56
-14
lines changed

bin/withdrawal-finalizer/src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Metrics for main binary
22
3+
#![allow(unexpected_cfgs)]
4+
35
use std::time::Duration;
46

57
use ethers::types::U256;

chain-events/src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Metrics for chain events
22
3+
#![allow(unexpected_cfgs)]
4+
35
use vise::{Counter, Gauge, Metrics};
46

57
/// Chain events metrics.

client/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,13 @@ impl<P: JsonRpcClient> ZksyncMiddleware for Provider<P> {
415415

416416
let sender = log.topics[1].into();
417417

418-
let proof = self
418+
// Proof can be not ready yet.
419+
let Some(proof) = self
419420
.get_log_proof(withdrawal_hash, Some(l2_to_l1_log_index as u64))
420421
.await?
421-
.expect("Log proof should be present. qed");
422+
else {
423+
return Ok(None);
424+
};
422425

423426
let message: Bytes = match ethers::abi::decode(&[ParamType::Bytes], &log.data)
424427
.expect("log data is valid rlp data; qed")

client/src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Metrics for client
22
3+
#![allow(unexpected_cfgs)]
4+
35
use std::time::Duration;
46

57
use vise::{Buckets, Histogram, LabeledFamily, Metrics};

deny.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ db-path = "~/.cargo/advisory-db"
1212
db-urls = ["https://github.com/rustsec/advisory-db"]
1313
yanked = "warn"
1414
ignore = [
15-
"RUSTSEC-2023-0052"
15+
"RUSTSEC-2023-0052",
16+
"RUSTSEC-2024-0384",
17+
"RUSTSEC-2024-0421"
1618
]
1719

1820
[licenses]

finalizer/src/lib.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const OUT_OF_FUNDS_BACKOFF: Duration = Duration::from_secs(10);
4444
/// Backoff period if one of the loop iterations has failed.
4545
const LOOP_ITERATION_ERROR_BACKOFF: Duration = Duration::from_secs(5);
4646

47+
/// Interval between successful loop iterations.
48+
const LOOP_ITERATION_OK_INTERVAL: Duration = Duration::from_secs(1);
49+
4750
/// A newtype that represents a set of addresses in JSON format.
4851
#[derive(Debug, Eq, PartialEq)]
4952
pub struct AddrList(pub Vec<Address>);
@@ -491,11 +494,12 @@ fn is_gas_required_exceeds_allowance<M: Middleware>(e: &<M as Middleware>::Error
491494
}
492495

493496
// Request finalization parameters for a set of withdrawals in parallel.
497+
// Returns `None` if params for some withdrawal are not ready yet.
494498
async fn request_finalize_params<M2>(
495499
pgpool: &PgPool,
496500
middleware: M2,
497501
hash_and_indices: &[(H256, u16, u64)],
498-
) -> Vec<WithdrawalParams>
502+
) -> Option<Vec<WithdrawalParams>>
499503
where
500504
M2: ZksyncMiddleware,
501505
{
@@ -504,20 +508,30 @@ where
504508
// Run all parametere fetching in parallel.
505509
// Filter out errors and log them and increment a metric counter.
506510
// Return successful fetches.
507-
for (i, result) in futures::future::join_all(hash_and_indices.iter().map(|(h, i, id)| {
511+
let params_opt = futures::future::join_all(hash_and_indices.iter().map(|(h, i, id)| {
508512
middleware
509513
.finalize_withdrawal_params(*h, *i as usize)
510-
.map_ok(|r| {
511-
let mut r = r.expect("always able to ask withdrawal params; qed");
512-
r.id = *id;
514+
.map_ok(|mut r| {
515+
if let Some(r) = r.as_mut() {
516+
r.id = *id;
517+
}
513518
r
514519
})
515520
.map_err(crate::Error::from)
516521
}))
517-
.await
518-
.into_iter()
519-
.enumerate()
520-
{
522+
.await;
523+
524+
// If any element is `None` then return `None`.
525+
let mut params = Vec::with_capacity(params_opt.len());
526+
for result in params_opt {
527+
match result {
528+
Ok(Some(param)) => params.push(Ok(param)),
529+
Ok(None) => return None,
530+
Err(err) => params.push(Err(err)),
531+
}
532+
}
533+
534+
for (i, result) in params.into_iter().enumerate() {
521535
match result {
522536
Ok(r) => ok_results.push(r),
523537
Err(e) => {
@@ -535,7 +549,7 @@ where
535549
}
536550
}
537551

538-
ok_results
552+
Some(ok_results)
539553
}
540554

541555
// Continiously query the new withdrawals that have been seen by watcher
@@ -556,6 +570,8 @@ async fn params_fetcher_loop<M1, M2>(
556570
{
557571
tracing::error!("params fetcher iteration ended with {e}");
558572
tokio::time::sleep(LOOP_ITERATION_ERROR_BACKOFF).await;
573+
} else {
574+
tokio::time::sleep(LOOP_ITERATION_OK_INTERVAL).await;
559575
}
560576
}
561577
}
@@ -584,7 +600,12 @@ where
584600
.map(|p| (p.key.tx_hash, p.key.event_index_in_tx as u16, p.id))
585601
.collect();
586602

587-
let params = request_finalize_params(pool, &middleware, &hash_and_index_and_id).await;
603+
let Some(params) = request_finalize_params(pool, &middleware, &hash_and_index_and_id).await
604+
else {
605+
// Early-return if params are not ready.
606+
tracing::info!("Params are not ready");
607+
return Ok(());
608+
};
588609

589610
let already_finalized: Vec<_> = get_finalized_withdrawals(&params, zksync_contract, l1_bridge)
590611
.await?

finalizer/src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Metrics for finalizer
22
3+
#![allow(unexpected_cfgs)]
4+
35
use vise::{Counter, Gauge, Metrics};
46

57
/// Finalizer metrics

storage/src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Metrics for storage
22
3+
#![allow(unexpected_cfgs)]
4+
35
use std::time::Duration;
46

57
use vise::{Buckets, Histogram, LabeledFamily, Metrics};

tx-sender/src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Metrics for tx sender
22
3+
#![allow(unexpected_cfgs)]
4+
35
use vise::{Counter, Metrics};
46

57
/// TX Sender metrics

watcher/src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Metrics for withdrawal watcher
22
3+
#![allow(unexpected_cfgs)]
4+
35
use vise::{Gauge, Metrics};
46

57
/// Watcher metrics

0 commit comments

Comments
 (0)