Skip to content

Commit

Permalink
feat: add EVM modules linking function (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgar2017 authored Oct 10, 2024
1 parent b319585 commit 9c94faa
Show file tree
Hide file tree
Showing 24 changed files with 233 additions and 248 deletions.
274 changes: 149 additions & 125 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion compiler_tester/src/compiler_tester/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ mod tests {
verbosity: false,
quiet: false,
debug: false,
modes: vec!["Y+M3B3 0.8.26".to_owned()],
modes: vec!["Y+M3B3 0.8.27".to_owned()],
paths: vec!["tests/solidity/simple/default.sol".to_owned()],
groups: vec![],
benchmark: None,
Expand Down
5 changes: 1 addition & 4 deletions compiler_tester/src/compilers/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ impl Compiler for LLVMCompiler {
.into_iter()
.map(|(path, build)| {
let build = build.expect("Always valid");
let build = EVMBuild::new(
era_compiler_llvm_context::EVMBuild::default(),
build.runtime_build,
);
let build = EVMBuild::new(vec![], build.runtime_build);
(path, build)
})
.collect();
Expand Down
13 changes: 4 additions & 9 deletions compiler_tester/src/compilers/solidity/upstream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,10 @@ impl Compiler for SolidityCompiler {
.object
.as_str();
let build = EVMBuild::new(
era_compiler_llvm_context::EVMBuild::new(
hex::decode(bytecode_string).map_err(|error| {
anyhow::anyhow!(
"EVM bytecode of the contract `{path}` is invalid: {error}"
)
})?,
None,
),
era_compiler_llvm_context::EVMBuild::default(),
hex::decode(bytecode_string).map_err(|error| {
anyhow::anyhow!("EVM bytecode of the contract `{path}` is invalid: {error}")
})?,
vec![],
);
builds.insert(path, build);
}
Expand Down
9 changes: 2 additions & 7 deletions compiler_tester/src/compilers/yul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,8 @@ impl Compiler for YulCompiler {
})?
.object
.as_str();
let build = EVMBuild::new(
era_compiler_llvm_context::EVMBuild::new(
hex::decode(bytecode_string).expect("Always valid"),
None,
),
era_compiler_llvm_context::EVMBuild::default(),
);
let build =
EVMBuild::new(hex::decode(bytecode_string).expect("Always valid"), vec![]);
builds.insert(path, build);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ impl EVMContract {
pub const RUNTIME_CODE_REPEATS: usize = 32;

///
/// Returns the init code.
/// Returns the deploy code.
///
pub fn init_code(&self, size: usize) -> String {
pub fn deploy_code(&self, size: usize) -> String {
if size > 0xffff {
panic!("The bytecode is too large");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Metadata {
/// The test contracts as `instance -> path`.
#[serde(default)]
pub contracts: BTreeMap<String, String>,
/// The EVM auxiliary contracts as `instance -> init code`.
/// The EVM auxiliary contracts as `instance -> deploy code`.
#[serde(default)]
pub evm_contracts: BTreeMap<String, EVMContract>,
/// The test libraries for linking.
Expand Down
2 changes: 1 addition & 1 deletion compiler_tester/src/directories/matter_labs/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl MatterLabsTest {
for (instance, evm_contract) in self.metadata.evm_contracts.iter() {
let instruction_name = instance.split('_').next().expect("Always exists");
let runtime_code = evm_contract.runtime_code(instruction_name);
let mut bytecode = evm_contract.init_code(runtime_code.len());
let mut bytecode = evm_contract.deploy_code(runtime_code.len());
bytecode.push_str(runtime_code.as_str());

let bytecode = hex::decode(bytecode.as_str()).map_err(|error| {
Expand Down
48 changes: 19 additions & 29 deletions compiler_tester/src/test/case/input/deploy_evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//! The EVM deploy contract call input variant.
//!
use std::collections::HashMap;
use std::hash::RandomState;
use std::sync::Arc;
use std::sync::Mutex;

Expand All @@ -18,7 +16,6 @@ use crate::test::case::input::output::Output;
use crate::test::case::input::storage::Storage;
use crate::vm::eravm::deployers::EraVMDeployer;
use crate::vm::eravm::EraVM;
use crate::vm::evm::input::build::Build;
use crate::vm::evm::EVM;

use crate::vm::revm::revm_type_conversions::revm_bytes_to_vec_value;
Expand All @@ -32,8 +29,8 @@ use crate::vm::revm::Revm;
pub struct DeployEVM {
/// The contract identifier.
identifier: String,
/// The contract init code.
init_code: Vec<u8>,
/// The contract deploy code.
deploy_code: Vec<u8>,
/// The calldata.
calldata: Calldata,
/// The caller.
Expand All @@ -52,7 +49,7 @@ impl DeployEVM {
///
pub fn new(
identifier: String,
init_code: Vec<u8>,
deploy_code: Vec<u8>,
calldata: Calldata,
caller: web3::types::Address,
value: Option<u128>,
Expand All @@ -61,7 +58,7 @@ impl DeployEVM {
) -> Self {
Self {
identifier,
init_code,
deploy_code,
calldata,
caller,
value,
Expand Down Expand Up @@ -131,20 +128,15 @@ impl DeployEVM {
mode: Mode,
test_group: Option<String>,
name_prefix: String,
evm_builds: &HashMap<String, Build, RandomState>,
evm_version: Option<EVMVersion>,
) -> Revm<'a> {
let name = format!("{}[#deployer:{}]", name_prefix, self.identifier);

let build = evm_builds
.get(self.identifier.as_str())
.expect("Always valid");
let mut deploy_code = build.deploy_build.bytecode.to_owned();
deploy_code.extend(self.calldata.inner.clone());
let size = self.deploy_code.len();

let vm = vm.update_deploy_balance(&self.caller);
let mut vm =
vm.fill_deploy_new_transaction(self.caller, self.value, evm_version, deploy_code);
vm.fill_deploy_new_transaction(self.caller, self.value, evm_version, self.deploy_code);

let result = match vm.state.transact_commit() {
Ok(res) => res,
Expand Down Expand Up @@ -195,29 +187,27 @@ impl DeployEVM {
}
};

let output = match result {
let (output, gas, error) = match result {
ExecutionResult::Success {
reason: _,
gas_used: _,
gas_used,
gas_refunded: _,
logs,
output,
} => transform_success_output(output, logs),
ExecutionResult::Revert {
gas_used: _,
output,
} => {
} => (transform_success_output(output, logs), gas_used, None),
ExecutionResult::Revert { gas_used, output } => {
let return_data_value = revm_bytes_to_vec_value(output);
Output::new(return_data_value, true, vec![])
(Output::new(return_data_value, true, vec![]), gas_used, None)
}
ExecutionResult::Halt { reason, gas_used } => {
(Output::new(vec![], true, vec![]), gas_used, Some(reason))
}
ExecutionResult::Halt {
reason: _,
gas_used: _,
} => Output::new(vec![], true, vec![]),
};

if output == self.expected {
Summary::passed_deploy(summary, mode, name, test_group, 0, 0, 0, 0);
Summary::passed_deploy(summary, mode, name, test_group, size, 0, 0, gas);
} else if let Some(error) = error {
Summary::invalid(summary, Some(mode), name, format!("{error:?}"));
} else {
Summary::failed(
summary,
Expand Down Expand Up @@ -248,13 +238,13 @@ impl DeployEVM {
{
let name = format!("{}[#deployer:{}]", name_prefix, self.identifier);

let size = self.init_code.len();
let size = self.deploy_code.len();

vm.populate_storage(self.storage.inner);
let result = match deployer.deploy_evm::<M>(
name.clone(),
self.caller,
self.init_code,
self.deploy_code,
self.calldata.inner.clone(),
self.value,
vm,
Expand Down
22 changes: 6 additions & 16 deletions compiler_tester/src/test/case/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub mod storage_empty;
pub mod value;

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::hash::RandomState;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::Mutex;
Expand All @@ -27,7 +25,6 @@ use crate::summary::Summary;
use crate::test::instance::Instance;
use crate::vm::eravm::deployers::EraVMDeployer;
use crate::vm::eravm::EraVM;
use crate::vm::evm::input::build::Build;
use crate::vm::evm::EVM;
use crate::vm::revm::Revm;

Expand Down Expand Up @@ -124,7 +121,7 @@ impl Input {
)),
Instance::EVM(instance) => Input::DeployEVM(DeployEVM::new(
instance.path.to_owned(),
instance.init_code.to_owned(),
instance.deploy_code.to_owned(),
calldata,
caller,
value,
Expand Down Expand Up @@ -263,7 +260,7 @@ impl Input {
))),
Instance::EVM(instance) => Some(Input::DeployEVM(DeployEVM::new(
instance.path.to_owned(),
instance.init_code.to_owned(),
instance.deploy_code.to_owned(),
calldata.clone().into(),
*caller,
value,
Expand Down Expand Up @@ -307,7 +304,7 @@ impl Input {
))),
Instance::EVM(instance) => Some(Input::DeployEVM(DeployEVM::new(
instance.path.to_owned(),
instance.init_code.to_owned(),
instance.deploy_code.to_owned(),
Calldata::default(),
*caller,
None,
Expand Down Expand Up @@ -444,20 +441,13 @@ impl Input {
test_group: Option<String>,
name_prefix: String,
index: usize,
evm_builds: &HashMap<String, Build, RandomState>,
evm_version: Option<EVMVersion>,
) -> Revm<'a> {
match self {
Self::DeployEraVM { .. } => panic!("EraVM deploy transaction cannot be run on REVM"),
Self::DeployEVM(deploy) => deploy.run_revm(
summary,
vm,
mode,
test_group,
name_prefix,
evm_builds,
evm_version,
),
Self::DeployEVM(deploy) => {
deploy.run_revm(summary, vm, mode, test_group, name_prefix, evm_version)
}
Self::Runtime(runtime) => runtime.run_revm(
summary,
vm,
Expand Down
5 changes: 0 additions & 5 deletions compiler_tester/src/test/case/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub mod input;

use solidity_adapter::test::params::evm_version;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::hash::RandomState;
use std::sync::Arc;
use std::sync::Mutex;

Expand All @@ -17,7 +15,6 @@ use crate::summary::Summary;
use crate::test::instance::Instance;
use crate::vm::eravm::deployers::EraVMDeployer;
use crate::vm::eravm::EraVM;
use crate::vm::evm::input::build::Build;
use crate::vm::evm::EVM;
use crate::vm::revm::Revm;

Expand Down Expand Up @@ -167,7 +164,6 @@ impl Case {
mode: &Mode,
test_name: String,
test_group: Option<String>,
evm_builds: HashMap<String, Build, RandomState>,
evm_version: Option<evm_version::EVMVersion>,
) {
let name = if let Some(case_name) = self.name {
Expand All @@ -185,7 +181,6 @@ impl Case {
test_group.clone(),
name.clone(),
index,
&evm_builds,
evm_version,
)
}
Expand Down
8 changes: 4 additions & 4 deletions compiler_tester/src/test/instance/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct Instance {
pub is_main: bool,
/// Whether the instance is a library.
pub is_library: bool,
/// The init bytecode.
pub init_code: Vec<u8>,
/// The deploy bytecode.
pub deploy_code: Vec<u8>,
}

impl Instance {
Expand All @@ -28,14 +28,14 @@ impl Instance {
address: Option<web3::types::Address>,
is_main: bool,
is_library: bool,
init_code: Vec<u8>,
deploy_code: Vec<u8>,
) -> Self {
Self {
path,
address,
is_main,
is_library,
init_code,
deploy_code,
}
}
}
8 changes: 6 additions & 2 deletions compiler_tester/src/test/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ impl Instance {
address: Option<web3::types::Address>,
is_main: bool,
is_library: bool,
init_code: Vec<u8>,
deploy_code: Vec<u8>,
) -> Self {
Self::EVM(EVMInstance::new(
path, address, is_main, is_library, init_code,
path,
address,
is_main,
is_library,
deploy_code,
))
}

Expand Down
1 change: 0 additions & 1 deletion compiler_tester/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ impl Test {
&self.mode,
self.name.clone(),
self.group.clone(),
self.evm_builds.clone(),
self.evm_version,
);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler_tester/src/vm/eravm/deployers/dummy_deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl EraVMDeployer for DummyDeployer {
&mut self,
_test_name: String,
_caller: web3::types::Address,
_init_code: Vec<u8>,
_deploy_code: Vec<u8>,
_constructor_calldata: Vec<u8>,
_value: Option<u128>,
_vm: &mut EraVM,
Expand Down
2 changes: 1 addition & 1 deletion compiler_tester/src/vm/eravm/deployers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait EraVMDeployer {
&mut self,
test_name: String,
caller: web3::types::Address,
init_code: Vec<u8>,
deploy_code: Vec<u8>,
constructor_calldata: Vec<u8>,
value: Option<u128>,
vm: &mut EraVM,
Expand Down
Loading

0 comments on commit 9c94faa

Please sign in to comment.