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

[Tx ext stage 2: 2/4] Introduce #[pallet::authorize(...)] macro attribute and AuthorizeCall system transaction extension #6324

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cfe8667
add source to transaction extension
gui1117 Nov 1, 2024
f84192b
impl authorize
gui1117 Nov 1, 2024
f14e5ca
fix
gui1117 Nov 1, 2024
16d691b
prdoc
gui1117 Nov 1, 2024
ab46f1b
fmt
gui1117 Nov 1, 2024
93d9ed3
fmt
gui1117 Nov 1, 2024
241817e
Merge branch 'gui-tx-ext-stage-2-part-1' into gui-tx-ext-stage-2-part-2
gui1117 Nov 1, 2024
de1d4f4
improve code gen
gui1117 Nov 1, 2024
baa6fc5
prdoc
gui1117 Nov 20, 2024
ffe33de
apply suggestions
gui1117 Nov 20, 2024
461059a
Merge remote-tracking branch 'origin/master' into gui-tx-ext-stage-2-…
gui1117 Nov 20, 2024
7862970
use AsTransctionAuthorizedOrigin::is_transaction_authorized
gui1117 Nov 20, 2024
3dd50ca
test for call without authorization
gui1117 Nov 20, 2024
ec1b263
do not force std in test
gui1117 Nov 20, 2024
25691b8
add authorize call tx ext everywhere
gui1117 Nov 20, 2024
acd4282
update doc + example weight is zero
gui1117 Nov 22, 2024
6b65245
fmt
gui1117 Dec 2, 2024
71ca562
Merge remote-tracking branch 'origin/master' into gui-tx-ext-stage-2-…
gui1117 Dec 4, 2024
743835c
do not leak runtime-benchmark feature in dev build
gui1117 Dec 4, 2024
7d08985
minor
gui1117 Dec 4, 2024
83873c6
taplo
gui1117 Dec 4, 2024
f62152a
cannot link to dev dependencies
gui1117 Dec 4, 2024
33646e7
clippy
gui1117 Dec 4, 2024
2bc6d0c
fix tests and doc
gui1117 Dec 5, 2024
62c9e6e
Merge branch 'master' into gui-tx-ext-stage-2-part-2
gui1117 Dec 5, 2024
d040d4f
rustdoc
gui1117 Dec 5, 2024
e538e29
rustdoc
gui1117 Dec 5, 2024
3a757e8
fix span for split feature
gui1117 Dec 5, 2024
0946d79
Update from gui1117 running command 'update-ui'
actions-user Dec 5, 2024
d60e170
keep same error message as before
gui1117 Dec 5, 2024
f9cde01
fix test
gui1117 Dec 5, 2024
2c905e3
prdoc bump versions
gui1117 Dec 5, 2024
4bc94dd
Update from gui1117 running command 'update-ui'
actions-user Dec 5, 2024
a4452bc
Merge branch 'master' into gui-tx-ext-stage-2-part-2
gui1117 Dec 5, 2024
1566519
Merge branch 'master' into gui-tx-ext-stage-2-part-2
gui1117 Dec 5, 2024
2e756bc
ui test
gui1117 Dec 5, 2024
c002f10
remove non deterministic UI test
gui1117 Dec 6, 2024
36620e1
Update substrate/frame/support/src/traits/tasks.rs
gui1117 Dec 8, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions bridges/bin/runtime-common/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ macro_rules! generate_bridge_reject_obsolete_headers_and_messages {
_len: usize,
_self_implicit: Self::Implicit,
_inherited_implication: &impl codec::Encode,
_source: sp_runtime::transaction_validity::TransactionSource,
) -> Result<
(
sp_runtime::transaction_validity::ValidTransaction,
Expand Down Expand Up @@ -390,7 +391,9 @@ mod tests {
parameter_types, AsSystemOriginSigner, AsTransactionAuthorizedOrigin, ConstU64,
DispatchTransaction, Header as _, TransactionExtension,
},
transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction},
transaction_validity::{
InvalidTransaction, TransactionSource::External, TransactionValidity, ValidTransaction,
},
DispatchError,
};

Expand Down Expand Up @@ -610,7 +613,8 @@ mod tests {
42u64.into(),
&MockCall { data: 1 },
&(),
0
0,
External,
),
InvalidTransaction::Custom(1)
);
Expand All @@ -629,7 +633,8 @@ mod tests {
42u64.into(),
&MockCall { data: 2 },
&(),
0
0,
External,
),
InvalidTransaction::Custom(2)
);
Expand All @@ -645,7 +650,7 @@ mod tests {

assert_eq!(
BridgeRejectObsoleteHeadersAndMessages
.validate_only(42u64.into(), &MockCall { data: 3 }, &(), 0)
.validate_only(42u64.into(), &MockCall { data: 3 }, &(), 0, External)
.unwrap()
.0,
ValidTransaction { priority: 3, ..Default::default() },
Expand Down
9 changes: 8 additions & 1 deletion bridges/modules/relayers/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use bp_runtime::{Chain, RangeInclusiveExt, StaticStrProvider};
use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchInfo, PostDispatchInfo},
pallet_prelude::TransactionSource,
weights::Weight,
CloneNoBound, DefaultNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
};
Expand Down Expand Up @@ -304,6 +305,7 @@ where
_len: usize,
_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)? {
Expand Down Expand Up @@ -463,7 +465,9 @@ mod tests {
use pallet_utility::Call as UtilityCall;
use sp_runtime::{
traits::{ConstU64, DispatchTransaction, Header as HeaderT},
transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction},
transaction_validity::{
InvalidTransaction, TransactionSource::External, TransactionValidity, ValidTransaction,
},
DispatchError,
};

Expand Down Expand Up @@ -1076,6 +1080,7 @@ mod tests {
&call,
&DispatchInfo::default(),
0,
External,
)
.map(|t| t.0)
}
Expand All @@ -1088,6 +1093,7 @@ mod tests {
&call,
&DispatchInfo::default(),
0,
External,
)
.map(|t| t.0)
}
Expand All @@ -1100,6 +1106,7 @@ mod tests {
&call,
&DispatchInfo::default(),
0,
External,
)
.map(|t| t.0)
}
Expand Down
11 changes: 7 additions & 4 deletions polkadot/runtime/common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ use sp_runtime::{
Dispatchable, TransactionExtension, Zero,
},
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction,
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
ValidTransaction,
},
RuntimeDebug,
};
Expand Down Expand Up @@ -652,6 +653,7 @@ where
_len: usize,
_self_implicit: Self::Implicit,
_inherited_implication: &impl Encode,
_source: TransactionSource,
) -> Result<
(ValidTransaction, Self::Val, <T::RuntimeCall as Dispatchable>::RuntimeOrigin),
TransactionValidityError,
Expand Down Expand Up @@ -705,6 +707,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 @@ -1064,7 +1067,7 @@ mod tests {
});
let di = c.get_dispatch_info();
assert_eq!(di.pays_fee, Pays::No);
let r = p.validate_only(Some(42).into(), &c, &di, 20);
let r = p.validate_only(Some(42).into(), &c, &di, 20, External);
assert_eq!(r.unwrap().0, ValidTransaction::default());
});
}
Expand All @@ -1077,13 +1080,13 @@ mod tests {
statement: StatementKind::Regular.to_text().to_vec(),
});
let di = c.get_dispatch_info();
let r = p.validate_only(Some(42).into(), &c, &di, 20);
let r = p.validate_only(Some(42).into(), &c, &di, 20, External);
assert!(r.is_err());
let c = RuntimeCall::Claims(ClaimsCall::attest {
statement: StatementKind::Saft.to_text().to_vec(),
});
let di = c.get_dispatch_info();
let r = p.validate_only(Some(69).into(), &c, &di, 20);
let r = p.validate_only(Some(69).into(), &c, &di, 20, External);
assert!(r.is_err());
});
}
Expand Down
19 changes: 19 additions & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,25 @@ where
}
}

impl<LocalCall> frame_system::offchain::CreateAuthorizedTransaction<LocalCall> for Runtime
where
RuntimeCall: From<LocalCall>,
{
fn create_extension() -> Self::Extension {
(
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckMortality::<Runtime>::from(generic::Era::Immortal),
frame_system::CheckNonce::<Runtime>::from(0),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
frame_metadata_hash_extension::CheckMetadataHash::new(false),
)
}
}

parameter_types! {
pub Prefix: &'static [u8] = b"Pay ROCs to the Rococo account:";
}
Expand Down
18 changes: 18 additions & 0 deletions polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ where
}
}

impl<C> frame_system::offchain::CreateAuthorizedTransaction<C> for Runtime
where
RuntimeCall: From<C>,
{
fn create_extension() -> Self::Extension {
(
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckMortality::<Runtime>::from(generic::Era::Immortal),
frame_system::CheckNonce::<Runtime>::from(0),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
)
}
}

parameter_types! {
pub storage EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS as u64;
pub storage ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
Expand Down
19 changes: 19 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,25 @@ where
}
}

impl<LocalCall> frame_system::offchain::CreateAuthorizedTransaction<LocalCall> for Runtime
where
RuntimeCall: From<LocalCall>,
{
fn create_extension() -> Self::Extension {
(
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckMortality::<Runtime>::from(generic::Era::Immortal),
frame_system::CheckNonce::<Runtime>::from(0),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
frame_metadata_hash_extension::CheckMetadataHash::<Runtime>::new(false),
)
}
}

parameter_types! {
// Minimum 100 bytes/KSM deposited (1 CENT/byte)
pub const BasicDeposit: Balance = 1000 * CENTS; // 258 bytes on-chain
Expand Down
12 changes: 12 additions & 0 deletions prdoc/pr_6323.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title: add `TransactionSource` to `TransactionExtension::validate`
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
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
21 changes: 21 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,27 @@ where
type RuntimeCall = RuntimeCall;
}

impl<C> frame_system::offchain::CreateAuthorizedTransaction<C> for Runtime
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
where
RuntimeCall: From<C>,
{
fn create_extension() -> Self::Extension {
(
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckEra::<Runtime>::from(Era::Immortal),
frame_system::CheckNonce::<Runtime>::from(0),
frame_system::CheckWeight::<Runtime>::new(),
pallet_skip_feeless_payment::SkipCheckIfFeeless::from(
pallet_asset_conversion_tx_payment::ChargeAssetTxPayment::<Runtime>::from(0, None),
),
frame_metadata_hash_extension::CheckMetadataHash::new(false),
)
}
}

impl pallet_im_online::Config for Runtime {
type AuthorityId = ImOnlineId;
type RuntimeEvent = RuntimeEvent;
Expand Down
10 changes: 9 additions & 1 deletion substrate/frame/benchmarking/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_io::hashing::blake2_256;
use sp_runtime::{traits::TrailingZeroInput, DispatchError};
use sp_runtime::{
traits::TrailingZeroInput, transaction_validity::TransactionValidityError, DispatchError,
};
use sp_storage::TrackedStorageKey;

/// An alphabet of possible parameters to use for benchmarking.
Expand Down Expand Up @@ -195,6 +197,12 @@ impl From<DispatchError> for BenchmarkError {
}
}

impl From<TransactionValidityError> for BenchmarkError {
fn from(e: TransactionValidityError) -> Self {
Self::Stop(e.into())
}
}

/// Configuration used to setup and run runtime benchmarks.
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug, TypeInfo)]
pub struct BenchmarkConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use core::{fmt, marker::PhantomData};

use codec::{Decode, Encode};
use frame_support::{traits::OriginTrait, Parameter};
use frame_support::{pallet_prelude::TransactionSource, traits::OriginTrait, Parameter};
use scale_info::TypeInfo;
use sp_runtime::{
impl_tx_ext_default,
Expand Down Expand Up @@ -94,6 +94,7 @@ where
_len: usize,
_self_implicit: Self::Implicit,
inherited_implication: &impl codec::Encode,
_source: TransactionSource,
) -> ValidateResult<Self::Val, T::RuntimeCall> {
// If the extension is inactive, just move on in the pipeline.
let Some(auth) = &self.inner else {
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/examples/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use codec::{Decode, Encode};
use core::marker::PhantomData;
use frame_support::{
dispatch::{ClassifyDispatch, DispatchClass, DispatchResult, Pays, PaysFee, WeighData},
pallet_prelude::TransactionSource,
traits::IsSubType,
weights::Weight,
};
Expand Down Expand Up @@ -508,6 +509,7 @@ where
len: usize,
_self_implicit: Self::Implicit,
_inherited_implication: &impl Encode,
_source: TransactionSource,
) -> ValidateResult<Self::Val, <T as frame_system::Config>::RuntimeCall> {
// if the transaction is too big, just drop it.
if len > 200 {
Expand Down
5 changes: 3 additions & 2 deletions substrate/frame/examples/basic/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use sp_core::H256;
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use sp_runtime::{
traits::{BlakeTwo256, DispatchTransaction, IdentityLookup},
transaction_validity::TransactionSource::External,
BuildStorage,
};
// Reexport crate as its pallet name for construct_runtime.
Expand Down Expand Up @@ -146,15 +147,15 @@ fn signed_ext_watch_dummy_works() {

assert_eq!(
WatchDummy::<Test>(PhantomData)
.validate_only(Some(1).into(), &call, &info, 150)
.validate_only(Some(1).into(), &call, &info, 150, External)
.unwrap()
.0
.priority,
u64::MAX,
);
assert_eq!(
WatchDummy::<Test>(PhantomData)
.validate_only(Some(1).into(), &call, &info, 250)
.validate_only(Some(1).into(), &call, &info, 250, External)
.unwrap_err(),
InvalidTransaction::ExhaustsResources.into(),
);
Expand Down
Loading
Loading