Skip to content

Commit

Permalink
fix: removes panics from client code (#185)
Browse files Browse the repository at this point in the history
remove panics from client code
  • Loading branch information
montekki committed Sep 14, 2023
1 parent 8d22f01 commit c06651c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
12 changes: 12 additions & 0 deletions client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M: Middleware> From<ContractError<M>> for Error {
Expand Down
26 changes: 9 additions & 17 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,10 @@ impl<P: JsonRpcClient> ZksyncMiddleware for Provider<P> {
<WithdrawalInitiatedFilter as EthEvent>::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;

Expand All @@ -395,12 +394,10 @@ impl<P: JsonRpcClient> ZksyncMiddleware for Provider<P> {
.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
Expand All @@ -411,12 +408,7 @@ impl<P: JsonRpcClient> ZksyncMiddleware for Provider<P> {
&& 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();

Expand All @@ -430,7 +422,7 @@ impl<P: JsonRpcClient> ZksyncMiddleware for Provider<P> {
.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;
Expand Down

0 comments on commit c06651c

Please sign in to comment.