From 5d01c7b035a2a240d0d5cb8875611ce404e5faa1 Mon Sep 17 00:00:00 2001 From: Yin Guanhao Date: Tue, 5 Dec 2023 13:04:35 +0800 Subject: [PATCH] Use derived decode and abi/selector for Panic --- crates/relayer/src/chain/axon/eth_err.rs | 21 ++++++++------------- crates/relayer/src/chain/axon/utils.rs | 4 ++-- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/crates/relayer/src/chain/axon/eth_err.rs b/crates/relayer/src/chain/axon/eth_err.rs index dbc4a3f31..140d2c9a9 100644 --- a/crates/relayer/src/chain/axon/eth_err.rs +++ b/crates/relayer/src/chain/axon/eth_err.rs @@ -1,17 +1,12 @@ -use ethers::abi::{Detokenize, ParamType, Uint}; +use ethers::abi::Uint; +use ethers::contract::EthCall; -/// Panic(uint) +/// For decoding and displaying `Panic(uint256)` errors. +/// +/// EthError derived decode_with_selector is buggy, so we derive `EthCall` instead. Decode with `AbiDecode::decode`. +#[derive(EthCall)] pub struct Panic(Uint); -impl Panic { - // Can't get the right selector with `derive(EthError)`, so I implement this manually. - pub fn decode_with_selector(bytes: &[u8]) -> Option { - let bytes = bytes.strip_prefix(b"\x4e\x48\x7b\x71")?; - let tokens = ethers::abi::decode(&[ParamType::Uint(32)], bytes).ok()?; - Some(Panic(Uint::from_tokens(tokens).ok()?)) - } -} - impl std::fmt::Display for Panic { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let e = PanicError::from_code(self.0.low_u32()); @@ -74,7 +69,7 @@ impl std::fmt::Display for PanicError { #[cfg(test)] mod test { - use ethers::contract::EthError; + use ethers::{abi::AbiDecode, contract::EthError}; use super::Panic; @@ -84,7 +79,7 @@ mod test { .unwrap(), ) .unwrap(); - if let Some(p) = Panic::decode_with_selector(&revert_data) { + if let Ok(p) = Panic::decode(&revert_data) { p.to_string() } else if let Some(s) = String::decode_with_selector(&revert_data) { s diff --git a/crates/relayer/src/chain/axon/utils.rs b/crates/relayer/src/chain/axon/utils.rs index 05adb88ba..bdac2b7b5 100644 --- a/crates/relayer/src/chain/axon/utils.rs +++ b/crates/relayer/src/chain/axon/utils.rs @@ -10,7 +10,7 @@ use crate::{ event::IbcEventWithHeight, ibc_contract::OwnableIBCHandlerEvents, }; -use ethers::{contract::ContractError, providers::Middleware, types::H256}; +use ethers::{abi::AbiDecode, contract::ContractError, providers::Middleware, types::H256}; use ibc_relayer_types::{ clients::{ ics07_axon::{client_state::AxonClientState, consensus_state::AxonConsensusState}, @@ -35,7 +35,7 @@ where { if let Some(r) = err.decode_revert::() { eyre::eyre!("Contract call reverted: {r}") - } else if let Some(p) = err.as_revert().and_then(|d| Panic::decode_with_selector(d)) { + } else if let Some(p) = err.as_revert().and_then(|d| Panic::decode(d).ok()) { eyre::eyre!("Contract call reverted: {p}") } else { err.into()