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

Feat: Enhance Error Resolution EVM #1758

Merged
merged 4 commits into from
Mar 6, 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
2 changes: 1 addition & 1 deletion pallets/ethereum-transaction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cfg-traits = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }

pallet-balances = { workspace = true }
pallet-balances = { workspace = true, default-features = true }
pallet-evm-precompile-simple = { workspace = true, default-features = true }
pallet-timestamp = { workspace = true, default-features = true }

Expand Down
40 changes: 38 additions & 2 deletions pallets/ethereum-transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod tests;

#[frame_support::pallet]
pub mod pallet {
use ethereum::ReceiptV3;
use frame_system::pallet_prelude::OriginFor;

use super::*;
Expand Down Expand Up @@ -64,6 +65,27 @@ pub mod pallet {
}
}

#[pallet::error]
pub enum Error<T> {
EvmExecutionFailed,
}

impl<T: Config> Pallet<T>
where
OriginFor<T>:
From<pallet_ethereum::Origin> + Into<Result<pallet_ethereum::Origin, OriginFor<T>>>,
{
fn valid_code(receipt: &ReceiptV3) -> bool {
let code = match receipt {
ReceiptV3::Legacy(inner)
| ReceiptV3::EIP2930(inner)
| ReceiptV3::EIP1559(inner) => inner.status_code,
};

code == 1
}
}

impl<T: Config> EthereumTransactor for Pallet<T>
where
OriginFor<T>:
Expand Down Expand Up @@ -107,7 +129,7 @@ pub mod pallet {

Nonce::<T>::put(nonce.saturating_add(U256::one()));

pallet_ethereum::Pallet::<T>::transact(
let info = pallet_ethereum::Pallet::<T>::transact(
pallet_ethereum::Origin::EthereumTransaction(from).into(),
transaction,
)
Expand All @@ -129,7 +151,21 @@ pub mod pallet {
.map_or(Some(read_weight), |weight| {
Some(weight.saturating_add(read_weight))
}),
})
})?;

// NOTE: The Ethereuem side of things never returns a DispatchError
// if the execution failed. But we can check that manually by
// querying the `Pending` storage of the pallet-ethereum.
let pending = pallet_ethereum::Pending::<T>::get();
let (_, _, receipt) = pending.last().ok_or(DispatchError::Other(
"Ethereuem not adding pending storage. Unexpected.",
))?;

if Pallet::<T>::valid_code(receipt) {
Ok(info)
} else {
Err(Error::<T>::EvmExecutionFailed.into())
}
}
}
}
29 changes: 12 additions & 17 deletions pallets/ethereum-transaction/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pallet_evm::{AddressMapping, Error::BalanceLow};
use sp_core::{crypto::AccountId32, H160, U256};

use super::mock::*;
use crate::pallet::Nonce;
use crate::{pallet::Nonce, Error};

mod utils {
use super::*;
Expand Down Expand Up @@ -101,22 +101,17 @@ mod call {

assert_eq!(Nonce::<Runtime>::get(), U256::from(0));

// NOTE: We can not check for errors as the internal `pallet-ethereum` logic
// does not transform an EVM error into an Substrate error and return
// `Ok(..)` in theses cases.
//
// We can also not mimic the `pallet-ethereum::Pallet::<T>::transact(..)`
// code path as some needed parts are private.
assert_ok!(<EthereumTransaction as EthereumTransactor>::call(
sender,
to,
data.as_slice(),
value,
gas_price,
gas_limit,
));

assert_eq!(Nonce::<Runtime>::get(), U256::from(1));
assert_eq!(
<EthereumTransaction as EthereumTransactor>::call(
sender,
to,
data.as_slice(),
value,
gas_price,
gas_limit,
),
Err(Error::<Runtime>::EvmExecutionFailed.into())
);
});
}
}
4 changes: 2 additions & 2 deletions pallets/liquidity-pools-gateway/routers/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod evm_router {
target_contract_address: test_contract_address,
target_contract_hash: test_contract_hash,
fee_values: FeeValues {
value: U256::from(10),
value: U256::from(0),
gas_limit: U256::from(10),
gas_price: U256::from(10),
},
Expand Down Expand Up @@ -337,7 +337,7 @@ mod axelar_evm {
target_contract_address: axelar_contract_address,
target_contract_hash: axelar_contract_hash,
fee_values: FeeValues {
value: U256::from(10),
value: U256::from(0),
gas_limit: U256::from(10),
gas_price: U256::from(10),
},
Expand Down
3 changes: 1 addition & 2 deletions runtime/integration-tests/src/evm/ethereum_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ async fn call() {
U256::from(0x100000),
);

// NOTE: WE CAN NOTE CHECK WHETHER THE EVM ERRORS OUT
assert!(res.is_ok());
assert!(res.is_err());
})
.unwrap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4854,8 +4854,8 @@ mod development {
target_contract_address: axelar_contract_address,
target_contract_hash: axelar_contract_hash,
fee_values: FeeValues {
value: U256::from(10),
gas_limit: U256::from(transaction_call_cost + 10_000),
value: U256::from(0),
gas_limit: U256::from(transaction_call_cost + 1_000_000),
gas_price: U256::from(10),
},
};
Expand Down
Loading