From 70dd2f012fd4b0ae6f79dadfdefdb05144a0f20b Mon Sep 17 00:00:00 2001 From: Bear Wang Date: Sat, 16 Mar 2024 00:12:35 +0800 Subject: [PATCH] Refactor the `forward_transact` (#1445) * Refactor the call input * Remove the source field from the request * Make thing simple * Refactor the test module * Fix the runtime things * Format * Fix CI * Fix CI 2 --------- Co-authored-by: fisher --- Cargo.lock | 1 - pallet/ethtx-forwarder/Cargo.toml | 1 - pallet/ethtx-forwarder/src/lib.rs | 239 ++++++++++------ pallet/ethtx-forwarder/src/mock.rs | 5 +- .../src/{tests/mod.rs => test.rs} | 255 +++++++----------- pallet/ethtx-forwarder/src/tests/eip1559.rs | 177 ------------ pallet/ethtx-forwarder/src/tests/eip2930.rs | 175 ------------ pallet/ethtx-forwarder/src/tests/legacy.rs | 167 ------------ pallet/staking/src/lib.rs | 43 +-- pallet/staking/src/tests.rs | 5 - 10 files changed, 264 insertions(+), 804 deletions(-) rename pallet/ethtx-forwarder/src/{tests/mod.rs => test.rs} (68%) delete mode 100644 pallet/ethtx-forwarder/src/tests/eip1559.rs delete mode 100644 pallet/ethtx-forwarder/src/tests/eip2930.rs delete mode 100644 pallet/ethtx-forwarder/src/tests/legacy.rs diff --git a/Cargo.lock b/Cargo.lock index ebfb12489..2ed3b50b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2861,7 +2861,6 @@ dependencies = [ "pallet-evm", "pallet-timestamp", "parity-scale-codec", - "rlp", "scale-info", "sha3", "sp-core", diff --git a/pallet/ethtx-forwarder/Cargo.toml b/pallet/ethtx-forwarder/Cargo.toml index 05016203a..19196ea97 100644 --- a/pallet/ethtx-forwarder/Cargo.toml +++ b/pallet/ethtx-forwarder/Cargo.toml @@ -27,7 +27,6 @@ sp-std = { workspace = true } [dev-dependencies] array-bytes = { workspace = true } libsecp256k1 = { workspace = true, features = ["std"] } -rlp = { version = "0.5" } sha3 = { workspace = true } # frontier diff --git a/pallet/ethtx-forwarder/src/lib.rs b/pallet/ethtx-forwarder/src/lib.rs index 4ee7eddd5..b13c9f0a3 100644 --- a/pallet/ethtx-forwarder/src/lib.rs +++ b/pallet/ethtx-forwarder/src/lib.rs @@ -22,13 +22,14 @@ #[cfg(test)] mod mock; #[cfg(test)] -mod tests; +mod test; -// core -use core::borrow::BorrowMut; // crates.io use codec::{Decode, Encode, MaxEncodedLen}; -use ethereum::TransactionV2 as Transaction; +use ethereum::{ + EIP1559Transaction, EIP2930Transaction, LegacyTransaction, TransactionAction, + TransactionSignature, TransactionV2 as Transaction, +}; use frame_support::sp_runtime::traits::UniqueSaturatedInto; use scale_info::TypeInfo; // frontier @@ -37,9 +38,9 @@ use fp_evm::{CheckEvmTransaction, CheckEvmTransactionConfig, TransactionValidati use pallet_evm::{FeeCalculator, GasWeightMapping}; // substrate use frame_support::{traits::EnsureOrigin, PalletError}; -use sp_core::{H160, U256}; -use sp_runtime::{traits::BadOrigin, RuntimeDebug}; -use sp_std::boxed::Box; +use sp_core::{Get, H160, H256, U256}; +use sp_runtime::{traits::BadOrigin, DispatchError, RuntimeDebug}; +use sp_std::vec::Vec; pub use pallet::*; @@ -102,8 +103,8 @@ pub mod pallet { #[pallet::error] pub enum Error { - /// EVM validation errors. - MessageTransactError(EvmTxErrorWrapper), + /// Transaction validation errors. + ValidationError(TxErrorWrapper), } #[pallet::call] @@ -114,65 +115,16 @@ pub mod pallet { //This call can only be used at runtime and is not available to EOA users. #[pallet::call_index(0)] #[pallet::weight({ - ::GasWeightMapping::gas_to_weight({ - let transaction_data: TransactionData = (&**transaction).into(); - transaction_data.gas_limit.unique_saturated_into() - }, true) + ::GasWeightMapping::gas_to_weight(request.gas_limit.unique_saturated_into(), true) })] pub fn forward_transact( origin: OriginFor, - mut transaction: Box, + request: ForwardRequest, ) -> DispatchResultWithPostInfo { let source = ensure_forward_transact(origin)?; - let (who, _) = pallet_evm::Pallet::::account_basic(&source); - let base_fee = T::FeeCalculator::min_gas_price().0; - - let transaction_mut = transaction.borrow_mut(); - match transaction_mut { - Transaction::Legacy(tx) => { - tx.nonce = who.nonce; - tx.gas_price = base_fee; - }, - Transaction::EIP2930(tx) => { - tx.nonce = who.nonce; - tx.gas_price = base_fee; - }, - Transaction::EIP1559(tx) => { - tx.nonce = who.nonce; - tx.max_fee_per_gas = base_fee; - tx.max_priority_fee_per_gas = U256::zero(); - }, - }; - let transaction_data: TransactionData = (&*transaction).into(); - let (weight_limit, proof_size_base_cost) = - match ::GasWeightMapping::gas_to_weight( - transaction_data.gas_limit.unique_saturated_into(), - true, - ) { - weight_limit if weight_limit.proof_size() > 0 => - (Some(weight_limit), Some(proof_size_base_cost(&transaction))), - _ => (None, None), - }; - - let _ = CheckEvmTransaction::::new( - CheckEvmTransactionConfig { - evm_config: T::config(), - block_gas_limit: T::BlockGasLimit::get(), - base_fee, - chain_id: T::ChainId::get(), - is_transactional: true, - }, - transaction_data.into(), - weight_limit, - proof_size_base_cost, - ) - .validate_in_block_for(&who) - .and_then(|v| v.with_base_fee()) - .and_then(|v| v.with_balance_for(&who)) - .map_err(|e| >::MessageTransactError(e))?; - - T::ValidatedTransaction::apply(source, *transaction).map(|(post_info, _)| post_info) + let transaction = Self::validated_transaction(source, request)?; + T::ValidatedTransaction::apply(source, transaction).map(|(post_info, _)| post_info) } } } @@ -183,17 +135,131 @@ impl Pallet { /// The gas_price of an EVM transaction is always the min_gas_price(), which is a fixed value. /// Therefore, only the gas_limit and value of the transaction should be considered in the /// calculation of the fee, and the gas_price of the transaction itself can be ignored. - pub fn total_payment(tx_data: TransactionData) -> U256 { + pub fn total_payment(request: &ForwardRequest) -> U256 { let base_fee = ::FeeCalculator::min_gas_price().0; - let fee = base_fee.saturating_mul(tx_data.gas_limit); + let fee = base_fee.saturating_mul(request.gas_limit); + + request.value.saturating_add(fee) + } + + fn validated_transaction( + source: H160, + req: ForwardRequest, + ) -> Result { + let (who, _) = pallet_evm::Pallet::::account_basic(&source); + let base_fee = T::FeeCalculator::min_gas_price().0; + + let transaction = match req.tx_type { + TxType::LegacyTransaction => Transaction::Legacy(LegacyTransaction { + nonce: who.nonce, + gas_price: base_fee, + gas_limit: req.gas_limit, + action: req.action, + value: req.value, + input: req.input, + // copied from: + // https://github.com/rust-ethereum/ethereum/blob/24739cc8ba6e9d8ee30ada8ec92161e4c48d578e/src/transaction.rs#L798 + signature: TransactionSignature::new( + 38, + H256([ + 190, 103, 224, 160, 125, 182, 125, 168, 212, 70, 247, 106, 221, 89, 14, 84, + 182, 233, 44, 182, 184, 249, 131, 90, 235, 103, 84, 5, 121, 162, 119, 23, + ]), + H256([ + 45, 105, 5, 22, 81, 32, 32, 23, 28, 30, 200, 112, 246, 255, 69, 57, 140, + 200, 96, 146, 80, 50, 107, 232, 153, 21, 251, 83, 142, 123, 215, 24, + ]), + ) + .expect("This signature is always valid"), + }), + TxType::EIP2930Transaction => { + Transaction::EIP2930(EIP2930Transaction { + chain_id: 0, + nonce: who.nonce, + gas_price: base_fee, + gas_limit: req.gas_limit, + action: req.action, + value: req.value, + input: req.input, + access_list: Default::default(), + // copied from: + // https://github.com/rust-ethereum/ethereum/blob/24739cc8ba6e9d8ee30ada8ec92161e4c48d578e/src/transaction.rs#L873-L875 + odd_y_parity: false, + // 36b241b061a36a32ab7fe86c7aa9eb592dd59018cd0443adc0903590c16b02b0 + r: H256([ + 54, 178, 65, 176, 97, 163, 106, 50, 171, 127, 232, 108, 122, 169, 235, 89, + 45, 213, 144, 24, 205, 4, 67, 173, 192, 144, 53, 144, 193, 107, 2, 176, + ]), + // 5edcc541b4741c5cc6dd347c5ed9577ef293a62787b4510465fadbfe39ee4094 + s: H256([ + 54, 178, 65, 176, 97, 163, 106, 50, 171, 127, 232, 108, 122, 169, 235, 89, + 45, 213, 144, 24, 205, 4, 67, 173, 192, 144, 53, 144, 193, 107, 2, 176, + ]), + }) + }, + TxType::EIP1559Transaction => { + Transaction::EIP1559(EIP1559Transaction { + chain_id: 0, + nonce: who.nonce, + max_fee_per_gas: base_fee, + max_priority_fee_per_gas: U256::zero(), + gas_limit: req.gas_limit, + action: req.action, + value: req.value, + input: req.input, + access_list: Default::default(), + // copied from: + // https://github.com/rust-ethereum/ethereum/blob/24739cc8ba6e9d8ee30ada8ec92161e4c48d578e/src/transaction.rs#L873-L875 + odd_y_parity: false, + // 36b241b061a36a32ab7fe86c7aa9eb592dd59018cd0443adc0903590c16b02b0 + r: H256([ + 54, 178, 65, 176, 97, 163, 106, 50, 171, 127, 232, 108, 122, 169, 235, 89, + 45, 213, 144, 24, 205, 4, 67, 173, 192, 144, 53, 144, 193, 107, 2, 176, + ]), + // 5edcc541b4741c5cc6dd347c5ed9577ef293a62787b4510465fadbfe39ee4094 + s: H256([ + 54, 178, 65, 176, 97, 163, 106, 50, 171, 127, 232, 108, 122, 169, 235, 89, + 45, 213, 144, 24, 205, 4, 67, 173, 192, 144, 53, 144, 193, 107, 2, 176, + ]), + }) + }, + }; + + let transaction_data: TransactionData = (&transaction).into(); + let (weight_limit, proof_size_base_cost) = + match ::GasWeightMapping::gas_to_weight( + transaction_data.gas_limit.unique_saturated_into(), + true, + ) { + weight_limit if weight_limit.proof_size() > 0 => + (Some(weight_limit), Some(proof_size_base_cost(&transaction))), + _ => (None, None), + }; + + let _ = CheckEvmTransaction::::new( + CheckEvmTransactionConfig { + evm_config: T::config(), + block_gas_limit: T::BlockGasLimit::get(), + base_fee, + chain_id: T::ChainId::get(), + is_transactional: true, + }, + transaction_data.into(), + weight_limit, + proof_size_base_cost, + ) + .validate_in_block_for(&who) + .and_then(|v| v.with_base_fee()) + .and_then(|v| v.with_balance_for(&who)) + .map_err(|e| >::ValidationError(e))?; - tx_data.value.saturating_add(fee) + Ok(transaction) } } // TODO: replace it with upstream error type #[derive(Encode, Decode, TypeInfo, PalletError)] -pub enum EvmTxErrorWrapper { +pub enum TxErrorWrapper { GasLimitTooLow, GasLimitTooHigh, GasPriceTooLow, @@ -207,20 +273,20 @@ pub enum EvmTxErrorWrapper { UnknownError, } -impl From for EvmTxErrorWrapper { +impl From for TxErrorWrapper { fn from(validation_error: TransactionValidationError) -> Self { match validation_error { - TransactionValidationError::GasLimitTooLow => EvmTxErrorWrapper::GasLimitTooLow, - TransactionValidationError::GasLimitTooHigh => EvmTxErrorWrapper::GasLimitTooHigh, - TransactionValidationError::GasPriceTooLow => EvmTxErrorWrapper::GasPriceTooLow, - TransactionValidationError::PriorityFeeTooHigh => EvmTxErrorWrapper::PriorityFeeTooHigh, - TransactionValidationError::BalanceTooLow => EvmTxErrorWrapper::BalanceTooLow, - TransactionValidationError::TxNonceTooLow => EvmTxErrorWrapper::TxNonceTooLow, - TransactionValidationError::TxNonceTooHigh => EvmTxErrorWrapper::TxNonceTooHigh, - TransactionValidationError::InvalidFeeInput => EvmTxErrorWrapper::InvalidFeeInput, - TransactionValidationError::InvalidChainId => EvmTxErrorWrapper::InvalidChainId, - TransactionValidationError::InvalidSignature => EvmTxErrorWrapper::InvalidSignature, - TransactionValidationError::UnknownError => EvmTxErrorWrapper::UnknownError, + TransactionValidationError::GasLimitTooLow => TxErrorWrapper::GasLimitTooLow, + TransactionValidationError::GasLimitTooHigh => TxErrorWrapper::GasLimitTooHigh, + TransactionValidationError::GasPriceTooLow => TxErrorWrapper::GasPriceTooLow, + TransactionValidationError::PriorityFeeTooHigh => TxErrorWrapper::PriorityFeeTooHigh, + TransactionValidationError::BalanceTooLow => TxErrorWrapper::BalanceTooLow, + TransactionValidationError::TxNonceTooLow => TxErrorWrapper::TxNonceTooLow, + TransactionValidationError::TxNonceTooHigh => TxErrorWrapper::TxNonceTooHigh, + TransactionValidationError::InvalidFeeInput => TxErrorWrapper::InvalidFeeInput, + TransactionValidationError::InvalidChainId => TxErrorWrapper::InvalidChainId, + TransactionValidationError::InvalidSignature => TxErrorWrapper::InvalidSignature, + TransactionValidationError::UnknownError => TxErrorWrapper::UnknownError, } } } @@ -235,3 +301,20 @@ fn proof_size_base_cost(transaction: &Transaction) -> u64 { // call index .saturating_add(1) as u64 } + +#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, TypeInfo)] +pub struct ForwardRequest { + pub tx_type: TxType, + pub gas_limit: U256, + pub action: TransactionAction, + pub value: U256, + pub input: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, TypeInfo, Default)] +pub enum TxType { + #[default] + LegacyTransaction, + EIP2930Transaction, + EIP1559Transaction, +} diff --git a/pallet/ethtx-forwarder/src/mock.rs b/pallet/ethtx-forwarder/src/mock.rs index c8670f839..5a8b07b58 100644 --- a/pallet/ethtx-forwarder/src/mock.rs +++ b/pallet/ethtx-forwarder/src/mock.rs @@ -87,7 +87,7 @@ impl pallet_timestamp::Config for Runtime { frame_support::parameter_types! { pub const TransactionByteFee: u64 = 1; - pub const ChainId: u64 = 42; + pub const ChainId: u64 = 888; pub const BlockGasLimit: sp_core::U256 = sp_core::U256::MAX; pub const WeightPerGas: frame_support::weights::Weight = frame_support::weights::Weight::from_parts(20_000, 0); } @@ -208,7 +208,6 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { pub(crate) struct AccountInfo { pub address: sp_core::H160, - pub private_key: sp_core::H256, } pub(crate) fn address_build(seed: u8) -> AccountInfo { @@ -221,7 +220,7 @@ pub(crate) fn address_build(seed: u8) -> AccountInfo { s }; - AccountInfo { private_key: raw_private_key.into(), address: raw_address.into() } + AccountInfo { address: raw_address.into() } } #[derive(Default)] diff --git a/pallet/ethtx-forwarder/src/tests/mod.rs b/pallet/ethtx-forwarder/src/test.rs similarity index 68% rename from pallet/ethtx-forwarder/src/tests/mod.rs rename to pallet/ethtx-forwarder/src/test.rs index f4f7bd6a2..a948e86a0 100644 --- a/pallet/ethtx-forwarder/src/tests/mod.rs +++ b/pallet/ethtx-forwarder/src/test.rs @@ -16,20 +16,14 @@ // You should have received a copy of the GNU General Public License // along with Darwinia. If not, see . -mod eip1559; -mod eip2930; -mod legacy; - -// crates.io -use ethereum::{TransactionAction, TransactionSignature}; -use rlp::RlpStream; -use sha3::{Digest, Keccak256}; -// frontier -use fp_ethereum::Transaction; // darwinia -use crate::mock::*; +use crate::{mock::*, ForwardEthOrigin, ForwardRequest, TxType}; +// crates.io +use ethereum::TransactionSignature; // substrate +use frame_support::{assert_err, assert_ok, traits::Currency}; use sp_core::{H256, U256}; +use sp_runtime::{DispatchError, ModuleError}; // This ERC-20 contract mints the maximum amount of tokens to the contract creator. // pragma solidity ^0.5.0; @@ -39,161 +33,100 @@ use sp_core::{H256, U256}; // } pub const ERC20_CONTRACT_BYTECODE: &str = "608060405234801561001057600080fd5b50610041337fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff61004660201b60201c565b610291565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156100e9576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601f8152602001807f45524332303a206d696e7420746f20746865207a65726f20616464726573730081525060200191505060405180910390fd5b6101028160025461020960201b610c7c1790919060201c565b60028190555061015d816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205461020960201b610c7c1790919060201c565b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b600080828401905083811015610287576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b8091505092915050565b610e3a806102a06000396000f3fe608060405234801561001057600080fd5b50600436106100885760003560e01c806370a082311161005b57806370a08231146101fd578063a457c2d714610255578063a9059cbb146102bb578063dd62ed3e1461032157610088565b8063095ea7b31461008d57806318160ddd146100f357806323b872dd146101115780633950935114610197575b600080fd5b6100d9600480360360408110156100a357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610399565b604051808215151515815260200191505060405180910390f35b6100fb6103b7565b6040518082815260200191505060405180910390f35b61017d6004803603606081101561012757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506103c1565b604051808215151515815260200191505060405180910390f35b6101e3600480360360408110156101ad57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061049a565b604051808215151515815260200191505060405180910390f35b61023f6004803603602081101561021357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061054d565b6040518082815260200191505060405180910390f35b6102a16004803603604081101561026b57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610595565b604051808215151515815260200191505060405180910390f35b610307600480360360408110156102d157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610662565b604051808215151515815260200191505060405180910390f35b6103836004803603604081101561033757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610680565b6040518082815260200191505060405180910390f35b60006103ad6103a6610707565b848461070f565b6001905092915050565b6000600254905090565b60006103ce848484610906565b61048f846103da610707565b61048a85604051806060016040528060288152602001610d7060289139600160008b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000610440610707565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610bbc9092919063ffffffff16565b61070f565b600190509392505050565b60006105436104a7610707565b8461053e85600160006104b8610707565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008973ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610c7c90919063ffffffff16565b61070f565b6001905092915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60006106586105a2610707565b8461065385604051806060016040528060258152602001610de160259139600160006105cc610707565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008a73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610bbc9092919063ffffffff16565b61070f565b6001905092915050565b600061067661066f610707565b8484610906565b6001905092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610795576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526024815260200180610dbd6024913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141561081b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526022815260200180610d286022913960400191505060405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925836040518082815260200191505060405180910390a3505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16141561098c576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526025815260200180610d986025913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610a12576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526023815260200180610d056023913960400191505060405180910390fd5b610a7d81604051806060016040528060268152602001610d4a602691396000808773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610bbc9092919063ffffffff16565b6000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610b10816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610c7c90919063ffffffff16565b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a3505050565b6000838311158290610c69576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610c2e578082015181840152602081019050610c13565b50505050905090810190601f168015610c5b5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060008385039050809150509392505050565b600080828401905083811015610cfa576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b809150509291505056fe45524332303a207472616e7366657220746f20746865207a65726f206164647265737345524332303a20617070726f766520746f20746865207a65726f206164647265737345524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e636545524332303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636545524332303a207472616e736665722066726f6d20746865207a65726f206164647265737345524332303a20617070726f76652066726f6d20746865207a65726f206164647265737345524332303a2064656372656173656420616c6c6f77616e63652062656c6f77207a65726fa265627a7a72315820c7a5ffabf642bda14700b2de42f8c57b36621af020441df825de45fd2b3e1c5c64736f6c63430005100032"; -pub struct LegacyUnsignedTransaction { - pub nonce: U256, - pub gas_price: U256, - pub gas_limit: U256, - pub action: TransactionAction, - pub value: U256, - pub input: Vec, -} -impl LegacyUnsignedTransaction { - fn signing_rlp_append(&self, s: &mut RlpStream) { - s.begin_list(9); - s.append(&self.nonce); - s.append(&self.gas_price); - s.append(&self.gas_limit); - s.append(&self.action); - s.append(&self.value); - s.append(&self.input); - s.append(&ChainId::get()); - s.append(&0u8); - s.append(&0u8); - } - - fn signing_hash(&self) -> H256 { - let mut stream = RlpStream::new(); - self.signing_rlp_append(&mut stream); - H256::from_slice(Keccak256::digest(&stream.out()).as_slice()) - } - - pub fn sign(&self, key: &H256) -> Transaction { - self.sign_with_chain_id(key, ChainId::get()) - } - - pub fn sign_with_chain_id(&self, key: &H256, chain_id: u64) -> Transaction { - let hash = self.signing_hash(); - let msg = libsecp256k1::Message::parse(hash.as_fixed_bytes()); - let s = libsecp256k1::sign(&msg, &libsecp256k1::SecretKey::parse_slice(&key[..]).unwrap()); - let sig = s.0.serialize(); - - let sig = TransactionSignature::new( - s.1.serialize() as u64 % 2 + chain_id * 2 + 35, - H256::from_slice(&sig[0..32]), - H256::from_slice(&sig[32..64]), - ) - .unwrap(); - - Transaction::Legacy(ethereum::LegacyTransaction { - nonce: self.nonce, - gas_price: self.gas_price, - gas_limit: self.gas_limit, - action: self.action, - value: self.value, - input: self.input.clone(), - signature: sig, - }) +pub fn mocked_request() -> ForwardRequest { + ForwardRequest { + tx_type: TxType::default(), + gas_limit: U256::from(1_000_000), + action: ethereum::TransactionAction::Create, + value: U256::zero(), + input: array_bytes::hex2bytes_unchecked(ERC20_CONTRACT_BYTECODE), } } -pub struct EIP2930UnsignedTransaction { - pub nonce: U256, - pub gas_price: U256, - pub gas_limit: U256, - pub action: TransactionAction, - pub value: U256, - pub input: Vec, -} - -impl EIP2930UnsignedTransaction { - pub fn sign(&self, secret: &H256, chain_id: Option) -> Transaction { - let secret = { - let mut sk: [u8; 32] = [0u8; 32]; - sk.copy_from_slice(&secret[0..]); - libsecp256k1::SecretKey::parse(&sk).unwrap() - }; - let chain_id = chain_id.unwrap_or(ChainId::get()); - let msg = ethereum::EIP2930TransactionMessage { - chain_id, - nonce: self.nonce, - gas_price: self.gas_price, - gas_limit: self.gas_limit, - action: self.action, - value: self.value, - input: self.input.clone(), - access_list: vec![], - }; - let signing_message = libsecp256k1::Message::parse_slice(&msg.hash()[..]).unwrap(); - - let (signature, recid) = libsecp256k1::sign(&signing_message, &secret); - let rs = signature.serialize(); - let r = H256::from_slice(&rs[0..32]); - let s = H256::from_slice(&rs[32..64]); - Transaction::EIP2930(ethereum::EIP2930Transaction { - chain_id: msg.chain_id, - nonce: msg.nonce, - gas_price: msg.gas_price, - gas_limit: msg.gas_limit, - action: msg.action, - value: msg.value, - input: msg.input.clone(), - access_list: msg.access_list, - odd_y_parity: recid.serialize() != 0, - r, - - s, - }) - } +#[test] +fn forward_request_works() { + let alice = address_build(1); + ExtBuilder::default() + .with_balances(vec![(alice.address, 1_000_000_000_000)]) + .build() + .execute_with(|| { + macro_rules! test_tx_types { + ($tx_type:expr) => { + let mut request = mocked_request(); + request.tx_type = $tx_type; + + assert_ok!(EthTxForwarder::forward_transact( + ForwardEthOrigin::ForwardEth(alice.address).into(), + request, + )); + assert!(System::events() + .iter() + .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); + }; + } + + test_tx_types!(TxType::LegacyTransaction); + test_tx_types!(TxType::EIP1559Transaction); + test_tx_types!(TxType::EIP2930Transaction); + }); } -pub struct EIP1559UnsignedTransaction { - pub nonce: U256, - pub max_priority_fee_per_gas: U256, - pub max_fee_per_gas: U256, - pub gas_limit: U256, - pub action: TransactionAction, - pub value: U256, - pub input: Vec, +#[test] +fn forward_legacy_request_sufficient_balance() { + let alice = address_build(1); + ExtBuilder::default().build().execute_with(|| { + macro_rules! test_tx_types { + ($tx_type:expr) => { + let mut request = mocked_request(); + request.tx_type = $tx_type; + + assert_err!( + EthTxForwarder::forward_transact( + ForwardEthOrigin::ForwardEth(alice.address).into(), + request.clone() + ), + DispatchError::Module(ModuleError { + index: 5, + error: [0, 4, 0, 0], + message: Some("ValidationError",) + }) + ); + + let fee = EthTxForwarder::total_payment(&request); + let _ = Balances::deposit_creating(&alice.address, fee.as_u64()); + assert_ok!(EthTxForwarder::forward_transact( + ForwardEthOrigin::ForwardEth(alice.address).into(), + request + )); + assert!(System::events() + .iter() + .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); + }; + } + test_tx_types!(TxType::LegacyTransaction); + test_tx_types!(TxType::EIP1559Transaction); + test_tx_types!(TxType::EIP2930Transaction); + }); } -impl EIP1559UnsignedTransaction { - pub fn sign(&self, secret: &H256, chain_id: Option) -> Transaction { - let secret = { - let mut sk: [u8; 32] = [0u8; 32]; - sk.copy_from_slice(&secret[0..]); - libsecp256k1::SecretKey::parse(&sk).unwrap() - }; - let chain_id = chain_id.unwrap_or(ChainId::get()); - let msg = ethereum::EIP1559TransactionMessage { - chain_id, - nonce: self.nonce, - max_priority_fee_per_gas: self.max_priority_fee_per_gas, - max_fee_per_gas: self.max_fee_per_gas, - gas_limit: self.gas_limit, - action: self.action, - value: self.value, - input: self.input.clone(), - access_list: vec![], - }; - let signing_message = libsecp256k1::Message::parse_slice(&msg.hash()[..]).unwrap(); - - let (signature, recid) = libsecp256k1::sign(&signing_message, &secret); - let rs = signature.serialize(); - let r = H256::from_slice(&rs[0..32]); - let s = H256::from_slice(&rs[32..64]); - Transaction::EIP1559(ethereum::EIP1559Transaction { - chain_id: msg.chain_id, - nonce: msg.nonce, - max_priority_fee_per_gas: msg.max_priority_fee_per_gas, - max_fee_per_gas: msg.max_fee_per_gas, - gas_limit: msg.gas_limit, - action: msg.action, - value: msg.value, - input: msg.input.clone(), - access_list: msg.access_list, - odd_y_parity: recid.serialize() != 0, - r, - s, - }) - } +#[test] +fn mock_signature_valid() { + assert!( + // copied from: + // https://github.com/rust-ethereum/ethereum/blob/24739cc8ba6e9d8ee30ada8ec92161e4c48d578e/src/transaction.rs#L798 + TransactionSignature::new( + 38, + // be67e0a07db67da8d446f76add590e54b6e92cb6b8f9835aeb67540579a27717 + H256([ + 190, 103, 224, 160, 125, 182, 125, 168, 212, 70, 247, 106, 221, 89, 14, 84, 182, + 233, 44, 182, 184, 249, 131, 90, 235, 103, 84, 5, 121, 162, 119, 23, + ]), + // 2d690516512020171c1ec870f6ff45398cc8609250326be89915fb538e7bd718 + H256([ + 45, 105, 5, 22, 81, 32, 32, 23, 28, 30, 200, 112, 246, 255, 69, 57, 140, 200, 96, + 146, 80, 50, 107, 232, 153, 21, 251, 83, 142, 123, 215, 24, + ]), + ) + .is_some(), + ) } diff --git a/pallet/ethtx-forwarder/src/tests/eip1559.rs b/pallet/ethtx-forwarder/src/tests/eip1559.rs deleted file mode 100644 index 6923fedda..000000000 --- a/pallet/ethtx-forwarder/src/tests/eip1559.rs +++ /dev/null @@ -1,177 +0,0 @@ -// This file is part of Darwinia. -// -// Copyright (C) Darwinia Network -// SPDX-License-Identifier: GPL-3.0 -// -// Darwinia is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Darwinia is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Darwinia. If not, see . - -// darwinia -use crate::{mock::*, tests::*, ForwardEthOrigin}; -use ethereum::EIP1559Transaction; -// frontier -use fp_evm::FeeCalculator; -// substrate -use frame_support::{assert_err, assert_ok, traits::Currency}; -use sp_core::U256; -use sp_runtime::{DispatchError, ModuleError}; - -pub fn eip1559_erc20_creation_unsigned_transaction() -> EIP1559UnsignedTransaction { - EIP1559UnsignedTransaction { - nonce: U256::zero(), - max_priority_fee_per_gas: U256::from(1), - max_fee_per_gas: U256::from(1), - gas_limit: U256::from(1_000_000), - action: ethereum::TransactionAction::Create, - value: U256::zero(), - input: array_bytes::hex2bytes_unchecked(ERC20_CONTRACT_BYTECODE), - } -} - -#[test] -fn test_eip1559_transaction_works() { - let alice = address_build(1); - - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let unsigned_tx = eip1559_erc20_creation_unsigned_transaction(); - let t = unsigned_tx.sign(&alice.private_key, None); - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_eip1559_transaction_with_auto_nonce() { - let alice = address_build(1); - - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let mut unsigned_tx = eip1559_erc20_creation_unsigned_transaction(); - unsigned_tx.nonce = U256::MAX; - let t = unsigned_tx.sign(&alice.private_key, None); - - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_eip1559_transaction_with_auto_gas_price() { - let alice = address_build(1); - - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let mut unsigned_tx = eip1559_erc20_creation_unsigned_transaction(); - unsigned_tx.max_fee_per_gas = - ::FeeCalculator::min_gas_price().0 - 1; - let t = unsigned_tx.sign(&alice.private_key, None); - - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_eip1559_transaction_with_sufficient_balance() { - let alice = address_build(1); - ExtBuilder::default().build().execute_with(|| { - let unsigned_tx = eip1559_erc20_creation_unsigned_transaction(); - let t = unsigned_tx.sign(&alice.private_key, None); - - assert_err!( - EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t.clone()) - ), - DispatchError::Module(ModuleError { - index: 5, - error: [0, 4, 0, 0], - message: Some("MessageTransactError",) - }) - ); - - let fee = EthTxForwarder::total_payment((&t).into()); - let _ = Balances::deposit_creating(&alice.address, fee.as_u64()); - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_eip1559_transaction_with_valid_signature() { - let alice = address_build(1); - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let t = EIP1559Transaction { - chain_id: 0, - nonce: U256::zero(), - max_priority_fee_per_gas: U256::zero(), - max_fee_per_gas: U256::zero(), - gas_limit: U256::from(1_000_000), - action: ethereum::TransactionAction::Create, - value: U256::zero(), - input: array_bytes::hex2bytes_unchecked(ERC20_CONTRACT_BYTECODE), - access_list: vec![], - // copied from: - // https://github.com/rust-ethereum/ethereum/blob/24739cc8ba6e9d8ee30ada8ec92161e4c48d578e/src/transaction.rs#L873-L875 - odd_y_parity: false, - // 36b241b061a36a32ab7fe86c7aa9eb592dd59018cd0443adc0903590c16b02b0 - r: H256([ - 54, 178, 65, 176, 97, 163, 106, 50, 171, 127, 232, 108, 122, 169, 235, 89, 45, - 213, 144, 24, 205, 4, 67, 173, 192, 144, 53, 144, 193, 107, 2, 176, - ]), - // 5edcc541b4741c5cc6dd347c5ed9577ef293a62787b4510465fadbfe39ee4094 - s: H256([ - 54, 178, 65, 176, 97, 163, 106, 50, 171, 127, 232, 108, 122, 169, 235, 89, 45, - 213, 144, 24, 205, 4, 67, 173, 192, 144, 53, 144, 193, 107, 2, 176, - ]), - }; - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(Transaction::EIP1559(t)) - )); - - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} diff --git a/pallet/ethtx-forwarder/src/tests/eip2930.rs b/pallet/ethtx-forwarder/src/tests/eip2930.rs deleted file mode 100644 index b686dc93c..000000000 --- a/pallet/ethtx-forwarder/src/tests/eip2930.rs +++ /dev/null @@ -1,175 +0,0 @@ -// This file is part of Darwinia. -// -// Copyright (C) Darwinia Network -// SPDX-License-Identifier: GPL-3.0 -// -// Darwinia is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Darwinia is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Darwinia. If not, see . - -// darwinia -use crate::{mock::*, tests::*, ForwardEthOrigin}; -use ethereum::EIP2930Transaction; -// frontier -use fp_evm::FeeCalculator; -// substrate -use frame_support::{assert_err, assert_ok, traits::Currency}; -use sp_core::U256; -use sp_runtime::{DispatchError, ModuleError}; - -fn eip2930_erc20_creation_unsigned_transaction() -> EIP2930UnsignedTransaction { - EIP2930UnsignedTransaction { - nonce: U256::zero(), - gas_price: U256::from(1), - gas_limit: U256::from(1_000_000), - action: ethereum::TransactionAction::Create, - value: U256::zero(), - input: array_bytes::hex2bytes_unchecked(ERC20_CONTRACT_BYTECODE), - } -} - -#[test] -fn test_eip2930_transaction_works() { - let alice = address_build(1); - - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let unsigned_tx = eip2930_erc20_creation_unsigned_transaction(); - let t = unsigned_tx.sign(&alice.private_key, None); - - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_eip2930_transaction_with_auto_nonce() { - let alice = address_build(1); - - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let mut unsigned_tx = eip2930_erc20_creation_unsigned_transaction(); - unsigned_tx.nonce = U256::MAX; - let t = unsigned_tx.sign(&alice.private_key, None); - - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_eip2930_transaction_with_auto_gas_price() { - let alice = address_build(1); - - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let mut unsigned_tx = eip2930_erc20_creation_unsigned_transaction(); - unsigned_tx.gas_price = - ::FeeCalculator::min_gas_price().0 - 1; - let t = unsigned_tx.sign(&alice.private_key, None); - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_eip2930_transaction_with_sufficient_balance() { - let alice = address_build(1); - ExtBuilder::default().build().execute_with(|| { - let unsigned_tx = eip2930_erc20_creation_unsigned_transaction(); - let t = unsigned_tx.sign(&alice.private_key, None); - - assert_err!( - EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t.clone()) - ), - DispatchError::Module(ModuleError { - index: 5, - error: [0, 4, 0, 0], - message: Some("MessageTransactError",) - }) - ); - - let fee = EthTxForwarder::total_payment((&t).into()); - let _ = Balances::deposit_creating(&alice.address, fee.as_u64()); - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_eip2930_transaction_with_valid_signature() { - let alice = address_build(1); - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let t = EIP2930Transaction { - chain_id: 0, - nonce: U256::zero(), - gas_price: U256::from(1), - gas_limit: U256::from(1_000_000), - action: ethereum::TransactionAction::Create, - value: U256::zero(), - input: array_bytes::hex2bytes_unchecked(ERC20_CONTRACT_BYTECODE), - access_list: vec![], - // copied from: - // https://github.com/rust-ethereum/ethereum/blob/24739cc8ba6e9d8ee30ada8ec92161e4c48d578e/src/transaction.rs#L873-L875 - odd_y_parity: false, - // 36b241b061a36a32ab7fe86c7aa9eb592dd59018cd0443adc0903590c16b02b0 - r: H256([ - 54, 178, 65, 176, 97, 163, 106, 50, 171, 127, 232, 108, 122, 169, 235, 89, 45, - 213, 144, 24, 205, 4, 67, 173, 192, 144, 53, 144, 193, 107, 2, 176, - ]), - // 5edcc541b4741c5cc6dd347c5ed9577ef293a62787b4510465fadbfe39ee4094 - s: H256([ - 54, 178, 65, 176, 97, 163, 106, 50, 171, 127, 232, 108, 122, 169, 235, 89, 45, - 213, 144, 24, 205, 4, 67, 173, 192, 144, 53, 144, 193, 107, 2, 176, - ]), - }; - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(Transaction::EIP2930(t)) - )); - - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} diff --git a/pallet/ethtx-forwarder/src/tests/legacy.rs b/pallet/ethtx-forwarder/src/tests/legacy.rs deleted file mode 100644 index 2fe15ecdd..000000000 --- a/pallet/ethtx-forwarder/src/tests/legacy.rs +++ /dev/null @@ -1,167 +0,0 @@ -// This file is part of Darwinia. -// -// Copyright (C) Darwinia Network -// SPDX-License-Identifier: GPL-3.0 -// -// Darwinia is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Darwinia is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Darwinia. If not, see . - -// darwinia -use crate::{mock::*, tests::*, ForwardEthOrigin}; -use ethereum::LegacyTransaction; -// frontier -use fp_evm::FeeCalculator; -// substrate -use frame_support::{assert_err, assert_ok, traits::Currency}; -use sp_core::U256; -use sp_runtime::{DispatchError, ModuleError}; - -pub fn legacy_erc20_creation_unsigned_transaction() -> LegacyUnsignedTransaction { - LegacyUnsignedTransaction { - nonce: U256::zero(), - gas_price: U256::from(1), - gas_limit: U256::from(1_000_000), - action: ethereum::TransactionAction::Create, - value: U256::zero(), - input: array_bytes::hex2bytes_unchecked(ERC20_CONTRACT_BYTECODE), - } -} - -#[test] -fn test_legacy_transaction_works() { - let alice = address_build(1); - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let t = legacy_erc20_creation_unsigned_transaction().sign(&alice.private_key); - - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_legacy_transaction_with_auto_nonce() { - let alice = address_build(1); - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let mut unsigned_tx = legacy_erc20_creation_unsigned_transaction(); - unsigned_tx.nonce = U256::MAX; - let t = unsigned_tx.sign(&alice.private_key); - - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_legacy_transaction_with_auto_gas_price() { - let alice = address_build(1); - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let mut unsigned_tx = legacy_erc20_creation_unsigned_transaction(); - unsigned_tx.gas_price = - ::FeeCalculator::min_gas_price().0 - 1; - let t = unsigned_tx.sign(&alice.private_key); - - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_legacy_transaction_with_sufficient_balance() { - let alice = address_build(1); - ExtBuilder::default().build().execute_with(|| { - let t = legacy_erc20_creation_unsigned_transaction().sign(&alice.private_key); - assert_err!( - EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t.clone()) - ), - DispatchError::Module(ModuleError { - index: 5, - error: [0, 4, 0, 0], - message: Some("MessageTransactError",) - }) - ); - - let fee = EthTxForwarder::total_payment((&t).into()); - let _ = Balances::deposit_creating(&alice.address, fee.as_u64()); - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(t) - )); - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} - -#[test] -fn test_legacy_transaction_with_valid_signature() { - let alice = address_build(1); - ExtBuilder::default() - .with_balances(vec![(alice.address, 1_000_000_000_000)]) - .build() - .execute_with(|| { - let t = LegacyTransaction { - nonce: U256::zero(), - gas_price: U256::from(1), - gas_limit: U256::from(1_000_000), - action: ethereum::TransactionAction::Create, - value: U256::zero(), - input: array_bytes::hex2bytes_unchecked(ERC20_CONTRACT_BYTECODE), - signature: TransactionSignature::new( - 38, - H256([ - 190, 103, 224, 160, 125, 182, 125, 168, 212, 70, 247, 106, 221, 89, 14, 84, - 182, 233, 44, 182, 184, 249, 131, 90, 235, 103, 84, 5, 121, 162, 119, 23, - ]), - H256([ - 45, 105, 5, 22, 81, 32, 32, 23, 28, 30, 200, 112, 246, 255, 69, 57, 140, - 200, 96, 146, 80, 50, 107, 232, 153, 21, 251, 83, 142, 123, 215, 24, - ]), - ) - .unwrap(), - }; - assert_ok!(EthTxForwarder::forward_transact( - ForwardEthOrigin::ForwardEth(alice.address).into(), - Box::new(Transaction::Legacy(t)) - )); - - assert!(System::events() - .iter() - .any(|record| matches!(record.event, RuntimeEvent::Ethereum(..)))); - }); -} diff --git a/pallet/staking/src/lib.rs b/pallet/staking/src/lib.rs index 68927caa6..4f9ed82d1 100644 --- a/pallet/staking/src/lib.rs +++ b/pallet/staking/src/lib.rs @@ -52,18 +52,16 @@ use core::mem; // crates.io use codec::FullCodec; use ethabi::{Function, Param, ParamType, StateMutability, Token}; -use ethereum::{ - LegacyTransaction, TransactionAction, TransactionSignature, TransactionV2 as Transaction, -}; +use ethereum::TransactionAction; // darwinia -use darwinia_ethtx_forwarder::ForwardEthOrigin; +use darwinia_ethtx_forwarder::{ForwardEthOrigin, ForwardRequest, TxType}; use dc_types::{Balance, Moment}; // substrate use frame_support::{ pallet_prelude::*, traits::Currency, DefaultNoBound, EqNoBound, PalletId, PartialEqNoBound, }; use frame_system::{pallet_prelude::*, RawOrigin}; -use sp_core::{H160, H256, U256}; +use sp_core::{H160, U256}; use sp_runtime::{ traits::{AccountIdConversion, Convert, One, Zero}, Perbill, Perquintill, @@ -1283,13 +1281,6 @@ where ::AccountId: Into, { fn notify(amount: Balance) { - let Some(signature) = mock_sig() else { - log::error!( - "[pallet::staking] Invalid mock signature for the staking notify transaction." - ); - - return; - }; // KTONStakingRewards // 0x000000000419683a1a03AbC21FC9da25fd2B4dD7 let staking_reward = @@ -1317,17 +1308,14 @@ where state_mutability: StateMutability::Payable, }; - let notify_transaction = LegacyTransaction { - nonce: U256::zero(), // Will be reset in the message transact call - gas_price: U256::zero(), // Will be reset in the message transact call - gas_limit: U256::from(1_000_000), /* It should be big enough for the evm - * transaction, otherwise it will out of gas. */ + let request = ForwardRequest { + tx_type: TxType::LegacyTransaction, action: TransactionAction::Call(reward_distr), value: U256::zero(), input: function .encode_input(&[Token::Address(staking_reward), Token::Uint(amount.into())]) .unwrap_or_default(), - signature, + gas_limit: U256::from(1_000_000), }; // Treasury account. let sender = H160([ @@ -1336,26 +1324,9 @@ where if let Err(e) = >::forward_transact( ForwardEthOrigin::ForwardEth(sender).into(), - Box::new(Transaction::Legacy(notify_transaction)), + request, ) { log::error!("[pallet::staking] failed to notify KTON staker contract due to {e:?}"); } } } -/// Mock a valid signature, copied from: -/// https://github.com/rust-ethereum/ethereum/blob/master/src/transaction/mod.rs#L230 -pub fn mock_sig() -> Option { - TransactionSignature::new( - 38, - // be67e0a07db67da8d446f76add590e54b6e92cb6b8f9835aeb67540579a27717 - H256([ - 190, 103, 224, 160, 125, 182, 125, 168, 212, 70, 247, 106, 221, 89, 14, 84, 182, 233, - 44, 182, 184, 249, 131, 90, 235, 103, 84, 5, 121, 162, 119, 23, - ]), - // 2d690516512020171c1ec870f6ff45398cc8609250326be89915fb538e7bd718 - H256([ - 45, 105, 5, 22, 81, 32, 32, 23, 28, 30, 200, 112, 246, 255, 69, 57, 140, 200, 96, 146, - 80, 50, 107, 232, 153, 21, 251, 83, 142, 123, 215, 24, - ]), - ) -} diff --git a/pallet/staking/src/tests.rs b/pallet/staking/src/tests.rs index 50b973e1f..eaf893d64 100644 --- a/pallet/staking/src/tests.rs +++ b/pallet/staking/src/tests.rs @@ -890,8 +890,3 @@ fn migration_curves_should_work() { ); }); } - -#[test] -fn mock_sig_should_work() { - assert!(mock_sig().is_some()); -}