Skip to content

Commit

Permalink
wip4
Browse files Browse the repository at this point in the history
  • Loading branch information
abukosek committed Jan 13, 2025
1 parent a496eaa commit 527cd08
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
26 changes: 13 additions & 13 deletions runtime-sdk/modules/evm-new/src/db.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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 =
<C::Runtime as Runtime>::Accounts::get_balance(sdk_address, Cfg::TOKEN_DENOMINATION)
.unwrap();
let mut nonce = <C::Runtime as Runtime>::Accounts::get_nonce(sdk_address).unwrap();
let nonce = <C::Runtime as Runtime>::Accounts::get_nonce(sdk_address).unwrap();

// Fetch code for this address from storage.
let code = CurrentState::with_store(|store| {
Expand All @@ -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),
Expand All @@ -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<Bytecode, Self::Error> {
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<U256, Self::Error> {
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::<Cfg, _, _, _>(self.ctx, &address, |store| {
store.get(index).unwrap_or_default()
Expand All @@ -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<B256, Self::Error> {
println!("&&& {:#?}", number);
CurrentState::with_store(|store| {
let block_hashes = state::block_hashes(store);

Expand Down
26 changes: 17 additions & 9 deletions runtime-sdk/modules/evm-new/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod types;

use base64::prelude::*;
use revm::{
primitives::{ExecutionResult, TxEnv, TxKind},
primitives::{Bytes, ExecutionResult, Output, SpecId, TxEnv, TxKind},
Evm,
};

Expand All @@ -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 _},
},
Expand Down Expand Up @@ -373,27 +372,36 @@ impl<Cfg: Config> API for Module<Cfg> {
impl<Cfg: Config> Module<Cfg> {
fn evm_execute<C: Context, F: FnOnce(&mut TxEnv)>(
ctx: &C,
estimate_gas: bool,
_estimate_gas: bool,
f: F,
) -> Result<Vec<u8>, 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 {
Expand Down Expand Up @@ -664,7 +672,7 @@ impl<Cfg: Config> Module<Cfg> {

impl<Cfg: Config> module::TransactionHandler for Module<Cfg> {
fn decode_tx<C: Context>(
ctx: &C,
_ctx: &C,
scheme: &str,
body: &[u8],
) -> Result<Option<Transaction>, CoreError> {
Expand Down
7 changes: 4 additions & 3 deletions runtime-sdk/modules/evm-new/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ fn do_test_evm_calls<C: Config>(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".
Expand Down Expand Up @@ -521,9 +522,9 @@ fn do_test_evm_runtime<C: Config>() {
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::<C>::tx_create(&ctx, cbor::from_value(call.body).unwrap()).unwrap(),
);
let tx = EVMModule::<C>::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::<C>::check_invariants(&ctx).expect("invariants should hold");

TransactionResult::Commit(addr)
Expand Down

0 comments on commit 527cd08

Please sign in to comment.