FRAME: Create TransactionExtension as a replacement for SignedExtension (#2280)

Closes #2160

First part of [Extrinsic
Horizon](https://github.com/paritytech/polkadot-sdk/issues/2415)

Introduces a new trait `TransactionExtension` to replace
`SignedExtension`. Introduce the idea of transactions which obey the
runtime's extensions and have according Extension data (né Extra data)
yet do not have hard-coded signatures.

Deprecate the terminology of "Unsigned" when used for
transactions/extrinsics owing to there now being "proper" unsigned
transactions which obey the extension framework and "old-style" unsigned
which do not. Instead we have __*General*__ for the former and
__*Bare*__ for the latter. (Ultimately, the latter will be phased out as
a type of transaction, and Bare will only be used for Inherents.)

Types of extrinsic are now therefore:
- Bare (no hardcoded signature, no Extra data; used to be known as
"Unsigned")
- Bare transactions (deprecated): Gossiped, validated with
`ValidateUnsigned` (deprecated) and the `_bare_compat` bits of
`TransactionExtension` (deprecated).
  - Inherents: Not gossiped, validated with `ProvideInherent`.
- Extended (Extra data): Gossiped, validated via `TransactionExtension`.
  - Signed transactions (with a hardcoded signature).
  - General transactions (without a hardcoded signature).

`TransactionExtension` differs from `SignedExtension` because:
- A signature on the underlying transaction may validly not be present.
- It may alter the origin during validation.
- `pre_dispatch` is renamed to `prepare` and need not contain the checks
present in `validate`.
- `validate` and `prepare` is passed an `Origin` rather than a
`AccountId`.
- `validate` may pass arbitrary information into `prepare` via a new
user-specifiable type `Val`.
- `AdditionalSigned`/`additional_signed` is renamed to
`Implicit`/`implicit`. It is encoded *for the entire transaction* and
passed in to each extension as a new argument to `validate`. This
facilitates the ability of extensions to acts as underlying crypto.

There is a new `DispatchTransaction` trait which contains only default
function impls and is impl'ed for any `TransactionExtension` impler. It
provides several utility functions which reduce some of the tedium from
using `TransactionExtension` (indeed, none of its regular functions
should now need to be called directly).

Three transaction version discriminator ("versions") are now
permissible:
- 0b000000100: Bare (used to be called "Unsigned"): contains Signature
or Extra (extension data). After bare transactions are no longer
supported, this will strictly identify an Inherents only.
- 0b100000100: Old-school "Signed" Transaction: contains Signature and
Extra (extension data).
- 0b010000100: New-school "General" Transaction: contains Extra
(extension data), but no Signature.

For the New-school General Transaction, it becomes trivial for authors
to publish extensions to the mechanism for authorizing an Origin, e.g.
through new kinds of key-signing schemes, ZK proofs, pallet state,
mutations over pre-authenticated origins or any combination of the
above.

## Code Migration

### NOW: Getting it to build

Wrap your `SignedExtension`s in `AsTransactionExtension`. This should be
accompanied by renaming your aggregate type in line with the new
terminology. E.g. Before:

```rust
/// The SignedExtension to the basic transaction logic.
pub type SignedExtra = (
	/* snip */
	MySpecialSignedExtension,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
	generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
```

After:

```rust
/// The extension to the basic transaction logic.
pub type TxExtension = (
	/* snip */
	AsTransactionExtension<MySpecialSignedExtension>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
	generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
```

You'll also need to alter any transaction building logic to add a
`.into()` to make the conversion happen. E.g. Before:

```rust
fn construct_extrinsic(
		/* snip */
) -> UncheckedExtrinsic {
	let extra: SignedExtra = (
		/* snip */
		MySpecialSignedExtension::new(/* snip */),
	);
	let payload = SignedPayload::new(call.clone(), extra.clone()).unwrap();
	let signature = payload.using_encoded(|e| sender.sign(e));
	UncheckedExtrinsic::new_signed(
		/* snip */
		Signature::Sr25519(signature),
		extra,
	)
}
```

After:

```rust
fn construct_extrinsic(
		/* snip */
) -> UncheckedExtrinsic {
	let tx_ext: TxExtension = (
		/* snip */
		MySpecialSignedExtension::new(/* snip */).into(),
	);
	let payload = SignedPayload::new(call.clone(), tx_ext.clone()).unwrap();
	let signature = payload.using_encoded(|e| sender.sign(e));
	UncheckedExtrinsic::new_signed(
		/* snip */
		Signature::Sr25519(signature),
		tx_ext,
	)
}
```

### SOON: Migrating to `TransactionExtension`

Most `SignedExtension`s can be trivially converted to become a
`TransactionExtension`. There are a few things to know.

- Instead of a single trait like `SignedExtension`, you should now
implement two traits individually: `TransactionExtensionBase` and
`TransactionExtension`.
- Weights are now a thing and must be provided via the new function `fn
weight`.

#### `TransactionExtensionBase`

This trait takes care of anything which is not dependent on types
specific to your runtime, most notably `Call`.

- `AdditionalSigned`/`additional_signed` is renamed to
`Implicit`/`implicit`.
- Weight must be returned by implementing the `weight` function. If your
extension is associated with a pallet, you'll probably want to do this
via the pallet's existing benchmarking infrastructure.

#### `TransactionExtension`

Generally:
- `pre_dispatch` is now `prepare` and you *should not reexecute the
`validate` functionality in there*!
- You don't get an account ID any more; you get an origin instead. If
you need to presume an account ID, then you can use the trait function
`AsSystemOriginSigner::as_system_origin_signer`.
- You get an additional ticket, similar to `Pre`, called `Val`. This
defines data which is passed from `validate` into `prepare`. This is
important since you should not be duplicating logic from `validate` to
`prepare`, you need a way of passing your working from the former into
the latter. This is it.
- This trait takes two type parameters: `Call` and `Context`. `Call` is
the runtime call type which used to be an associated type; you can just
move it to become a type parameter for your trait impl. `Context` is not
currently used and you can safely implement over it as an unbounded
type.
- There's no `AccountId` associated type any more. Just remove it.

Regarding `validate`:
- You get three new parameters in `validate`; all can be ignored when
migrating from `SignedExtension`.
- `validate` returns a tuple on success; the second item in the tuple is
the new ticket type `Self::Val` which gets passed in to `prepare`. If
you use any information extracted during `validate` (off-chain and
on-chain, non-mutating) in `prepare` (on-chain, mutating) then you can
pass it through with this. For the tuple's last item, just return the
`origin` argument.

Regarding `prepare`:
- This is renamed from `pre_dispatch`, but there is one change:
- FUNCTIONALITY TO VALIDATE THE TRANSACTION NEED NOT BE DUPLICATED FROM
`validate`!!
- (This is different to `SignedExtension` which was required to run the
same checks in `pre_dispatch` as in `validate`.)

Regarding `post_dispatch`:
- Since there are no unsigned transactions handled by
`TransactionExtension`, `Pre` is always defined, so the first parameter
is `Self::Pre` rather than `Option<Self::Pre>`.

If you make use of `SignedExtension::validate_unsigned` or
`SignedExtension::pre_dispatch_unsigned`, then:
- Just use the regular versions of these functions instead.
- Have your logic execute in the case that the `origin` is `None`.
- Ensure your transaction creation logic creates a General Transaction
rather than a Bare Transaction; this means having to include all
`TransactionExtension`s' data.
- `ValidateUnsigned` can still be used (for now) if you need to be able
to construct transactions which contain none of the extension data,
however these will be phased out in stage 2 of the Transactions Horizon,
so you should consider moving to an extension-centric design.

## TODO

- [x] Introduce `CheckSignature` impl of `TransactionExtension` to
ensure it's possible to have crypto be done wholly in a
`TransactionExtension`.
- [x] Deprecate `SignedExtension` and move all uses in codebase to
`TransactionExtension`.
  - [x] `ChargeTransactionPayment`
  - [x] `DummyExtension`
  - [x] `ChargeAssetTxPayment` (asset-tx-payment)
  - [x] `ChargeAssetTxPayment` (asset-conversion-tx-payment)
  - [x] `CheckWeight`
  - [x] `CheckTxVersion`
  - [x] `CheckSpecVersion`
  - [x] `CheckNonce`
  - [x] `CheckNonZeroSender`
  - [x] `CheckMortality`
  - [x] `CheckGenesis`
  - [x] `CheckOnlySudoAccount`
  - [x] `WatchDummy`
  - [x] `PrevalidateAttests`
  - [x] `GenericSignedExtension`
  - [x] `SignedExtension` (chain-polkadot-bulletin)
  - [x] `RefundSignedExtensionAdapter`
- [x] Implement `fn weight` across the board.
- [ ] Go through all pre-existing extensions which assume an account
signer and explicitly handle the possibility of another kind of origin.
- [x] `CheckNonce` should probably succeed in the case of a non-account
origin.
- [x] `CheckNonZeroSender` should succeed in the case of a non-account
origin.
- [x] `ChargeTransactionPayment` and family should fail in the case of a
non-account origin.
  - [ ] 
- [x] Fix any broken tests.

---------

Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>
Co-authored-by: Nikhil Gupta <17176722+gupnik@users.noreply.github.com>
Co-authored-by: georgepisaltu <52418509+georgepisaltu@users.noreply.github.com>
Co-authored-by: Chevdor <chevdor@users.noreply.github.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Maciej <maciej.zyszkiewicz@parity.io>
Co-authored-by: Javier Viola <javier@parity.io>
Co-authored-by: Marcin S. <marcin@realemail.net>
Co-authored-by: Tsvetomir Dimitrov <tsvetomir@parity.io>
Co-authored-by: Javier Bullrich <javier@bullrich.dev>
Co-authored-by: Koute <koute@users.noreply.github.com>
Co-authored-by: Adrian Catangiu <adrian@parity.io>
Co-authored-by: Vladimir Istyufeev <vladimir@parity.io>
Co-authored-by: Ross Bulat <ross@parity.io>
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
Co-authored-by: Svyatoslav Nikolsky <svyatonik@gmail.com>
Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: s0me0ne-unkn0wn <48632512+s0me0ne-unkn0wn@users.noreply.github.com>
Co-authored-by: ordian <write@reusable.software>
Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
Co-authored-by: Aaro Altonen <48052676+altonen@users.noreply.github.com>
Co-authored-by: Dmitry Markin <dmitry@markin.tech>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Co-authored-by: Alexander Samusev <41779041+alvicsam@users.noreply.github.com>
Co-authored-by: Julian Eager <eagr@tutanota.com>
Co-authored-by: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com>
Co-authored-by: Davide Galassi <davxy@datawok.net>
Co-authored-by: Dónal Murray <donal.murray@parity.io>
Co-authored-by: yjh <yjh465402634@gmail.com>
Co-authored-by: Tom Mi <tommi@niemi.lol>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Will | Paradox | ParaNodes.io <79228812+paradox-tt@users.noreply.github.com>
Co-authored-by: Bastian Köcher <info@kchr.de>
Co-authored-by: Joshy Orndorff <JoshOrndorff@users.noreply.github.com>
Co-authored-by: Joshy Orndorff <git-user-email.h0ly5@simplelogin.com>
Co-authored-by: PG Herveou <pgherveou@gmail.com>
Co-authored-by: Alexander Theißen <alex.theissen@me.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Juan Girini <juangirini@gmail.com>
Co-authored-by: bader y <ibnbassem@gmail.com>
Co-authored-by: James Wilson <james@jsdw.me>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: asynchronous rob <rphmeier@gmail.com>
Co-authored-by: Parth <desaiparth08@gmail.com>
Co-authored-by: Andrew Jones <ascjones@gmail.com>
Co-authored-by: Jonathan Udd <jonathan@dwellir.com>
Co-authored-by: Serban Iorga <serban@parity.io>
Co-authored-by: Egor_P <egor@parity.io>
Co-authored-by: Branislav Kontur <bkontur@gmail.com>
Co-authored-by: Evgeny Snitko <evgeny@parity.io>
Co-authored-by: Just van Stam <vstam1@users.noreply.github.com>
Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com>
Co-authored-by: gupnik <nikhilgupta.iitk@gmail.com>
Co-authored-by: dzmitry-lahoda <dzmitry@lahoda.pro>
Co-authored-by: zhiqiangxu <652732310@qq.com>
Co-authored-by: Nazar Mokrynskyi <nazar@mokrynskyi.com>
Co-authored-by: Anwesh <anweshknayak@gmail.com>
Co-authored-by: cheme <emericchevalier.pro@gmail.com>
Co-authored-by: Sam Johnson <sam@durosoft.com>
Co-authored-by: kianenigma <kian@parity.io>
Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com>
Co-authored-by: Muharem <ismailov.m.h@gmail.com>
Co-authored-by: joepetrowski <joe@parity.io>
Co-authored-by: Alexandru Gheorghe <49718502+alexggh@users.noreply.github.com>
Co-authored-by: Gabriel Facco de Arruda <arrudagates@gmail.com>
Co-authored-by: Squirrel <gilescope@gmail.com>
Co-authored-by: Andrei Sandu <54316454+sandreim@users.noreply.github.com>
Co-authored-by: georgepisaltu <george.pisaltu@parity.io>
Co-authored-by: command-bot <>
This commit is contained in:
Gavin Wood
2024-03-04 20:12:43 +01:00
committed by GitHub
parent b0741d4f78
commit fd5f9292f5
349 changed files with 25581 additions and 17082 deletions
+156 -69
View File
@@ -327,10 +327,42 @@ impl frame_system::Config for Runtime {
type Balance = u64;
pub struct BalancesWeights;
impl pallet_balances::WeightInfo for BalancesWeights {
fn transfer_allow_death() -> Weight {
Weight::from_parts(25, 0)
}
fn transfer_keep_alive() -> Weight {
Weight::zero()
}
fn force_set_balance_creating() -> Weight {
Weight::zero()
}
fn force_set_balance_killing() -> Weight {
Weight::zero()
}
fn force_transfer() -> Weight {
Weight::zero()
}
fn transfer_all() -> Weight {
Weight::zero()
}
fn force_unreserve() -> Weight {
Weight::zero()
}
fn upgrade_accounts(_u: u32) -> Weight {
Weight::zero()
}
fn force_adjust_total_issuance() -> Weight {
Weight::zero()
}
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)]
impl pallet_balances::Config for Runtime {
type Balance = Balance;
type AccountStore = System;
type WeightInfo = BalancesWeights;
}
parameter_types! {
@@ -343,6 +375,7 @@ impl pallet_transaction_payment::Config for Runtime {
type WeightToFee = IdentityFee<Balance>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = ();
type WeightInfo = ();
}
impl custom::Config for Runtime {}
@@ -355,19 +388,46 @@ impl frame_support::traits::Get<sp_version::RuntimeVersion> for RuntimeVersion {
}
}
#[derive(Clone, Debug, Encode, codec::Decode, PartialEq, Eq, scale_info::TypeInfo)]
pub struct AccountU64(u64);
impl sp_runtime::traits::IdentifyAccount for AccountU64 {
type AccountId = u64;
fn into_account(self) -> u64 {
self.0
}
}
impl sp_runtime::traits::Verify for AccountU64 {
type Signer = AccountU64;
fn verify<L: sp_runtime::traits::Lazy<[u8]>>(
&self,
_msg: L,
_signer: &<Self::Signer as sp_runtime::traits::IdentifyAccount>::AccountId,
) -> bool {
true
}
}
impl From<u64> for AccountU64 {
fn from(value: u64) -> Self {
Self(value)
}
}
parameter_types! {
pub static RuntimeVersionTestValues: sp_version::RuntimeVersion =
Default::default();
}
type SignedExtra = (
type TxExtension = (
frame_system::CheckEra<Runtime>,
frame_system::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
);
type TestXt = sp_runtime::testing::TestXt<RuntimeCall, SignedExtra>;
type TestBlock = Block<TestXt>;
type UncheckedXt =
sp_runtime::generic::UncheckedExtrinsic<u64, RuntimeCall, AccountU64, TxExtension>;
type TestBlock = Block<UncheckedXt>;
// Will contain `true` when the custom runtime logic was called.
const CUSTOM_ON_RUNTIME_KEY: &[u8] = b":custom:on_runtime";
@@ -387,7 +447,7 @@ impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
type Executive = super::Executive<
Runtime,
Block<TestXt>,
Block<UncheckedXt>,
ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
@@ -462,17 +522,14 @@ impl MultiStepMigrator for MockedModeGetter {
}
}
fn extra(nonce: u64, fee: Balance) -> SignedExtra {
fn tx_ext(nonce: u64, fee: Balance) -> TxExtension {
(
frame_system::CheckEra::from(Era::Immortal),
frame_system::CheckNonce::from(nonce),
frame_system::CheckWeight::new(),
pallet_transaction_payment::ChargeTransactionPayment::from(fee),
)
}
fn sign_extra(who: u64, nonce: u64, fee: Balance) -> Option<(u64, SignedExtra)> {
Some((who, extra(nonce, fee)))
.into()
}
fn call_transfer(dest: u64, value: u64) -> RuntimeCall {
@@ -485,7 +542,7 @@ fn balance_transfer_dispatch_works() {
pallet_balances::GenesisConfig::<Runtime> { balances: vec![(1, 211)] }
.assimilate_storage(&mut t)
.unwrap();
let xt = TestXt::new(call_transfer(2, 69), sign_extra(1, 0, 0));
let xt = UncheckedXt::new_signed(call_transfer(2, 69), 1, 1.into(), tx_ext(0, 0));
let weight = xt.get_dispatch_info().weight +
<Runtime as frame_system::Config>::BlockWeights::get()
.get(DispatchClass::Normal)
@@ -596,7 +653,7 @@ fn block_import_of_bad_extrinsic_root_fails() {
fn bad_extrinsic_not_inserted() {
let mut t = new_test_ext(1);
// bad nonce check!
let xt = TestXt::new(call_transfer(33, 69), sign_extra(1, 30, 0));
let xt = UncheckedXt::new_signed(call_transfer(33, 69), 1, 1.into(), tx_ext(30, 0));
t.execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
assert_err!(
@@ -610,27 +667,24 @@ fn bad_extrinsic_not_inserted() {
#[test]
fn block_weight_limit_enforced() {
let mut t = new_test_ext(10000);
// given: TestXt uses the encoded len as fixed Len:
let xt = TestXt::new(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
sign_extra(1, 0, 0),
);
let encoded = xt.encode();
let encoded_len = encoded.len() as u64;
let transfer_weight =
<<Runtime as pallet_balances::Config>::WeightInfo as pallet_balances::WeightInfo>::transfer_allow_death();
// on_initialize weight + base block execution weight
let block_weights = <Runtime as frame_system::Config>::BlockWeights::get();
let base_block_weight = Weight::from_parts(175, 0) + block_weights.base_block;
let limit = block_weights.get(DispatchClass::Normal).max_total.unwrap() - base_block_weight;
let num_to_exhaust_block = limit.ref_time() / (encoded_len + 5);
let num_to_exhaust_block = limit.ref_time() / (transfer_weight.ref_time() + 5);
t.execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
// Base block execution weight + `on_initialize` weight from the custom module.
assert_eq!(<frame_system::Pallet<Runtime>>::block_weight().total(), base_block_weight);
for nonce in 0..=num_to_exhaust_block {
let xt = TestXt::new(
let xt = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
sign_extra(1, nonce.into(), 0),
1,
1.into(),
tx_ext(nonce.into(), 0),
);
let res = Executive::apply_extrinsic(xt);
if nonce != num_to_exhaust_block {
@@ -638,7 +692,8 @@ fn block_weight_limit_enforced() {
assert_eq!(
<frame_system::Pallet<Runtime>>::block_weight().total(),
//--------------------- on_initialize + block_execution + extrinsic_base weight
Weight::from_parts((encoded_len + 5) * (nonce + 1), 0) + base_block_weight,
Weight::from_parts((transfer_weight.ref_time() + 5) * (nonce + 1), 0) +
base_block_weight,
);
assert_eq!(
<frame_system::Pallet<Runtime>>::extrinsic_index(),
@@ -653,19 +708,26 @@ fn block_weight_limit_enforced() {
#[test]
fn block_weight_and_size_is_stored_per_tx() {
let xt = TestXt::new(
let xt = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
sign_extra(1, 0, 0),
1,
1.into(),
tx_ext(0, 0),
);
let x1 = TestXt::new(
let x1 = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
sign_extra(1, 1, 0),
1,
1.into(),
tx_ext(1, 0),
);
let x2 = TestXt::new(
let x2 = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
sign_extra(1, 2, 0),
1,
1.into(),
tx_ext(2, 0),
);
let len = xt.clone().encode().len() as u32;
let transfer_weight = <<Runtime as pallet_balances::Config>::WeightInfo as pallet_balances::WeightInfo>::transfer_allow_death();
let mut t = new_test_ext(1);
t.execute_with(|| {
// Block execution weight + on_initialize weight from custom module
@@ -681,8 +743,7 @@ fn block_weight_and_size_is_stored_per_tx() {
assert!(Executive::apply_extrinsic(x1.clone()).unwrap().is_ok());
assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok());
// default weight for `TestXt` == encoded length.
let extrinsic_weight = Weight::from_parts(len as u64, 0) +
let extrinsic_weight = transfer_weight +
<Runtime as frame_system::Config>::BlockWeights::get()
.get(DispatchClass::Normal)
.base_extrinsic;
@@ -707,8 +768,8 @@ fn block_weight_and_size_is_stored_per_tx() {
#[test]
fn validate_unsigned() {
let valid = TestXt::new(RuntimeCall::Custom(custom::Call::allowed_unsigned {}), None);
let invalid = TestXt::new(RuntimeCall::Custom(custom::Call::unallowed_unsigned {}), None);
let valid = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::allowed_unsigned {}));
let invalid = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::unallowed_unsigned {}));
let mut t = new_test_ext(1);
t.execute_with(|| {
@@ -745,9 +806,11 @@ fn can_not_pay_for_tx_fee_on_full_lock() {
t.execute_with(|| {
<pallet_balances::Pallet<Runtime> as fungible::MutateFreeze<u64>>::set_freeze(&(), &1, 110)
.unwrap();
let xt = TestXt::new(
let xt = UncheckedXt::new_signed(
RuntimeCall::System(frame_system::Call::remark { remark: vec![1u8] }),
sign_extra(1, 0, 0),
1,
1.into(),
tx_ext(0, 0),
);
Executive::initialize_block(&Header::new_from_number(1));
@@ -872,9 +935,11 @@ fn event_from_runtime_upgrade_is_included() {
/// used through the `ExecuteBlock` trait.
#[test]
fn custom_runtime_upgrade_is_called_when_using_execute_block_trait() {
let xt = TestXt::new(
let xt = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
sign_extra(1, 0, 0),
1,
1.into(),
tx_ext(0, 0),
);
let header = new_test_ext(1).execute_with(|| {
@@ -902,7 +967,10 @@ fn custom_runtime_upgrade_is_called_when_using_execute_block_trait() {
*v = sp_version::RuntimeVersion { spec_version: 1, ..Default::default() }
});
<Executive as ExecuteBlock<Block<TestXt>>>::execute_block(Block::new(header, vec![xt]));
<Executive as ExecuteBlock<Block<UncheckedXt>>>::execute_block(Block::new(
header,
vec![xt],
));
assert_eq!(&sp_io::storage::get(TEST_KEY).unwrap()[..], *b"module");
assert_eq!(sp_io::storage::get(CUSTOM_ON_RUNTIME_KEY).unwrap(), true.encode());
@@ -968,7 +1036,7 @@ fn offchain_worker_works_as_expected() {
#[test]
fn calculating_storage_root_twice_works() {
let call = RuntimeCall::Custom(custom::Call::calculate_storage_root {});
let xt = TestXt::new(call, sign_extra(1, 0, 0));
let xt = UncheckedXt::new_signed(call, 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
// Let's build some fake block.
@@ -987,11 +1055,13 @@ fn calculating_storage_root_twice_works() {
#[test]
#[should_panic(expected = "Invalid inherent position for extrinsic at index 1")]
fn invalid_inherent_position_fail() {
let xt1 = TestXt::new(
let xt1 = UncheckedXt::new_signed(
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 33, value: 0 }),
sign_extra(1, 0, 0),
1,
1.into(),
tx_ext(0, 0),
);
let xt2 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let xt2 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let header = new_test_ext(1).execute_with(|| {
// Let's build some fake block.
@@ -1010,8 +1080,8 @@ fn invalid_inherent_position_fail() {
#[test]
fn valid_inherents_position_works() {
let xt1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let xt2 = TestXt::new(call_transfer(33, 0), sign_extra(1, 0, 0));
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
// Let's build some fake block.
@@ -1031,7 +1101,12 @@ fn valid_inherents_position_works() {
#[test]
#[should_panic(expected = "A call was labelled as mandatory, but resulted in an Error.")]
fn invalid_inherents_fail_block_execution() {
let xt1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), sign_extra(1, 0, 0));
let xt1 = UncheckedXt::new_signed(
RuntimeCall::Custom(custom::Call::inherent {}),
1,
1.into(),
tx_ext(0, 0),
);
new_test_ext(1).execute_with(|| {
Executive::execute_block(Block::new(
@@ -1044,7 +1119,7 @@ fn invalid_inherents_fail_block_execution() {
// Inherents are created by the runtime and don't need to be validated.
#[test]
fn inherents_fail_validate_block() {
let xt1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
new_test_ext(1).execute_with(|| {
assert_eq!(
@@ -1058,7 +1133,7 @@ fn inherents_fail_validate_block() {
/// Inherents still work while `initialize_block` forbids transactions.
#[test]
fn inherents_ok_while_exts_forbidden_works() {
let xt1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
@@ -1078,8 +1153,8 @@ fn inherents_ok_while_exts_forbidden_works() {
#[test]
#[should_panic = "Only inherents are allowed in this block"]
fn transactions_in_only_inherents_block_errors() {
let xt1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let xt2 = TestXt::new(call_transfer(33, 0), sign_extra(1, 0, 0));
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
@@ -1099,8 +1174,8 @@ fn transactions_in_only_inherents_block_errors() {
/// Same as above but no error.
#[test]
fn transactions_in_normal_block_works() {
let xt1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let xt2 = TestXt::new(call_transfer(33, 0), sign_extra(1, 0, 0));
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
@@ -1120,8 +1195,8 @@ fn transactions_in_normal_block_works() {
#[test]
#[cfg(feature = "try-runtime")]
fn try_execute_block_works() {
let xt1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let xt2 = TestXt::new(call_transfer(33, 0), sign_extra(1, 0, 0));
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
@@ -1148,8 +1223,8 @@ fn try_execute_block_works() {
#[cfg(feature = "try-runtime")]
#[should_panic = "Only inherents allowed"]
fn try_execute_tx_forbidden_errors() {
let xt1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let xt2 = TestXt::new(call_transfer(33, 0), sign_extra(1, 0, 0));
let xt1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
let header = new_test_ext(1).execute_with(|| {
// Let's build some fake block.
@@ -1176,9 +1251,9 @@ fn try_execute_tx_forbidden_errors() {
/// Check that `ensure_inherents_are_first` reports the correct indices.
#[test]
fn ensure_inherents_are_first_works() {
let in1 = TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None);
let in2 = TestXt::new(RuntimeCall::Custom2(custom2::Call::inherent {}), None);
let xt2 = TestXt::new(call_transfer(33, 0), sign_extra(1, 0, 0));
let in1 = UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}));
let in2 = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::inherent {}));
let xt2 = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
// Mocked empty header:
let header = new_test_ext(1).execute_with(|| {
@@ -1256,18 +1331,20 @@ fn callbacks_in_block_execution_works_inner(mbms_active: bool) {
for i in 0..n_in {
let xt = if i % 2 == 0 {
TestXt::new(RuntimeCall::Custom(custom::Call::inherent {}), None)
UncheckedXt::new_bare(RuntimeCall::Custom(custom::Call::inherent {}))
} else {
TestXt::new(RuntimeCall::Custom2(custom2::Call::optional_inherent {}), None)
UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::optional_inherent {}))
};
Executive::apply_extrinsic(xt.clone()).unwrap().unwrap();
extrinsics.push(xt);
}
for t in 0..n_tx {
let xt = TestXt::new(
let xt = UncheckedXt::new_signed(
RuntimeCall::Custom2(custom2::Call::some_call {}),
sign_extra(1, t as u64, 0),
1,
1.into(),
tx_ext(t as u64, 0),
);
// Extrinsics can be applied even when MBMs are active. Only the `execute_block`
// will reject it.
@@ -1307,8 +1384,13 @@ fn callbacks_in_block_execution_works_inner(mbms_active: bool) {
#[test]
fn post_inherent_called_after_all_inherents() {
let in1 = TestXt::new(RuntimeCall::Custom2(custom2::Call::inherent {}), None);
let xt1 = TestXt::new(RuntimeCall::Custom2(custom2::Call::some_call {}), sign_extra(1, 0, 0));
let in1 = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::inherent {}));
let xt1 = UncheckedXt::new_signed(
RuntimeCall::Custom2(custom2::Call::some_call {}),
1,
1.into(),
tx_ext(0, 0),
);
let header = new_test_ext(1).execute_with(|| {
// Let's build some fake block.
@@ -1342,8 +1424,13 @@ fn post_inherent_called_after_all_inherents() {
/// Regression test for AppSec finding #40.
#[test]
fn post_inherent_called_after_all_optional_inherents() {
let in1 = TestXt::new(RuntimeCall::Custom2(custom2::Call::optional_inherent {}), None);
let xt1 = TestXt::new(RuntimeCall::Custom2(custom2::Call::some_call {}), sign_extra(1, 0, 0));
let in1 = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::optional_inherent {}));
let xt1 = UncheckedXt::new_signed(
RuntimeCall::Custom2(custom2::Call::some_call {}),
1,
1.into(),
tx_ext(0, 0),
);
let header = new_test_ext(1).execute_with(|| {
// Let's build some fake block.
@@ -1376,14 +1463,14 @@ fn post_inherent_called_after_all_optional_inherents() {
#[test]
fn is_inherent_works() {
let ext = TestXt::new(RuntimeCall::Custom2(custom2::Call::inherent {}), None);
let ext = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::inherent {}));
assert!(Runtime::is_inherent(&ext));
let ext = TestXt::new(RuntimeCall::Custom2(custom2::Call::optional_inherent {}), None);
let ext = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::optional_inherent {}));
assert!(Runtime::is_inherent(&ext));
let ext = TestXt::new(call_transfer(33, 0), sign_extra(1, 0, 0));
let ext = UncheckedXt::new_signed(call_transfer(33, 0), 1, 1.into(), tx_ext(0, 0));
assert!(!Runtime::is_inherent(&ext));
let ext = TestXt::new(RuntimeCall::Custom2(custom2::Call::allowed_unsigned {}), None);
let ext = UncheckedXt::new_bare(RuntimeCall::Custom2(custom2::Call::allowed_unsigned {}));
assert!(!Runtime::is_inherent(&ext), "Unsigned ext are not automatically inherents");
}