mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 07:31:08 +00:00
Fix executive test (#7243)
* fix executive test * add unallowed unsigned test * better to have also unsigned pre_dispatch check tested
This commit is contained in:
committed by
GitHub
parent
a2c4b38eb6
commit
a31637def8
@@ -481,20 +481,25 @@ mod tests {
|
|||||||
use sp_runtime::{
|
use sp_runtime::{
|
||||||
generic::Era, Perbill, DispatchError, testing::{Digest, Header, Block},
|
generic::Era, Perbill, DispatchError, testing::{Digest, Header, Block},
|
||||||
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup},
|
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup},
|
||||||
transaction_validity::{InvalidTransaction, UnknownTransaction, TransactionValidityError},
|
transaction_validity::{
|
||||||
|
InvalidTransaction, ValidTransaction, TransactionValidityError, UnknownTransaction
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use frame_support::{
|
use frame_support::{
|
||||||
impl_outer_event, impl_outer_origin, parameter_types, impl_outer_dispatch,
|
parameter_types,
|
||||||
weights::{Weight, RuntimeDbWeight, IdentityFee, WeightToFeePolynomial},
|
weights::{Weight, RuntimeDbWeight, IdentityFee, WeightToFeePolynomial},
|
||||||
traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason},
|
traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason},
|
||||||
};
|
};
|
||||||
use frame_system::{self as system, Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo};
|
use frame_system::{Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo};
|
||||||
use pallet_balances::Call as BalancesCall;
|
use pallet_balances::Call as BalancesCall;
|
||||||
use hex_literal::hex;
|
use hex_literal::hex;
|
||||||
const TEST_KEY: &[u8] = &*b":test:key:";
|
const TEST_KEY: &[u8] = &*b":test:key:";
|
||||||
|
|
||||||
mod custom {
|
mod custom {
|
||||||
use frame_support::weights::{Weight, DispatchClass};
|
use frame_support::weights::{Weight, DispatchClass};
|
||||||
|
use sp_runtime::transaction_validity::{
|
||||||
|
UnknownTransaction, TransactionSource, TransactionValidity
|
||||||
|
};
|
||||||
|
|
||||||
pub trait Trait: frame_system::Trait {}
|
pub trait Trait: frame_system::Trait {}
|
||||||
|
|
||||||
@@ -514,6 +519,16 @@ mod tests {
|
|||||||
let _ = frame_system::ensure_none(origin);
|
let _ = frame_system::ensure_none(origin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[weight = 0]
|
||||||
|
fn allowed_unsigned(origin) {
|
||||||
|
let _ = frame_system::ensure_root(origin)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[weight = 0]
|
||||||
|
fn unallowed_unsigned(origin) {
|
||||||
|
let _ = frame_system::ensure_root(origin)?;
|
||||||
|
}
|
||||||
|
|
||||||
// module hooks.
|
// module hooks.
|
||||||
// one with block number arg and one without
|
// one with block number arg and one without
|
||||||
fn on_initialize(n: T::BlockNumber) -> Weight {
|
fn on_initialize(n: T::BlockNumber) -> Weight {
|
||||||
@@ -531,33 +546,34 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
type System = frame_system::Module<Runtime>;
|
impl<T: Trait> sp_runtime::traits::ValidateUnsigned for Module<T> {
|
||||||
type Balances = pallet_balances::Module<Runtime>;
|
type Call = Call<T>;
|
||||||
type Custom = custom::Module<Runtime>;
|
|
||||||
|
|
||||||
use pallet_balances as balances;
|
fn validate_unsigned(
|
||||||
|
_source: TransactionSource,
|
||||||
impl_outer_origin! {
|
call: &Self::Call,
|
||||||
pub enum Origin for Runtime { }
|
) -> TransactionValidity {
|
||||||
}
|
match call {
|
||||||
|
Call::allowed_unsigned(..) => Ok(Default::default()),
|
||||||
impl_outer_event!{
|
_ => UnknownTransaction::NoUnsignedValidator.into(),
|
||||||
pub enum MetaEvent for Runtime {
|
}
|
||||||
system<T>,
|
}
|
||||||
balances<T>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl_outer_dispatch! {
|
|
||||||
pub enum Call for Runtime where origin: Origin {
|
|
||||||
frame_system::System,
|
|
||||||
pallet_balances::Balances,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Eq, PartialEq)]
|
frame_support::construct_runtime!(
|
||||||
pub struct Runtime;
|
pub enum Runtime where
|
||||||
|
Block = TestBlock,
|
||||||
|
NodeBlock = TestBlock,
|
||||||
|
UncheckedExtrinsic = TestUncheckedExtrinsic
|
||||||
|
{
|
||||||
|
System: frame_system::{Module, Call, Config, Storage, Event<T>},
|
||||||
|
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
|
||||||
|
Custom: custom::{Module, Call, ValidateUnsigned},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
parameter_types! {
|
parameter_types! {
|
||||||
pub const BlockHashCount: u64 = 250;
|
pub const BlockHashCount: u64 = 250;
|
||||||
pub const MaximumBlockWeight: Weight = 1024;
|
pub const MaximumBlockWeight: Weight = 1024;
|
||||||
@@ -581,7 +597,7 @@ mod tests {
|
|||||||
type AccountId = u64;
|
type AccountId = u64;
|
||||||
type Lookup = IdentityLookup<u64>;
|
type Lookup = IdentityLookup<u64>;
|
||||||
type Header = Header;
|
type Header = Header;
|
||||||
type Event = MetaEvent;
|
type Event = Event;
|
||||||
type BlockHashCount = BlockHashCount;
|
type BlockHashCount = BlockHashCount;
|
||||||
type MaximumBlockWeight = MaximumBlockWeight;
|
type MaximumBlockWeight = MaximumBlockWeight;
|
||||||
type DbWeight = DbWeight;
|
type DbWeight = DbWeight;
|
||||||
@@ -591,7 +607,7 @@ mod tests {
|
|||||||
type AvailableBlockRatio = AvailableBlockRatio;
|
type AvailableBlockRatio = AvailableBlockRatio;
|
||||||
type MaximumBlockLength = MaximumBlockLength;
|
type MaximumBlockLength = MaximumBlockLength;
|
||||||
type Version = RuntimeVersion;
|
type Version = RuntimeVersion;
|
||||||
type PalletInfo = ();
|
type PalletInfo = PalletInfo;
|
||||||
type AccountData = pallet_balances::AccountData<Balance>;
|
type AccountData = pallet_balances::AccountData<Balance>;
|
||||||
type OnNewAccount = ();
|
type OnNewAccount = ();
|
||||||
type OnKilledAccount = ();
|
type OnKilledAccount = ();
|
||||||
@@ -604,7 +620,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
impl pallet_balances::Trait for Runtime {
|
impl pallet_balances::Trait for Runtime {
|
||||||
type Balance = Balance;
|
type Balance = Balance;
|
||||||
type Event = MetaEvent;
|
type Event = Event;
|
||||||
type DustRemoval = ();
|
type DustRemoval = ();
|
||||||
type ExistentialDeposit = ExistentialDeposit;
|
type ExistentialDeposit = ExistentialDeposit;
|
||||||
type AccountStore = System;
|
type AccountStore = System;
|
||||||
@@ -624,24 +640,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
impl custom::Trait for Runtime {}
|
impl custom::Trait for Runtime {}
|
||||||
|
|
||||||
impl ValidateUnsigned for Runtime {
|
|
||||||
type Call = Call;
|
|
||||||
|
|
||||||
fn pre_dispatch(_call: &Self::Call) -> Result<(), TransactionValidityError> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn validate_unsigned(
|
|
||||||
_source: TransactionSource,
|
|
||||||
call: &Self::Call,
|
|
||||||
) -> TransactionValidity {
|
|
||||||
match call {
|
|
||||||
Call::Balances(BalancesCall::set_balance(_, _, _)) => Ok(Default::default()),
|
|
||||||
_ => UnknownTransaction::NoUnsignedValidator.into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct RuntimeVersion;
|
pub struct RuntimeVersion;
|
||||||
impl frame_support::traits::Get<sp_version::RuntimeVersion> for RuntimeVersion {
|
impl frame_support::traits::Get<sp_version::RuntimeVersion> for RuntimeVersion {
|
||||||
fn get() -> sp_version::RuntimeVersion {
|
fn get() -> sp_version::RuntimeVersion {
|
||||||
@@ -660,8 +658,14 @@ mod tests {
|
|||||||
frame_system::CheckWeight<Runtime>,
|
frame_system::CheckWeight<Runtime>,
|
||||||
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
|
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
|
||||||
);
|
);
|
||||||
type AllModules = (System, Balances, Custom);
|
|
||||||
type TestXt = sp_runtime::testing::TestXt<Call, SignedExtra>;
|
type TestXt = sp_runtime::testing::TestXt<Call, SignedExtra>;
|
||||||
|
type TestBlock = Block<TestXt>;
|
||||||
|
type TestUncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<
|
||||||
|
<Runtime as frame_system::Trait>::AccountId,
|
||||||
|
<Runtime as frame_system::Trait>::Call,
|
||||||
|
(),
|
||||||
|
SignedExtra,
|
||||||
|
>;
|
||||||
|
|
||||||
// Will contain `true` when the custom runtime logic was called.
|
// Will contain `true` when the custom runtime logic was called.
|
||||||
const CUSTOM_ON_RUNTIME_KEY: &[u8] = &*b":custom:on_runtime";
|
const CUSTOM_ON_RUNTIME_KEY: &[u8] = &*b":custom:on_runtime";
|
||||||
@@ -895,15 +899,26 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_unsigned() {
|
fn validate_unsigned() {
|
||||||
let xt = TestXt::new(Call::Balances(BalancesCall::set_balance(33, 69, 69)), None);
|
let valid = TestXt::new(Call::Custom(custom::Call::allowed_unsigned()), None);
|
||||||
|
let invalid = TestXt::new(Call::Custom(custom::Call::unallowed_unsigned()), None);
|
||||||
let mut t = new_test_ext(1);
|
let mut t = new_test_ext(1);
|
||||||
|
|
||||||
|
let mut default_with_prio_3 = ValidTransaction::default();
|
||||||
|
default_with_prio_3.priority = 3;
|
||||||
t.execute_with(|| {
|
t.execute_with(|| {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Executive::validate_transaction(TransactionSource::InBlock, xt.clone()),
|
Executive::validate_transaction(TransactionSource::InBlock, valid.clone()),
|
||||||
Ok(Default::default()),
|
Ok(default_with_prio_3),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Executive::validate_transaction(TransactionSource::InBlock, invalid.clone()),
|
||||||
|
Err(TransactionValidityError::Unknown(UnknownTransaction::NoUnsignedValidator)),
|
||||||
|
);
|
||||||
|
assert_eq!(Executive::apply_extrinsic(valid), Ok(Err(DispatchError::BadOrigin)));
|
||||||
|
assert_eq!(
|
||||||
|
Executive::apply_extrinsic(invalid),
|
||||||
|
Err(TransactionValidityError::Unknown(UnknownTransaction::NoUnsignedValidator))
|
||||||
);
|
);
|
||||||
assert_eq!(Executive::apply_extrinsic(xt), Ok(Err(DispatchError::BadOrigin)));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -169,6 +169,12 @@ fn construct_runtime_parsed(definition: RuntimeDefinition) -> Result<TokenStream
|
|||||||
let res = quote!(
|
let res = quote!(
|
||||||
#scrate_decl
|
#scrate_decl
|
||||||
|
|
||||||
|
// Prevent UncheckedExtrinsic to print unused warning.
|
||||||
|
const _: () = {
|
||||||
|
#[allow(unused)]
|
||||||
|
type __hidden_use_of_unchecked_extrinsic = #unchecked_extrinsic;
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, #scrate::sp_runtime::RuntimeDebug)]
|
#[derive(Clone, Copy, PartialEq, Eq, #scrate::sp_runtime::RuntimeDebug)]
|
||||||
pub struct #name;
|
pub struct #name;
|
||||||
impl #scrate::sp_runtime::traits::GetNodeBlockType for #name {
|
impl #scrate::sp_runtime::traits::GetNodeBlockType for #name {
|
||||||
|
|||||||
@@ -313,11 +313,17 @@ impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
|
|||||||
/// Checks to see if this is a valid *transaction*. It returns information on it if so.
|
/// Checks to see if this is a valid *transaction*. It returns information on it if so.
|
||||||
fn validate<U: ValidateUnsigned<Call=Self::Call>>(
|
fn validate<U: ValidateUnsigned<Call=Self::Call>>(
|
||||||
&self,
|
&self,
|
||||||
_source: TransactionSource,
|
source: TransactionSource,
|
||||||
_info: &DispatchInfoOf<Self::Call>,
|
info: &DispatchInfoOf<Self::Call>,
|
||||||
_len: usize,
|
len: usize,
|
||||||
) -> TransactionValidity {
|
) -> TransactionValidity {
|
||||||
Ok(Default::default())
|
if let Some((ref id, ref extra)) = self.signature {
|
||||||
|
Extra::validate(extra, id, &self.call, info, len)
|
||||||
|
} else {
|
||||||
|
let valid = Extra::validate_unsigned(&self.call, info, len)?;
|
||||||
|
let unsigned_validation = U::validate_unsigned(source, &self.call)?;
|
||||||
|
Ok(valid.combine_with(unsigned_validation))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Executes all necessary logic needed prior to dispatch and deconstructs into function call,
|
/// Executes all necessary logic needed prior to dispatch and deconstructs into function call,
|
||||||
@@ -332,6 +338,7 @@ impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
|
|||||||
Some(who)
|
Some(who)
|
||||||
} else {
|
} else {
|
||||||
Extra::pre_dispatch_unsigned(&self.call, info, len)?;
|
Extra::pre_dispatch_unsigned(&self.call, info, len)?;
|
||||||
|
U::pre_dispatch(&self.call)?;
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user