Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Refactor SignedExtension #5540

Merged
merged 8 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
23 changes: 18 additions & 5 deletions frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@

#![cfg(test)]

#[derive(Debug)]
pub struct CallWithDispatchInfo;
impl sp_runtime::traits::Dispatchable for CallWithDispatchInfo {
type Origin = ();
type Trait = ();
type Info = frame_support::weights::DispatchInfo;
type PostInfo = frame_support::weights::PostDispatchInfo;
fn dispatch(self, _origin: Self::Origin)
-> sp_runtime::DispatchResultWithInfo<Self::PostInfo> {
panic!("Do not use dummy implementation for dispatch.");
}
}

#[macro_export]
macro_rules! decl_tests {
($test:ty, $ext_builder:ty, $existential_deposit:expr) => {
Expand All @@ -40,7 +53,7 @@ macro_rules! decl_tests {
pub type System = frame_system::Module<$test>;
pub type Balances = Module<$test>;

pub const CALL: &<$test as frame_system::Trait>::Call = &();
pub const CALL: &<$test as frame_system::Trait>::Call = &$crate::tests::CallWithDispatchInfo;

/// create a transaction info struct from weight. Handy to avoid building the whole struct.
pub fn info_from_weight(w: Weight) -> DispatchInfo {
Expand Down Expand Up @@ -154,14 +167,14 @@ macro_rules! decl_tests {
ChargeTransactionPayment::from(1),
&1,
CALL,
info_from_weight(1),
&info_from_weight(1),
1,
).is_err());
assert!(<ChargeTransactionPayment<$test> as SignedExtension>::pre_dispatch(
ChargeTransactionPayment::from(0),
&1,
CALL,
info_from_weight(1),
&info_from_weight(1),
1,
).is_ok());

Expand All @@ -172,14 +185,14 @@ macro_rules! decl_tests {
ChargeTransactionPayment::from(1),
&1,
CALL,
info_from_weight(1),
&info_from_weight(1),
1,
).is_err());
assert!(<ChargeTransactionPayment<$test> as SignedExtension>::pre_dispatch(
ChargeTransactionPayment::from(0),
&1,
CALL,
info_from_weight(1),
&info_from_weight(1),
1,
).is_err());
});
Expand Down
11 changes: 8 additions & 3 deletions frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@

#![cfg(test)]

use sp_runtime::{Perbill, traits::{ConvertInto, IdentityLookup}, testing::Header};
use sp_runtime::{
Perbill,
traits::{ConvertInto, IdentityLookup},
testing::Header,

athei marked this conversation as resolved.
Show resolved Hide resolved
};
use sp_core::H256;
use sp_io;
use frame_support::{impl_outer_origin, parameter_types};
use frame_support::traits::Get;
use frame_support::weights::{Weight, DispatchInfo};
use std::cell::RefCell;
use crate::{GenesisConfig, Module, Trait, decl_tests};
use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo};

use frame_system as system;
impl_outer_origin!{
Expand Down Expand Up @@ -54,7 +59,7 @@ impl frame_system::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = ();
type Call = CallWithDispatchInfo;
type Hash = H256;
type Hashing = ::sp_runtime::traits::BlakeTwo256;
type AccountId = u64;
Expand Down
10 changes: 7 additions & 3 deletions frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@

#![cfg(test)]

use sp_runtime::{Perbill, traits::{ConvertInto, IdentityLookup}, testing::Header};
use sp_runtime::{
Perbill,
traits::{ConvertInto, IdentityLookup},
testing::Header,
};
use sp_core::H256;
use sp_io;
use frame_support::{impl_outer_origin, parameter_types};
use frame_support::traits::{Get, StorageMapShim};
use frame_support::weights::{Weight, DispatchInfo};
use std::cell::RefCell;
use crate::{GenesisConfig, Module, Trait, decl_tests};
use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo};

use frame_system as system;
impl_outer_origin!{
Expand Down Expand Up @@ -54,7 +58,7 @@ impl frame_system::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = ();
type Call = CallWithDispatchInfo;
type Hash = H256;
type Hashing = ::sp_runtime::traits::BlakeTwo256;
type AccountId = u64;
Expand Down
9 changes: 5 additions & 4 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ use sp_std::{prelude::*, marker::PhantomData, fmt::Debug};
use codec::{Codec, Encode, Decode};
use sp_io::hashing::blake2_256;
use sp_runtime::{
traits::{Hash, StaticLookup, Zero, MaybeSerializeDeserialize, Member, SignedExtension},
traits::{
Hash, StaticLookup, Zero, MaybeSerializeDeserialize, Member, SignedExtension,
DispatchInfoOf,
},
transaction_validity::{
ValidTransaction, InvalidTransaction, TransactionValidity, TransactionValidityError,
},
Expand All @@ -123,7 +126,6 @@ use frame_support::dispatch::{DispatchResult, Dispatchable};
use frame_support::{
Parameter, decl_module, decl_event, decl_storage, decl_error, storage::child,
parameter_types, IsSubType,
weights::DispatchInfo,
};
use frame_support::traits::{OnUnbalanced, Currency, Get, Time, Randomness};
use frame_system::{self as system, ensure_signed, RawOrigin, ensure_root};
Expand Down Expand Up @@ -1091,7 +1093,6 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckBlockGasLimit<T> {
type AccountId = T::AccountId;
type Call = <T as Trait>::Call;
type AdditionalSigned = ();
type DispatchInfo = DispatchInfo;
type Pre = ();

fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> { Ok(()) }
Expand All @@ -1100,7 +1101,7 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckBlockGasLimit<T> {
&self,
_: &Self::AccountId,
call: &Self::Call,
_: Self::DispatchInfo,
_: &DispatchInfoOf<Self::Call>,
_: usize,
) -> TransactionValidity {
let call = match call.is_sub_type() {
Expand Down
4 changes: 2 additions & 2 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2633,11 +2633,11 @@ fn check_block_gas_limit_works() {
let call: Call = crate::Call::put_code(1000, vec![]).into();

assert_eq!(
check.validate(&0, &call, info, 0), InvalidTransaction::ExhaustsResources.into(),
check.validate(&0, &call, &info, 0), InvalidTransaction::ExhaustsResources.into(),
);

let call: Call = crate::Call::update_schedule(Default::default()).into();
assert_eq!(check.validate(&0, &call, info, 0), Ok(Default::default()));
assert_eq!(check.validate(&0, &call, &info, 0), Ok(Default::default()));
});
}

Expand Down
16 changes: 8 additions & 8 deletions frame/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,16 @@ use sp_std::marker::PhantomData;
use frame_support::{
dispatch::DispatchResult, decl_module, decl_storage, decl_event,
weights::{
SimpleDispatchInfo, DispatchInfo, DispatchClass, ClassifyDispatch, WeighData, Weight,
PaysFee,
SimpleDispatchInfo, DispatchClass, ClassifyDispatch, WeighData, Weight, PaysFee,
},
};
use sp_std::prelude::*;
use frame_system::{self as system, ensure_signed, ensure_root};
use codec::{Encode, Decode};
use sp_runtime::{
traits::{SignedExtension, Bounded, SaturatedConversion},
traits::{
SignedExtension, Bounded, SaturatedConversion, DispatchInfoOf,
},
transaction_validity::{
ValidTransaction, TransactionValidityError, InvalidTransaction, TransactionValidity,
},
Expand Down Expand Up @@ -619,7 +620,6 @@ impl<T: Trait + Send + Sync> SignedExtension for WatchDummy<T> {
// other pallets.
type Call = Call<T>;
type AdditionalSigned = ();
type DispatchInfo = DispatchInfo;
type Pre = ();

fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> { Ok(()) }
Expand All @@ -628,7 +628,7 @@ impl<T: Trait + Send + Sync> SignedExtension for WatchDummy<T> {
&self,
_who: &Self::AccountId,
call: &Self::Call,
_info: Self::DispatchInfo,
_info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> TransactionValidity {
// if the transaction is too big, just drop it.
Expand Down Expand Up @@ -698,7 +698,7 @@ mod tests {
use super::*;

use frame_support::{
assert_ok, impl_outer_origin, parameter_types, weights::GetDispatchInfo,
assert_ok, impl_outer_origin, parameter_types, weights::{DispatchInfo, GetDispatchInfo},
traits::{OnInitialize, OnFinalize}
};
use sp_core::H256;
Expand Down Expand Up @@ -814,13 +814,13 @@ mod tests {
let info = DispatchInfo::default();

assert_eq!(
WatchDummy::<Test>(PhantomData).validate(&1, &call, info, 150)
WatchDummy::<Test>(PhantomData).validate(&1, &call, &info, 150)
.unwrap()
.priority,
Bounded::max_value(),
);
assert_eq!(
WatchDummy::<Test>(PhantomData).validate(&1, &call, info, 250),
WatchDummy::<Test>(PhantomData).validate(&1, &call, &info, 250),
InvalidTransaction::ExhaustsResources.into(),
);
})
Expand Down
12 changes: 6 additions & 6 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ impl<
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>:
Applyable<DispatchInfo=DispatchInfo> +
Applyable +
GetDispatchInfo,
CallOf<Block::Extrinsic, Context>: Dispatchable,
CallOf<Block::Extrinsic, Context>: Dispatchable<Info=DispatchInfo>,
OriginOf<Block::Extrinsic, Context>: From<Option<System::AccountId>>,
UnsignedValidator: ValidateUnsigned<Call=CallOf<Block::Extrinsic, Context>>,
{
Expand All @@ -145,9 +145,9 @@ impl<
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>:
Applyable<DispatchInfo=DispatchInfo> +
Applyable +
GetDispatchInfo,
tomusdrw marked this conversation as resolved.
Show resolved Hide resolved
CallOf<Block::Extrinsic, Context>: Dispatchable,
CallOf<Block::Extrinsic, Context>: Dispatchable<Info=DispatchInfo>,
OriginOf<Block::Extrinsic, Context>: From<Option<System::AccountId>>,
UnsignedValidator: ValidateUnsigned<Call=CallOf<Block::Extrinsic, Context>>,
{
Expand Down Expand Up @@ -307,7 +307,7 @@ where

// Decode parameters and dispatch
let dispatch_info = xt.get_dispatch_info();
let r = Applyable::apply::<UnsignedValidator>(xt, dispatch_info, encoded_len)?;
let r = Applyable::apply::<UnsignedValidator>(xt, &dispatch_info, encoded_len)?;

<frame_system::Module<System>>::note_applied_extrinsic(&r, encoded_len as u32, dispatch_info);

Expand Down Expand Up @@ -348,7 +348,7 @@ where
let xt = uxt.check(&Default::default())?;

let dispatch_info = xt.get_dispatch_info();
xt.validate::<UnsignedValidator>(source, dispatch_info, encoded_len)
xt.validate::<UnsignedValidator>(source, &dispatch_info, encoded_len)
}

/// Start an offchain worker and generate extrinsics.
Expand Down
7 changes: 3 additions & 4 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ use sp_runtime::{
curve::PiecewiseLinear,
traits::{
Convert, Zero, StaticLookup, CheckedSub, Saturating, SaturatedConversion, AtLeast32Bit,
SignedExtension,
SignedExtension, Dispatchable, DispatchInfoOf,
},
transaction_validity::{
TransactionValidityError, TransactionValidity, ValidTransaction, InvalidTransaction,
Expand Down Expand Up @@ -772,7 +772,7 @@ pub trait Trait: frame_system::Trait {
type ElectionLookahead: Get<Self::BlockNumber>;

/// The overarching call type.
type Call: From<Call<Self>> + IsSubType<Module<Self>, Self> + Clone;
type Call: Dispatchable + From<Call<Self>> + IsSubType<Module<Self>, Self> + Clone;

/// A transaction submitter.
type SubmitTransaction: SubmitUnsignedTransaction<Self, <Self as Trait>::Call>;
Expand Down Expand Up @@ -3125,7 +3125,6 @@ impl<T: Trait + Send + Sync> SignedExtension for LockStakingStatus<T> {
type AccountId = T::AccountId;
type Call = <T as Trait>::Call;
type AdditionalSigned = ();
type DispatchInfo = frame_support::weights::DispatchInfo;
type Pre = ();

fn additional_signed(&self) -> Result<(), TransactionValidityError> { Ok(()) }
Expand All @@ -3134,7 +3133,7 @@ impl<T: Trait + Send + Sync> SignedExtension for LockStakingStatus<T> {
&self,
_who: &Self::AccountId,
call: &Self::Call,
_info: Self::DispatchInfo,
_info: &DispatchInfoOf<Self::Call>,
_len: usize,
) -> TransactionValidity {
if let Some(inner_call) = call.is_sub_type() {
Expand Down
2 changes: 1 addition & 1 deletion frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2939,7 +2939,7 @@ mod offchain_phragmen {

let lock_staking: LockStakingStatus<Test> = Default::default();
assert_eq!(
lock_staking.validate(&10, &outer, Default::default(), Default::default(),),
lock_staking.validate(&10, &outer, &Default::default(), Default::default(),),
TransactionValidity::Err(InvalidTransaction::Stale.into()),
)
})
Expand Down
4 changes: 3 additions & 1 deletion frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub enum Never {}
/// Serializable version of Dispatchable.
/// This value can be used as a "function" in an extrinsic.
pub trait Callable<T> {
type Call: Dispatchable<PostInfo=PostDispatchInfo> + Codec + Clone + PartialEq + Eq;
type Call: Dispatchable<Info=DispatchInfo, PostInfo=PostDispatchInfo> + Codec + Clone + PartialEq + Eq;
}

// dirty hack to work around serde_derive issue
Expand Down Expand Up @@ -1593,6 +1593,7 @@ macro_rules! decl_module {
{
type Trait = $trait_instance;
type Origin = $origin_type;
type Info = $crate::weights::DispatchInfo;
type PostInfo = $crate::weights::PostDispatchInfo;
fn dispatch(self, _origin: Self::Origin) -> $crate::dispatch::DispatchResultWithPostInfo {
match self {
Expand Down Expand Up @@ -1720,6 +1721,7 @@ macro_rules! impl_outer_dispatch {
impl $crate::dispatch::Dispatchable for $call_type {
type Origin = $origin;
type Trait = $call_type;
type Info = $crate::weights::DispatchInfo;
type PostInfo = $crate::weights::PostDispatchInfo;
fn dispatch(
self,
Expand Down
6 changes: 2 additions & 4 deletions frame/support/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,8 @@ mod tests {
struct TestExtension;
impl sp_runtime::traits::SignedExtension for TestExtension {
type AccountId = u32;
type Call = u32;
type Call = ();
type AdditionalSigned = u32;
type DispatchInfo = ();
type Pre = ();
const IDENTIFIER: &'static str = "testextension";
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Expand All @@ -270,9 +269,8 @@ mod tests {
struct TestExtension2;
impl sp_runtime::traits::SignedExtension for TestExtension2 {
type AccountId = u32;
type Call = u32;
type Call = ();
type AdditionalSigned = u32;
type DispatchInfo = ();
type Pre = ();
const IDENTIFIER: &'static str = "testextension2";
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Expand Down
Loading