Skip to content
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
7 changes: 4 additions & 3 deletions src/benches/eth_deploy_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ use criterion::{BatchSize, BenchmarkId, Criterion, Throughput};
use secp256k1::SecretKey;

use crate::test_utils::{address_from_secret_key, create_eth_transaction, deploy_evm, SUBMIT};
use crate::types::Wei;

const INITIAL_BALANCE: u64 = 1000;
const INITIAL_BALANCE: Wei = Wei::new_u64(1000);
const INITIAL_NONCE: u64 = 0;
const TRANSFER_AMOUNT: u64 = 0;
const TRANSFER_AMOUNT: Wei = Wei::zero();

pub(crate) fn eth_deploy_code_benchmark(c: &mut Criterion) {
let mut runner = deploy_evm();
let mut rng = rand::thread_rng();
let source_account = SecretKey::random(&mut rng);
runner.create_address(
address_from_secret_key(&source_account),
INITIAL_BALANCE.into(),
INITIAL_BALANCE,
INITIAL_NONCE.into(),
);
let inputs: Vec<_> = [1, 4, 8, 12, 16]
Expand Down
2 changes: 1 addition & 1 deletion src/benches/eth_erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) fn eth_erc20_benchmark(c: &mut Criterion) {
let source_account = SecretKey::random(&mut rng);
runner.create_address(
address_from_secret_key(&source_account),
INITIAL_BALANCE.into(),
crate::types::Wei::new_u64(INITIAL_BALANCE),
INITIAL_NONCE.into(),
);
let calling_account_id = "some-account.near".to_string();
Expand Down
5 changes: 3 additions & 2 deletions src/benches/eth_standard_precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use secp256k1::SecretKey;

use crate::test_utils::standard_precompiles::{PrecompilesConstructor, PrecompilesContract};
use crate::test_utils::{address_from_secret_key, deploy_evm, sign_transaction, SUBMIT};
use crate::types::Wei;

const INITIAL_BALANCE: u64 = 1000;
const INITIAL_BALANCE: Wei = Wei::new_u64(1000);
const INITIAL_NONCE: u64 = 0;

pub(crate) fn eth_standard_precompiles_benchmark(c: &mut Criterion) {
Expand All @@ -14,7 +15,7 @@ pub(crate) fn eth_standard_precompiles_benchmark(c: &mut Criterion) {
let source_account = SecretKey::random(&mut rng);
runner.create_address(
address_from_secret_key(&source_account),
INITIAL_BALANCE.into(),
INITIAL_BALANCE,
INITIAL_NONCE.into(),
);
let calling_account_id = "some-account.near".to_string();
Expand Down
9 changes: 5 additions & 4 deletions src/benches/eth_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ use criterion::{BatchSize, Criterion};
use secp256k1::SecretKey;

use crate::test_utils::{address_from_secret_key, create_eth_transaction, deploy_evm, SUBMIT};
use crate::types::Wei;

const INITIAL_BALANCE: u64 = 1000;
const INITIAL_BALANCE: Wei = Wei::new_u64(1000);
const INITIAL_NONCE: u64 = 0;
const TRANSFER_AMOUNT: u64 = 123;
const TRANSFER_AMOUNT: Wei = Wei::new_u64(123);

pub(crate) fn eth_transfer_benchmark(c: &mut Criterion) {
let mut runner = deploy_evm();
let mut rng = rand::thread_rng();
let source_account = SecretKey::random(&mut rng);
runner.create_address(
address_from_secret_key(&source_account),
INITIAL_BALANCE.into(),
INITIAL_BALANCE,
INITIAL_NONCE.into(),
);
let dest_account = address_from_secret_key(&SecretKey::random(&mut rng));
let transaction = create_eth_transaction(
Some(dest_account),
TRANSFER_AMOUNT.into(),
TRANSFER_AMOUNT,
vec![],
Some(runner.chain_id),
&source_account,
Expand Down
39 changes: 21 additions & 18 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::precompiles;
use crate::prelude::{Address, TryInto, Vec, H256, U256};
use crate::sdk;
use crate::storage::{address_to_key, bytes_to_key, storage_to_key, KeyPrefix, KeyPrefixU8};
use crate::types::{u256_to_arr, AccountId};
use crate::types::{u256_to_arr, AccountId, Wei};

/// Errors with the EVM engine.
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -205,21 +205,22 @@ impl Engine {
.unwrap_or_else(U256::zero)
}

pub fn set_balance(address: &Address, balance: &U256) {
pub fn set_balance(address: &Address, balance: &Wei) {
sdk::write_storage(
&address_to_key(KeyPrefix::Balance, address),
&u256_to_arr(balance),
&balance.to_bytes(),
);
}

pub fn remove_balance(address: &Address) {
sdk::remove_storage(&address_to_key(KeyPrefix::Balance, address))
}

pub fn get_balance(address: &Address) -> U256 {
sdk::read_storage(&address_to_key(KeyPrefix::Balance, address))
pub fn get_balance(address: &Address) -> Wei {
let raw = sdk::read_storage(&address_to_key(KeyPrefix::Balance, address))
.map(|value| U256::from_big_endian(&value))
.unwrap_or_else(U256::zero)
.unwrap_or_else(U256::zero);
Wei::new(raw)
}

pub fn remove_storage(address: &Address, key: &H256) {
Expand All @@ -240,7 +241,7 @@ impl Engine {
let balance = Self::get_balance(address);
let nonce = Self::get_nonce(address);
let code_len = Self::get_code_size(address);
balance == U256::zero() && nonce == U256::zero() && code_len == 0
balance.is_zero() && nonce.is_zero() && code_len == 0
}

/// Removes all storage for the given address.
Expand Down Expand Up @@ -272,20 +273,20 @@ impl Engine {

pub fn deploy_code_with_input(&mut self, input: Vec<u8>) -> EngineResult<SubmitResult> {
let origin = self.origin();
let value = U256::zero();
let value = Wei::zero();
self.deploy_code(origin, value, input)
}

pub fn deploy_code(
&mut self,
origin: Address,
value: U256,
value: Wei,
input: Vec<u8>,
) -> EngineResult<SubmitResult> {
let mut executor = self.make_executor();
let address = executor.create_address(CreateScheme::Legacy { caller: origin });
let (status, result) = (
executor.transact_create(origin, value, input, u64::MAX),
executor.transact_create(origin, value.raw(), input, u64::MAX),
address,
);

Expand All @@ -306,19 +307,20 @@ impl Engine {
pub fn call_with_args(&mut self, args: FunctionCallArgs) -> EngineResult<SubmitResult> {
let origin = self.origin();
let contract = Address(args.contract);
let value = U256::zero();
let value = Wei::zero();
self.call(origin, contract, value, args.input)
}

pub fn call(
&mut self,
origin: Address,
contract: Address,
value: U256,
value: Wei,
input: Vec<u8>,
) -> EngineResult<SubmitResult> {
let mut executor = self.make_executor();
let (status, result) = executor.transact_call(origin, contract, value, input, u64::MAX);
let (status, result) =
executor.transact_call(origin, contract, value.raw(), input, u64::MAX);

let used_gas = executor.used_gas();
let (values, logs) = executor.into_state().deconstruct();
Expand Down Expand Up @@ -362,18 +364,19 @@ impl Engine {
let origin = Address::from_slice(&args.sender);
let contract = Address::from_slice(&args.address);
let value = U256::from_big_endian(&args.amount);
self.view(origin, contract, value, args.input)
self.view(origin, contract, Wei::new(value), args.input)
}

pub fn view(
&self,
origin: Address,
contract: Address,
value: U256,
value: Wei,
input: Vec<u8>,
) -> EngineResult<Vec<u8>> {
let mut executor = self.make_executor();
let (status, result) = executor.transact_call(origin, contract, value, input, u64::MAX);
let (status, result) =
executor.transact_call(origin, contract, value.raw(), input, u64::MAX);
status.into_result()?;
Ok(result)
}
Expand Down Expand Up @@ -492,7 +495,7 @@ impl evm::backend::Backend for Engine {
fn basic(&self, address: Address) -> Basic {
Basic {
nonce: Engine::get_nonce(&address),
balance: Engine::get_balance(&address),
balance: Engine::get_balance(&address).raw(),
}
}

Expand Down Expand Up @@ -531,7 +534,7 @@ impl ApplyBackend for Engine {
reset_storage,
} => {
Engine::set_nonce(&address, &basic.nonce);
Engine::set_balance(&address, &basic.balance);
Engine::set_balance(&address, &Wei::new(basic.balance));
if let Some(code) = code {
Engine::set_code(&address, &code)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ mod contract {
pub extern "C" fn get_balance() {
let address = sdk::read_input_arr20();
let balance = Engine::get_balance(&Address(address));
sdk::return_output(&u256_to_arr(&balance))
sdk::return_output(&balance.to_bytes())
}

#[no_mangle]
Expand Down
10 changes: 5 additions & 5 deletions src/meta_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rlp::{Decodable, DecoderError, Rlp};

use crate::parameters::MetaCallArgs;
use crate::prelude::{vec, Address, Box, HashMap, String, ToOwned, ToString, Vec, H256, U256};
use crate::types::{keccak, u256_to_arr, InternalMetaCallArgs, RawU256};
use crate::types::{keccak, u256_to_arr, InternalMetaCallArgs, RawU256, Wei};

/// Internal errors to propagate up and format in the single place.
pub enum ParsingError {
Expand Down Expand Up @@ -493,10 +493,10 @@ pub fn prepare_meta_call_args(
bytes.extend_from_slice(&keccak(types.as_bytes()).as_bytes());
bytes.extend_from_slice(&keccak(account_id).as_bytes());
bytes.extend_from_slice(&u256_to_arr(&input.nonce));
bytes.extend_from_slice(&u256_to_arr(&input.fee_amount));
bytes.extend_from_slice(&input.fee_amount.to_bytes());
bytes.extend_from_slice(&encode_address(input.fee_address));
bytes.extend_from_slice(&encode_address(input.contract_address));
bytes.extend_from_slice(&u256_to_arr(&input.value));
bytes.extend_from_slice(&input.value.to_bytes());

let methods = MethodAndTypes::parse(&method_def)?;
let method_sig = method_signature(&methods);
Expand Down Expand Up @@ -544,10 +544,10 @@ pub fn parse_meta_call(
let meta_tx =
MetaCallArgs::try_from_slice(&args).map_err(|_| ParsingError::ArgumentParseError)?;
let nonce = U256::from(meta_tx.nonce);
let fee_amount = U256::from(meta_tx.fee_amount);
let fee_amount = Wei::new(U256::from(meta_tx.fee_amount));
let fee_address = Address::from(meta_tx.fee_address);
let contract_address = Address::from(meta_tx.contract_address);
let value = U256::from(meta_tx.value);
let value = Wei::new(U256::from(meta_tx.value));

let mut result = InternalMetaCallArgs {
sender: Address::zero(),
Expand Down
25 changes: 21 additions & 4 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,29 @@ pub use alloc::{
vec::Vec,
};
#[cfg(not(feature = "std"))]
pub use core::{convert::TryInto, marker::PhantomData, mem};
pub use core::{
convert::TryInto,
marker::PhantomData,
mem,
ops::{Add, Sub},
};
#[cfg(feature = "std")]
pub use std::{
borrow::Cow, borrow::Cow::Borrowed, borrow::ToOwned, boxed::Box, collections::HashMap,
convert::TryInto, error::Error, fmt, format, marker::PhantomData, mem, string::String,
string::ToString, vec, vec::Vec,
borrow::Cow,
borrow::Cow::Borrowed,
borrow::ToOwned,
boxed::Box,
collections::HashMap,
convert::TryInto,
error::Error,
fmt, format,
marker::PhantomData,
mem,
ops::{Add, Sub},
string::String,
string::ToString,
vec,
vec::Vec,
};

pub use primitive_types::{H160, H256, U256};
Expand Down
12 changes: 6 additions & 6 deletions src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ impl AuroraRunner {
)
}

pub fn create_address(&mut self, address: Address, init_balance: U256, init_nonce: U256) {
pub fn create_address(&mut self, address: Address, init_balance: types::Wei, init_nonce: U256) {
let trie = &mut self.ext.fake_trie;

let balance_key = storage::address_to_key(storage::KeyPrefix::Balance, &address);
let balance_value = types::u256_to_arr(&init_balance);
let balance_value = init_balance.to_bytes();

let nonce_key = storage::address_to_key(storage::KeyPrefix::Nonce, &address);
let nonce_value = types::u256_to_arr(&init_nonce);
Expand Down Expand Up @@ -170,8 +170,8 @@ impl AuroraRunner {
}
}

pub fn get_balance(&self, address: Address) -> U256 {
self.getter_method_call("get_balance", address)
pub fn get_balance(&self, address: Address) -> types::Wei {
types::Wei::new(self.getter_method_call("get_balance", address))
}

pub fn get_nonce(&self, address: Address) -> U256 {
Expand Down Expand Up @@ -262,7 +262,7 @@ pub(crate) fn deploy_evm() -> AuroraRunner {

pub(crate) fn create_eth_transaction(
to: Option<Address>,
value: U256,
value: types::Wei,
data: Vec<u8>,
chain_id: Option<u64>,
secret_key: &SecretKey,
Expand Down Expand Up @@ -322,7 +322,7 @@ pub(crate) fn parse_eth_gas(output: &VMOutcome) -> u64 {
pub(crate) fn validate_address_balance_and_nonce(
runner: &AuroraRunner,
address: Address,
expected_balance: U256,
expected_balance: types::Wei,
expected_nonce: U256,
) {
assert_eq!(runner.get_balance(address), expected_balance, "balance");
Expand Down
7 changes: 6 additions & 1 deletion src/tests/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::test_utils::{
self,
erc20::{ERC20Constructor, ERC20},
};
use crate::types::Wei;
use bstr::ByteSlice;
use secp256k1::SecretKey;

Expand Down Expand Up @@ -211,7 +212,11 @@ fn initialize_erc20() -> (test_utils::AuroraRunner, SecretKey, Address, ERC20) {
let mut rng = rand::thread_rng();
let source_account = SecretKey::random(&mut rng);
let source_address = test_utils::address_from_secret_key(&source_account);
runner.create_address(source_address, INITIAL_BALANCE.into(), INITIAL_NONCE.into());
runner.create_address(
source_address,
Wei::new_u64(INITIAL_BALANCE),
INITIAL_NONCE.into(),
);
let dest_address = test_utils::address_from_secret_key(&SecretKey::random(&mut rng));

let constructor = ERC20Constructor::load();
Expand Down
Loading