Skip to content

Commit

Permalink
Replace ethers abi encode/decode with alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianBland committed May 17, 2024
1 parent 32283ec commit a9f0988
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
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.

13 changes: 7 additions & 6 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,22 @@ ethers-core = { version = "2.0", optional = true }
# alloy-transport = { git = "https://github.com/alloy-rs/alloy.git", rev = "44b8a6d", optional = true, default-features = false }

[dev-dependencies]
alloy-provider = { git = "https://github.com/alloy-rs/alloy.git", rev = "44b8a6d", default-features = false, features = [
"reqwest",
] }
alloy-sol-types = { version = "0.7.0", default-features = false, features = [
"std",
] }
alloy-sol-macro = "0.7.0"
# needed for enabling TLS to use HTTPS connections when testing alloy DB
alloy-transport-http = { git = "https://github.com/alloy-rs/alloy.git", rev = "44b8a6d" }

ethers-contract = { version = "2.0.14", default-features = false }
anyhow = "1.0.83"
criterion = "0.5"
indicatif = "0.17"
reqwest = { version = "0.12" }

alloy-provider = { git = "https://github.com/alloy-rs/alloy.git", rev = "44b8a6d", default-features = false, features = [
"reqwest",
] }
# needed for enabling TLS to use HTTPS connections when testing alloy DB
alloy-transport-http = { git = "https://github.com/alloy-rs/alloy.git", rev = "44b8a6d" }

[features]
default = ["std", "c-kzg", "secp256k1", "portable", "blst"]
std = [
Expand Down
27 changes: 16 additions & 11 deletions crates/revm/src/optimism/fast_lz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,16 @@ fn solady_flz_compress(ib: &[u8]) -> u32 {

#[cfg(test)]
mod tests {
use ethers_contract::BaseContract;
use ethers_core::abi::{parse_abi, Uint};
use ethers_core::rand::RngCore;

use alloy_sol_macro::sol;
use alloy_sol_types::SolCall;

use super::*;
use crate::db::BenchmarkDB;
use crate::{
primitives::address, primitives::bytes, primitives::Bytecode, primitives::TransactTo, Evm,
primitives::address, primitives::bytes, primitives::Bytecode, primitives::TransactTo,
primitives::U256, Evm,
};

#[test]
Expand Down Expand Up @@ -269,9 +271,12 @@ mod tests {
fn test_flz_native_evm_parity() {
// This bytecode and ABI is for a contract, which wraps the LibZip library for easier fuzz testing.
// The source of this contract is here: https://github.com/danyalprout/fastlz/blob/main/src/FastLz.sol#L6-L10
let abi = BaseContract::from(
parse_abi(&["function fastLz(bytes) external view returns (uint256)"]).unwrap(),
);
sol! {
interface FastLz {
function fastLz(bytes input) external view returns (uint256);
}
}

let contract_bytecode = Bytecode::new_raw(bytes!("608060405234801561001057600080fd5b506004361061002b5760003560e01c8063920a769114610030575b600080fd5b61004361003e366004610374565b610055565b60405190815260200160405180910390f35b600061006082610067565b5192915050565b60606101e0565b818153600101919050565b600082840393505b838110156100a25782810151828201511860001a1590930292600101610081565b9392505050565b825b602082106100d75782516100c0601f8361006e565b5260209290920191601f19909101906021016100ab565b81156100a25782516100ec600184038361006e565b520160010192915050565b60006001830392505b61010782106101385761012a8360ff1661012560fd6101258760081c60e0018961006e565b61006e565b935061010682039150610100565b600782106101655761015e8360ff16610125600785036101258760081c60e0018961006e565b90506100a2565b61017e8360ff166101258560081c8560051b018761006e565b949350505050565b80516101d890838303906101bc90600081901a600182901a60081b1760029190911a60101b17639e3779b90260131c611fff1690565b8060021b6040510182815160e01c1860e01b8151188152505050565b600101919050565b5060405161800038823961800081016020830180600d8551820103826002015b81811015610313576000805b50508051604051600082901a600183901a60081b1760029290921a60101b91909117639e3779b9810260111c617ffc16909101805160e081811c878603811890911b9091189091528401908183039084841061026857506102a3565b600184019350611fff821161029d578251600081901a600182901a60081b1760029190911a60101b17810361029d57506102a3565b5061020c565b8383106102b1575050610313565b600183039250858311156102cf576102cc87878886036100a9565b96505b6102e3600985016003850160038501610079565b91506102f08782846100f7565b9650506103088461030386848601610186565b610186565b915050809350610200565b5050617fe061032884848589518601036100a9565b03925050506020820180820383525b81811161034e57617fe08101518152602001610337565b5060008152602001604052919050565b634e487b7160e01b600052604160045260246000fd5b60006020828403121561038657600080fd5b813567ffffffffffffffff8082111561039e57600080fd5b818401915084601f8301126103b257600080fd5b8135818111156103c4576103c461035e565b604051601f8201601f19908116603f011681019083821181831017156103ec576103ec61035e565b8160405282815287602084870101111561040557600080fd5b82602086016020830137600092810160200192909252509594505050505056fea264697066735822122000646b2953fc4a6f501bd0456ac52203089443937719e16b3190b7979c39511264736f6c63430008190033"));

let inputs = [
Expand All @@ -292,23 +297,23 @@ mod tests {
for call_bytes in inputs {
let native_val = flz_compress_len(&call_bytes);

let encoded = abi.encode("fastLz", bytes::Bytes::from(call_bytes.to_vec()));

let mut evm = Evm::builder()
.with_db(BenchmarkDB::new_bytecode(contract_bytecode.clone()))
.modify_tx_env(|tx| {
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to =
TransactTo::Call(address!("0000000000000000000000000000000000000000"));
tx.data = encoded.unwrap().0.into();
tx.data = FastLz::fastLzCall::new((call_bytes,)).abi_encode().into();
})
.build();

let result_and_state = evm.transact().unwrap();
let output = result_and_state.result.output().unwrap();
let evm_val: Uint = abi.decode_output("fastLz", output).unwrap();
let evm_val = FastLz::fastLzCall::abi_decode_returns(output, true)
.unwrap()
._0;

assert_eq!(native_val, evm_val.as_u32());
assert_eq!(U256::from(native_val), evm_val);
}
}
}
28 changes: 13 additions & 15 deletions examples/fork_ref_transact.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ethers_contract::BaseContract;
use ethers_core::abi::parse_abi;
use alloy_sol_macro::sol;
use alloy_sol_types::SolCall;
use ethers_providers::{Http, Provider};
use revm::{
db::{CacheDB, EmptyDB, EthersDB},
Expand Down Expand Up @@ -34,15 +34,13 @@ async fn main() -> anyhow::Result<()> {
// ETH/USDT pair on Uniswap V2
let pool_address = address!("0d4a11d5EEaaC28EC3F61d100daF4d40471f1852");

// generate abi for the calldata from the human readable interface
let abi = BaseContract::from(
parse_abi(&[
"function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)",
])?
);
// // generate abi for the calldata from the human readable interface
sol! {
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
}

// encode abi into Bytes
let encoded = abi.encode("getReserves", ())?;
let encoded = getReservesCall::new(()).abi_encode();

// initialize new EthersDB
let mut ethersdb = EthersDB::new(Arc::clone(&client), None).unwrap();
Expand Down Expand Up @@ -74,7 +72,7 @@ async fn main() -> anyhow::Result<()> {
// account you want to transact with
tx.transact_to = TransactTo::Call(pool_address);
// calldata formed via abigen
tx.data = encoded.0.into();
tx.data = encoded.into();
// transaction value in wei
tx.value = U256::from(0);
})
Expand All @@ -94,13 +92,13 @@ async fn main() -> anyhow::Result<()> {
result => panic!("Execution failed: {result:?}"),
};

// decode bytes to reserves + ts via ethers-rs's abi decode
let (reserve0, reserve1, ts): (u128, u128, u32) = abi.decode_output("getReserves", value)?;
// decode bytes to reserves + ts via alloy's abi decode
let return_vals = getReservesCall::abi_decode_returns(&value, true)?;

// Print emulated getReserves() call output
println!("Reserve0: {:#?}", reserve0);
println!("Reserve1: {:#?}", reserve1);
println!("Timestamp: {:#?}", ts);
println!("Reserve0: {:#?}", return_vals.reserve0);
println!("Reserve1: {:#?}", return_vals.reserve1);
println!("Timestamp: {:#?}", return_vals.blockTimestampLast);

Ok(())
}

0 comments on commit a9f0988

Please sign in to comment.