Refactor EVM operations to module functions (#6056)

* Refactor EVM operations to module functions

* Bump impl version
This commit is contained in:
Wei Tang
2020-05-18 18:42:56 +02:00
committed by GitHub
parent 7536de97b1
commit cd0ffec1fe
2 changed files with 96 additions and 35 deletions
+1 -1
View File
@@ -94,7 +94,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 248,
impl_version: 1,
impl_version: 2,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
};
+95 -34
View File
@@ -256,19 +256,14 @@ decl_module! {
let sender = ensure_signed(origin)?;
let source = T::ConvertAccountId::convert_account_id(&sender);
Self::execute_evm(
Self::execute_call(
source,
target,
input,
value,
gas_limit,
gas_price,
nonce,
|executor| ((), executor.transact_call(
source,
target,
value,
input,
gas_limit as usize,
)),
).map_err(Into::into)
}
@@ -291,22 +286,13 @@ decl_module! {
let sender = ensure_signed(origin)?;
let source = T::ConvertAccountId::convert_account_id(&sender);
let create_address = Self::execute_evm(
let create_address = Self::execute_create(
source,
init,
value,
gas_limit,
gas_price,
nonce,
|executor| {
(executor.create_address(
evm::CreateScheme::Legacy { caller: source },
), executor.transact_create(
source,
value,
init,
gas_limit as usize,
))
},
nonce
)?;
Module::<T>::deposit_event(Event::<T>::Created(create_address));
@@ -332,24 +318,14 @@ decl_module! {
let sender = ensure_signed(origin)?;
let source = T::ConvertAccountId::convert_account_id(&sender);
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
let create_address = Self::execute_evm(
let create_address = Self::execute_create2(
source,
init,
salt,
value,
gas_limit,
gas_price,
nonce,
|executor| {
(executor.create_address(
evm::CreateScheme::Create2 { caller: source, code_hash, salt },
), executor.transact_create2(
source,
value,
init,
salt,
gas_limit as usize,
))
},
nonce
)?;
Module::<T>::deposit_event(Event::<T>::Created(create_address));
@@ -391,6 +367,91 @@ impl<T: Trait> Module<T> {
AccountStorages::remove_prefix(address);
}
/// Execute a create transaction on behalf of given sender.
pub fn execute_create(
source: H160,
init: Vec<u8>,
value: U256,
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>
) -> Result<H160, Error<T>> {
Self::execute_evm(
source,
value,
gas_limit,
gas_price,
nonce,
|executor| {
(executor.create_address(
evm::CreateScheme::Legacy { caller: source },
), executor.transact_create(
source,
value,
init,
gas_limit as usize,
))
},
)
}
/// Execute a create2 transaction on behalf of a given sender.
pub fn execute_create2(
source: H160,
init: Vec<u8>,
salt: H256,
value: U256,
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>
) -> Result<H160, Error<T>> {
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
Self::execute_evm(
source,
value,
gas_limit,
gas_price,
nonce,
|executor| {
(executor.create_address(
evm::CreateScheme::Create2 { caller: source, code_hash, salt },
), executor.transact_create2(
source,
value,
init,
salt,
gas_limit as usize,
))
},
)
}
/// Execute a call transaction on behalf of a given sender.
pub fn execute_call(
source: H160,
target: H160,
input: Vec<u8>,
value: U256,
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>,
) -> Result<(), Error<T>> {
Self::execute_evm(
source,
value,
gas_limit,
gas_price,
nonce,
|executor| ((), executor.transact_call(
source,
target,
value,
input,
gas_limit as usize,
)),
)
}
/// Execute an EVM operation.
fn execute_evm<F, R>(
source: H160,