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

EVM test cleanup #732

Merged
merged 9 commits into from
Aug 25, 2023
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

6 changes: 2 additions & 4 deletions examples/demo-rollup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ sov-sequencer = { path = "../../full-node/sov-sequencer" }
sov-stf-runner = { path = "../../full-node/sov-stf-runner" }
risc0-adapter = { path = "../../adapters/risc0", features = ["native"] }
sov-modules-stf-template = { path = "../../module-system/sov-modules-stf-template", features = ["native"] }

sov-bank = { path = "../../module-system/module-implementations/sov-bank", features = ["native"] }
sov-election = { path = "../../module-system/module-implementations/examples/sov-election", features = ["native"] }
sov-value-setter = { path = "../../module-system/module-implementations/examples/sov-value-setter", features = ["native"] }
sov-modules-api = { path = "../../module-system/sov-modules-api", features = ["native"] }
sov-state = { path = "../../module-system/sov-state", features = ["native"] }
const-rollup-config = { path = "../const-rollup-config" }

[dev-dependencies]
sov-evm = { path = "../../module-system/module-implementations/sov-evm", features = ["smart_contracts"] }
sov-bank = { path = "../../module-system/module-implementations/sov-bank", features = ["native"] }
sha2 = { workspace = true }
reqwest = "0.11"
tendermint = "0.32"
Expand Down
1 change: 0 additions & 1 deletion examples/demo-rollup/tests/bank/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use sov_modules_api::{PrivateKey, Spec};
use sov_sequencer::utils::SimpleClient;

use super::test_helpers::start_rollup;

const TOKEN_SALT: u64 = 0;
const TOKEN_NAME: &str = "test_token";

Expand Down
11 changes: 5 additions & 6 deletions examples/demo-rollup/tests/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use ethers_core::types::Eip1559TransactionRequest;
use ethers_middleware::SignerMiddleware;
use ethers_providers::{Http, Middleware, Provider};
use ethers_signers::{LocalWallet, Signer, Wallet};
use test_helpers::{start_rollup, SimpleStorageContract};
use sov_evm::smart_contracts::SimpleStorageContract;

use super::test_helpers;
use super::test_helpers::start_rollup;

const MAX_FEE_PER_GAS: u64 = 100000001;

Expand All @@ -23,7 +23,7 @@ struct TestClient {

impl TestClient {
#[allow(dead_code)]
async fn new_demo_rollup_client(
async fn new(
chain_id: u64,
key: Wallet<SigningKey>,
from_addr: Address,
Expand Down Expand Up @@ -123,12 +123,11 @@ async fn send_tx_test_to_eth(rpc_address: SocketAddr) -> Result<(), Box<dyn std:
.unwrap()
.with_chain_id(chain_id);

let contract = SimpleStorageContract::new();
let contract = SimpleStorageContract::default();

let from_addr = Address::from_str("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").unwrap();

let test_client =
TestClient::new_demo_rollup_client(chain_id, key, from_addr, contract, rpc_address).await;
let test_client = TestClient::new(chain_id, key, from_addr, contract, rpc_address).await;
test_client.execute().await
}

Expand Down
1 change: 0 additions & 1 deletion examples/demo-rollup/tests/evm/test_data/SimpleStorage.abi

This file was deleted.

1 change: 0 additions & 1 deletion examples/demo-rollup/tests/evm/test_data/SimpleStorage.bin

This file was deleted.

15 changes: 0 additions & 15 deletions examples/demo-rollup/tests/evm/test_data/Store.sol

This file was deleted.

79 changes: 0 additions & 79 deletions examples/demo-rollup/tests/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
use std::fs::remove_dir_all;
use std::net::SocketAddr;
use std::path::PathBuf;

use demo_stf::app::App;
use ethers_contract::BaseContract;
use ethers_core::abi::Abi;
use ethers_core::types::Bytes;
use revm::primitives::{ExecutionResult, Output};
use risc0_adapter::host::Risc0Verifier;
use sov_demo_rollup::{get_genesis_config, initialize_ledger, Rollup};
use sov_rollup_interface::mocks::{MockAddress, MockDaService};
use sov_stf_runner::{RollupConfig, RpcConfig, RunnerConfig, StorageConfig};
use tokio::sync::oneshot;

#[allow(dead_code)]
pub(crate) fn output(result: ExecutionResult) -> bytes::Bytes {
match result {
ExecutionResult::Success { output, .. } => match output {
Output::Call(out) => out,
Output::Create(out, _) => out,
},
_ => panic!("Expected successful ExecutionResult"),
}
}

#[allow(dead_code)]
fn test_data_path() -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests");
path.push("evm");
path.push("test_data");
path
}

#[allow(dead_code)]
fn make_contract_from_abi(path: PathBuf) -> BaseContract {
let abi_json = std::fs::read_to_string(path).unwrap();
let abi: Abi = serde_json::from_str(&abi_json).unwrap();
BaseContract::from(abi)
}

fn create_mock_da_rollup(rollup_config: RollupConfig<()>) -> Rollup<Risc0Verifier, MockDaService> {
let _ = remove_dir_all(&rollup_config.storage.path);
let ledger_db = initialize_ledger(rollup_config.storage.path.clone());
Expand Down Expand Up @@ -85,50 +53,3 @@ pub async fn start_rollup(rpc_reporting_channel: oneshot::Sender<SocketAddr>) {
// Close the tempdir explicitly to ensure that rustc doesn't see that it's unused and drop it unexpectedly
temp_dir.close().unwrap();
}

#[allow(dead_code)]
pub(crate) struct SimpleStorageContract {
bytecode: Bytes,
base_contract: BaseContract,
}

impl SimpleStorageContract {
#[allow(dead_code)]
pub(crate) fn new() -> Self {
let contract_data = {
let mut path = test_data_path();
path.push("SimpleStorage.bin");

let contract_data = std::fs::read_to_string(path).unwrap();
hex::decode(contract_data).unwrap()
};

let contract = {
let mut path = test_data_path();
path.push("SimpleStorage.abi");

make_contract_from_abi(path)
};

Self {
bytecode: Bytes::from(contract_data),
base_contract: contract,
}
}

#[allow(dead_code)]
pub(crate) fn byte_code(&self) -> Bytes {
self.bytecode.clone()
}

#[allow(dead_code)]
pub(crate) fn set_call_data(&self, set_arg: u32) -> Bytes {
let set_arg = ethereum_types::U256::from(set_arg);
self.base_contract.encode("set", set_arg).unwrap()
}

#[allow(dead_code)]
pub(crate) fn get_call_data(&self) -> Bytes {
self.base_contract.encode("get", ()).unwrap()
}
}
2 changes: 2 additions & 0 deletions module-system/module-implementations/sov-evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ reth-revm = { workspace = true }


[dev-dependencies]
sov-evm = { path = ".", features = ["smart_contracts"] }
primitive-types = "0.12.1"
tokio = { workspace = true }
tempfile = { workspace = true }
Expand All @@ -58,4 +59,5 @@ default = []
serde = ["dep:serde", "dep:serde_json"]
native = ["serde", "dep:jsonrpsee", "dep:schemars", "dep:clap", "sov-state/native", "sov-modules-api/native", ]
experimental = ["native"]
smart_contracts = ["experimental"]

2 changes: 0 additions & 2 deletions module-system/module-implementations/sov-evm/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ pub(crate) mod db_init;
pub(crate) mod executor;
mod serialize;
#[cfg(test)]
pub(crate) mod test_helpers;
#[cfg(test)]
mod tests;
pub(crate) mod transaction;

Expand Down
16 changes: 13 additions & 3 deletions module-system/module-implementations/sov-evm/src/evm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@ use std::convert::Infallible;

use reth_primitives::TransactionKind;
use revm::db::CacheDB;
use revm::primitives::{CfgEnv, KECCAK_EMPTY, U256};
use revm::primitives::{CfgEnv, ExecutionResult, Output, KECCAK_EMPTY, U256};
use revm::{Database, DatabaseCommit};
use sov_state::{ProverStorage, WorkingSet};

use super::db::EvmDb;
use super::db_init::InitEvmDb;
use super::executor;
use crate::evm::test_helpers::{output, SimpleStorageContract};
use crate::evm::transaction::BlockEnv;
use crate::evm::{contract_address, AccountInfo};
use crate::smart_contracts::SimpleStorageContract;
use crate::tests::dev_signer::DevSigner;
use crate::Evm;
type C = sov_modules_api::default_context::DefaultContext;

pub(crate) fn output(result: ExecutionResult) -> bytes::Bytes {
match result {
ExecutionResult::Success { output, .. } => match output {
Output::Call(out) => out,
Output::Create(out, _) => out,
},
_ => panic!("Expected successful ExecutionResult"),
}
}

#[test]
fn simple_contract_execution_sov_state() {
let tmpdir = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -49,7 +59,7 @@ fn simple_contract_execution<DB: Database<Error = Infallible> + DatabaseCommit +
},
);

let contract = SimpleStorageContract::new();
let contract = SimpleStorageContract::default();

let contract_address = {
let tx = dev_signer
Expand Down
2 changes: 2 additions & 0 deletions module-system/module-implementations/sov-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub mod genesis;
#[cfg(feature = "native")]
#[cfg(feature = "experimental")]
pub mod query;
#[cfg(feature = "smart_contracts")]
pub mod smart_contracts;
#[cfg(feature = "experimental")]
#[cfg(test)]
mod tests;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod simple_storage_contract;
pub use simple_storage_contract::SimpleStorageContract;
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ use std::path::PathBuf;
use ethers_contract::BaseContract;
use ethers_core::abi::Abi;
use ethers_core::types::Bytes;
use revm::primitives::{ExecutionResult, Output};

pub(crate) fn output(result: ExecutionResult) -> bytes::Bytes {
match result {
ExecutionResult::Success { output, .. } => match output {
Output::Call(out) => out,
Output::Create(out, _) => out,
},
_ => panic!("Expected successful ExecutionResult"),
}
}

fn test_data_path() -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand All @@ -29,13 +18,14 @@ fn make_contract_from_abi(path: PathBuf) -> BaseContract {
BaseContract::from(abi)
}

pub(crate) struct SimpleStorageContract {
/// SimpleStorageContract wrapper.
pub struct SimpleStorageContract {
bytecode: Bytes,
base_contract: BaseContract,
}

impl SimpleStorageContract {
pub(crate) fn new() -> Self {
impl Default for SimpleStorageContract {
fn default() -> Self {
let contract_data = {
let mut path = test_data_path();
path.push("SimpleStorage.bin");
Expand All @@ -56,17 +46,22 @@ impl SimpleStorageContract {
base_contract: contract,
}
}
}

pub(crate) fn byte_code(&self) -> Bytes {
impl SimpleStorageContract {
/// SimpleStorage bytecode.
pub fn byte_code(&self) -> Bytes {
self.bytecode.clone()
}

pub(crate) fn set_call_data(&self, set_arg: u32) -> Bytes {
/// Setter for the smart contract.
pub fn set_call_data(&self, set_arg: u32) -> Bytes {
let set_arg = ethereum_types::U256::from(set_arg);
self.base_contract.encode("set", set_arg).unwrap()
}

pub(crate) fn get_call_data(&self) -> Bytes {
/// Getter for the smart contract.
pub fn get_call_data(&self) -> Bytes {
self.base_contract.encode("get", ()).unwrap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use sov_modules_api::{Context, Module, PrivateKey, Spec};
use sov_state::{ProverStorage, WorkingSet};

use crate::call::CallMessage;
use crate::evm::test_helpers::SimpleStorageContract;
use crate::evm::EthAddress;
use crate::smart_contracts::SimpleStorageContract;
use crate::tests::dev_signer::DevSigner;
use crate::{AccountData, Evm, EvmConfig};
type C = DefaultContext;
Expand All @@ -18,7 +18,7 @@ fn create_messages(
dev_signer: DevSigner,
) -> Vec<CallMessage> {
let mut transactions = Vec::default();
let contract = SimpleStorageContract::new();
let contract = SimpleStorageContract::default();

// Contract creation.
{
Expand Down