Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return an API error if eth_call reverts #1812

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion evm_scilla_js_tests/test/ContractRevert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("Revert Contract Call", function () {
});

it("Should revert transaction with a custom message if the called function reverts with custom message", async function () {
const REVERT_MESSAGE = "reverted!!";
const REVERT_MESSAGE = "revert: reverted!!";
try {
await contract.revertCallWithMessage(REVERT_MESSAGE, {value: 1000});
} catch (error: any) {
Expand Down
1 change: 1 addition & 0 deletions zilliqa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ blsful = { git = "https://github.com/JamesHinshelwood/agora-blsful", branch = "u
bech32 = "0.11.0"
cfg-if = "1.0.0"
serde_repr = "0.1.19"
thiserror = "2.0.3"

[dev-dependencies]
alloy = { version = "0.4.2", default-features = false, features = ["rand"] }
Expand Down
8 changes: 6 additions & 2 deletions zilliqa/src/api/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use super::{
use crate::{
api::zil::ZilAddress,
crypto::Hash,
error::ensure_success,
message::Block,
node::Node,
pool::TxAddResult,
Expand Down Expand Up @@ -183,7 +184,7 @@ fn call(params: Params, node: &Arc<Mutex<Node>>) -> Result<String> {
let block = node.get_block(block_id)?;
let block = build_errored_response_for_missing_block(block_id, block)?;

let ret = node.call_contract(
let result = node.call_contract(
&block,
call_params.from,
call_params.to,
Expand All @@ -195,7 +196,10 @@ fn call(params: Params, node: &Arc<Mutex<Node>>) -> Result<String> {
call_params.value.to(),
)?;

Ok(ret.to_hex())
match ensure_success(result) {
Ok(output) => Ok(output.to_hex()),
Err(err) => Err(ErrorObjectOwned::from(err).into()),
}
}

fn chain_id(params: Params, node: &Arc<Mutex<Node>>) -> Result<String> {
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod net;
mod others;
pub mod ots;
pub mod subscription_id_provider;
mod to_hex;
pub mod to_hex;
mod trace;
pub mod types;
mod web3;
Expand Down
99 changes: 99 additions & 0 deletions zilliqa/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::{
error::Error,
fmt::{self, Display, Formatter},
};

use alloy::{
primitives::Bytes, rpc::types::error::EthRpcErrorCode, sol_types::decode_revert_reason,
};
use jsonrpsee::types::ErrorObjectOwned;
use revm::primitives::{ExecutionResult, HaltReason, OutOfGasError};

use crate::api::to_hex::ToHex;

pub fn ensure_success(result: ExecutionResult) -> Result<Bytes, TransactionError> {
match result {
ExecutionResult::Success { output, .. } => Ok(output.into_data()),
ExecutionResult::Revert { output, .. } => {
Err(TransactionError::Revert(RevertError::new(output)))
}
ExecutionResult::Halt { reason, gas_used } => match reason {
HaltReason::OutOfGas(err) => match err {
OutOfGasError::Basic => Err(TransactionError::BasicOutOfGas(gas_used)),
OutOfGasError::MemoryLimit | OutOfGasError::Memory => {
Err(TransactionError::MemoryOutOfGas(gas_used))
}
OutOfGasError::Precompile => Err(TransactionError::PrecompileOutOfGas(gas_used)),
OutOfGasError::InvalidOperand => {
Err(TransactionError::InvalidOperandOutOfGas(gas_used))
}
},
reason => Err(TransactionError::EvmHalt(reason)),
},
}
}

/// An error from an executed transaction.
///
/// Many of the error strings and codes are de-facto standardised by other Ethereum clients.
///
// Much of this is derived from reth:
// https://github.com/paradigmxyz/reth/blob/bf44c9724f68d4aabc9ff1e27d278f36328b8d8f/crates/rpc/rpc-eth-types/src/error/mod.rs#L303
// Licensed under the Apache and MIT licenses.
#[derive(thiserror::Error, Debug)]
pub enum TransactionError {
#[error(transparent)]
Revert(RevertError),
#[error("out of gas: gas required exceeds allowance: {0}")]
BasicOutOfGas(u64),
#[error("out of gas: gas exhausted during memory expansion: {0}")]
MemoryOutOfGas(u64),
#[error("out of gas: gas exhausted during precompiled contract execution: {0}")]
PrecompileOutOfGas(u64),
#[error("out of gas: invalid operand to an opcode: {0}")]
InvalidOperandOutOfGas(u64),
#[error("EVM error: {0:?}")]
EvmHalt(HaltReason),
}

impl TransactionError {
pub fn error_code(&self) -> i32 {
match self {
TransactionError::Revert(_) => EthRpcErrorCode::ExecutionError.code(),
_ => EthRpcErrorCode::TransactionRejected.code(),
}
}
}

impl From<TransactionError> for ErrorObjectOwned {
fn from(error: TransactionError) -> Self {
let data = if let TransactionError::Revert(RevertError(ref output)) = error {
output.as_ref().map(|o| o.to_hex())
} else {
None
};

ErrorObjectOwned::owned(error.error_code(), error.to_string(), data)
}
}

#[derive(Debug)]
pub struct RevertError(Option<Bytes>);

impl RevertError {
pub fn new(output: Bytes) -> Self {
RevertError((!output.is_empty()).then_some(output))
}
}

impl Error for RevertError {}

impl Display for RevertError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("execution reverted")?;
if let Some(reason) = self.0.as_ref().and_then(|b| decode_revert_reason(b)) {
write!(f, ": {reason}")?;
}
Ok(())
}
}
64 changes: 0 additions & 64 deletions zilliqa/src/eth_helpers.rs

This file was deleted.

Loading
Loading