Skip to content

Commit

Permalink
Change SetEvmOrigin order (#2302)
Browse files Browse the repository at this point in the history
* fix origin using erc20 as fee token

* Change SetEvmOrigin order

* fix test

* comments
  • Loading branch information
zqhxuyuan authored Aug 2, 2022
1 parent 8f9ba11 commit 1d98cdb
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 14 deletions.
11 changes: 11 additions & 0 deletions modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,17 @@ impl<T: Config + Send + Sync> SignedExtension for SetEvmOrigin<T> {
Ok(())
}

fn validate(
&self,
who: &Self::AccountId,
_call: &Self::Call,
_info: &DispatchInfoOf<Self::Call>,
_len: usize,
) -> TransactionValidity {
ExtrinsicOrigin::<T>::set(Some(who.clone()));
Ok(ValidTransaction::default())
}

fn pre_dispatch(
self,
who: &Self::AccountId,
Expand Down
2 changes: 1 addition & 1 deletion node/e2e-tests/test-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ pub fn construct_extrinsic(
frame_system::CheckEra::<Runtime>::from(Era::mortal(period, current_block)),
runtime_common::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
module_evm::SetEvmOrigin::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
);
let raw_payload = runtime::SignedPayload::from_raw(
function,
Expand Down
9 changes: 6 additions & 3 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,8 @@ where
frame_system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
runtime_common::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
module_evm::SetEvmOrigin::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
);
let raw_payload = SignedPayload::new(call, extra)
.map_err(|e| {
Expand Down Expand Up @@ -1745,8 +1745,11 @@ pub type SignedExtra = (
frame_system::CheckEra<Runtime>,
runtime_common::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
module_transaction_payment::ChargeTransactionPayment<Runtime>,
// `SetEvmOrigin` needs ahead of `ChargeTransactionPayment`, we set origin in `SetEvmOrigin::validate()`, then
// `ChargeTransactionPayment::validate()` can process erc20 token transfer successfully in the case of using erc20
// as fee token.
module_evm::SetEvmOrigin<Runtime>,
module_transaction_payment::ChargeTransactionPayment<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic = AcalaUncheckedExtrinsic<
Expand Down Expand Up @@ -2195,7 +2198,7 @@ impl Convert<(Call, SignedExtra), Result<(EthereumTransactionMessage, SignedExtr
return Err(InvalidTransaction::Stale);
}

let (_, _, _, _, mortality, check_nonce, _, charge, ..) = extra.clone();
let (_, _, _, _, mortality, check_nonce, _, _, charge) = extra.clone();

if mortality != frame_system::CheckEra::from(sp_runtime::generic::Era::Immortal) {
// require immortal
Expand Down
6 changes: 5 additions & 1 deletion runtime/integration-tests/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use crate::setup::*;
use crate::stable_asset::enable_stable_asset;
use frame_support::weights::{DispatchClass, DispatchInfo, Pays, Weight};
use frame_support::weights::{DispatchClass, DispatchInfo, Pays, PostDispatchInfo, Weight};
use module_support::AggregatedSwapPath;
use sp_runtime::{
traits::{AccountIdConversion, SignedExtension, UniqueSaturatedInto},
Expand Down Expand Up @@ -104,6 +104,10 @@ pub const INFO: DispatchInfo = DispatchInfo {
class: DispatchClass::Normal,
pays_fee: Pays::Yes,
};
pub const POST_INFO: PostDispatchInfo = PostDispatchInfo {
actual_weight: Some(80),
pays_fee: Pays::Yes,
};

pub fn with_fee_currency_call(currency_id: CurrencyId) -> <Runtime as module_transaction_payment::Config>::Call {
let fee_call: <Runtime as module_transaction_payment::Config>::Call =
Expand Down
47 changes: 46 additions & 1 deletion runtime/integration-tests/src/stable_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::evm::alice_evm_addr;
use crate::payment::{with_fee_aggregated_path_call, with_fee_currency_call, with_fee_path_call, INFO};
use crate::payment::{with_fee_aggregated_path_call, with_fee_currency_call, with_fee_path_call, INFO, POST_INFO};
use crate::setup::*;
use module_aggregated_dex::SwapPath;
use module_support::{AggregatedSwapPath, ExchangeRate, Swap, SwapLimit, EVM as EVMTrait};
Expand Down Expand Up @@ -318,6 +318,50 @@ fn three_usd_pool_works() {
);
}

let set_evm_origin = module_evm::SetEvmOrigin::<Runtime>::new();
let pre = set_evm_origin
.clone()
.pre_dispatch(&AccountId::from(BOB), &with_fee_currency_call(usdc), &INFO, 50)
.unwrap();

let origin = <module_evm_bridge::EVMBridge<Runtime> as module_support::evm::EVMBridge<
AccountId,
Balance,
>>::get_origin();
assert_eq!(origin, Some(AccountId::from(BOB)));

assert_ok!(module_evm::SetEvmOrigin::<Runtime>::post_dispatch(
Some(pre),
&INFO,
&POST_INFO,
50,
&Ok(())
));
let origin = <module_evm_bridge::EVMBridge<Runtime> as module_support::evm::EVMBridge<
AccountId,
Balance,
>>::get_origin();
assert_eq!(origin, None);

// Origin is None, transfer erc20 failed.
assert_noop!(
<module_transaction_payment::ChargeTransactionPayment::<Runtime>>::from(0).validate(
&AccountId::from(BOB),
&with_fee_currency_call(usdc),
&INFO,
50
),
TransactionValidityError::Invalid(InvalidTransaction::Payment)
);

// set origin in SetEvmOrigin::validate() then transfer erc20 will success.
assert_ok!(set_evm_origin.validate(&AccountId::from(BOB), &with_fee_currency_call(usdc), &INFO, 50));
let origin = <module_evm_bridge::EVMBridge<Runtime> as module_support::evm::EVMBridge<
AccountId,
Balance,
>>::get_origin();
assert_eq!(origin, Some(AccountId::from(BOB)));

// USDC=Erc20(contract) or USDT=ForeignAsset(0) as fee token.
// before USDC/USDT enabled as fee pool, it works by direct swap.
assert_aggregated_dex_event(usdc, with_fee_currency_call(usdc), None);
Expand Down Expand Up @@ -495,6 +539,7 @@ fn assert_aggregated_dex_event(
with_fee_call: <Runtime as module_transaction_payment::Config>::Call,
len: Option<usize>,
) {
System::reset_events();
assert_ok!(
<module_transaction_payment::ChargeTransactionPayment::<Runtime>>::from(0).validate(
&AccountId::from(BOB),
Expand Down
9 changes: 6 additions & 3 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,8 @@ where
frame_system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
runtime_common::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
module_evm::SetEvmOrigin::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
);
let raw_payload = SignedPayload::new(call, extra)
.map_err(|e| {
Expand Down Expand Up @@ -1778,8 +1778,11 @@ pub type SignedExtra = (
frame_system::CheckEra<Runtime>,
runtime_common::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
module_transaction_payment::ChargeTransactionPayment<Runtime>,
// `SetEvmOrigin` needs ahead of `ChargeTransactionPayment`, we set origin in `SetEvmOrigin::validate()`, then
// `ChargeTransactionPayment::validate()` can process erc20 token transfer successfully in the case of using erc20
// as fee token.
module_evm::SetEvmOrigin<Runtime>,
module_transaction_payment::ChargeTransactionPayment<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic = AcalaUncheckedExtrinsic<
Expand Down Expand Up @@ -2230,7 +2233,7 @@ impl Convert<(Call, SignedExtra), Result<(EthereumTransactionMessage, SignedExtr
return Err(InvalidTransaction::Stale);
}

let (_, _, _, _, mortality, check_nonce, _, charge, ..) = extra.clone();
let (_, _, _, _, mortality, check_nonce, _, _, charge) = extra.clone();

if mortality != frame_system::CheckEra::from(sp_runtime::generic::Era::Immortal) {
// require immortal
Expand Down
13 changes: 8 additions & 5 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,8 @@ where
frame_system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
runtime_common::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
module_evm::SetEvmOrigin::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
);
let raw_payload = SignedPayload::new(call, extra)
.map_err(|e| {
Expand Down Expand Up @@ -1822,7 +1822,7 @@ impl Convert<(Call, SignedExtra), Result<(EthereumTransactionMessage, SignedExtr
return Err(InvalidTransaction::Stale);
}

let (_, _, _, _, mortality, check_nonce, _, charge, ..) = extra.clone();
let (_, _, _, _, mortality, check_nonce, _, _, charge) = extra.clone();

if mortality != frame_system::CheckEra::from(sp_runtime::generic::Era::Immortal) {
// require immortal
Expand Down Expand Up @@ -1899,8 +1899,11 @@ pub type SignedExtra = (
frame_system::CheckEra<Runtime>,
runtime_common::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
module_transaction_payment::ChargeTransactionPayment<Runtime>,
// `SetEvmOrigin` needs ahead of `ChargeTransactionPayment`, we set origin in `SetEvmOrigin::validate()`, then
// `ChargeTransactionPayment::validate()` can process erc20 token transfer successfully in the case of using erc20
// as fee token.
module_evm::SetEvmOrigin<Runtime>,
module_transaction_payment::ChargeTransactionPayment<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic = AcalaUncheckedExtrinsic<
Expand Down Expand Up @@ -2548,8 +2551,8 @@ mod tests {
frame_system::CheckEra::<Runtime>::from(generic::Era::Immortal),
runtime_common::CheckNonce::<Runtime>::from(3),
frame_system::CheckWeight::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
module_evm::SetEvmOrigin::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
);

let mut expected_extra = extra.clone();
Expand Down Expand Up @@ -2636,8 +2639,8 @@ mod tests {
frame_system::CheckEra::<Runtime>::from(generic::Era::Immortal),
runtime_common::CheckNonce::<Runtime>::from(0),
frame_system::CheckWeight::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
module_evm::SetEvmOrigin::<Runtime>::new(),
module_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
);

// correct payer signature
Expand Down

0 comments on commit 1d98cdb

Please sign in to comment.