From c06651c52c3f590600bc084b1fa170c958fb958c Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Thu, 14 Sep 2023 15:05:37 +0200 Subject: [PATCH] fix: removes panics from client code (#185) remove panics from client code --- client/src/error.rs | 12 ++++++++++++ client/src/lib.rs | 26 +++++++++----------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/client/src/error.rs b/client/src/error.rs index 4c03f467..52e7d93d 100644 --- a/client/src/error.rs +++ b/client/src/error.rs @@ -32,6 +32,18 @@ pub enum Error { #[error("Failed to decode withdrawal event from log")] FailedToDecodeLog, + + #[error("WithdrawalInitiatedFilter is not found for {0:?} at index {1}")] + WithdrawalInitiatedFilterNotFound(H256, usize), + + #[error("L2ToL1 message for transaction {0:?} with value {1:?} not found")] + L2ToL1WithValueNotFound(H256, H256), + + #[error("L1MessageSent log not found for transaction {0:?} at index {1}")] + L1MessageSentNotFound(H256, usize), + + #[error("Message not RLP bytes encoded: {0}")] + MessageNotRlpBytes(String), } impl From> for Error { diff --git a/client/src/lib.rs b/client/src/lib.rs index 7148cbb4..ecfedd93 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -375,11 +375,10 @@ impl ZksyncMiddleware for Provider

{ ::decode_log(&raw_log).ok() }) .nth(index) - .unwrap_or_else(|| panic!( - "A matching WithdrawalInitiatedFilter event is not found for {:?} at index {}", + .ok_or(Error::WithdrawalInitiatedFilterNotFound( withdrawal_hash, - index) - ); + index, + ))?; let l1_receiver = withdrawal_initiated_event.l_1_receiver; @@ -395,12 +394,10 @@ impl ZksyncMiddleware for Provider

{ .filter(|(_, log)| log.value == l2_to_l1_message_hash) .nth(index) .map(|(i, _)| i) - .unwrap_or_else(|| { - panic!( - "An L2ToL1 message for {:?} with value {:?} not found", - withdrawal_hash, l2_to_l1_message_hash, - ) - }); + .ok_or(Error::L2ToL1WithValueNotFound( + withdrawal_hash, + l2_to_l1_message_hash, + ))?; let l1_batch_tx_id = receipt.l1_batch_tx_index; let log = receipt @@ -411,12 +408,7 @@ impl ZksyncMiddleware for Provider

{ && entry.topics[0] == L1MessageSentFilter::signature() }) .nth(index) - .unwrap_or_else(|| { - panic!( - "A L1MessageSent log is not found for {:?} at index {}", - withdrawal_hash, index - ) - }); + .ok_or(Error::L1MessageSentNotFound(withdrawal_hash, index))?; let sender = log.topics[1].into(); @@ -430,7 +422,7 @@ impl ZksyncMiddleware for Provider

{ .swap_remove(0) { Token::Bytes(b) => b.into(), - b => panic!("log data is expected to be rlp bytes, got {b:?}"), + b => return Err(Error::MessageNotRlpBytes(format!("{b:?}"))), }; let l2_message_index = proof.id;