From 1efa90bc3ecb0a992ad28012f2133de169b3e524 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Vella Date: Tue, 19 Sep 2023 20:40:59 +0100 Subject: [PATCH] Added revm test project. --- riscv/tests/riscv.rs | 10 +++ riscv/tests/riscv_data/evm/Cargo.toml | 10 +++ .../tests/riscv_data/evm/rust-toolchain.toml | 4 ++ riscv/tests/riscv_data/evm/src/lib.rs | 64 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 riscv/tests/riscv_data/evm/Cargo.toml create mode 100644 riscv/tests/riscv_data/evm/rust-toolchain.toml create mode 100644 riscv/tests/riscv_data/evm/src/lib.rs diff --git a/riscv/tests/riscv.rs b/riscv/tests/riscv.rs index 75772fa4b..5df9ae0fb 100644 --- a/riscv/tests/riscv.rs +++ b/riscv/tests/riscv.rs @@ -86,6 +86,16 @@ fn test_password() { verify_crate(case, vec![]); } +// TODO: uncomment this when we properly support revm, so we don't break nightly +/* +#[test] +#[ignore = "Too slow"] +fn test_evm() { + let case = "evm"; + verify_crate(case, vec![]); +} +*/ + #[test] #[ignore = "Too slow"] #[should_panic(expected = "Witness generation failed.")] diff --git a/riscv/tests/riscv_data/evm/Cargo.toml b/riscv/tests/riscv_data/evm/Cargo.toml new file mode 100644 index 000000000..6dbe455d8 --- /dev/null +++ b/riscv/tests/riscv_data/evm/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "evm" +version = "0.1.0" +edition = "2021" + +[dependencies] +revm = { git = "https://github.com/bluealloy/revm.git", default-features = false } +runtime = { path = "../../../runtime" } + +[workspace] diff --git a/riscv/tests/riscv_data/evm/rust-toolchain.toml b/riscv/tests/riscv_data/evm/rust-toolchain.toml new file mode 100644 index 000000000..8e6c8652a --- /dev/null +++ b/riscv/tests/riscv_data/evm/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "nightly-2023-01-03" +targets = ["riscv32imac-unknown-none-elf"] +profile = "minimal" diff --git a/riscv/tests/riscv_data/evm/src/lib.rs b/riscv/tests/riscv_data/evm/src/lib.rs new file mode 100644 index 000000000..f2b75a672 --- /dev/null +++ b/riscv/tests/riscv_data/evm/src/lib.rs @@ -0,0 +1,64 @@ +#![no_std] + +use revm::{ + db::{CacheDB, EmptyDB}, + primitives::{ruint::Uint, AccountInfo, Bytecode, Bytes, TransactTo, B160, B256, U256}, + EVM, +}; +use runtime::print; + +/* +mstore(0, 666) +return(0, 32) +*/ +static BYTECODE: &[u8] = &[ + 0x61, 0x02, 0x9a, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3, +]; + +static CODE_HASH: B256 = B256([ + 0xe3, 0xc8, 0x4e, 0x69, 0xba, 0xc7, 0x1c, 0x15, 0x9b, 0x2f, 0xf0, 0xd6, 0x2b, 0x9a, 0x5c, 0x23, + 0x18, 0x87, 0xa8, 0x09, 0xa9, 0x6c, 0xb4, 0xa2, 0x62, 0xa4, 0xb9, 0x6e, 0xd7, 0x8a, 0x1d, 0xb2, +]); + +static CONTRACT_ADDR: B160 = B160([ + 0x0d, 0x4a, 0x11, 0xd5, 0xEE, 0xaa, 0xC2, 0x8E, 0xC3, 0xF6, 0x1d, 0x10, 0x0d, 0xaF, 0x4d, 0x40, + 0x47, 0x1f, 0x18, 0x52, +]); + +#[no_mangle] +fn main() { + let mut db = CacheDB::new(EmptyDB::default()); + + // Fill database: + let bytecode = Bytes::from_static(BYTECODE); + let account = AccountInfo::new(Uint::from(10), 0, CODE_HASH, Bytecode::new_raw(bytecode)); + + db.insert_account_info(CONTRACT_ADDR, account); + + let mut evm: EVM> = EVM::new(); + evm.database(db); + + let result = evm.transact().unwrap(); + + // fill in missing bits of env struc + // change that to whatever caller you want to be + evm.env.tx.caller = B160::from_slice(&[0; 20]); + // account you want to transact with + evm.env.tx.transact_to = TransactTo::Call(CONTRACT_ADDR); + // calldata formed via abigen + evm.env.tx.data = Bytes::new(); + // transaction value in wei + evm.env.tx.value = U256::try_from(0).unwrap(); + + match result.result { + revm::primitives::ExecutionResult::Success { + reason, + gas_used, + gas_refunded, + logs, + output, + } => print!("Success: {:#?}", output.into_data()), + revm::primitives::ExecutionResult::Revert { gas_used, output } => panic!("Revert!"), + revm::primitives::ExecutionResult::Halt { reason, gas_used } => panic!("Halt!"), + }; +}