From 25bbaad3926856079c3d331a3f125db6839685ec Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Thu, 11 Nov 2021 17:48:29 +0100 Subject: [PATCH] Remove sdk::current_account_id usage from engine-precompiles (#346) --- engine-precompiles/src/blake2.rs | 20 +++-- engine-precompiles/src/bn128.rs | 56 +++++++++---- engine-precompiles/src/hash.rs | 9 +- engine-precompiles/src/identity.rs | 12 ++- engine-precompiles/src/lib.rs | 125 +++++++++++++--------------- engine-precompiles/src/modexp.rs | 13 ++- engine-precompiles/src/native.rs | 28 +++++-- engine-precompiles/src/secp256k1.rs | 18 ++-- engine-tests/src/tests/erc20.rs | 4 +- engine/src/engine.rs | 3 +- 10 files changed, 177 insertions(+), 111 deletions(-) diff --git a/engine-precompiles/src/blake2.rs b/engine-precompiles/src/blake2.rs index 25121e85e..9509a167b 100644 --- a/engine-precompiles/src/blake2.rs +++ b/engine-precompiles/src/blake2.rs @@ -39,6 +39,7 @@ impl Precompile for Blake2F { /// See: https://eips.ethereum.org/EIPS/eip-152 /// See: https://etherscan.io/address/0000000000000000000000000000000000000009 fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -120,12 +121,12 @@ mod tests { fn test_blake2f_out_of_gas() -> EvmPrecompileResult { let input = hex::decode(INPUT).unwrap(); - Blake2F::run(&input, Some(11), &new_context(), false) + Blake2F.run(&input, Some(11), &new_context(), false) } fn test_blake2f_empty() -> EvmPrecompileResult { let input = [0u8; 0]; - Blake2F::run(&input, Some(0), &new_context(), false) + Blake2F.run(&input, Some(0), &new_context(), false) } fn test_blake2f_invalid_len_1() -> EvmPrecompileResult { @@ -143,7 +144,7 @@ mod tests { 01", ) .unwrap(); - Blake2F::run(&input, Some(12), &new_context(), false) + Blake2F.run(&input, Some(12), &new_context(), false) } fn test_blake2f_invalid_len_2() -> EvmPrecompileResult { @@ -161,7 +162,7 @@ mod tests { 01", ) .unwrap(); - Blake2F::run(&input, Some(12), &new_context(), false) + Blake2F.run(&input, Some(12), &new_context(), false) } fn test_blake2f_invalid_flag() -> EvmPrecompileResult { @@ -179,7 +180,7 @@ mod tests { 02", ) .unwrap(); - Blake2F::run(&input, Some(12), &new_context(), false) + Blake2F.run(&input, Some(12), &new_context(), false) } fn test_blake2f_r_0() -> Vec { @@ -197,14 +198,16 @@ mod tests { 01", ) .unwrap(); - Blake2F::run(&input, Some(12), &new_context(), false) + Blake2F + .run(&input, Some(12), &new_context(), false) .unwrap() .output } fn test_blake2f_r_12() -> Vec { let input = hex::decode(INPUT).unwrap(); - Blake2F::run(&input, Some(12), &new_context(), false) + Blake2F + .run(&input, Some(12), &new_context(), false) .unwrap() .output } @@ -224,7 +227,8 @@ mod tests { 00", ) .unwrap(); - Blake2F::run(&input, Some(12), &new_context(), false) + Blake2F + .run(&input, Some(12), &new_context(), false) .unwrap() .output } diff --git a/engine-precompiles/src/bn128.rs b/engine-precompiles/src/bn128.rs index f6fce193e..22acae839 100644 --- a/engine-precompiles/src/bn128.rs +++ b/engine-precompiles/src/bn128.rs @@ -68,6 +68,10 @@ pub(super) struct Bn128Add(PhantomData); impl Bn128Add { pub(super) const ADDRESS: Address = super::make_address(0, 6); + + pub fn new() -> Self { + Self(Default::default()) + } } impl Bn128Add { @@ -103,6 +107,7 @@ impl Precompile for Bn128Add { /// See: https://eips.ethereum.org/EIPS/eip-196 /// See: https://etherscan.io/address/0000000000000000000000000000000000000006 fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -131,6 +136,7 @@ impl Precompile for Bn128Add { /// See: https://eips.ethereum.org/EIPS/eip-196 /// See: https://etherscan.io/address/0000000000000000000000000000000000000006 fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -151,6 +157,10 @@ pub(super) struct Bn128Mul(PhantomData); impl Bn128Mul { pub(super) const ADDRESS: Address = super::make_address(0, 7); + + pub fn new() -> Self { + Self(Default::default()) + } } impl Bn128Mul { @@ -188,6 +198,7 @@ impl Precompile for Bn128Mul { /// See: https://eips.ethereum.org/EIPS/eip-196 /// See: https://etherscan.io/address/0000000000000000000000000000000000000007 fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -215,6 +226,7 @@ impl Precompile for Bn128Mul { /// See: https://eips.ethereum.org/EIPS/eip-196 /// See: https://etherscan.io/address/0000000000000000000000000000000000000007 fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -236,6 +248,10 @@ pub(super) struct Bn128Pair(PhantomData); impl Bn128Pair { pub(super) const ADDRESS: Address = super::make_address(0, 8); + + pub fn new() -> Self { + Self(Default::default()) + } } impl Bn128Pair { @@ -345,6 +361,7 @@ impl Precompile for Bn128Pair { /// See: https://eips.ethereum.org/EIPS/eip-197 /// See: https://etherscan.io/address/0000000000000000000000000000000000000008 fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -375,6 +392,7 @@ impl Precompile for Bn128Pair { /// See: https://eips.ethereum.org/EIPS/eip-197 /// See: https://etherscan.io/address/0000000000000000000000000000000000000008 fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -415,7 +433,8 @@ mod tests { ) .unwrap(); - let res = Bn128Add::::run(&input, Some(500), &new_context(), false) + let res = Bn128Add::::new() + .run(&input, Some(500), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -436,7 +455,8 @@ mod tests { ) .unwrap(); - let res = Bn128Add::::run(&input, Some(500), &new_context(), false) + let res = Bn128Add::::new() + .run(&input, Some(500), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -450,7 +470,7 @@ mod tests { 0000000000000000000000000000000000000000000000000000000000000000", ) .unwrap(); - let res = Bn128Add::::run(&input, Some(499), &new_context(), false); + let res = Bn128Add::::new().run(&input, Some(499), &new_context(), false); assert!(matches!(res, Err(ExitError::OutOfGas))); // no input test @@ -462,7 +482,8 @@ mod tests { ) .unwrap(); - let res = Bn128Add::::run(&input, Some(500), &new_context(), false) + let res = Bn128Add::::new() + .run(&input, Some(500), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -477,7 +498,7 @@ mod tests { ) .unwrap(); - let res = Bn128Add::::run(&input, Some(500), &new_context(), false); + let res = Bn128Add::::new().run(&input, Some(500), &new_context(), false); assert!(matches!( res, Err(ExitError::Other(Borrowed("ERR_BN128_INVALID_POINT"))) @@ -500,7 +521,8 @@ mod tests { ) .unwrap(); - let res = Bn128Mul::::run(&input, Some(40_000), &new_context(), false) + let res = Bn128Mul::::new() + .run(&input, Some(40_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -513,7 +535,7 @@ mod tests { 0200000000000000000000000000000000000000000000000000000000000000", ) .unwrap(); - let res = Bn128Mul::::run(&input, Some(39_999), &new_context(), false); + let res = Bn128Mul::::new().run(&input, Some(39_999), &new_context(), false); assert!(matches!(res, Err(ExitError::OutOfGas))); // zero multiplication test @@ -531,7 +553,8 @@ mod tests { ) .unwrap(); - let res = Bn128Mul::::run(&input, Some(40_000), &new_context(), false) + let res = Bn128Mul::::new() + .run(&input, Some(40_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -545,7 +568,8 @@ mod tests { ) .unwrap(); - let res = Bn128Mul::::run(&input, Some(40_000), &new_context(), false) + let res = Bn128Mul::::new() + .run(&input, Some(40_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -559,7 +583,7 @@ mod tests { ) .unwrap(); - let res = Bn128Mul::::run(&input, Some(40_000), &new_context(), false); + let res = Bn128Mul::::new().run(&input, Some(40_000), &new_context(), false); assert!(matches!( res, Err(ExitError::Other(Borrowed("ERR_BN128_INVALID_POINT"))) @@ -588,7 +612,8 @@ mod tests { hex::decode("0000000000000000000000000000000000000000000000000000000000000001") .unwrap(); - let res = Bn128Pair::::run(&input, Some(260_000), &new_context(), false) + let res = Bn128Pair::::new() + .run(&input, Some(260_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -610,7 +635,7 @@ mod tests { 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa", ) .unwrap(); - let res = Bn128Pair::::run(&input, Some(259_999), &new_context(), false); + let res = Bn128Pair::::new().run(&input, Some(259_999), &new_context(), false); assert!(matches!(res, Err(ExitError::OutOfGas))); // no input test @@ -619,7 +644,8 @@ mod tests { hex::decode("0000000000000000000000000000000000000000000000000000000000000001") .unwrap(); - let res = Bn128Pair::::run(&input, Some(260_000), &new_context(), false) + let res = Bn128Pair::::new() + .run(&input, Some(260_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -636,7 +662,7 @@ mod tests { ) .unwrap(); - let res = Bn128Pair::::run(&input, Some(260_000), &new_context(), false); + let res = Bn128Pair::::new().run(&input, Some(260_000), &new_context(), false); assert!(matches!( res, Err(ExitError::Other(Borrowed("ERR_BN128_INVALID_A"))) @@ -652,7 +678,7 @@ mod tests { ) .unwrap(); - let res = Bn128Pair::::run(&input, Some(260_000), &new_context(), false); + let res = Bn128Pair::::new().run(&input, Some(260_000), &new_context(), false); assert!(matches!( res, Err(ExitError::Other(Borrowed("ERR_BN128_INVALID_LEN",))) diff --git a/engine-precompiles/src/hash.rs b/engine-precompiles/src/hash.rs index f1c357a79..31c6e0847 100644 --- a/engine-precompiles/src/hash.rs +++ b/engine-precompiles/src/hash.rs @@ -41,6 +41,7 @@ impl Precompile for SHA256 { /// See: https://etherscan.io/address/0000000000000000000000000000000000000002 #[cfg(not(feature = "contract"))] fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -64,6 +65,7 @@ impl Precompile for SHA256 { /// See: https://etherscan.io/address/0000000000000000000000000000000000000002 #[cfg(feature = "contract")] fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -110,6 +112,7 @@ impl Precompile for RIPEMD160 { /// See: https://docs.soliditylang.org/en/develop/units-and-global-variables.html#mathematical-and-cryptographic-functions /// See: https://etherscan.io/address/0000000000000000000000000000000000000003 fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -147,7 +150,8 @@ mod tests { hex::decode("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") .unwrap(); - let res = SHA256::run(input, Some(60), &new_context(), false) + let res = SHA256 + .run(input, Some(60), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -160,7 +164,8 @@ mod tests { hex::decode("0000000000000000000000009c1185a5c5e9fc54612808977ee8f548b2258d31") .unwrap(); - let res = RIPEMD160::run(input, Some(600), &new_context(), false) + let res = RIPEMD160 + .run(input, Some(600), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); diff --git a/engine-precompiles/src/identity.rs b/engine-precompiles/src/identity.rs index a7b37305f..4f4f9cacd 100644 --- a/engine-precompiles/src/identity.rs +++ b/engine-precompiles/src/identity.rs @@ -36,6 +36,7 @@ impl Precompile for Identity { /// See: https://ethereum.github.io/yellowpaper/paper.pdf /// See: https://etherscan.io/address/0000000000000000000000000000000000000004 fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -65,19 +66,21 @@ mod tests { let input = [0u8, 1, 2, 3]; let expected = input[0..2].to_vec(); - let res = Identity::run(&input[0..2], Some(18), &new_context(), false) + let res = Identity + .run(&input[0..2], Some(18), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); let expected = input.to_vec(); - let res = Identity::run(&input, Some(18), &new_context(), false) + let res = Identity + .run(&input, Some(18), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); // gas fail - let res = Identity::run(&input[0..2], Some(17), &new_context(), false); + let res = Identity.run(&input[0..2], Some(17), &new_context(), false); assert!(matches!(res, Err(ExitError::OutOfGas))); @@ -86,7 +89,8 @@ mod tests { 0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, ]; - let res = Identity::run(&input, Some(21), &new_context(), false) + let res = Identity + .run(&input, Some(21), &new_context(), false) .unwrap() .output; assert_eq!(res, input.to_vec()); diff --git a/engine-precompiles/src/lib.rs b/engine-precompiles/src/lib.rs index a03226a2b..184fd72b8 100644 --- a/engine-precompiles/src/lib.rs +++ b/engine-precompiles/src/lib.rs @@ -22,6 +22,7 @@ use crate::identity::Identity; use crate::modexp::ModExp; use crate::native::{ExitToEthereum, ExitToNear}; use crate::secp256k1::ECRecover; +use aurora_engine_types::{account_id::AccountId, Address, BTreeMap, Box}; use evm::backend::Log; use evm::executor; use evm::{Context, ExitError, ExitSucceed}; @@ -59,10 +60,13 @@ type EvmPrecompileResult = Result; /// A precompiled function for use in the EVM. pub trait Precompile { /// The required gas in order to run the precompile function. - fn required_gas(input: &[u8]) -> Result; + fn required_gas(input: &[u8]) -> Result + where + Self: Sized; /// Runs the precompile function. fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -93,9 +97,7 @@ impl HardFork for Istanbul {} impl HardFork for Berlin {} -type PrecompileFn = fn(&[u8], Option, &Context, bool) -> EvmPrecompileResult; - -pub struct Precompiles(pub prelude::BTreeMap); +pub struct Precompiles(pub prelude::BTreeMap>); impl executor::PrecompileSet for Precompiles { fn execute( @@ -106,8 +108,8 @@ impl executor::PrecompileSet for Precompiles { context: &Context, is_static: bool, ) -> Option> { - self.0.get(&address).map(|f| { - f(input, gas_limit, context, is_static) + self.0.get(&address).map(|p| { + p.run(input, gas_limit, context, is_static) .map_err(|exit_status| executor::PrecompileFailure::Error { exit_status }) }) } @@ -119,7 +121,7 @@ impl executor::PrecompileSet for Precompiles { impl Precompiles { #[allow(dead_code)] - pub fn new_homestead() -> Self { + pub fn new_homestead(current_account_id: AccountId) -> Self { let addresses = prelude::vec![ ECRecover::ADDRESS, SHA256::ADDRESS, @@ -127,20 +129,20 @@ impl Precompiles { ExitToNear::ADDRESS, ExitToEthereum::ADDRESS, ]; - let fun: prelude::Vec = prelude::vec![ - ECRecover::run, - SHA256::run, - RIPEMD160::run, - ExitToNear::run, - ExitToEthereum::run, + let fun: prelude::Vec> = prelude::vec![ + Box::new(ECRecover), + Box::new(SHA256), + Box::new(RIPEMD160), + Box::new(ExitToNear::new(current_account_id.clone())), + Box::new(ExitToEthereum::new(current_account_id)), ]; - let map = addresses.into_iter().zip(fun).collect(); + let map: BTreeMap> = addresses.into_iter().zip(fun).collect(); Precompiles(map) } #[allow(dead_code)] - pub fn new_byzantium() -> Self { + pub fn new_byzantium(current_account_id: AccountId) -> Self { let addresses = prelude::vec![ ECRecover::ADDRESS, SHA256::ADDRESS, @@ -153,27 +155,24 @@ impl Precompiles { ExitToNear::ADDRESS, ExitToEthereum::ADDRESS, ]; - let fun: prelude::Vec = prelude::vec![ - ECRecover::run, - SHA256::run, - RIPEMD160::run, - Identity::run, - ModExp::::run, - Bn128Add::::run, - Bn128Mul::::run, - Bn128Pair::::run, - ExitToNear::run, - ExitToEthereum::run, + let fun: prelude::Vec> = prelude::vec![ + Box::new(ECRecover), + Box::new(SHA256), + Box::new(RIPEMD160), + Box::new(Identity), + Box::new(ModExp::::new()), + Box::new(Bn128Add::::new()), + Box::new(Bn128Mul::::new()), + Box::new(Bn128Pair::::new()), + Box::new(ExitToNear::new(current_account_id.clone())), + Box::new(ExitToEthereum::new(current_account_id)), ]; - let mut map = prelude::BTreeMap::new(); - for (address, fun) in addresses.into_iter().zip(fun) { - map.insert(address, fun); - } + let map: BTreeMap> = addresses.into_iter().zip(fun).collect(); Precompiles(map) } - pub fn new_istanbul() -> Self { + pub fn new_istanbul(current_account_id: AccountId) -> Self { let addresses = prelude::vec![ ECRecover::ADDRESS, SHA256::ADDRESS, @@ -187,28 +186,25 @@ impl Precompiles { ExitToNear::ADDRESS, ExitToEthereum::ADDRESS, ]; - let fun: prelude::Vec = prelude::vec![ - ECRecover::run, - SHA256::run, - RIPEMD160::run, - Identity::run, - ModExp::::run, - Bn128Add::::run, - Bn128Mul::::run, - Bn128Pair::::run, - Blake2F::run, - ExitToNear::run, - ExitToEthereum::run, + let fun: prelude::Vec> = prelude::vec![ + Box::new(ECRecover), + Box::new(SHA256), + Box::new(RIPEMD160), + Box::new(Identity), + Box::new(ModExp::::new()), + Box::new(Bn128Add::::new()), + Box::new(Bn128Mul::::new()), + Box::new(Bn128Pair::::new()), + Box::new(Blake2F), + Box::new(ExitToNear::new(current_account_id.clone())), + Box::new(ExitToEthereum::new(current_account_id)), ]; - let mut map = prelude::BTreeMap::new(); - for (address, fun) in addresses.into_iter().zip(fun) { - map.insert(address, fun); - } + let map: BTreeMap> = addresses.into_iter().zip(fun).collect(); Precompiles(map) } - pub fn new_berlin() -> Self { + pub fn new_berlin(current_account_id: AccountId) -> Self { let addresses = prelude::vec![ ECRecover::ADDRESS, SHA256::ADDRESS, @@ -222,30 +218,27 @@ impl Precompiles { ExitToNear::ADDRESS, ExitToEthereum::ADDRESS, ]; - let fun: prelude::Vec = prelude::vec![ - ECRecover::run, - SHA256::run, - RIPEMD160::run, - Identity::run, - ModExp::::run, - Bn128Add::::run, - Bn128Mul::::run, - Bn128Pair::::run, - Blake2F::run, - ExitToNear::run, - ExitToEthereum::run, + let fun: prelude::Vec> = prelude::vec![ + Box::new(ECRecover), + Box::new(SHA256), + Box::new(RIPEMD160), + Box::new(Identity), + Box::new(ModExp::::new()), + Box::new(Bn128Add::::new()), + Box::new(Bn128Mul::::new()), + Box::new(Bn128Pair::::new()), + Box::new(Blake2F), + Box::new(ExitToNear::new(current_account_id.clone())), + Box::new(ExitToEthereum::new(current_account_id)), ]; - let mut map = prelude::BTreeMap::new(); - for (address, fun) in addresses.into_iter().zip(fun) { - map.insert(address, fun); - } + let map: BTreeMap> = addresses.into_iter().zip(fun).collect(); Precompiles(map) } - pub fn new_london() -> Self { + pub fn new_london(current_account_id: AccountId) -> Self { // no precompile changes in London HF - Self::new_berlin() + Self::new_berlin(current_account_id) } } diff --git a/engine-precompiles/src/modexp.rs b/engine-precompiles/src/modexp.rs index 829de0e4a..2b1e3d12a 100644 --- a/engine-precompiles/src/modexp.rs +++ b/engine-precompiles/src/modexp.rs @@ -8,6 +8,10 @@ pub(super) struct ModExp(PhantomData); impl ModExp { pub(super) const ADDRESS: Address = super::make_address(0, 5); + + pub fn new() -> Self { + Self(Default::default()) + } } impl ModExp { @@ -104,6 +108,7 @@ impl Precompile for ModExp { /// See: https://eips.ethereum.org/EIPS/eip-198 /// See: https://etherscan.io/address/0000000000000000000000000000000000000005 fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -143,6 +148,7 @@ impl Precompile for ModExp { } fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -378,7 +384,8 @@ mod tests { for (test, test_gas) in TESTS.iter().zip(BYZANTIUM_GAS.iter()) { let input = hex::decode(&test.input).unwrap(); - let res = ModExp::::run(&input, Some(*test_gas), &new_context(), false) + let res = ModExp::::new() + .run(&input, Some(*test_gas), &new_context(), false) .unwrap() .output; let expected = hex::decode(&test.expected).unwrap(); @@ -446,7 +453,9 @@ mod tests { #[test] fn test_berlin_modexp_empty_input() { - let res = ModExp::::run(&[], Some(100_000), &new_context(), false).unwrap(); + let res = ModExp::::new() + .run(&[], Some(100_000), &new_context(), false) + .unwrap(); let expected: Vec = Vec::new(); assert_eq!(res.output, expected) } diff --git a/engine-precompiles/src/native.rs b/engine-precompiles/src/native.rs index 32c518360..867bfe0e3 100644 --- a/engine-precompiles/src/native.rs +++ b/engine-precompiles/src/native.rs @@ -5,7 +5,7 @@ use crate::prelude::{ parameters::{PromiseArgs, PromiseCreateArgs, WithdrawCallArgs}, sdk, storage::{bytes_to_key, KeyPrefix}, - vec, AccountId, BorshSerialize, Cow, String, ToString, TryFrom, TryInto, Vec, H160, U256, + vec, BorshSerialize, Cow, String, ToString, TryFrom, TryInto, Vec, H160, U256, }; #[cfg(all(feature = "error_refund", feature = "contract"))] use crate::prelude::{ @@ -15,6 +15,7 @@ use crate::prelude::{ use crate::prelude::Address; use crate::PrecompileOutput; +use aurora_engine_types::account_id::AccountId; #[cfg(feature = "contract")] use evm::backend::Log; use evm::{Context, ExitError}; @@ -188,7 +189,10 @@ pub mod events { } } -pub struct ExitToNear; //TransferEthToNear +//TransferEthToNear +pub struct ExitToNear { + current_account_id: AccountId, +} impl ExitToNear { /// Exit to NEAR precompile address @@ -197,6 +201,10 @@ impl ExitToNear { /// This address is computed as: `&keccak("exitToNear")[12..]` pub const ADDRESS: Address = super::make_address(0xe9217bc7, 0x0b7ed1f598ddd3199e80b093fa71124f); + + pub fn new(current_account_id: AccountId) -> Self { + Self { current_account_id } + } } #[cfg(feature = "contract")] @@ -218,6 +226,7 @@ impl Precompile for ExitToNear { #[cfg(not(feature = "contract"))] fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -234,6 +243,7 @@ impl Precompile for ExitToNear { #[cfg(feature = "contract")] fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -268,7 +278,7 @@ impl Precompile for ExitToNear { let (refund_address, mut input) = parse_input(input); #[cfg(not(feature = "error_refund"))] let mut input = parse_input(input); - let current_account_id = AccountId::try_from(sdk::current_account_id()).unwrap(); + let current_account_id = self.current_account_id.clone(); #[cfg(feature = "error_refund")] let refund_on_error_target = current_account_id.clone(); @@ -405,7 +415,9 @@ impl Precompile for ExitToNear { } } -pub struct ExitToEthereum; +pub struct ExitToEthereum { + current_account_id: AccountId, +} impl ExitToEthereum { /// Exit to Ethereum precompile address @@ -414,6 +426,10 @@ impl ExitToEthereum { /// This address is computed as: `&keccak("exitToEthereum")[12..]` pub const ADDRESS: Address = super::make_address(0xb0bd02f6, 0xa392af548bdf1cfaee5dfa0eefcc8eab); + + pub fn new(current_account_id: AccountId) -> Self { + Self { current_account_id } + } } impl Precompile for ExitToEthereum { @@ -423,6 +439,7 @@ impl Precompile for ExitToEthereum { #[cfg(not(feature = "contract"))] fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -439,6 +456,7 @@ impl Precompile for ExitToEthereum { #[cfg(feature = "contract")] fn run( + &self, input: &[u8], target_gas: Option, context: &Context, @@ -472,7 +490,7 @@ impl Precompile for ExitToEthereum { .try_into() .map_err(|_| ExitError::Other(Cow::from("ERR_INVALID_RECIPIENT_ADDRESS")))?; ( - AccountId::try_from(sdk::current_account_id()).unwrap(), + self.current_account_id.clone(), // There is no way to inject json, given the encoding of both arguments // as decimal and hexadecimal respectively. WithdrawCallArgs { diff --git a/engine-precompiles/src/secp256k1.rs b/engine-precompiles/src/secp256k1.rs index 633ef8c12..35fb857e7 100644 --- a/engine-precompiles/src/secp256k1.rs +++ b/engine-precompiles/src/secp256k1.rs @@ -61,6 +61,7 @@ impl Precompile for ECRecover { } fn run( + &self, input: &[u8], target_gas: Option, _context: &Context, @@ -140,7 +141,8 @@ mod tests { hex::decode("000000000000000000000000c08b5542d177ac6686946920409741463a15dddb") .unwrap(); - let res = ECRecover::run(&input, Some(3_000), &new_context(), false) + let res = ECRecover + .run(&input, Some(3_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -148,7 +150,7 @@ mod tests { // out of gas let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap(); - let res = ECRecover::run(&input, Some(2_999), &new_context(), false); + let res = ECRecover.run(&input, Some(2_999), &new_context(), false); assert!(matches!(res, Err(ExitError::OutOfGas))); // bad inputs @@ -157,7 +159,8 @@ mod tests { hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") .unwrap(); - let res = ECRecover::run(&input, Some(3_000), &new_context(), false) + let res = ECRecover + .run(&input, Some(3_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -167,7 +170,8 @@ mod tests { hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") .unwrap(); - let res = ECRecover::run(&input, Some(3_000), &new_context(), false) + let res = ECRecover + .run(&input, Some(3_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -177,7 +181,8 @@ mod tests { hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") .unwrap(); - let res = ECRecover::run(&input, Some(3_000), &new_context(), false) + let res = ECRecover + .run(&input, Some(3_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); @@ -187,7 +192,8 @@ mod tests { hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") .unwrap(); - let res = ECRecover::run(&input, Some(3_000), &new_context(), false) + let res = ECRecover + .run(&input, Some(3_000), &new_context(), false) .unwrap() .output; assert_eq!(res, expected); diff --git a/engine-tests/src/tests/erc20.rs b/engine-tests/src/tests/erc20.rs index fe14d5d91..24ce19697 100644 --- a/engine-tests/src/tests/erc20.rs +++ b/engine-tests/src/tests/erc20.rs @@ -112,8 +112,8 @@ fn profile_erc20_get_balance() { // at least 70% of the cost is spent on wasm computation (as opposed to host functions) let wasm_fraction = (100 * profile.wasm_gas()) / profile.all_gas(); assert!( - wasm_fraction > 69, - "{}% is not greater than 70%", + wasm_fraction >= 68, + "{}% is not greater than 68%", wasm_fraction ); } diff --git a/engine/src/engine.rs b/engine/src/engine.rs index 1574e3030..ddf14300a 100644 --- a/engine/src/engine.rs +++ b/engine/src/engine.rs @@ -267,8 +267,9 @@ struct StackExecutorParams { impl StackExecutorParams { fn new(gas_limit: u64) -> Self { + let current_account_id = AccountId::try_from(sdk::current_account_id()).unwrap(); Self { - precompiles: Precompiles::new_london(), + precompiles: Precompiles::new_london(current_account_id), gas_limit, } }