From f5de168f2a479d24ccdf17466ce531980cb607d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Buko=C5=A1ek?= Date: Mon, 13 Jan 2025 14:35:00 +0100 Subject: [PATCH] wip4 --- runtime-sdk/modules/evm-new/src/db.rs | 26 ++++++++++++------------- runtime-sdk/modules/evm-new/src/lib.rs | 26 ++++++++++++++++--------- runtime-sdk/modules/evm-new/src/test.rs | 7 ++++--- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/runtime-sdk/modules/evm-new/src/db.rs b/runtime-sdk/modules/evm-new/src/db.rs index 24cd935284..96fa4d8f18 100644 --- a/runtime-sdk/modules/evm-new/src/db.rs +++ b/runtime-sdk/modules/evm-new/src/db.rs @@ -1,22 +1,14 @@ use revm::{ - primitives::{keccak256, AccountInfo, Address, Bytecode, Log, B256, KECCAK_EMPTY, U256}, + primitives::{AccountInfo, Address, Bytecode, B256, KECCAK_EMPTY, U256}, Database, }; -use std::{convert::Infallible, vec::Vec}; +use std::vec::Vec; use std::marker::PhantomData; use oasis_runtime_sdk::{ - context::Context, - core::common::crypto::hash::Hash, - modules::{ - accounts::API as _, - core::{self, API as _}, - }, - state::CurrentState, - subcall, - types::token, - Runtime, + context::Context, core::common::crypto::hash::Hash, modules::accounts::API as _, + state::CurrentState, Runtime, }; use crate::{state, types, Config}; @@ -43,11 +35,13 @@ impl<'ctx, C: Context, Cfg: Config> Database for OasisDB<'ctx, C, Cfg> { // Derive SDK account address from the Ethereum address. let sdk_address = Cfg::map_address(address); + //print!("*** {:#?} -> {:#?}", address, sdk_address); + // Fetch balance and nonce from SDK accounts. Note that these can never fail. let balance = ::Accounts::get_balance(sdk_address, Cfg::TOKEN_DENOMINATION) .unwrap(); - let mut nonce = ::Accounts::get_nonce(sdk_address).unwrap(); + let nonce = ::Accounts::get_nonce(sdk_address).unwrap(); // Fetch code for this address from storage. let code = CurrentState::with_store(|store| { @@ -66,6 +60,8 @@ impl<'ctx, C: Context, Cfg: Config> Database for OasisDB<'ctx, C, Cfg> { Some(ref bc) => bc.hash_slow(), }; + //println!(": {:#?}", balance); + Ok(Some(AccountInfo { nonce: nonce.into(), balance: U256::from(balance), @@ -76,13 +72,16 @@ impl<'ctx, C: Context, Cfg: Config> Database for OasisDB<'ctx, C, Cfg> { /// Get account code by its hash (unimplemented). fn code_by_hash(&mut self, _code_hash: B256) -> Result { + println!("###### code_by_hash called ######"); Err("getting code by hash is not supported".to_string()) } /// Get storage value of address at index. fn storage(&mut self, address: Address, index: U256) -> Result { let address: types::H160 = address.into_array().into(); + println!(">### {:#?}", index); let index: types::H256 = index.to_be_bytes().into(); // XXX: is BE ok? + println!("<### {:#?}", index); let res: types::H256 = state::with_storage::(self.ctx, &address, |store| { store.get(index).unwrap_or_default() @@ -92,6 +91,7 @@ impl<'ctx, C: Context, Cfg: Config> Database for OasisDB<'ctx, C, Cfg> { /// Get block hash by block number. fn block_hash(&mut self, number: u64) -> Result { + println!("&&& {:#?}", number); CurrentState::with_store(|store| { let block_hashes = state::block_hashes(store); diff --git a/runtime-sdk/modules/evm-new/src/lib.rs b/runtime-sdk/modules/evm-new/src/lib.rs index 7bb950ea1c..33dc03be1e 100644 --- a/runtime-sdk/modules/evm-new/src/lib.rs +++ b/runtime-sdk/modules/evm-new/src/lib.rs @@ -11,7 +11,7 @@ pub mod types; use base64::prelude::*; use revm::{ - primitives::{ExecutionResult, TxEnv, TxKind}, + primitives::{Bytes, ExecutionResult, Output, SpecId, TxEnv, TxKind}, Evm, }; @@ -21,7 +21,6 @@ use oasis_runtime_sdk::{ handler, migration, module::{self, Module as _}, modules::{ - self, accounts::API as _, core::{Error as CoreError, API as _}, }, @@ -373,27 +372,36 @@ impl API for Module { impl Module { fn evm_execute( ctx: &C, - estimate_gas: bool, + _estimate_gas: bool, f: F, ) -> Result, Error> { // TODO: precompiles let is_query = CurrentState::with_env(|env| !env.is_execute()); - let mut db = db::OasisDB::<'_, C, Cfg>::new(ctx); - let mut evm = Evm::builder().with_db(db).modify_tx_env(f).build(); + let db = db::OasisDB::<'_, C, Cfg>::new(ctx); + let mut evm = Evm::builder() + .with_spec_id(SpecId::SHANGHAI) + .with_db(db) + .modify_tx_env(f) + .build(); let tx = evm.transact().unwrap(); // XXX: err checking let ret = match tx.result { ExecutionResult::Success { - reason, + reason: _, gas_used, - gas_refunded, + gas_refunded: _, logs, output, } => { + let data = match output { + Output::Call(out) => out, + Output::Create(_, Some(addr)) => addr.into_array().into(), + Output::Create(_, None) => Bytes::new(), + }; + // Clamp data based on maximum allowed result size. - let data = output.into_data(); let data = if !is_query && data.len() > Cfg::MAX_RESULT_SIZE { data[..Cfg::MAX_RESULT_SIZE].to_vec() } else { @@ -664,7 +672,7 @@ impl Module { impl module::TransactionHandler for Module { fn decode_tx( - ctx: &C, + _ctx: &C, scheme: &str, body: &[u8], ) -> Result, CoreError> { diff --git a/runtime-sdk/modules/evm-new/src/test.rs b/runtime-sdk/modules/evm-new/src/test.rs index 7a777f9729..2ae5c2ae08 100644 --- a/runtime-sdk/modules/evm-new/src/test.rs +++ b/runtime-sdk/modules/evm-new/src/test.rs @@ -270,6 +270,7 @@ fn do_test_evm_calls(force_plain: bool) { TransactionResult::Commit(name) }); + assert_eq!(erc20_name.len(), 96); assert_eq!(erc20_name[63], 0x04); // Name is 4 bytes long. assert_eq!(erc20_name[64..68], vec![0x54, 0x65, 0x73, 0x74]); // "Test". @@ -521,9 +522,9 @@ fn do_test_evm_runtime() { let call = create_tx.call.clone(); let erc20_addr = CurrentState::with_transaction_opts(Options::new().with_tx(create_tx.into()), || { - let addr = H160::from_slice( - &EVMModule::::tx_create(&ctx, cbor::from_value(call.body).unwrap()).unwrap(), - ); + let tx = EVMModule::::tx_create(&ctx, cbor::from_value(call.body).unwrap()); + assert_eq!(tx.is_ok(), true, "tx_create failed"); + let addr = H160::from_slice(&tx.unwrap()); EVMModule::::check_invariants(&ctx).expect("invariants should hold"); TransactionResult::Commit(addr)