Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable2409] Backport #6323 #6472

Closed
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
51 changes: 51 additions & 0 deletions bridges/bin/runtime-common/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ macro_rules! generate_bridge_reject_obsolete_headers_and_messages {
call: &Self::Call,
_info: &sp_runtime::traits::DispatchInfoOf<Self::Call>,
_len: usize,
<<<<<<< HEAD
) -> sp_runtime::transaction_validity::TransactionValidity {
let tx_validity = sp_runtime::transaction_validity::ValidTransaction::default();
let to_prepare = ();
Expand All @@ -317,6 +318,20 @@ macro_rules! generate_bridge_reject_obsolete_headers_and_messages {
)*
Ok(tx_validity)
}
=======
_self_implicit: Self::Implicit,
_inherited_implication: &impl codec::Encode,
_source: sp_runtime::transaction_validity::TransactionSource,
) -> Result<
(
sp_runtime::transaction_validity::ValidTransaction,
Self::Val,
<$call as sp_runtime::traits::Dispatchable>::RuntimeOrigin,
), sp_runtime::transaction_validity::TransactionValidityError
> {
use $crate::extensions::__private::tuplex::PushBack;
use sp_runtime::traits::AsSystemOriginSigner;
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))

#[allow(unused_variables)]
fn pre_dispatch(
Expand Down Expand Up @@ -384,8 +399,18 @@ mod tests {
use pallet_bridge_grandpa::{Call as GrandpaCall, StoredAuthoritySet};
use pallet_bridge_parachains::Call as ParachainsCall;
use sp_runtime::{
<<<<<<< HEAD
traits::{parameter_types, ConstU64, Header as _, SignedExtension},
transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction},
=======
traits::{
parameter_types, AsSystemOriginSigner, AsTransactionAuthorizedOrigin, ConstU64,
DispatchTransaction, Header as _, TransactionExtension,
},
transaction_validity::{
InvalidTransaction, TransactionSource::External, TransactionValidity, ValidTransaction,
},
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
DispatchError,
};

Expand Down Expand Up @@ -579,7 +604,17 @@ mod tests {

run_test(|| {
assert_err!(
<<<<<<< HEAD
BridgeRejectObsoleteHeadersAndMessages.validate(&42, &MockCall { data: 1 }, &(), 0),
=======
BridgeRejectObsoleteHeadersAndMessages.validate_only(
42u64.into(),
&MockCall { data: 1 },
&(),
0,
External,
),
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
InvalidTransaction::Custom(1)
);
assert_err!(
Expand All @@ -593,7 +628,17 @@ mod tests {
);

assert_err!(
<<<<<<< HEAD
BridgeRejectObsoleteHeadersAndMessages.validate(&42, &MockCall { data: 2 }, &(), 0),
=======
BridgeRejectObsoleteHeadersAndMessages.validate_only(
42u64.into(),
&MockCall { data: 2 },
&(),
0,
External,
),
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
InvalidTransaction::Custom(2)
);
assert_err!(
Expand All @@ -608,8 +653,14 @@ mod tests {

assert_eq!(
BridgeRejectObsoleteHeadersAndMessages
<<<<<<< HEAD
.validate(&42, &MockCall { data: 3 }, &(), 0)
.unwrap(),
=======
.validate_only(42u64.into(), &MockCall { data: 3 }, &(), 0, External)
.unwrap()
.0,
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
ValidTransaction { priority: 3, ..Default::default() },
);
assert_eq!(
Expand Down
65 changes: 65 additions & 0 deletions bridges/modules/relayers/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ use bp_runtime::{Chain, RangeInclusiveExt, StaticStrProvider};
use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchInfo, PostDispatchInfo},
<<<<<<< HEAD
=======
pallet_prelude::TransactionSource,
weights::Weight,
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
CloneNoBound, DefaultNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
};
use frame_system::Config as SystemConfig;
Expand Down Expand Up @@ -302,13 +307,27 @@ where
call: &Self::Call,
_info: &DispatchInfoOf<Self::Call>,
_len: usize,
<<<<<<< HEAD
) -> TransactionValidity {
// this is the only relevant line of code for the `pre_dispatch`
//
// we're not calling `validate` from `pre_dispatch` directly because of performance
// reasons, so if you're adding some code that may fail here, please check if it needs
// to be added to the `pre_dispatch` as well
let parsed_call = C::parse_and_check_for_obsolete_call(call)?;
=======
_self_implicit: Self::Implicit,
_inherited_implication: &impl Encode,
_source: TransactionSource,
) -> ValidateResult<Self::Val, R::RuntimeCall> {
// Prepare relevant data for `prepare`
let parsed_call = match C::parse_and_check_for_obsolete_call(call)? {
Some(parsed_call) => parsed_call,
None => return Ok((Default::default(), None, origin)),
};
// Those calls are only for signed transactions.
let relayer = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))

// the following code just plays with transaction priority and never returns an error

Expand Down Expand Up @@ -463,8 +482,15 @@ mod tests {
use pallet_bridge_parachains::{Call as ParachainsCall, Pallet as ParachainsPallet};
use pallet_utility::Call as UtilityCall;
use sp_runtime::{
<<<<<<< HEAD
traits::{ConstU64, Header as HeaderT},
transaction_validity::{InvalidTransaction, ValidTransaction},
=======
traits::{ConstU64, DispatchTransaction, Header as HeaderT},
transaction_validity::{
InvalidTransaction, TransactionSource::External, TransactionValidity, ValidTransaction,
},
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
DispatchError,
};

Expand Down Expand Up @@ -1067,6 +1093,7 @@ mod tests {
}

fn run_validate(call: RuntimeCall) -> TransactionValidity {
<<<<<<< HEAD
let extension: TestExtension = BridgeRelayersSignedExtension(PhantomData);
extension.validate(&relayer_account_at_this_chain(), &call, &DispatchInfo::default(), 0)
}
Expand All @@ -1079,6 +1106,44 @@ mod tests {
fn run_messages_validate(call: RuntimeCall) -> TransactionValidity {
let extension: TestMessagesExtension = BridgeRelayersSignedExtension(PhantomData);
extension.validate(&relayer_account_at_this_chain(), &call, &DispatchInfo::default(), 0)
=======
let extension: TestExtension = BridgeRelayersTransactionExtension(PhantomData);
extension
.validate_only(
Some(relayer_account_at_this_chain()).into(),
&call,
&DispatchInfo::default(),
0,
External,
)
.map(|t| t.0)
}

fn run_grandpa_validate(call: RuntimeCall) -> TransactionValidity {
let extension: TestGrandpaExtension = BridgeRelayersTransactionExtension(PhantomData);
extension
.validate_only(
Some(relayer_account_at_this_chain()).into(),
&call,
&DispatchInfo::default(),
0,
External,
)
.map(|t| t.0)
}

fn run_messages_validate(call: RuntimeCall) -> TransactionValidity {
let extension: TestMessagesExtension = BridgeRelayersTransactionExtension(PhantomData);
extension
.validate_only(
Some(relayer_account_at_this_chain()).into(),
&call,
&DispatchInfo::default(),
0,
External,
)
.map(|t| t.0)
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
}

fn ignore_priority(tx: TransactionValidity) -> TransactionValidity {
Expand Down
34 changes: 33 additions & 1 deletion polkadot/runtime/common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ use sp_io::{crypto::secp256k1_ecdsa_recover, hashing::keccak_256};
use sp_runtime::{
traits::{CheckedSub, DispatchInfoOf, SignedExtension, Zero},
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction,
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
ValidTransaction,
},
RuntimeDebug,
};
Expand Down Expand Up @@ -650,6 +651,7 @@ where
call: &Self::Call,
_info: &DispatchInfoOf<Self::Call>,
_len: usize,
<<<<<<< HEAD
) -> TransactionValidity {
if let Some(local_call) = call.is_sub_type() {
if let Call::attest { statement: attested_statement } = local_call {
Expand All @@ -659,6 +661,22 @@ where
let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
ensure!(&attested_statement[..] == s.to_text(), e);
}
=======
_self_implicit: Self::Implicit,
_inherited_implication: &impl Encode,
_source: TransactionSource,
) -> Result<
(ValidTransaction, Self::Val, <T::RuntimeCall as Dispatchable>::RuntimeOrigin),
TransactionValidityError,
> {
if let Some(Call::attest { statement: attested_statement }) = call.is_sub_type() {
let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
let signer = Preclaims::<T>::get(who)
.ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?;
if let Some(s) = Signing::<T>::get(signer) {
let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
ensure!(&attested_statement[..] == s.to_text(), e);
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
}
}
Ok(ValidTransaction::default())
Expand Down Expand Up @@ -699,6 +717,7 @@ mod tests {
use super::*;
use hex_literal::hex;
use secp_utils::*;
use sp_runtime::transaction_validity::TransactionSource::External;

use codec::Encode;
// The testing primitives are very useful for avoiding having to work with signatures
Expand Down Expand Up @@ -1055,8 +1074,13 @@ mod tests {
});
let di = c.get_dispatch_info();
assert_eq!(di.pays_fee, Pays::No);
<<<<<<< HEAD
let r = p.validate(&42, &c, &di, 20);
assert_eq!(r, TransactionValidity::Ok(ValidTransaction::default()));
=======
let r = p.validate_only(Some(42).into(), &c, &di, 20, External);
assert_eq!(r.unwrap().0, ValidTransaction::default());
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
});
}

Expand All @@ -1068,13 +1092,21 @@ mod tests {
statement: StatementKind::Regular.to_text().to_vec(),
});
let di = c.get_dispatch_info();
<<<<<<< HEAD
let r = p.validate(&42, &c, &di, 20);
=======
let r = p.validate_only(Some(42).into(), &c, &di, 20, External);
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
assert!(r.is_err());
let c = RuntimeCall::Claims(ClaimsCall::attest {
statement: StatementKind::Saft.to_text().to_vec(),
});
let di = c.get_dispatch_info();
<<<<<<< HEAD
let r = p.validate(&69, &c, &di, 20);
=======
let r = p.validate_only(Some(69).into(), &c, &di, 20, External);
>>>>>>> 8e3d9296 ([Tx ext stage 2: 1/4] Add `TransactionSource` as argument in `TransactionExtension::validate` (#6323))
assert!(r.is_err());
});
}
Expand Down
32 changes: 32 additions & 0 deletions prdoc/pr_6323.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
title: add `TransactionSource` to `TransactionExtension::validate`
doc:
- audience: Runtime Dev
description: |
Add a the source of the extrinsic as an argument in `TransactionExtension::validate`.
The transaction source can be useful for transactions that should only be valid if it comes from the node. For example from offchain worker.
To update the current code. The transaction source can simply be ignored: `_source: TransactionSource`


crates:
- name: sp-runtime
bump: major
- name: bridge-runtime-common
bump: patch
- name: frame-system
bump: patch
- name: pallet-transaction-payment
bump: patch
- name: polkadot-runtime-common
bump: patch
- name: pallet-sudo
bump: patch
- name: pallet-verify-signature
bump: patch
- name: pallet-asset-tx-payment
bump: patch
- name: pallet-bridge-relayers
bump: patch
- name: pallet-asset-conversion-tx-payment
bump: patch
- name: pallet-skip-feeless-payment
bump: patch
Loading
Loading