mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 15:47:58 +00:00
frame-support-test: migrate tests from decl_* macros to the new pallet macros (#12445)
* frame-support: migrate some tests from decl macros to new pallet attribute macros * Remove useless type alias * Remove useless type alias * frame-support-test: migrate old decl_macros to new pallet attribute macros * fmt Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix features Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove deprecated stuff Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update UI tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix UI test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update UI tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Cleanup Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: parity-processbot <>
This commit is contained in:
@@ -10,7 +10,7 @@ mod benches {
|
||||
#[benchmark]
|
||||
fn bench() {
|
||||
#[extrinsic_call]
|
||||
thing(1);
|
||||
noop(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,3 @@
|
||||
error[E0599]: no variant or associated item named `new_call_variant_thing` found for enum `frame_support_test::Call` in the current scope
|
||||
--> tests/benchmark_ui/invalid_origin.rs:6:1
|
||||
|
|
||||
6 | #[benchmarks]
|
||||
| ^^^^^^^^^^^^^ variant or associated item not found in `Call<T>`
|
||||
|
|
||||
= note: this error originates in the attribute macro `benchmarks` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error[E0277]: the trait bound `<T as frame_support_test::Config>::RuntimeOrigin: From<{integer}>` is not satisfied
|
||||
--> tests/benchmark_ui/invalid_origin.rs:6:1
|
||||
|
|
||||
|
||||
@@ -24,234 +24,233 @@
|
||||
use codec::MaxEncodedLen;
|
||||
use frame_support::{parameter_types, traits::PalletInfo as _};
|
||||
use scale_info::TypeInfo;
|
||||
use sp_core::{sr25519, H256};
|
||||
use sp_core::sr25519;
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, Verify},
|
||||
DispatchError, ModuleError,
|
||||
};
|
||||
|
||||
mod system;
|
||||
|
||||
pub trait Currency {}
|
||||
|
||||
parameter_types! {
|
||||
pub static IntegrityTestExec: u32 = 0;
|
||||
}
|
||||
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
mod module1 {
|
||||
use super::*;
|
||||
use self::frame_system::pallet_prelude::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
pub trait Config<I>: system::Config {}
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T, I = ()>(_);
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config<I>, I: Instance = DefaultInstance> for enum Call
|
||||
where origin: <T as system::Config>::RuntimeOrigin, system=system
|
||||
{
|
||||
#[weight = 0]
|
||||
pub fn fail(_origin) -> frame_support::dispatch::DispatchResult {
|
||||
Err(Error::<T, I>::Something.into())
|
||||
}
|
||||
#[pallet::config]
|
||||
pub trait Config<I: 'static = ()>: frame_system::Config {
|
||||
type RuntimeEvent: From<Event<Self, I>>
|
||||
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
pub fn fail(_origin: OriginFor<T>) -> DispatchResult {
|
||||
Err(Error::<T, I>::Something.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub struct Origin<T, I: Instance = DefaultInstance>(pub core::marker::PhantomData<(T, I)>);
|
||||
#[pallet::origin]
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
#[scale_info(skip_type_params(I))]
|
||||
pub struct Origin<T, I = ()>(pub PhantomData<(T, I)>);
|
||||
|
||||
frame_support::decl_event! {
|
||||
pub enum Event<T, I: Instance = DefaultInstance> where
|
||||
<T as system::Config>::AccountId
|
||||
{
|
||||
A(AccountId),
|
||||
}
|
||||
#[pallet::event]
|
||||
pub enum Event<T: Config<I>, I: 'static = ()> {
|
||||
A(<T as frame_system::Config>::AccountId),
|
||||
}
|
||||
|
||||
frame_support::decl_error! {
|
||||
pub enum Error for Module<T: Config<I>, I: Instance> {
|
||||
Something
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config<I>, I: Instance=DefaultInstance> as Module {}
|
||||
#[pallet::error]
|
||||
pub enum Error<T, I = ()> {
|
||||
Something,
|
||||
}
|
||||
}
|
||||
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
mod module2 {
|
||||
use self::frame_system::pallet_prelude::*;
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
pub trait Config: system::Config {}
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call
|
||||
where origin: <T as system::Config>::RuntimeOrigin, system=system
|
||||
{
|
||||
#[weight = 0]
|
||||
pub fn fail(_origin) -> frame_support::dispatch::DispatchResult {
|
||||
Err(Error::<T>::Something.into())
|
||||
}
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
fn integrity_test() {
|
||||
IntegrityTestExec::mutate(|i| *i += 1);
|
||||
}
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||
fn integrity_test() {
|
||||
IntegrityTestExec::mutate(|i| *i += 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
pub fn fail(_origin: OriginFor<T>) -> DispatchResult {
|
||||
Err(Error::<T>::Something.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::origin]
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
pub struct Origin;
|
||||
|
||||
frame_support::decl_event! {
|
||||
pub enum Event {
|
||||
A,
|
||||
}
|
||||
#[pallet::event]
|
||||
pub enum Event<T> {
|
||||
A,
|
||||
}
|
||||
|
||||
frame_support::decl_error! {
|
||||
pub enum Error for Module<T: Config> {
|
||||
Something
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Module {}
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
Something,
|
||||
}
|
||||
}
|
||||
|
||||
mod nested {
|
||||
use super::*;
|
||||
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
pub mod module3 {
|
||||
use self::frame_system::pallet_prelude::*;
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
pub trait Config: system::Config {}
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call
|
||||
where origin: <T as system::Config>::RuntimeOrigin, system=system
|
||||
{
|
||||
#[weight = 0]
|
||||
pub fn fail(_origin) -> frame_support::dispatch::DispatchResult {
|
||||
Err(Error::<T>::Something.into())
|
||||
}
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
type RuntimeEvent: From<Event<Self>>
|
||||
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
fn integrity_test() {
|
||||
IntegrityTestExec::mutate(|i| *i += 1);
|
||||
}
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||
fn integrity_test() {
|
||||
IntegrityTestExec::mutate(|i| *i += 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub struct Origin;
|
||||
|
||||
frame_support::decl_event! {
|
||||
pub enum Event {
|
||||
A,
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_error! {
|
||||
pub enum Error for Module<T: Config> {
|
||||
Something
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Module {}
|
||||
add_extra_genesis {
|
||||
build(|_config| {})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod module3 {
|
||||
use super::*;
|
||||
|
||||
pub trait Config: system::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call
|
||||
where origin: <T as system::Config>::RuntimeOrigin, system=system
|
||||
{
|
||||
#[weight = 0]
|
||||
pub fn fail(_origin) -> frame_support::dispatch::DispatchResult {
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
pub fn fail(_origin: OriginFor<T>) -> DispatchResult {
|
||||
Err(Error::<T>::Something.into())
|
||||
}
|
||||
#[weight = 0]
|
||||
pub fn aux_1(_origin, #[compact] _data: u32) -> frame_support::dispatch::DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[weight = 0]
|
||||
pub fn aux_2(_origin, _data: i32, #[compact] _data2: u32) -> frame_support::dispatch::DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[weight = 0]
|
||||
fn aux_3(_origin, _data: i32, _data2: String) -> frame_support::dispatch::DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[weight = 3]
|
||||
fn aux_4(_origin) -> frame_support::dispatch::DispatchResult { unreachable!() }
|
||||
#[weight = (5, frame_support::dispatch::DispatchClass::Operational)]
|
||||
fn operational(_origin) { unreachable!() }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub struct Origin<T>(pub core::marker::PhantomData<T>);
|
||||
#[pallet::origin]
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
pub struct Origin;
|
||||
|
||||
frame_support::decl_event! {
|
||||
pub enum Event {
|
||||
#[pallet::event]
|
||||
pub enum Event<T> {
|
||||
A,
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_error! {
|
||||
pub enum Error for Module<T: Config> {
|
||||
Something
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
Something,
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Module {}
|
||||
add_extra_genesis {
|
||||
build(|_config| {})
|
||||
#[pallet::genesis_config]
|
||||
#[derive(Default)]
|
||||
pub struct GenesisConfig {}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig {
|
||||
fn build(&self) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> module1::Config<I> for Runtime {}
|
||||
impl module2::Config for Runtime {}
|
||||
impl nested::module3::Config for Runtime {}
|
||||
impl module3::Config for Runtime {}
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
pub mod module3 {
|
||||
use self::frame_system::pallet_prelude::*;
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
pub fn fail(_origin: OriginFor<T>) -> DispatchResult {
|
||||
Err(Error::<T>::Something.into())
|
||||
}
|
||||
pub fn aux_1(_origin: OriginFor<T>, #[pallet::compact] _data: u32) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
pub fn aux_2(
|
||||
_origin: OriginFor<T>,
|
||||
_data: i32,
|
||||
#[pallet::compact] _data2: u32,
|
||||
) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[pallet::weight(0)]
|
||||
pub fn aux_3(_origin: OriginFor<T>, _data: i32, _data2: String) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[pallet::weight(3)]
|
||||
pub fn aux_4(_origin: OriginFor<T>) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[pallet::weight((5, DispatchClass::Operational))]
|
||||
pub fn operational(_origin: OriginFor<T>) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::origin]
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
pub struct Origin<T>(pub PhantomData<T>);
|
||||
|
||||
#[pallet::event]
|
||||
pub enum Event<T> {
|
||||
A,
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
Something,
|
||||
}
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[derive(Default)]
|
||||
pub struct GenesisConfig {}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig {
|
||||
fn build(&self) {}
|
||||
}
|
||||
}
|
||||
|
||||
pub type BlockNumber = u64;
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type BlockNumber = u64;
|
||||
pub type Index = u64;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
fn test_pub() -> AccountId {
|
||||
AccountId::from_raw([0; 32])
|
||||
}
|
||||
|
||||
impl system::Config for Runtime {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type Hash = H256;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type DbWeight = ();
|
||||
}
|
||||
use frame_support_test as system;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub struct Runtime where
|
||||
@@ -261,12 +260,12 @@ frame_support::construct_runtime!(
|
||||
{
|
||||
System: system::{Pallet, Call, Event<T>, Origin<T>} = 30,
|
||||
Module1_1: module1::<Instance1>::{Pallet, Call, Storage, Event<T>, Origin<T>},
|
||||
Module2: module2::{Pallet, Call, Storage, Event, Origin},
|
||||
Module2: module2::{Pallet, Call, Storage, Event<T>, Origin},
|
||||
Module1_2: module1::<Instance2>::{Pallet, Call, Storage, Event<T>, Origin<T>},
|
||||
NestedModule3: nested::module3::{Pallet, Call, Config, Storage, Event, Origin},
|
||||
Module3: self::module3::{Pallet, Call, Config, Storage, Event, Origin<T>},
|
||||
Module1_3: module1::<Instance3>::{Pallet, Storage} = 6,
|
||||
Module1_4: module1::<Instance4>::{Pallet, Call} = 3,
|
||||
NestedModule3: nested::module3::{Pallet, Call, Config, Storage, Event<T>, Origin},
|
||||
Module3: self::module3::{Pallet, Call, Config, Storage, Event<T>, Origin<T>},
|
||||
Module1_3: module1::<Instance3>::{Pallet, Storage, Event<T> } = 6,
|
||||
Module1_4: module1::<Instance4>::{Pallet, Call, Event<T> } = 3,
|
||||
Module1_5: module1::<Instance5>::{Pallet, Event<T>},
|
||||
Module1_6: module1::<Instance6>::{Pallet, Call, Storage, Event<T>, Origin<T>} = 1,
|
||||
Module1_7: module1::<Instance7>::{Pallet, Call, Storage, Event<T>, Origin<T>},
|
||||
@@ -275,9 +274,57 @@ frame_support::construct_runtime!(
|
||||
}
|
||||
);
|
||||
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
impl frame_support_test::Config for Runtime {
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl module1::Config<module1::Instance1> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module1::Config<module1::Instance2> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module1::Config<module1::Instance3> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module1::Config<module1::Instance4> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module1::Config<module1::Instance5> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module1::Config<module1::Instance6> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module1::Config<module1::Instance7> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module1::Config<module1::Instance8> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module1::Config<module1::Instance9> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module2::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl nested::module3::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module3::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
|
||||
fn test_pub() -> AccountId {
|
||||
AccountId::from_raw([0; 32])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_modules_error_type() {
|
||||
@@ -471,12 +518,12 @@ fn call_codec() {
|
||||
#[test]
|
||||
fn call_compact_attr() {
|
||||
use codec::Encode;
|
||||
let call: module3::Call<Runtime> = module3::Call::aux_1 { _data: 1 };
|
||||
let call: module3::Call<Runtime> = module3::Call::aux_1 { data: 1 };
|
||||
let encoded = call.encode();
|
||||
assert_eq!(2, encoded.len());
|
||||
assert_eq!(vec![1, 4], encoded);
|
||||
|
||||
let call: module3::Call<Runtime> = module3::Call::aux_2 { _data: 1, _data2: 2 };
|
||||
let call: module3::Call<Runtime> = module3::Call::aux_2 { data: 1, data2: 2 };
|
||||
let encoded = call.encode();
|
||||
assert_eq!(6, encoded.len());
|
||||
assert_eq!(vec![2, 1, 0, 0, 0, 8], encoded);
|
||||
@@ -491,7 +538,7 @@ fn call_encode_is_correct_and_decode_works() {
|
||||
let decoded = module3::Call::<Runtime>::decode(&mut &encoded[..]).unwrap();
|
||||
assert_eq!(decoded, call);
|
||||
|
||||
let call: module3::Call<Runtime> = module3::Call::aux_3 { _data: 32, _data2: "hello".into() };
|
||||
let call: module3::Call<Runtime> = module3::Call::aux_3 { data: 32, data2: "hello".into() };
|
||||
let encoded = call.encode();
|
||||
assert_eq!(vec![3, 32, 0, 0, 0, 20, 104, 101, 108, 108, 111], encoded);
|
||||
let decoded = module3::Call::<Runtime>::decode(&mut &encoded[..]).unwrap();
|
||||
@@ -594,70 +641,70 @@ fn test_metadata() {
|
||||
calls: Some(meta_type::<system::Call<Runtime>>().into()),
|
||||
event: Some(meta_type::<system::Event<Runtime>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<system::Error<Runtime>>().into()),
|
||||
index: 30,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module1_1",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Instance1Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module1_1", entries: vec![] }),
|
||||
calls: Some(meta_type::<module1::Call<Runtime, module1::Instance1>>().into()),
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance1>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime>>().into()),
|
||||
index: 31,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module2",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module2", entries: vec![] }),
|
||||
calls: Some(meta_type::<module2::Call<Runtime>>().into()),
|
||||
event: Some(meta_type::<module2::Event>().into()),
|
||||
event: Some(meta_type::<module2::Event<Runtime>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module2::Error<Runtime>>().into()),
|
||||
index: 32,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module1_2",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Instance2Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module1_2", entries: vec![] }),
|
||||
calls: Some(meta_type::<module1::Call<Runtime, module1::Instance2>>().into()),
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance2>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime, module1::Instance2>>().into()),
|
||||
index: 33,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "NestedModule3",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "NestedModule3", entries: vec![] }),
|
||||
calls: Some(meta_type::<nested::module3::Call<Runtime>>().into()),
|
||||
event: Some(meta_type::<nested::module3::Event>().into()),
|
||||
event: Some(meta_type::<nested::module3::Event<Runtime>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<nested::module3::Error<Runtime>>().into()),
|
||||
index: 34,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module3",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module3", entries: vec![] }),
|
||||
calls: Some(meta_type::<module3::Call<Runtime>>().into()),
|
||||
event: Some(meta_type::<module3::Event>().into()),
|
||||
event: Some(meta_type::<module3::Event<Runtime>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module3::Error<Runtime>>().into()),
|
||||
index: 35,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module1_3",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Instance3Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module1_3", entries: vec![] }),
|
||||
calls: None,
|
||||
event: None,
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance3>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime, module1::Instance3>>().into()),
|
||||
index: 6,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module1_4",
|
||||
storage: None,
|
||||
calls: Some(meta_type::<module1::Call<Runtime, module1::Instance4>>().into()),
|
||||
event: None,
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance4>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime, module1::Instance4>>().into()),
|
||||
index: 3,
|
||||
},
|
||||
PalletMetadata {
|
||||
@@ -666,45 +713,43 @@ fn test_metadata() {
|
||||
calls: None,
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance5>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime, module1::Instance5>>().into()),
|
||||
index: 4,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module1_6",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Instance6Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module1_6", entries: vec![] }),
|
||||
calls: Some(meta_type::<module1::Call<Runtime, module1::Instance6>>().into()),
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance6>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime, module1::Instance6>>().into()),
|
||||
index: 1,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module1_7",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Instance7Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module1_7", entries: vec![] }),
|
||||
calls: Some(meta_type::<module1::Call<Runtime, module1::Instance7>>().into()),
|
||||
event: Some(PalletEventMetadata {
|
||||
ty: meta_type::<module1::Event<Runtime, module1::Instance7>>(),
|
||||
}),
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance7>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime, module1::Instance7>>().into()),
|
||||
index: 2,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module1_8",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Instance8Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module1_8", entries: vec![] }),
|
||||
calls: Some(meta_type::<module1::Call<Runtime, module1::Instance8>>().into()),
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance8>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime, module1::Instance8>>().into()),
|
||||
index: 12,
|
||||
},
|
||||
PalletMetadata {
|
||||
name: "Module1_9",
|
||||
storage: Some(PalletStorageMetadata { prefix: "Instance9Module", entries: vec![] }),
|
||||
storage: Some(PalletStorageMetadata { prefix: "Module1_9", entries: vec![] }),
|
||||
calls: Some(meta_type::<module1::Call<Runtime, module1::Instance9>>().into()),
|
||||
event: Some(meta_type::<module1::Event<Runtime, module1::Instance9>>().into()),
|
||||
constants: vec![],
|
||||
error: None,
|
||||
error: Some(meta_type::<module1::Error<Runtime, module1::Instance9>>().into()),
|
||||
index: 13,
|
||||
},
|
||||
];
|
||||
@@ -722,6 +767,7 @@ fn test_metadata() {
|
||||
let expected_metadata: RuntimeMetadataPrefixed =
|
||||
RuntimeMetadataLastVersion::new(pallets, extrinsic, meta_type::<Runtime>()).into();
|
||||
let actual_metadata = Runtime::metadata();
|
||||
|
||||
pretty_assertions::assert_eq!(actual_metadata, expected_metadata);
|
||||
}
|
||||
|
||||
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
use frame_support::construct_runtime;
|
||||
|
||||
mod pallet_old {
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Config> as Example {}
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {}
|
||||
}
|
||||
|
||||
}
|
||||
construct_runtime! {
|
||||
pub struct Runtime where
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
{
|
||||
System: frame_system,
|
||||
OldPallet: pallet_old,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
error[E0433]: failed to resolve: could not find `tt_default_parts` in `pallet_old`
|
||||
--> tests/construct_runtime_ui/old_unsupported_pallet_decl.rs:15:1
|
||||
|
|
||||
15 | / construct_runtime! {
|
||||
16 | | pub struct Runtime where
|
||||
17 | | UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
18 | | Block = Block,
|
||||
... |
|
||||
23 | | }
|
||||
24 | | }
|
||||
| |_^ could not find `tt_default_parts` in `pallet_old`
|
||||
|
|
||||
= note: this error originates in the macro `construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: cannot find macro `decl_storage` in this scope
|
||||
--> tests/construct_runtime_ui/old_unsupported_pallet_decl.rs:6:2
|
||||
|
|
||||
6 | decl_storage! {
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider importing this macro:
|
||||
frame_support::decl_storage
|
||||
|
||||
error: cannot find macro `decl_module` in this scope
|
||||
--> tests/construct_runtime_ui/old_unsupported_pallet_decl.rs:10:2
|
||||
|
|
||||
10 | decl_module! {
|
||||
| ^^^^^^^^^^^
|
||||
|
|
||||
= help: consider importing this macro:
|
||||
frame_support::decl_module
|
||||
@@ -28,7 +28,9 @@ error[E0412]: cannot find type `Event` in module `pallet`
|
||||
| |_^ not found in `pallet`
|
||||
|
|
||||
= note: this error originates in the macro `construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
help: consider importing this enum
|
||||
help: consider importing one of these items
|
||||
|
|
||||
1 | use frame_support_test::Event;
|
||||
|
|
||||
1 | use frame_system::Event;
|
||||
|
|
||||
|
||||
+3
-1
@@ -28,7 +28,9 @@ error[E0412]: cannot find type `Origin` in module `pallet`
|
||||
| |_^ not found in `pallet`
|
||||
|
|
||||
= note: this error originates in the macro `construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
help: consider importing this type alias
|
||||
help: consider importing one of these items
|
||||
|
|
||||
1 | use frame_support_test::Origin;
|
||||
|
|
||||
1 | use frame_system::Origin;
|
||||
|
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#[rustversion::attr(not(stable), ignore)]
|
||||
#[cfg(not(feature = "disable-ui-tests"))]
|
||||
#[test]
|
||||
fn decl_module_ui() {
|
||||
// Only run the ui tests when `RUN_UI_TESTS` is set.
|
||||
if std::env::var("RUN_UI_TESTS").is_err() {
|
||||
return
|
||||
}
|
||||
|
||||
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
|
||||
std::env::set_var("SKIP_WASM_BUILD", "1");
|
||||
|
||||
let t = trybuild::TestCases::new();
|
||||
t.compile_fail("tests/decl_module_ui/*.rs");
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {
|
||||
fn integrity_test() {}
|
||||
|
||||
fn integrity_test() {}
|
||||
}
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
error: `integrity_test` can only be passed once as input.
|
||||
--> tests/decl_module_ui/reserved_keyword_two_times_integrity_test.rs:1:1
|
||||
|
|
||||
1 | / frame_support::decl_module! {
|
||||
2 | | pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {
|
||||
3 | | fn integrity_test() {}
|
||||
4 | |
|
||||
5 | | fn integrity_test() {}
|
||||
6 | | }
|
||||
7 | | }
|
||||
| |_^
|
||||
|
|
||||
= note: this error originates in the macro `$crate::decl_module` which comes from the expansion of the macro `frame_support::decl_module` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error[E0601]: `main` function not found in crate `$CRATE`
|
||||
--> tests/decl_module_ui/reserved_keyword_two_times_integrity_test.rs:7:2
|
||||
|
|
||||
7 | }
|
||||
| ^ consider adding a `main` function to `$DIR/tests/decl_module_ui/reserved_keyword_two_times_integrity_test.rs`
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {
|
||||
fn on_initialize() -> Weight {
|
||||
0
|
||||
}
|
||||
|
||||
fn on_initialize() -> Weight {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
error: `on_initialize` can only be passed once as input.
|
||||
--> tests/decl_module_ui/reserved_keyword_two_times_on_initialize.rs:1:1
|
||||
|
|
||||
1 | / frame_support::decl_module! {
|
||||
2 | | pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {
|
||||
3 | | fn on_initialize() -> Weight {
|
||||
4 | | 0
|
||||
... |
|
||||
10 | | }
|
||||
11 | | }
|
||||
| |_^
|
||||
|
|
||||
= note: this error originates in the macro `$crate::decl_module` which comes from the expansion of the macro `frame_support::decl_module` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
@@ -1,881 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#[cfg(test)]
|
||||
// Do not complain about unused `dispatch` and `dispatch_aux`.
|
||||
#[allow(dead_code)]
|
||||
mod tests {
|
||||
use frame_support::metadata_ir::*;
|
||||
use sp_io::TestExternalities;
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
}
|
||||
|
||||
pub trait Config: frame_support_test::Config {
|
||||
type Origin2: codec::Codec
|
||||
+ codec::EncodeLike
|
||||
+ Default
|
||||
+ codec::MaxEncodedLen
|
||||
+ scale_info::TypeInfo;
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
generate_storage_info
|
||||
trait Store for Module<T: Config> as TestStorage {
|
||||
// non-getters: pub / $default
|
||||
|
||||
/// Hello, this is doc!
|
||||
U32: Option<u32>;
|
||||
pub PUBU32: Option<u32>;
|
||||
U32MYDEF: Option<u32>;
|
||||
pub PUBU32MYDEF: Option<u32>;
|
||||
|
||||
// getters: pub / $default
|
||||
// we need at least one type which uses T, otherwise GenesisConfig will complain.
|
||||
GETU32 get(fn u32_getter): T::Origin2;
|
||||
pub PUBGETU32 get(fn pub_u32_getter): u32;
|
||||
GETU32WITHCONFIG get(fn u32_getter_with_config) config(): u32;
|
||||
pub PUBGETU32WITHCONFIG get(fn pub_u32_getter_with_config) config(): u32;
|
||||
GETU32MYDEF get(fn u32_getter_mydef): Option<u32>;
|
||||
pub PUBGETU32MYDEF get(fn pub_u32_getter_mydef) config(): u32 = 3;
|
||||
GETU32WITHCONFIGMYDEF get(fn u32_getter_with_config_mydef) config(): u32 = 2;
|
||||
pub PUBGETU32WITHCONFIGMYDEF get(fn pub_u32_getter_with_config_mydef) config(): u32 = 1;
|
||||
PUBGETU32WITHCONFIGMYDEFOPT get(fn pub_u32_getter_with_config_mydef_opt) config(): Option<u32>;
|
||||
|
||||
GetU32WithBuilder get(fn u32_with_builder) build(|_| 1): u32;
|
||||
GetOptU32WithBuilderSome get(fn opt_u32_with_builder_some) build(|_| Some(1)): Option<u32>;
|
||||
GetOptU32WithBuilderNone get(fn opt_u32_with_builder_none) build(|_| None): Option<u32>;
|
||||
|
||||
// map non-getters: pub / $default
|
||||
MAPU32 max_values(3): map hasher(blake2_128_concat) u32 => Option<[u8; 4]>;
|
||||
pub PUBMAPU32: map hasher(blake2_128_concat) u32 => Option<[u8; 4]>;
|
||||
|
||||
// map getters: pub / $default
|
||||
GETMAPU32 get(fn map_u32_getter): map hasher(blake2_128_concat) u32 => [u8; 4];
|
||||
pub PUBGETMAPU32 get(fn pub_map_u32_getter): map hasher(blake2_128_concat) u32 => [u8; 4];
|
||||
GETMAPU32MYDEF get(fn map_u32_getter_mydef):
|
||||
map hasher(blake2_128_concat) u32 => [u8; 4] = *b"mapd";
|
||||
pub PUBGETMAPU32MYDEF get(fn pub_map_u32_getter_mydef):
|
||||
map hasher(blake2_128_concat) u32 => [u8; 4] = *b"pubm";
|
||||
|
||||
DOUBLEMAP max_values(3): double_map
|
||||
hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => Option<[u8; 4]>;
|
||||
|
||||
DOUBLEMAP2: double_map
|
||||
hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => Option<[u8; 4]>;
|
||||
|
||||
COMPLEXTYPE1: (::std::option::Option<T::Origin2>,);
|
||||
COMPLEXTYPE2: ([[(u16, Option<()>); 32]; 12], u32);
|
||||
COMPLEXTYPE3: [u32; 25];
|
||||
|
||||
NMAP: nmap hasher(blake2_128_concat) u32, hasher(twox_64_concat) u16 => u8;
|
||||
NMAP2: nmap hasher(blake2_128_concat) u32 => u8;
|
||||
}
|
||||
add_extra_genesis {
|
||||
build(|_| {});
|
||||
}
|
||||
}
|
||||
|
||||
struct TraitImpl {}
|
||||
|
||||
impl frame_support_test::Config for TraitImpl {
|
||||
type RuntimeOrigin = u32;
|
||||
type BlockNumber = u32;
|
||||
type PalletInfo = frame_support_test::PanicPalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl Config for TraitImpl {
|
||||
type Origin2 = u32;
|
||||
}
|
||||
|
||||
fn expected_metadata() -> PalletStorageMetadataIR {
|
||||
PalletStorageMetadataIR {
|
||||
prefix: "TestStorage",
|
||||
entries: vec![
|
||||
StorageEntryMetadataIR {
|
||||
name: "U32",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![" Hello, this is doc!"],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBU32",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "U32MYDEF",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBU32MYDEF",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GETU32",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBGETU32",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GETU32WITHCONFIG",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBGETU32WITHCONFIG",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GETU32MYDEF",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBGETU32MYDEF",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![3, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GETU32WITHCONFIGMYDEF",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![2, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBGETU32WITHCONFIGMYDEF",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![1, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBGETU32WITHCONFIGMYDEFOPT",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GetU32WithBuilder",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GetOptU32WithBuilderSome",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GetOptU32WithBuilderNone",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "MAPU32",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
hashers: vec![StorageHasherIR::Blake2_128Concat],
|
||||
key: scale_info::meta_type::<u32>(),
|
||||
value: scale_info::meta_type::<[u8; 4]>(),
|
||||
},
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBMAPU32",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
hashers: vec![StorageHasherIR::Blake2_128Concat],
|
||||
key: scale_info::meta_type::<u32>(),
|
||||
value: scale_info::meta_type::<[u8; 4]>(),
|
||||
},
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GETMAPU32",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
hashers: vec![StorageHasherIR::Blake2_128Concat],
|
||||
key: scale_info::meta_type::<u32>(),
|
||||
value: scale_info::meta_type::<[u8; 4]>(),
|
||||
},
|
||||
default: vec![0, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBGETMAPU32",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
hashers: vec![StorageHasherIR::Blake2_128Concat],
|
||||
key: scale_info::meta_type::<u32>(),
|
||||
value: scale_info::meta_type::<[u8; 4]>(),
|
||||
},
|
||||
default: vec![0, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "GETMAPU32MYDEF",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
hashers: vec![StorageHasherIR::Blake2_128Concat],
|
||||
key: scale_info::meta_type::<u32>(),
|
||||
value: scale_info::meta_type::<[u8; 4]>(),
|
||||
},
|
||||
default: vec![109, 97, 112, 100], // "map"
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "PUBGETMAPU32MYDEF",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
hashers: vec![StorageHasherIR::Blake2_128Concat],
|
||||
key: scale_info::meta_type::<u32>(),
|
||||
value: scale_info::meta_type::<[u8; 4]>(),
|
||||
},
|
||||
default: vec![112, 117, 98, 109], // "pubmap"
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "DOUBLEMAP",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
hashers: vec![
|
||||
StorageHasherIR::Blake2_128Concat,
|
||||
StorageHasherIR::Blake2_128Concat,
|
||||
],
|
||||
key: scale_info::meta_type::<(u32, u32)>(),
|
||||
value: scale_info::meta_type::<[u8; 4]>(),
|
||||
},
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "DOUBLEMAP2",
|
||||
modifier: StorageEntryModifierIR::Optional,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
hashers: vec![
|
||||
StorageHasherIR::Blake2_128Concat,
|
||||
StorageHasherIR::Blake2_128Concat,
|
||||
],
|
||||
key: scale_info::meta_type::<(u32, u32)>(),
|
||||
value: scale_info::meta_type::<[u8; 4]>(),
|
||||
},
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "COMPLEXTYPE1",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<(Option<u32>,)>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "COMPLEXTYPE2",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<(
|
||||
[[(u16, Option<()>); 32]; 12],
|
||||
u32,
|
||||
)>()),
|
||||
default: [0u8; 1156].to_vec(),
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "COMPLEXTYPE3",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<[u32; 25]>()),
|
||||
default: [0u8; 100].to_vec(),
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "NMAP",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
key: scale_info::meta_type::<(u32, u16)>(),
|
||||
hashers: vec![
|
||||
StorageHasherIR::Blake2_128Concat,
|
||||
StorageHasherIR::Twox64Concat,
|
||||
],
|
||||
value: scale_info::meta_type::<u8>(),
|
||||
},
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadataIR {
|
||||
name: "NMAP2",
|
||||
modifier: StorageEntryModifierIR::Default,
|
||||
ty: StorageEntryTypeIR::Map {
|
||||
key: scale_info::meta_type::<u32>(),
|
||||
hashers: vec![StorageHasherIR::Blake2_128Concat],
|
||||
value: scale_info::meta_type::<u8>(),
|
||||
},
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_info() {
|
||||
use frame_support::{
|
||||
storage::storage_prefix as prefix,
|
||||
traits::{StorageInfo, StorageInfoTrait},
|
||||
};
|
||||
|
||||
pretty_assertions::assert_eq!(
|
||||
<Module<TraitImpl>>::storage_info(),
|
||||
vec![
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"U32".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"U32").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBU32".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBU32").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"U32MYDEF".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"U32MYDEF").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBU32MYDEF".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBU32MYDEF").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GETU32".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GETU32").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBGETU32".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBGETU32").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GETU32WITHCONFIG".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GETU32WITHCONFIG").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBGETU32WITHCONFIG".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBGETU32WITHCONFIG").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GETU32MYDEF".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GETU32MYDEF").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBGETU32MYDEF".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBGETU32MYDEF").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GETU32WITHCONFIGMYDEF".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GETU32WITHCONFIGMYDEF").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBGETU32WITHCONFIGMYDEF".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBGETU32WITHCONFIGMYDEF").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBGETU32WITHCONFIGMYDEFOPT".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBGETU32WITHCONFIGMYDEFOPT").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GetU32WithBuilder".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GetU32WithBuilder").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GetOptU32WithBuilderSome".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GetOptU32WithBuilderSome").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GetOptU32WithBuilderNone".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GetOptU32WithBuilderNone").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(4),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"MAPU32".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"MAPU32").to_vec(),
|
||||
max_values: Some(3),
|
||||
max_size: Some(8 + 16),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBMAPU32".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBMAPU32").to_vec(),
|
||||
max_values: None,
|
||||
max_size: Some(8 + 16),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GETMAPU32".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GETMAPU32").to_vec(),
|
||||
max_values: None,
|
||||
max_size: Some(8 + 16),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBGETMAPU32".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBGETMAPU32").to_vec(),
|
||||
max_values: None,
|
||||
max_size: Some(8 + 16),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"GETMAPU32MYDEF".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"GETMAPU32MYDEF").to_vec(),
|
||||
max_values: None,
|
||||
max_size: Some(8 + 16),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PUBGETMAPU32MYDEF".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PUBGETMAPU32MYDEF").to_vec(),
|
||||
max_values: None,
|
||||
max_size: Some(8 + 16),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"DOUBLEMAP".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"DOUBLEMAP").to_vec(),
|
||||
max_values: Some(3),
|
||||
max_size: Some(12 + 16 + 16),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"DOUBLEMAP2".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"DOUBLEMAP2").to_vec(),
|
||||
max_values: None,
|
||||
max_size: Some(12 + 16 + 16),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"COMPLEXTYPE1".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"COMPLEXTYPE1").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(5),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"COMPLEXTYPE2".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"COMPLEXTYPE2").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(1156),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"COMPLEXTYPE3".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"COMPLEXTYPE3").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: Some(100),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"NMAP".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"NMAP").to_vec(),
|
||||
max_values: None,
|
||||
max_size: Some(16 + 4 + 8 + 2 + 1),
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"NMAP2".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"NMAP2").to_vec(),
|
||||
max_values: None,
|
||||
max_size: Some(16 + 4 + 1),
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_metadata() {
|
||||
let metadata = Module::<TraitImpl>::storage_metadata();
|
||||
pretty_assertions::assert_eq!(expected_metadata(), metadata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_genesis_config() {
|
||||
let config = GenesisConfig::default();
|
||||
assert_eq!(config.u32_getter_with_config, 0u32);
|
||||
assert_eq!(config.pub_u32_getter_with_config, 0u32);
|
||||
|
||||
assert_eq!(config.pub_u32_getter_mydef, 3u32);
|
||||
assert_eq!(config.u32_getter_with_config_mydef, 2u32);
|
||||
assert_eq!(config.pub_u32_getter_with_config_mydef, 1u32);
|
||||
assert_eq!(config.pub_u32_getter_with_config_mydef_opt, 0u32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_builder_config() {
|
||||
let config = GenesisConfig::default();
|
||||
let storage = config.build_storage().unwrap();
|
||||
TestExternalities::from(storage).execute_with(|| {
|
||||
assert_eq!(Module::<TraitImpl>::u32_with_builder(), 1);
|
||||
assert_eq!(Module::<TraitImpl>::opt_u32_with_builder_some(), Some(1));
|
||||
assert_eq!(Module::<TraitImpl>::opt_u32_with_builder_none(), None);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(dead_code)]
|
||||
mod test2 {
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
}
|
||||
|
||||
type PairOf<T> = (T, T);
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as TestStorage {
|
||||
SingleDef : u32;
|
||||
PairDef : PairOf<u32>;
|
||||
Single : Option<u32>;
|
||||
Pair : (u32, u32);
|
||||
}
|
||||
add_extra_genesis {
|
||||
config(_marker) : ::std::marker::PhantomData<T>;
|
||||
config(extra_field) : u32 = 32;
|
||||
build(|_| {});
|
||||
}
|
||||
}
|
||||
|
||||
struct TraitImpl {}
|
||||
|
||||
impl frame_support_test::Config for TraitImpl {
|
||||
type RuntimeOrigin = u32;
|
||||
type BlockNumber = u32;
|
||||
type PalletInfo = frame_support_test::PanicPalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl Config for TraitImpl {}
|
||||
|
||||
#[test]
|
||||
fn storage_info() {
|
||||
use frame_support::{
|
||||
storage::storage_prefix as prefix,
|
||||
traits::{StorageInfo, StorageInfoTrait},
|
||||
};
|
||||
pretty_assertions::assert_eq!(
|
||||
<Module<TraitImpl>>::storage_info(),
|
||||
vec![
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"SingleDef".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"SingleDef").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: None,
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"PairDef".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"PairDef").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: None,
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"Single".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"Single").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: None,
|
||||
},
|
||||
StorageInfo {
|
||||
pallet_name: b"TestStorage".to_vec(),
|
||||
storage_name: b"Pair".to_vec(),
|
||||
prefix: prefix(b"TestStorage", b"Pair").to_vec(),
|
||||
max_values: Some(1),
|
||||
max_size: None,
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(dead_code)]
|
||||
mod test3 {
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
}
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Test {
|
||||
Foo get(fn foo) config(initial_foo): u32;
|
||||
}
|
||||
}
|
||||
|
||||
type PairOf<T> = (T, T);
|
||||
|
||||
struct TraitImpl {}
|
||||
|
||||
impl frame_support_test::Config for TraitImpl {
|
||||
type RuntimeOrigin = u32;
|
||||
type BlockNumber = u32;
|
||||
type PalletInfo = frame_support_test::PanicPalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl Config for TraitImpl {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(dead_code)]
|
||||
mod test_append_and_len {
|
||||
use codec::{Decode, Encode};
|
||||
use sp_io::TestExternalities;
|
||||
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Encode, Decode, scale_info::TypeInfo)]
|
||||
struct NoDef(u32);
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Test {
|
||||
NoDefault: Option<NoDef>;
|
||||
|
||||
JustVec: Vec<u32>;
|
||||
JustVecWithDefault: Vec<u32> = vec![6, 9];
|
||||
OptionVec: Option<Vec<u32>>;
|
||||
|
||||
MapVec: map hasher(blake2_128_concat) u32 => Vec<u32>;
|
||||
MapVecWithDefault: map hasher(blake2_128_concat) u32 => Vec<u32> = vec![6, 9];
|
||||
OptionMapVec: map hasher(blake2_128_concat) u32 => Option<Vec<u32>>;
|
||||
|
||||
DoubleMapVec: double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => Vec<u32>;
|
||||
DoubleMapVecWithDefault: double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => Vec<u32> = vec![6, 9];
|
||||
OptionDoubleMapVec: double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => Option<Vec<u32>>;
|
||||
}
|
||||
}
|
||||
|
||||
struct Test {}
|
||||
|
||||
impl frame_support_test::Config for Test {
|
||||
type RuntimeOrigin = u32;
|
||||
type BlockNumber = u32;
|
||||
type PalletInfo = frame_support_test::PanicPalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl Config for Test {}
|
||||
|
||||
#[test]
|
||||
fn default_for_option() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
assert_eq!(OptionVec::get(), None);
|
||||
assert!(JustVec::get().is_empty());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn append_works() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
for val in &[1, 2, 3, 4, 5] {
|
||||
MapVec::append(1, val);
|
||||
}
|
||||
assert_eq!(MapVec::get(1), vec![1, 2, 3, 4, 5]);
|
||||
|
||||
MapVec::remove(1);
|
||||
MapVec::append(1, 1);
|
||||
assert_eq!(MapVec::get(1), vec![1]);
|
||||
|
||||
for val in &[1, 2, 3, 4, 5] {
|
||||
JustVec::append(val);
|
||||
}
|
||||
assert_eq!(JustVec::get(), vec![1, 2, 3, 4, 5]);
|
||||
|
||||
JustVec::kill();
|
||||
JustVec::append(1);
|
||||
assert_eq!(JustVec::get(), vec![1]);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn append_overwrites_invalid_data() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let key = JustVec::hashed_key();
|
||||
// Set it to some invalid value.
|
||||
frame_support::storage::unhashed::put_raw(&key, b"1");
|
||||
assert!(JustVec::get().is_empty());
|
||||
assert_eq!(frame_support::storage::unhashed::get_raw(&key), Some(b"1".to_vec()));
|
||||
|
||||
JustVec::append(1);
|
||||
JustVec::append(2);
|
||||
assert_eq!(JustVec::get(), vec![1, 2]);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn append_overwrites_default() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
assert_eq!(JustVecWithDefault::get(), vec![6, 9]);
|
||||
JustVecWithDefault::append(1);
|
||||
assert_eq!(JustVecWithDefault::get(), vec![1]);
|
||||
|
||||
assert_eq!(MapVecWithDefault::get(0), vec![6, 9]);
|
||||
MapVecWithDefault::append(0, 1);
|
||||
assert_eq!(MapVecWithDefault::get(0), vec![1]);
|
||||
|
||||
assert_eq!(OptionVec::get(), None);
|
||||
OptionVec::append(1);
|
||||
assert_eq!(OptionVec::get(), Some(vec![1]));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn len_works() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
JustVec::put(&vec![1, 2, 3, 4]);
|
||||
OptionVec::put(&vec![1, 2, 3, 4, 5]);
|
||||
MapVec::insert(1, &vec![1, 2, 3, 4, 5, 6]);
|
||||
DoubleMapVec::insert(0, 1, &vec![1, 2]);
|
||||
|
||||
assert_eq!(JustVec::decode_len().unwrap(), 4);
|
||||
assert_eq!(OptionVec::decode_len().unwrap(), 5);
|
||||
assert_eq!(MapVec::decode_len(1).unwrap(), 6);
|
||||
assert_eq!(DoubleMapVec::decode_len(0, 1).unwrap(), 2);
|
||||
});
|
||||
}
|
||||
|
||||
// `decode_len` should always return `None` for default assigments
|
||||
// in `decl_storage!`.
|
||||
#[test]
|
||||
fn len_works_ignores_default_assignment() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
// vec
|
||||
assert!(JustVec::get().is_empty());
|
||||
assert_eq!(JustVec::decode_len(), None);
|
||||
|
||||
assert_eq!(JustVecWithDefault::get(), vec![6, 9]);
|
||||
assert_eq!(JustVecWithDefault::decode_len(), None);
|
||||
|
||||
assert_eq!(OptionVec::get(), None);
|
||||
assert_eq!(OptionVec::decode_len(), None);
|
||||
|
||||
// map
|
||||
assert!(MapVec::get(0).is_empty());
|
||||
assert_eq!(MapVec::decode_len(0), None);
|
||||
|
||||
assert_eq!(MapVecWithDefault::get(0), vec![6, 9]);
|
||||
assert_eq!(MapVecWithDefault::decode_len(0), None);
|
||||
|
||||
assert_eq!(OptionMapVec::get(0), None);
|
||||
assert_eq!(OptionMapVec::decode_len(0), None);
|
||||
|
||||
// Double map
|
||||
assert!(DoubleMapVec::get(0, 0).is_empty());
|
||||
assert_eq!(DoubleMapVec::decode_len(0, 1), None);
|
||||
|
||||
assert_eq!(DoubleMapVecWithDefault::get(0, 0), vec![6, 9]);
|
||||
assert_eq!(DoubleMapVecWithDefault::decode_len(0, 1), None);
|
||||
|
||||
assert_eq!(OptionDoubleMapVec::get(0, 0), None);
|
||||
assert_eq!(OptionDoubleMapVec::decode_len(0, 1), None);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#[rustversion::attr(not(stable), ignore)]
|
||||
#[cfg(not(feature = "disable-ui-tests"))]
|
||||
#[test]
|
||||
fn decl_storage_ui() {
|
||||
// Only run the ui tests when `RUN_UI_TESTS` is set.
|
||||
if std::env::var("RUN_UI_TESTS").is_err() {
|
||||
return
|
||||
}
|
||||
|
||||
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
|
||||
std::env::set_var("SKIP_WASM_BUILD", "1");
|
||||
|
||||
let t = trybuild::TestCases::new();
|
||||
t.compile_fail("tests/decl_storage_ui/*.rs");
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
}
|
||||
|
||||
frame_support::decl_storage!{
|
||||
trait Store for Module<T: Config> as FinalKeysNone {
|
||||
pub Value config(value): u32;
|
||||
pub Value2 config(value): u32;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -1,5 +0,0 @@
|
||||
error: `config()`/`get()` with the same name already defined.
|
||||
--> $DIR/config_duplicate.rs:27:21
|
||||
|
|
||||
27 | pub Value2 config(value): u32;
|
||||
| ^^^^^
|
||||
@@ -1,31 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
}
|
||||
|
||||
frame_support::decl_storage!{
|
||||
trait Store for Module<T: Config> as FinalKeysNone {
|
||||
pub Value get(fn value) config(): u32;
|
||||
pub Value2 config(value): u32;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -1,5 +0,0 @@
|
||||
error: `config()`/`get()` with the same name already defined.
|
||||
--> $DIR/config_get_duplicate.rs:27:21
|
||||
|
|
||||
27 | pub Value2 config(value): u32;
|
||||
| ^^^^^
|
||||
@@ -1,31 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
}
|
||||
|
||||
frame_support::decl_storage!{
|
||||
trait Store for Module<T: Config> as FinalKeysNone {
|
||||
pub Value get(fn value) config(): u32;
|
||||
pub Value2 get(fn value) config(): u32;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -1,5 +0,0 @@
|
||||
error: `config()`/`get()` with the same name already defined.
|
||||
--> $DIR/get_duplicate.rs:27:21
|
||||
|
|
||||
27 | pub Value2 get(fn value) config(): u32;
|
||||
| ^^^^^
|
||||
@@ -16,65 +16,166 @@
|
||||
// limitations under the License.
|
||||
|
||||
use codec::Encode;
|
||||
use frame_support::{
|
||||
storage::unhashed, StorageDoubleMap, StorageMap, StoragePrefixedMap, StorageValue,
|
||||
};
|
||||
use frame_support::{storage::unhashed, StoragePrefixedMap};
|
||||
use sp_core::sr25519;
|
||||
use sp_io::{
|
||||
hashing::{blake2_128, twox_128, twox_64},
|
||||
TestExternalities,
|
||||
};
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, Verify},
|
||||
};
|
||||
|
||||
#[frame_support::pallet]
|
||||
mod no_instance {
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Value<T> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Map<T> = StorageMap<_, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
#[pallet::storage]
|
||||
pub type Map2<T> = StorageMap<_, Twox64Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap<T> =
|
||||
StorageDoubleMap<_, Blake2_128Concat, u32, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap2<T> =
|
||||
StorageDoubleMap<_, Twox64Concat, u32, Twox64Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn test_generic_value)]
|
||||
pub type TestGenericValue<T: Config> = StorageValue<_, T::BlockNumber, OptionQuery>;
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn foo2)]
|
||||
pub type TestGenericDoubleMap<T: Config> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
u32,
|
||||
Blake2_128Concat,
|
||||
T::BlockNumber,
|
||||
u32,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
pub value: u32,
|
||||
pub test_generic_value: T::BlockNumber,
|
||||
pub test_generic_double_map: Vec<(u32, T::BlockNumber, u32)>,
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as FinalKeysNone {
|
||||
pub Value config(value): u32;
|
||||
impl<T: Config> Default for GenesisConfig<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: Default::default(),
|
||||
test_generic_value: Default::default(),
|
||||
test_generic_double_map: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub Map: map hasher(blake2_128_concat) u32 => u32;
|
||||
pub Map2: map hasher(twox_64_concat) u32 => u32;
|
||||
|
||||
pub DoubleMap: double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => u32;
|
||||
pub DoubleMap2: double_map hasher(twox_64_concat) u32, hasher(twox_64_concat) u32 => u32;
|
||||
|
||||
pub TestGenericValue get(fn test_generic_value) config(): Option<T::BlockNumber>;
|
||||
pub TestGenericDoubleMap get(fn foo2) config(test_generic_double_map):
|
||||
double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) T::BlockNumber => Option<u32>;
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
<Value<T>>::put(self.value);
|
||||
<TestGenericValue<T>>::put(&self.test_generic_value);
|
||||
for (k1, k2, v) in &self.test_generic_double_map {
|
||||
<TestGenericDoubleMap<T>>::insert(k1, k2, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[frame_support::pallet]
|
||||
mod instance {
|
||||
pub trait Config<I = DefaultInstance>: frame_support_test::Config {}
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config<I>, I: Instance = DefaultInstance>
|
||||
for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config<I: 'static = ()>: frame_system::Config {}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Value<T: Config<I>, I: 'static = ()> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Map<T: Config<I>, I: 'static = ()> =
|
||||
StorageMap<_, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
#[pallet::storage]
|
||||
pub type Map2<T: Config<I>, I: 'static = ()> =
|
||||
StorageMap<_, Twox64Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap<T: Config<I>, I: 'static = ()> =
|
||||
StorageDoubleMap<_, Blake2_128Concat, u32, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap2<T: Config<I>, I: 'static = ()> =
|
||||
StorageDoubleMap<_, Twox64Concat, u32, Twox64Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn test_generic_value)]
|
||||
pub type TestGenericValue<T: Config<I>, I: 'static = ()> =
|
||||
StorageValue<_, T::BlockNumber, OptionQuery>;
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn foo2)]
|
||||
pub type TestGenericDoubleMap<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
u32,
|
||||
Blake2_128Concat,
|
||||
T::BlockNumber,
|
||||
u32,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
|
||||
pub value: u32,
|
||||
pub test_generic_value: T::BlockNumber,
|
||||
pub test_generic_double_map: Vec<(u32, T::BlockNumber, u32)>,
|
||||
pub phantom: PhantomData<I>,
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config<I>, I: Instance = DefaultInstance>
|
||||
as FinalKeysSome
|
||||
{
|
||||
pub Value config(value): u32;
|
||||
|
||||
pub Map: map hasher(blake2_128_concat) u32 => u32;
|
||||
pub Map2: map hasher(twox_64_concat) u32 => u32;
|
||||
|
||||
pub DoubleMap: double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => u32;
|
||||
pub DoubleMap2: double_map hasher(twox_64_concat) u32, hasher(twox_64_concat) u32 => u32;
|
||||
|
||||
pub TestGenericValue get(fn test_generic_value) config(): Option<T::BlockNumber>;
|
||||
pub TestGenericDoubleMap get(fn foo2) config(test_generic_double_map):
|
||||
double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) T::BlockNumber => Option<u32>;
|
||||
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: Default::default(),
|
||||
test_generic_value: Default::default(),
|
||||
test_generic_double_map: Default::default(),
|
||||
phantom: Default::default(),
|
||||
}
|
||||
}
|
||||
add_extra_genesis {
|
||||
// See `decl_storage` limitation.
|
||||
config(phantom): core::marker::PhantomData<I>;
|
||||
}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
|
||||
fn build(&self) {
|
||||
<Value<T, I>>::put(self.value);
|
||||
<TestGenericValue<T, I>>::put(&self.test_generic_value);
|
||||
for (k1, k2, v) in &self.test_generic_double_map {
|
||||
<TestGenericDoubleMap<T, I>>::insert(k1, k2, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,107 +192,144 @@ fn blake2_128_concat(d: &[u8]) -> Vec<u8> {
|
||||
v
|
||||
}
|
||||
|
||||
pub type BlockNumber = u32;
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Runtime
|
||||
where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: frame_support_test,
|
||||
FinalKeysNone: no_instance,
|
||||
FinalKeysSome: instance,
|
||||
Instance2FinalKeysSome: instance::<Instance2>,
|
||||
}
|
||||
);
|
||||
|
||||
impl frame_support_test::Config for Runtime {
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl no_instance::Config for Runtime {}
|
||||
|
||||
impl instance::Config for Runtime {}
|
||||
impl instance::Config<instance::Instance2> for Runtime {}
|
||||
|
||||
#[test]
|
||||
fn final_keys_no_instance() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
no_instance::Value::put(1);
|
||||
<no_instance::Value<Runtime>>::put(1);
|
||||
let k = [twox_128(b"FinalKeysNone"), twox_128(b"Value")].concat();
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(1u32));
|
||||
|
||||
no_instance::Map::insert(1, 2);
|
||||
<no_instance::Map<Runtime>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"FinalKeysNone"), twox_128(b"Map")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<no_instance::Map>::final_prefix());
|
||||
assert_eq!(&k[..32], &<no_instance::Map<Runtime>>::final_prefix());
|
||||
|
||||
no_instance::Map2::insert(1, 2);
|
||||
<no_instance::Map2<Runtime>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"FinalKeysNone"), twox_128(b"Map2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<no_instance::Map2>::final_prefix());
|
||||
assert_eq!(&k[..32], &<no_instance::Map2<Runtime>>::final_prefix());
|
||||
|
||||
no_instance::DoubleMap::insert(&1, &2, &3);
|
||||
<no_instance::DoubleMap<Runtime>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"FinalKeysNone"), twox_128(b"DoubleMap")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
k.extend(2u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<no_instance::DoubleMap>::final_prefix());
|
||||
assert_eq!(&k[..32], &<no_instance::DoubleMap<Runtime>>::final_prefix());
|
||||
|
||||
no_instance::DoubleMap2::insert(&1, &2, &3);
|
||||
<no_instance::DoubleMap2<Runtime>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"FinalKeysNone"), twox_128(b"DoubleMap2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
k.extend(2u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<no_instance::DoubleMap2>::final_prefix());
|
||||
assert_eq!(&k[..32], &<no_instance::DoubleMap2<Runtime>>::final_prefix());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn final_keys_default_instance() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
<instance::Value<instance::DefaultInstance>>::put(1);
|
||||
<instance::Value<Runtime>>::put(1);
|
||||
let k = [twox_128(b"FinalKeysSome"), twox_128(b"Value")].concat();
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(1u32));
|
||||
|
||||
<instance::Map<instance::DefaultInstance>>::insert(1, 2);
|
||||
<instance::Map<Runtime>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"FinalKeysSome"), twox_128(b"Map")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<instance::Map<instance::DefaultInstance>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::Map<Runtime>>::final_prefix());
|
||||
|
||||
<instance::Map2<instance::DefaultInstance>>::insert(1, 2);
|
||||
<instance::Map2<Runtime>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"FinalKeysSome"), twox_128(b"Map2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<instance::Map2<instance::DefaultInstance>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::Map2<Runtime>>::final_prefix());
|
||||
|
||||
<instance::DoubleMap<instance::DefaultInstance>>::insert(&1, &2, &3);
|
||||
<instance::DoubleMap<Runtime>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"FinalKeysSome"), twox_128(b"DoubleMap")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
k.extend(2u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap<instance::DefaultInstance>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap<Runtime>>::final_prefix());
|
||||
|
||||
<instance::DoubleMap2<instance::DefaultInstance>>::insert(&1, &2, &3);
|
||||
<instance::DoubleMap2<Runtime>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"FinalKeysSome"), twox_128(b"DoubleMap2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
k.extend(2u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap2<instance::DefaultInstance>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap2<Runtime>>::final_prefix());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn final_keys_instance_2() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
<instance::Value<instance::Instance2>>::put(1);
|
||||
<instance::Value<Runtime, instance::Instance2>>::put(1);
|
||||
let k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"Value")].concat();
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(1u32));
|
||||
|
||||
<instance::Map<instance::Instance2>>::insert(1, 2);
|
||||
<instance::Map<Runtime, instance::Instance2>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"Map")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<instance::Map<instance::Instance2>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::Map<Runtime, instance::Instance2>>::final_prefix());
|
||||
|
||||
<instance::Map2<instance::Instance2>>::insert(1, 2);
|
||||
<instance::Map2<Runtime, instance::Instance2>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"Map2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<instance::Map2<instance::Instance2>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::Map2<Runtime, instance::Instance2>>::final_prefix());
|
||||
|
||||
<instance::DoubleMap<instance::Instance2>>::insert(&1, &2, &3);
|
||||
<instance::DoubleMap<Runtime, instance::Instance2>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"DoubleMap")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
k.extend(2u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap<instance::Instance2>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap<Runtime, instance::Instance2>>::final_prefix());
|
||||
|
||||
<instance::DoubleMap2<instance::Instance2>>::insert(&1, &2, &3);
|
||||
<instance::DoubleMap2<Runtime, instance::Instance2>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"DoubleMap2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
k.extend(2u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap2<instance::Instance2>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap2<Runtime, instance::Instance2>>::final_prefix());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,30 +15,86 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
use sp_core::sr25519;
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, Verify},
|
||||
};
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
}
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Test {
|
||||
pub AppendableDM config(t): double_map hasher(identity) u32, hasher(identity) T::BlockNumber => Vec<u32>;
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {}
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::unbounded]
|
||||
pub type AppendableDM<T: Config> =
|
||||
StorageDoubleMap<_, Identity, u32, Identity, T::BlockNumber, Vec<u32>>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
pub t: Vec<(u32, T::BlockNumber, Vec<u32>)>,
|
||||
}
|
||||
|
||||
impl<T: Config> Default for GenesisConfig<T> {
|
||||
fn default() -> Self {
|
||||
Self { t: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
for (k1, k2, v) in &self.t {
|
||||
<AppendableDM<T>>::insert(k1, k2, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Test;
|
||||
pub type BlockNumber = u32;
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Test
|
||||
where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: frame_support_test,
|
||||
MyPallet: pallet,
|
||||
}
|
||||
);
|
||||
|
||||
impl frame_support_test::Config for Test {
|
||||
type BlockNumber = u32;
|
||||
type RuntimeOrigin = ();
|
||||
type PalletInfo = frame_support_test::PanicPalletInfo;
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl Config for Test {}
|
||||
impl pallet::Config for Test {}
|
||||
|
||||
#[test]
|
||||
fn init_genesis_config() {
|
||||
GenesisConfig::<Test>::default();
|
||||
pallet::GenesisConfig::<Test>::default();
|
||||
}
|
||||
|
||||
@@ -17,111 +17,111 @@
|
||||
|
||||
#![recursion_limit = "128"]
|
||||
|
||||
use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen};
|
||||
use frame_support::{
|
||||
inherent::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent},
|
||||
metadata_ir::{
|
||||
PalletStorageMetadataIR, StorageEntryMetadataIR, StorageEntryModifierIR,
|
||||
StorageEntryTypeIR, StorageHasherIR,
|
||||
},
|
||||
traits::{ConstU32, Get},
|
||||
Parameter, StorageDoubleMap, StorageMap, StorageValue,
|
||||
traits::ConstU32,
|
||||
};
|
||||
use scale_info::TypeInfo;
|
||||
use sp_core::{sr25519, H256};
|
||||
use sp_core::sr25519;
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, Verify},
|
||||
BuildStorage,
|
||||
};
|
||||
|
||||
mod system;
|
||||
|
||||
pub trait Currency {}
|
||||
|
||||
// Test for:
|
||||
// * No default instance
|
||||
// * Origin, Inherent, Event
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
mod module1 {
|
||||
use self::frame_system::pallet_prelude::*;
|
||||
use super::*;
|
||||
use sp_std::ops::Add;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
pub trait Config<I>: system::Config
|
||||
where
|
||||
<Self as system::Config>::BlockNumber: From<u32>,
|
||||
{
|
||||
type RuntimeEvent: From<Event<Self, I>> + Into<<Self as system::Config>::RuntimeEvent>;
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T, I = ()>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config<I: 'static = ()>: frame_system::Config {
|
||||
type RuntimeEvent: From<Event<Self, I>>
|
||||
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
type RuntimeOrigin: From<Origin<Self, I>>;
|
||||
type SomeParameter: Get<u32>;
|
||||
type GenericType: Default + Clone + Codec + EncodeLike + TypeInfo;
|
||||
type GenericType: Parameter + Member + MaybeSerializeDeserialize + Default + MaxEncodedLen;
|
||||
}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config<I>, I: Instance> for enum Call where
|
||||
origin: <T as system::Config>::RuntimeOrigin,
|
||||
system = system,
|
||||
T::BlockNumber: From<u32>
|
||||
{
|
||||
fn offchain_worker() {}
|
||||
|
||||
fn deposit_event() = default;
|
||||
|
||||
#[weight = 0]
|
||||
fn one(origin) {
|
||||
system::ensure_root(origin)?;
|
||||
Self::deposit_event(RawEvent::AnotherVariant(3));
|
||||
}
|
||||
#[pallet::call]
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
#[pallet::weight(0)]
|
||||
pub fn one(origin: OriginFor<T>) -> DispatchResult {
|
||||
ensure_root(origin)?;
|
||||
Self::deposit_event(Event::AnotherVariant(3));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config<I>, I: Instance> as Module1 where
|
||||
T::BlockNumber: From<u32> + std::fmt::Display
|
||||
{
|
||||
pub Value config(value): T::GenericType;
|
||||
pub Map: map hasher(identity) u32 => u64;
|
||||
}
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn value)]
|
||||
pub type Value<T: Config<I>, I: 'static = ()> = StorageValue<_, T::GenericType, ValueQuery>;
|
||||
|
||||
add_extra_genesis {
|
||||
config(test) : T::BlockNumber;
|
||||
build(|config: &Self| {
|
||||
println!("{}", config.test);
|
||||
});
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn map)]
|
||||
pub type Map<T: Config<I>, I: 'static = ()> = StorageMap<_, Identity, u32, u64, ValueQuery>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
|
||||
pub value: <T as Config<I>>::GenericType,
|
||||
pub test: <T as frame_system::Config>::BlockNumber,
|
||||
}
|
||||
|
||||
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
||||
fn default() -> Self {
|
||||
Self { value: Default::default(), test: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_error! {
|
||||
pub enum Error for Module<T: Config<I>, I: Instance> where
|
||||
T::BlockNumber: From<u32>,
|
||||
T::BlockNumber: Add,
|
||||
T::AccountId: AsRef<[u8]>,
|
||||
{
|
||||
/// Test
|
||||
Test,
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_event! {
|
||||
pub enum Event<T, I> where Phantom = std::marker::PhantomData<T> {
|
||||
_Phantom(Phantom),
|
||||
AnotherVariant(u32),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
PartialEq, Eq, Clone, sp_runtime::RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub enum Origin<T: Config<I>, I>
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I>
|
||||
where
|
||||
T::BlockNumber: From<u32>,
|
||||
T::BlockNumber: std::fmt::Display,
|
||||
{
|
||||
fn build(&self) {
|
||||
<Value<T, I>>::put(self.value.clone());
|
||||
println!("{}", self.test);
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T, I = ()> {
|
||||
/// Test
|
||||
Test,
|
||||
}
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||
pub enum Event<T: Config<I>, I: 'static = ()> {
|
||||
_Phantom(PhantomData<T>),
|
||||
AnotherVariant(u32),
|
||||
}
|
||||
|
||||
#[pallet::origin]
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
#[scale_info(skip_type_params(I))]
|
||||
pub enum Origin<T, I = ()> {
|
||||
Members(u32),
|
||||
_Phantom(std::marker::PhantomData<(T, I)>),
|
||||
_Phantom(PhantomData<(T, I)>),
|
||||
}
|
||||
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"12345678";
|
||||
|
||||
impl<T: Config<I>, I: Instance> ProvideInherent for Module<T, I>
|
||||
#[pallet::inherent]
|
||||
impl<T: Config<I>, I: 'static> ProvideInherent for Pallet<T, I>
|
||||
where
|
||||
T::BlockNumber: From<u32>,
|
||||
{
|
||||
@@ -133,10 +133,7 @@ mod module1 {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn check_inherent(
|
||||
_: &Self::Call,
|
||||
_: &InherentData,
|
||||
) -> std::result::Result<(), Self::Error> {
|
||||
fn check_inherent(_: &Self::Call, _: &InherentData) -> Result<(), Self::Error> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
@@ -149,51 +146,88 @@ mod module1 {
|
||||
// Test for:
|
||||
// * default instance
|
||||
// * use of no_genesis_config_phantom_data
|
||||
#[frame_support::pallet]
|
||||
mod module2 {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
pub trait Config<I = DefaultInstance>: system::Config {
|
||||
type Amount: Parameter + Default;
|
||||
type RuntimeEvent: From<Event<Self, I>> + Into<<Self as system::Config>::RuntimeEvent>;
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config<I: 'static = ()>: frame_system::Config {
|
||||
type Amount: Parameter + MaybeSerializeDeserialize + Default + MaxEncodedLen;
|
||||
type RuntimeEvent: From<Event<Self, I>>
|
||||
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
type RuntimeOrigin: From<Origin<Self, I>>;
|
||||
}
|
||||
|
||||
impl<T: Config<I>, I: Instance> Currency for Module<T, I> {}
|
||||
impl<T: Config<I>, I: 'static> Currency for Pallet<T, I> {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config<I>, I: Instance=DefaultInstance> for enum Call where
|
||||
origin: <T as system::Config>::RuntimeOrigin,
|
||||
system = system
|
||||
{
|
||||
fn deposit_event() = default;
|
||||
#[pallet::call]
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Value<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Amount, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Map<T: Config<I>, I: 'static = ()> = StorageMap<_, Identity, u64, u64, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap<T: Config<I>, I: 'static = ()> =
|
||||
StorageDoubleMap<_, Identity, u64, Identity, u64, u64, ValueQuery>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
|
||||
pub value: T::Amount,
|
||||
pub map: Vec<(u64, u64)>,
|
||||
pub double_map: Vec<(u64, u64, u64)>,
|
||||
}
|
||||
|
||||
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: Default::default(),
|
||||
map: Default::default(),
|
||||
double_map: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config<I>, I: Instance=DefaultInstance> as Module2 {
|
||||
pub Value config(value): T::Amount;
|
||||
pub Map config(map): map hasher(identity) u64 => u64;
|
||||
pub DoubleMap config(double_map): double_map hasher(identity) u64, hasher(identity) u64 => u64;
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I>
|
||||
where
|
||||
T::BlockNumber: std::fmt::Display,
|
||||
{
|
||||
fn build(&self) {
|
||||
<Value<T, I>>::put(self.value.clone());
|
||||
for (k, v) in &self.map {
|
||||
<Map<T, I>>::insert(k, v);
|
||||
}
|
||||
for (k1, k2, v) in &self.double_map {
|
||||
<DoubleMap<T, I>>::insert(k1, k2, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_event! {
|
||||
pub enum Event<T, I=DefaultInstance> where Amount = <T as Config<I>>::Amount {
|
||||
Variant(Amount),
|
||||
}
|
||||
#[pallet::event]
|
||||
pub enum Event<T: Config<I>, I: 'static = ()> {
|
||||
Variant(T::Amount),
|
||||
}
|
||||
|
||||
#[derive(
|
||||
PartialEq, Eq, Clone, sp_runtime::RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub enum Origin<T: Config<I>, I = DefaultInstance> {
|
||||
#[pallet::origin]
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
#[scale_info(skip_type_params(I))]
|
||||
pub enum Origin<T, I = ()> {
|
||||
Members(u32),
|
||||
_Phantom(std::marker::PhantomData<(T, I)>),
|
||||
_Phantom(PhantomData<(T, I)>),
|
||||
}
|
||||
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"12345678";
|
||||
|
||||
impl<T: Config<I>, I: Instance> ProvideInherent for Module<T, I> {
|
||||
#[pallet::inherent]
|
||||
impl<T: Config<I>, I: 'static> ProvideInherent for Pallet<T, I> {
|
||||
type Call = Call<T, I>;
|
||||
type Error = MakeFatalError<()>;
|
||||
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
|
||||
@@ -202,10 +236,7 @@ mod module2 {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn check_inherent(
|
||||
_call: &Self::Call,
|
||||
_data: &InherentData,
|
||||
) -> std::result::Result<(), Self::Error> {
|
||||
fn check_inherent(_call: &Self::Call, _data: &InherentData) -> Result<(), Self::Error> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
@@ -217,19 +248,70 @@ mod module2 {
|
||||
|
||||
// Test for:
|
||||
// * Depends on multiple instances of a module with instances
|
||||
#[frame_support::pallet]
|
||||
mod module3 {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
pub trait Config:
|
||||
module2::Config + module2::Config<module2::Instance1> + system::Config
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config<I: 'static = ()>:
|
||||
frame_system::Config + module2::Config<I> + module2::Config<module2::Instance1>
|
||||
{
|
||||
type Currency: Currency;
|
||||
type Currency2: Currency;
|
||||
}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: <T as system::Config>::RuntimeOrigin, system=system {}
|
||||
#[pallet::call]
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {}
|
||||
}
|
||||
|
||||
pub type BlockNumber = u32;
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Runtime where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: frame_support_test::{Pallet, Call, Event<T>},
|
||||
Module1_1: module1::<Instance1>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module1_2: module1::<Instance2>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module2: module2::{Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent},
|
||||
Module2_1: module2::<Instance1>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module2_2: module2::<Instance2>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module2_3: module2::<Instance3>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module3: module3::{Pallet, Call},
|
||||
}
|
||||
);
|
||||
|
||||
impl frame_support_test::Config for Runtime {
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl module1::Config<module1::Instance1> for Runtime {
|
||||
@@ -269,54 +351,6 @@ impl module3::Config for Runtime {
|
||||
type Currency2 = Module2_3;
|
||||
}
|
||||
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type BlockNumber = u64;
|
||||
pub type Index = u64;
|
||||
|
||||
impl system::Config for Runtime {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type Hash = H256;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub struct Runtime where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: system::{Pallet, Call, Event<T>},
|
||||
Module1_1: module1::<Instance1>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module1_2: module1::<Instance2>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module2: module2::{Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent},
|
||||
Module2_1: module2::<Instance1>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module2_2: module2::<Instance2>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module2_3: module2::<Instance3>::{
|
||||
Pallet, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module3: module3::{Pallet, Call},
|
||||
}
|
||||
);
|
||||
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
|
||||
fn new_test_ext() -> sp_io::TestExternalities {
|
||||
GenesisConfig {
|
||||
module_1_1: module1::GenesisConfig { value: 3, test: 2 },
|
||||
@@ -350,14 +384,14 @@ fn storage_instance_independence() {
|
||||
module2::Value::<Runtime, module2::Instance1>::put(0);
|
||||
module2::Value::<Runtime, module2::Instance2>::put(0);
|
||||
module2::Value::<Runtime, module2::Instance3>::put(0);
|
||||
module2::Map::<module2::DefaultInstance>::insert(0, 0);
|
||||
module2::Map::<module2::Instance1>::insert(0, 0);
|
||||
module2::Map::<module2::Instance2>::insert(0, 0);
|
||||
module2::Map::<module2::Instance3>::insert(0, 0);
|
||||
module2::DoubleMap::<module2::DefaultInstance>::insert(&0, &0, &0);
|
||||
module2::DoubleMap::<module2::Instance1>::insert(&0, &0, &0);
|
||||
module2::DoubleMap::<module2::Instance2>::insert(&0, &0, &0);
|
||||
module2::DoubleMap::<module2::Instance3>::insert(&0, &0, &0);
|
||||
module2::Map::<Runtime>::insert(0, 0);
|
||||
module2::Map::<Runtime, module2::Instance1>::insert(0, 0);
|
||||
module2::Map::<Runtime, module2::Instance2>::insert(0, 0);
|
||||
module2::Map::<Runtime, module2::Instance3>::insert(0, 0);
|
||||
module2::DoubleMap::<Runtime>::insert(&0, &0, &0);
|
||||
module2::DoubleMap::<Runtime, module2::Instance1>::insert(&0, &0, &0);
|
||||
module2::DoubleMap::<Runtime, module2::Instance2>::insert(&0, &0, &0);
|
||||
module2::DoubleMap::<Runtime, module2::Instance3>::insert(&0, &0, &0);
|
||||
});
|
||||
// 12 storage values.
|
||||
assert_eq!(storage.top.len(), 12);
|
||||
@@ -367,8 +401,8 @@ fn storage_instance_independence() {
|
||||
fn storage_with_instance_basic_operation() {
|
||||
new_test_ext().execute_with(|| {
|
||||
type Value = module2::Value<Runtime, module2::Instance1>;
|
||||
type Map = module2::Map<module2::Instance1>;
|
||||
type DoubleMap = module2::DoubleMap<module2::Instance1>;
|
||||
type Map = module2::Map<Runtime, module2::Instance1>;
|
||||
type DoubleMap = module2::DoubleMap<Runtime, module2::Instance1>;
|
||||
|
||||
assert_eq!(Value::exists(), true);
|
||||
assert_eq!(Value::get(), 4);
|
||||
@@ -412,7 +446,7 @@ fn storage_with_instance_basic_operation() {
|
||||
|
||||
fn expected_metadata() -> PalletStorageMetadataIR {
|
||||
PalletStorageMetadataIR {
|
||||
prefix: "Instance2Module2",
|
||||
prefix: "Module2_2",
|
||||
entries: vec![
|
||||
StorageEntryMetadataIR {
|
||||
name: "Value",
|
||||
|
||||
@@ -15,32 +15,28 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use frame_support::{
|
||||
codec::{Decode, Encode},
|
||||
scale_info::TypeInfo,
|
||||
sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, Verify},
|
||||
},
|
||||
use sp_core::{sr25519, ConstU64};
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, Verify},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sp_core::{sr25519, H256};
|
||||
|
||||
mod system;
|
||||
|
||||
#[frame_support::pallet]
|
||||
mod module {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
pub type Request<T> =
|
||||
(<T as system::Config>::AccountId, Role, <T as system::Config>::BlockNumber);
|
||||
(<T as frame_system::Config>::AccountId, Role, <T as frame_system::Config>::BlockNumber);
|
||||
pub type Requests<T> = Vec<Request<T>>;
|
||||
|
||||
#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
pub enum Role {
|
||||
Storage,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
pub struct RoleParameters<T: Config> {
|
||||
// minimum actors to maintain - if role is unstaking
|
||||
// and remaining actors would be less that this value - prevent or punish for unstaking
|
||||
@@ -83,102 +79,108 @@ mod module {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Config: system::Config + TypeInfo {}
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=system {}
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config + TypeInfo {}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {}
|
||||
|
||||
/// requirements to enter and maintain status in roles
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn parameters)]
|
||||
pub type Parameters<T: Config> =
|
||||
StorageMap<_, Blake2_128Concat, Role, RoleParameters<T>, OptionQuery>;
|
||||
|
||||
/// the roles members can enter into
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn available_roles)]
|
||||
#[pallet::unbounded]
|
||||
pub type AvailableRoles<T: Config> = StorageValue<_, Vec<Role>, ValueQuery>;
|
||||
|
||||
/// Actors list
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn actor_account_ids)]
|
||||
#[pallet::unbounded]
|
||||
pub type ActorAccountIds<T: Config> = StorageValue<_, Vec<T::AccountId>>;
|
||||
|
||||
/// actor accounts associated with a role
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn account_ids_by_role)]
|
||||
#[pallet::unbounded]
|
||||
pub type AccountIdsByRole<T: Config> = StorageMap<_, Blake2_128Concat, Role, Vec<T::AccountId>>;
|
||||
|
||||
/// tokens locked until given block number
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn bondage)]
|
||||
pub type Bondage<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, T::BlockNumber>;
|
||||
|
||||
/// First step before enter a role is registering intent with a new account/key.
|
||||
/// This is done by sending a role_entry_request() from the new account.
|
||||
/// The member must then send a stake() transaction to approve the request and enter the desired
|
||||
/// role. The account making the request will be bonded and must have
|
||||
/// sufficient balance to cover the minimum stake for the role.
|
||||
/// Bonding only occurs after successful entry into a role.
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn role_entry_requests)]
|
||||
#[pallet::unbounded]
|
||||
pub type RoleEntryRequests<T: Config> = StorageValue<_, Requests<T>>;
|
||||
|
||||
/// Entry request expires after this number of blocks
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn request_life_time)]
|
||||
pub type RequestLifeTime<T: Config> = StorageValue<_, u64, ValueQuery, ConstU64<0>>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[derive(Default)]
|
||||
pub struct GenesisConfig {
|
||||
pub enable_storage_role: bool,
|
||||
pub request_life_time: u64,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Copy, Clone, Serialize, Deserialize)]
|
||||
pub struct Data<T: Config> {
|
||||
pub data: T::BlockNumber,
|
||||
}
|
||||
|
||||
impl<T: Config> Default for Data<T> {
|
||||
fn default() -> Self {
|
||||
Self { data: T::BlockNumber::default() }
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Actors {
|
||||
/// requirements to enter and maintain status in roles
|
||||
pub Parameters get(fn parameters) build(|config: &GenesisConfig| {
|
||||
if config.enable_storage_role {
|
||||
let storage_params: RoleParameters<T> = Default::default();
|
||||
vec![(Role::Storage, storage_params)]
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}): map hasher(blake2_128_concat) Role => Option<RoleParameters<T>>;
|
||||
|
||||
/// the roles members can enter into
|
||||
pub AvailableRoles get(fn available_roles) build(|config: &GenesisConfig| {
|
||||
if config.enable_storage_role {
|
||||
vec![(Role::Storage)]
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}): Vec<Role>;
|
||||
|
||||
/// Actors list
|
||||
pub ActorAccountIds get(fn actor_account_ids) : Vec<T::AccountId>;
|
||||
|
||||
/// actor accounts associated with a role
|
||||
pub AccountIdsByRole get(fn account_ids_by_role):
|
||||
map hasher(blake2_128_concat) Role => Vec<T::AccountId>;
|
||||
|
||||
/// tokens locked until given block number
|
||||
pub Bondage get(fn bondage):
|
||||
map hasher(blake2_128_concat) T::AccountId => T::BlockNumber;
|
||||
|
||||
/// First step before enter a role is registering intent with a new account/key.
|
||||
/// This is done by sending a role_entry_request() from the new account.
|
||||
/// The member must then send a stake() transaction to approve the request and enter the desired role.
|
||||
/// The account making the request will be bonded and must have
|
||||
/// sufficient balance to cover the minimum stake for the role.
|
||||
/// Bonding only occurs after successful entry into a role.
|
||||
pub RoleEntryRequests get(fn role_entry_requests) : Requests<T>;
|
||||
|
||||
/// Entry request expires after this number of blocks
|
||||
pub RequestLifeTime get(fn request_life_time) config(request_life_time) : u64 = 0;
|
||||
}
|
||||
add_extra_genesis {
|
||||
config(enable_storage_role): bool;
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig {
|
||||
fn build(&self) {
|
||||
if self.enable_storage_role {
|
||||
<Parameters<T>>::insert(Role::Storage, <RoleParameters<T>>::default());
|
||||
<AvailableRoles<T>>::put(vec![Role::Storage]);
|
||||
}
|
||||
<RequestLifeTime<T>>::put(self.request_life_time);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type BlockNumber = u64;
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type BlockNumber = u64;
|
||||
pub type Index = u64;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
impl system::Config for Runtime {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type Hash = H256;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
impl frame_support_test::Config for Runtime {
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl module::Config for Runtime {}
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub struct Runtime where
|
||||
pub struct Runtime
|
||||
where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: system::{Pallet, Call, Event<T>},
|
||||
Module: module::{Pallet, Call, Storage, Config},
|
||||
System: frame_support_test,
|
||||
Module: module,
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -19,119 +19,123 @@
|
||||
|
||||
#![recursion_limit = "128"]
|
||||
|
||||
use codec::MaxEncodedLen;
|
||||
use frame_support::traits::{Contains, OriginTrait};
|
||||
use scale_info::TypeInfo;
|
||||
use sp_core::{sr25519, H256};
|
||||
use sp_runtime::{generic, traits::BlakeTwo256};
|
||||
|
||||
mod system;
|
||||
|
||||
mod nested {
|
||||
use super::*;
|
||||
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
pub mod module {
|
||||
use self::frame_system::pallet_prelude::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
use super::*;
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
pub trait Config: system::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call
|
||||
where origin: <T as system::Config>::RuntimeOrigin, system=system
|
||||
{
|
||||
#[weight = 0]
|
||||
pub fn fail(_origin) -> frame_support::dispatch::DispatchResult {
|
||||
Err(Error::<T>::Something.into())
|
||||
}
|
||||
}
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
type RuntimeEvent: From<Event<Self>>
|
||||
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub struct Origin;
|
||||
|
||||
frame_support::decl_event! {
|
||||
pub enum Event {
|
||||
A,
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_error! {
|
||||
pub enum Error for Module<T: Config> {
|
||||
Something
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Module {}
|
||||
add_extra_genesis {
|
||||
build(|_config| {})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod module {
|
||||
use super::*;
|
||||
|
||||
pub trait Config: system::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call
|
||||
where origin: <T as system::Config>::RuntimeOrigin, system=system
|
||||
{
|
||||
#[weight = 0]
|
||||
pub fn fail(_origin) -> frame_support::dispatch::DispatchResult {
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
pub fn fail(_origin: OriginFor<T>) -> DispatchResult {
|
||||
Err(Error::<T>::Something.into())
|
||||
}
|
||||
#[weight = 0]
|
||||
pub fn aux_1(_origin, #[compact] _data: u32) -> frame_support::dispatch::DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[weight = 0]
|
||||
pub fn aux_2(_origin, _data: i32, #[compact] _data2: u32) -> frame_support::dispatch::DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[weight = 0]
|
||||
fn aux_3(_origin, _data: i32, _data2: String) -> frame_support::dispatch::DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[weight = 3]
|
||||
fn aux_4(_origin) -> frame_support::dispatch::DispatchResult { unreachable!() }
|
||||
#[weight = (5, frame_support::dispatch::DispatchClass::Operational)]
|
||||
fn operational(_origin) { unreachable!() }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub struct Origin<T>(pub core::marker::PhantomData<T>);
|
||||
#[pallet::origin]
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
pub struct Origin;
|
||||
|
||||
frame_support::decl_event! {
|
||||
pub enum Event {
|
||||
#[pallet::event]
|
||||
pub enum Event<T> {
|
||||
A,
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_error! {
|
||||
pub enum Error for Module<T: Config> {
|
||||
Something
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
Something,
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as Module {}
|
||||
add_extra_genesis {
|
||||
build(|_config| {})
|
||||
#[pallet::genesis_config]
|
||||
#[derive(Default)]
|
||||
pub struct GenesisConfig {}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig {
|
||||
fn build(&self) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl nested::module::Config for RuntimeOriginTest {}
|
||||
impl module::Config for RuntimeOriginTest {}
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
pub mod module {
|
||||
use self::frame_system::pallet_prelude::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
pub fn fail(_origin: OriginFor<T>) -> DispatchResult {
|
||||
Err(Error::<T>::Something.into())
|
||||
}
|
||||
pub fn aux_1(_origin: OriginFor<T>, #[pallet::compact] _data: u32) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
pub fn aux_2(
|
||||
_origin: OriginFor<T>,
|
||||
_data: i32,
|
||||
#[pallet::compact] _data2: u32,
|
||||
) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[pallet::weight(0)]
|
||||
pub fn aux_3(_origin: OriginFor<T>, _data: i32, _data2: String) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[pallet::weight(3)]
|
||||
pub fn aux_4(_origin: OriginFor<T>) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
#[pallet::weight((5, DispatchClass::Operational))]
|
||||
pub fn operational(_origin: OriginFor<T>) -> DispatchResult {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::origin]
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
pub struct Origin<T>(pub PhantomData<T>);
|
||||
|
||||
#[pallet::event]
|
||||
pub enum Event<T> {
|
||||
A,
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
Something,
|
||||
}
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[derive(Default)]
|
||||
pub struct GenesisConfig {}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig {
|
||||
fn build(&self) {}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BaseCallFilter;
|
||||
impl Contains<RuntimeCall> for BaseCallFilter {
|
||||
@@ -143,17 +147,11 @@ impl Contains<RuntimeCall> for BaseCallFilter {
|
||||
}
|
||||
}
|
||||
|
||||
impl system::Config for RuntimeOriginTest {
|
||||
type BaseCallFilter = BaseCallFilter;
|
||||
type Hash = H256;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = u32;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type DbWeight = ();
|
||||
}
|
||||
pub type BlockNumber = u32;
|
||||
pub type AccountId = u32;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, (), ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum RuntimeOriginTest where
|
||||
@@ -161,17 +159,29 @@ frame_support::construct_runtime!(
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: system::{Pallet, Event<T>, Origin<T>},
|
||||
NestedModule: nested::module::{Pallet, Origin, Call},
|
||||
Module: module::{Pallet, Origin<T>, Call},
|
||||
System: frame_support_test,
|
||||
NestedModule: nested::module,
|
||||
Module: module,
|
||||
}
|
||||
);
|
||||
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type BlockNumber = u64;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
impl frame_support_test::Config for RuntimeOriginTest {
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type BaseCallFilter = BaseCallFilter;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl nested::module::Config for RuntimeOriginTest {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
impl module::Config for RuntimeOriginTest {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn origin_default_filter() {
|
||||
@@ -199,7 +209,7 @@ fn origin_default_filter() {
|
||||
// Now test for root origin and filters:
|
||||
let mut origin = RuntimeOrigin::from(Some(0));
|
||||
origin.set_caller_from(RuntimeOrigin::root());
|
||||
assert!(matches!(origin.caller, OriginCaller::system(system::RawOrigin::Root)));
|
||||
assert!(matches!(origin.caller, OriginCaller::system(frame_support_test::RawOrigin::Root)));
|
||||
|
||||
// Root origin bypass all filter.
|
||||
assert_eq!(origin.filter_call(&accepted_call), true);
|
||||
|
||||
@@ -1,371 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Old macros don't support the flag `no-metadata-docs` so the result differs when the feature is
|
||||
// activated.
|
||||
#![cfg(not(feature = "no-metadata-docs"))]
|
||||
|
||||
use frame_support::traits::{ConstU32, ConstU64};
|
||||
|
||||
pub trait SomeAssociation {
|
||||
type A: frame_support::dispatch::Parameter + Default;
|
||||
}
|
||||
impl SomeAssociation for u64 {
|
||||
type A = u64;
|
||||
}
|
||||
|
||||
mod pallet_old {
|
||||
use super::SomeAssociation;
|
||||
use frame_support::{
|
||||
decl_error, decl_event, decl_module, decl_storage, traits::Get, weights::Weight, Parameter,
|
||||
};
|
||||
use frame_system::ensure_root;
|
||||
|
||||
pub trait Config: frame_system::Config {
|
||||
type SomeConst: Get<Self::Balance>;
|
||||
type Balance: Parameter
|
||||
+ codec::HasCompact
|
||||
+ From<u32>
|
||||
+ Into<u64>
|
||||
+ Default
|
||||
+ SomeAssociation;
|
||||
type RuntimeEvent: From<Event<Self>> + Into<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Config> as Example {
|
||||
/// Some documentation
|
||||
Dummy get(fn dummy) config(): Option<T::Balance>;
|
||||
Bar get(fn bar) config(): map hasher(blake2_128_concat) T::AccountId => T::Balance;
|
||||
Foo get(fn foo) config(): T::Balance = 3.into();
|
||||
Double get(fn double): double_map
|
||||
hasher(blake2_128_concat) u32,
|
||||
hasher(twox_64_concat) u64
|
||||
=> <T::Balance as SomeAssociation>::A;
|
||||
}
|
||||
}
|
||||
|
||||
decl_event!(
|
||||
pub enum Event<T>
|
||||
where
|
||||
Balance = <T as Config>::Balance,
|
||||
{
|
||||
/// Dummy event, just here so there's a generic type that's used.
|
||||
Dummy(Balance),
|
||||
}
|
||||
);
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
|
||||
type Error = Error<T>;
|
||||
fn deposit_event() = default;
|
||||
const SomeConst: T::Balance = T::SomeConst::get();
|
||||
|
||||
#[weight = <T::Balance as Into<u64>>::into(new_value.clone())]
|
||||
fn set_dummy(origin, #[compact] new_value: T::Balance) {
|
||||
ensure_root(origin)?;
|
||||
|
||||
<Dummy<T>>::put(&new_value);
|
||||
Self::deposit_event(RawEvent::Dummy(new_value));
|
||||
}
|
||||
|
||||
fn on_initialize(_n: T::BlockNumber) -> Weight {
|
||||
<Dummy<T>>::put(T::Balance::from(10));
|
||||
Weight::from_parts(10, 0)
|
||||
}
|
||||
|
||||
fn on_finalize(_n: T::BlockNumber) {
|
||||
<Dummy<T>>::put(T::Balance::from(11));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decl_error! {
|
||||
pub enum Error for Module<T: Config> {
|
||||
/// Some wrong behavior
|
||||
Wrong,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::SomeAssociation;
|
||||
use frame_support::{pallet_prelude::*, scale_info};
|
||||
use frame_system::{ensure_root, pallet_prelude::*};
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
type Balance: Parameter
|
||||
+ codec::HasCompact
|
||||
+ From<u32>
|
||||
+ Into<u64>
|
||||
+ Default
|
||||
+ MaybeSerializeDeserialize
|
||||
+ SomeAssociation
|
||||
+ scale_info::StaticTypeInfo;
|
||||
#[pallet::constant]
|
||||
type SomeConst: Get<Self::Balance>;
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::without_storage_info]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
|
||||
fn on_initialize(_n: T::BlockNumber) -> Weight {
|
||||
<Dummy<T>>::put(T::Balance::from(10));
|
||||
Weight::from_parts(10, 0)
|
||||
}
|
||||
|
||||
fn on_finalize(_n: T::BlockNumber) {
|
||||
<Dummy<T>>::put(T::Balance::from(11));
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
#[pallet::call_index(0)]
|
||||
#[pallet::weight(<T::Balance as Into<u64>>::into(new_value.clone()))]
|
||||
pub fn set_dummy(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] new_value: T::Balance,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
ensure_root(origin)?;
|
||||
|
||||
<Dummy<T>>::put(&new_value);
|
||||
Self::deposit_event(Event::Dummy(new_value));
|
||||
|
||||
Ok(().into())
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
/// Some wrong behavior
|
||||
Wrong,
|
||||
}
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(fn deposit_event)]
|
||||
pub enum Event<T: Config> {
|
||||
/// Dummy event, just here so there's a generic type that's used.
|
||||
Dummy(T::Balance),
|
||||
}
|
||||
|
||||
#[pallet::storage]
|
||||
/// Some documentation
|
||||
type Dummy<T: Config> = StorageValue<_, T::Balance, OptionQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
type Bar<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, T::Balance, ValueQuery>;
|
||||
|
||||
#[pallet::type_value]
|
||||
pub fn OnFooEmpty<T: Config>() -> T::Balance {
|
||||
3.into()
|
||||
}
|
||||
#[pallet::storage]
|
||||
type Foo<T: Config> = StorageValue<_, T::Balance, ValueQuery, OnFooEmpty<T>>;
|
||||
|
||||
#[pallet::storage]
|
||||
type Double<T: Config> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
u32,
|
||||
Twox64Concat,
|
||||
u64,
|
||||
<T::Balance as SomeAssociation>::A,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
dummy: Option<T::Balance>,
|
||||
bar: Vec<(T::AccountId, T::Balance)>,
|
||||
foo: T::Balance,
|
||||
}
|
||||
|
||||
impl<T: Config> Default for GenesisConfig<T> {
|
||||
fn default() -> Self {
|
||||
GenesisConfig {
|
||||
dummy: Default::default(),
|
||||
bar: Default::default(),
|
||||
foo: OnFooEmpty::<T>::get(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
if let Some(dummy) = self.dummy.as_ref() {
|
||||
<Dummy<T>>::put(dummy);
|
||||
}
|
||||
for (k, v) in &self.bar {
|
||||
<Bar<T>>::insert(k, v);
|
||||
}
|
||||
<Foo<T>>::put(&self.foo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl frame_system::Config for Runtime {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u32;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Hash = sp_runtime::testing::H256;
|
||||
type Hashing = sp_runtime::traits::BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type BlockHashCount = ConstU32<250>;
|
||||
type BlockWeights = ();
|
||||
type BlockLength = ();
|
||||
type DbWeight = ();
|
||||
type Version = ();
|
||||
type PalletInfo = PalletInfo;
|
||||
type AccountData = ();
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
type SystemWeightInfo = ();
|
||||
type SS58Prefix = ();
|
||||
type OnSetCode = ();
|
||||
type MaxConsumers = ConstU32<16>;
|
||||
}
|
||||
impl pallet::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type SomeConst = ConstU64<10>;
|
||||
type Balance = u64;
|
||||
}
|
||||
impl pallet_old::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type SomeConst = ConstU64<10>;
|
||||
type Balance = u64;
|
||||
}
|
||||
|
||||
pub type Header = sp_runtime::generic::Header<u32, sp_runtime::traits::BlakeTwo256>;
|
||||
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, RuntimeCall, (), ()>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub struct Runtime where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: frame_system::{Pallet, Call, Event<T>},
|
||||
// NOTE: name Example here is needed in order to have same module prefix
|
||||
Example: pallet::{Pallet, Call, Event<T>, Config<T>, Storage},
|
||||
PalletOld: pallet_old::{Pallet, Call, Event<T>, Config<T>, Storage},
|
||||
}
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{pallet, pallet_old, Runtime};
|
||||
use codec::{Decode, Encode};
|
||||
use scale_info::{form::PortableForm, Variant};
|
||||
|
||||
#[test]
|
||||
fn metadata() {
|
||||
let metadata = Runtime::metadata();
|
||||
let (pallets, types) = match metadata.1 {
|
||||
frame_support::metadata::RuntimeMetadata::V14(metadata) =>
|
||||
(metadata.pallets, metadata.types),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let assert_meta_types = |ty_id1, ty_id2| {
|
||||
let ty1 = types.resolve(ty_id1).map(|ty| ty.type_def.clone());
|
||||
let ty2 = types.resolve(ty_id2).map(|ty| ty.type_def.clone());
|
||||
pretty_assertions::assert_eq!(ty1, ty2);
|
||||
};
|
||||
|
||||
let get_enum_variants = |ty_id| match types.resolve(ty_id).map(|ty| ty.type_def.clone()) {
|
||||
Some(ty) => match ty {
|
||||
scale_info::TypeDef::Variant(var) => var.variants,
|
||||
_ => panic!("Expected variant type"),
|
||||
},
|
||||
_ => panic!("No type found"),
|
||||
};
|
||||
|
||||
let assert_enum_variants = |vs1: &[Variant<PortableForm>],
|
||||
vs2: &[Variant<PortableForm>]| {
|
||||
assert_eq!(vs1.len(), vs2.len());
|
||||
for i in 0..vs1.len() {
|
||||
let v1 = &vs2[i];
|
||||
let v2 = &vs2[i];
|
||||
assert_eq!(v1.fields.len(), v2.fields.len());
|
||||
for f in 0..v1.fields.len() {
|
||||
let f1 = &v1.fields[f];
|
||||
let f2 = &v2.fields[f];
|
||||
pretty_assertions::assert_eq!(f1.name, f2.name);
|
||||
pretty_assertions::assert_eq!(f1.ty, f2.ty);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pretty_assertions::assert_eq!(pallets[1].storage, pallets[2].storage);
|
||||
|
||||
let calls1 = pallets[1].calls.as_ref().unwrap();
|
||||
let calls2 = pallets[2].calls.as_ref().unwrap();
|
||||
assert_meta_types(calls1.ty.id, calls2.ty.id);
|
||||
|
||||
// event: check variants and fields but ignore the type name which will be different
|
||||
let event1_variants = get_enum_variants(pallets[1].event.as_ref().unwrap().ty.id);
|
||||
let event2_variants = get_enum_variants(pallets[2].event.as_ref().unwrap().ty.id);
|
||||
assert_enum_variants(&event1_variants, &event2_variants);
|
||||
|
||||
let err1 = get_enum_variants(pallets[1].error.as_ref().unwrap().ty.id)
|
||||
.iter()
|
||||
.filter(|v| v.name == "__Ignore")
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
let err2 = get_enum_variants(pallets[2].error.as_ref().unwrap().ty.id)
|
||||
.iter()
|
||||
.filter(|v| v.name == "__Ignore")
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
assert_enum_variants(&err1, &err2);
|
||||
|
||||
pretty_assertions::assert_eq!(pallets[1].constants, pallets[2].constants);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn types() {
|
||||
assert_eq!(
|
||||
pallet_old::Event::<Runtime>::decode(
|
||||
&mut &pallet::Event::<Runtime>::Dummy(10).encode()[..]
|
||||
)
|
||||
.unwrap(),
|
||||
pallet_old::Event::<Runtime>::Dummy(10),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
pallet_old::Call::<Runtime>::decode(
|
||||
&mut &pallet::Call::<Runtime>::set_dummy { new_value: 10 }.encode()[..]
|
||||
)
|
||||
.unwrap(),
|
||||
pallet_old::Call::<Runtime>::set_dummy { new_value: 10 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,371 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Old macros don't support the flag `no-metadata-docs` so the result differs when the feature is
|
||||
// activated.
|
||||
#![cfg(not(feature = "no-metadata-docs"))]
|
||||
|
||||
use frame_support::traits::{ConstU32, ConstU64};
|
||||
|
||||
mod pallet_old {
|
||||
use frame_support::{
|
||||
decl_error, decl_event, decl_module, decl_storage, traits::Get, weights::Weight, Parameter,
|
||||
};
|
||||
use frame_system::ensure_root;
|
||||
|
||||
pub trait Config<I: Instance = DefaultInstance>: frame_system::Config {
|
||||
type SomeConst: Get<Self::Balance>;
|
||||
type Balance: Parameter + codec::HasCompact + From<u32> + Into<u64> + Default;
|
||||
type RuntimeEvent: From<Event<Self, I>> + Into<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Config<I>, I: Instance = DefaultInstance> as Example {
|
||||
/// Some documentation
|
||||
Dummy get(fn dummy) config(): Option<T::Balance>;
|
||||
Bar get(fn bar) config(): map hasher(blake2_128_concat) T::AccountId => T::Balance;
|
||||
Foo get(fn foo) config(): T::Balance = 3.into();
|
||||
Double get(fn double):
|
||||
double_map hasher(blake2_128_concat) u32, hasher(twox_64_concat) u64 => u16;
|
||||
}
|
||||
}
|
||||
|
||||
decl_event!(
|
||||
pub enum Event<T, I = DefaultInstance>
|
||||
where
|
||||
Balance = <T as Config<I>>::Balance,
|
||||
{
|
||||
/// Dummy event, just here so there's a generic type that's used.
|
||||
Dummy(Balance),
|
||||
}
|
||||
);
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Config<I>, I: Instance = DefaultInstance> for enum Call
|
||||
where origin: T::RuntimeOrigin
|
||||
{
|
||||
type Error = Error<T, I>;
|
||||
fn deposit_event() = default;
|
||||
const SomeConst: T::Balance = T::SomeConst::get();
|
||||
|
||||
#[weight = <T::Balance as Into<u64>>::into(new_value.clone())]
|
||||
fn set_dummy(origin, #[compact] new_value: T::Balance) {
|
||||
ensure_root(origin)?;
|
||||
|
||||
<Dummy<T, I>>::put(&new_value);
|
||||
Self::deposit_event(RawEvent::Dummy(new_value));
|
||||
}
|
||||
|
||||
fn on_initialize(_n: T::BlockNumber) -> Weight {
|
||||
<Dummy<T, I>>::put(T::Balance::from(10));
|
||||
Weight::from_parts(10, 0)
|
||||
}
|
||||
|
||||
fn on_finalize(_n: T::BlockNumber) {
|
||||
<Dummy<T, I>>::put(T::Balance::from(11));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decl_error! {
|
||||
pub enum Error for Module<T: Config<I>, I: Instance> {
|
||||
/// Some wrong behavior
|
||||
Wrong,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use frame_support::{pallet_prelude::*, scale_info};
|
||||
use frame_system::{ensure_root, pallet_prelude::*};
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config<I: 'static = ()>: frame_system::Config {
|
||||
type Balance: Parameter
|
||||
+ codec::HasCompact
|
||||
+ From<u32>
|
||||
+ Into<u64>
|
||||
+ Default
|
||||
+ MaybeSerializeDeserialize
|
||||
+ scale_info::StaticTypeInfo;
|
||||
#[pallet::constant]
|
||||
type SomeConst: Get<Self::Balance>;
|
||||
type RuntimeEvent: From<Event<Self, I>>
|
||||
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::without_storage_info]
|
||||
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
|
||||
|
||||
#[pallet::hooks]
|
||||
impl<T: Config<I>, I: 'static> Hooks<T::BlockNumber> for Pallet<T, I> {
|
||||
fn on_initialize(_n: T::BlockNumber) -> Weight {
|
||||
<Dummy<T, I>>::put(T::Balance::from(10));
|
||||
Weight::from_parts(10, 0)
|
||||
}
|
||||
|
||||
fn on_finalize(_n: T::BlockNumber) {
|
||||
<Dummy<T, I>>::put(T::Balance::from(11));
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
#[pallet::call_index(0)]
|
||||
#[pallet::weight(<T::Balance as Into<u64>>::into(new_value.clone()))]
|
||||
pub fn set_dummy(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] new_value: T::Balance,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
ensure_root(origin)?;
|
||||
|
||||
<Dummy<T, I>>::put(&new_value);
|
||||
Self::deposit_event(Event::Dummy(new_value));
|
||||
|
||||
Ok(().into())
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T, I = ()> {
|
||||
/// Some wrong behavior
|
||||
Wrong,
|
||||
}
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(fn deposit_event)]
|
||||
pub enum Event<T: Config<I>, I: 'static = ()> {
|
||||
/// Dummy event, just here so there's a generic type that's used.
|
||||
Dummy(T::Balance),
|
||||
}
|
||||
|
||||
#[pallet::storage]
|
||||
/// Some documentation
|
||||
type Dummy<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Balance, OptionQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
type Bar<T: Config<I>, I: 'static = ()> =
|
||||
StorageMap<_, Blake2_128Concat, T::AccountId, T::Balance, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
type Foo<T: Config<I>, I: 'static = ()> =
|
||||
StorageValue<_, T::Balance, ValueQuery, OnFooEmpty<T, I>>;
|
||||
#[pallet::type_value]
|
||||
pub fn OnFooEmpty<T: Config<I>, I: 'static>() -> T::Balance {
|
||||
3.into()
|
||||
}
|
||||
|
||||
#[pallet::storage]
|
||||
type Double<T, I = ()> =
|
||||
StorageDoubleMap<_, Blake2_128Concat, u32, Twox64Concat, u64, u16, ValueQuery>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
|
||||
dummy: Option<T::Balance>,
|
||||
bar: Vec<(T::AccountId, T::Balance)>,
|
||||
foo: T::Balance,
|
||||
}
|
||||
|
||||
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
||||
fn default() -> Self {
|
||||
GenesisConfig {
|
||||
dummy: Default::default(),
|
||||
bar: Default::default(),
|
||||
foo: OnFooEmpty::<T, I>::get(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
|
||||
fn build(&self) {
|
||||
if let Some(dummy) = self.dummy.as_ref() {
|
||||
<Dummy<T, I>>::put(dummy);
|
||||
}
|
||||
for (k, v) in &self.bar {
|
||||
<Bar<T, I>>::insert(k, v);
|
||||
}
|
||||
<Foo<T, I>>::put(&self.foo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl frame_system::Config for Runtime {
|
||||
type BlockWeights = ();
|
||||
type BlockLength = ();
|
||||
type DbWeight = ();
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u32;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Hash = sp_runtime::testing::H256;
|
||||
type Hashing = sp_runtime::traits::BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type BlockHashCount = ConstU32<250>;
|
||||
type Version = ();
|
||||
type PalletInfo = PalletInfo;
|
||||
type AccountData = ();
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
type SystemWeightInfo = ();
|
||||
type SS58Prefix = ();
|
||||
type OnSetCode = ();
|
||||
type MaxConsumers = ConstU32<16>;
|
||||
}
|
||||
impl pallet::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type SomeConst = ConstU64<10>;
|
||||
type Balance = u64;
|
||||
}
|
||||
impl pallet::Config<pallet::Instance2> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type SomeConst = ConstU64<10>;
|
||||
type Balance = u64;
|
||||
}
|
||||
impl pallet::Config<pallet::Instance3> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type SomeConst = ConstU64<10>;
|
||||
type Balance = u64;
|
||||
}
|
||||
impl pallet_old::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type SomeConst = ConstU64<10>;
|
||||
type Balance = u64;
|
||||
}
|
||||
impl pallet_old::Config<pallet_old::Instance2> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type SomeConst = ConstU64<10>;
|
||||
type Balance = u64;
|
||||
}
|
||||
impl pallet_old::Config<pallet_old::Instance3> for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type SomeConst = ConstU64<10>;
|
||||
type Balance = u64;
|
||||
}
|
||||
|
||||
pub type Header = sp_runtime::generic::Header<u32, sp_runtime::traits::BlakeTwo256>;
|
||||
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, RuntimeCall, (), ()>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub struct Runtime where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: frame_system::{Pallet, Call, Event<T>},
|
||||
Example: pallet::{Pallet, Call, Event<T>, Config<T>, Storage},
|
||||
PalletOld: pallet_old::{Pallet, Call, Event<T>, Config<T>, Storage},
|
||||
Instance2Example: pallet::<Instance2>::{Pallet, Call, Event<T>, Config<T>, Storage},
|
||||
PalletOld2: pallet_old::<Instance2>::{Pallet, Call, Event<T>, Config<T>, Storage},
|
||||
Instance3Example: pallet::<Instance3>::{Pallet, Call, Event<T>, Config<T>, Storage},
|
||||
PalletOld3: pallet_old::<Instance3>::{Pallet, Call, Event<T>, Config<T>, Storage},
|
||||
}
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{pallet, pallet_old, Runtime};
|
||||
use codec::{Decode, Encode};
|
||||
use scale_info::{form::PortableForm, Variant};
|
||||
|
||||
#[test]
|
||||
fn metadata() {
|
||||
let metadata = Runtime::metadata();
|
||||
let (pallets, types) = match metadata.1 {
|
||||
frame_support::metadata::RuntimeMetadata::V14(metadata) =>
|
||||
(metadata.pallets, metadata.types),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let get_enum_variants = |ty_id| match types.resolve(ty_id).map(|ty| ty.type_def.clone()) {
|
||||
Some(ty) => match ty {
|
||||
scale_info::TypeDef::Variant(var) => var.variants,
|
||||
_ => panic!("Expected variant type"),
|
||||
},
|
||||
_ => panic!("No type found"),
|
||||
};
|
||||
|
||||
let assert_enum_variants = |vs1: &[Variant<PortableForm>],
|
||||
vs2: &[Variant<PortableForm>]| {
|
||||
assert_eq!(vs1.len(), vs2.len());
|
||||
for i in 0..vs1.len() {
|
||||
let v1 = &vs2[i];
|
||||
let v2 = &vs2[i];
|
||||
assert_eq!(v1.fields.len(), v2.fields.len());
|
||||
for f in 0..v1.fields.len() {
|
||||
let f1 = &v1.fields[f];
|
||||
let f2 = &v2.fields[f];
|
||||
pretty_assertions::assert_eq!(f1.name, f2.name);
|
||||
pretty_assertions::assert_eq!(f1.ty, f2.ty);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for i in vec![1, 3, 5].into_iter() {
|
||||
pretty_assertions::assert_eq!(pallets[i].storage, pallets[i + 1].storage);
|
||||
|
||||
let call1_variants = get_enum_variants(pallets[i].calls.as_ref().unwrap().ty.id);
|
||||
let call2_variants = get_enum_variants(pallets[i + 1].calls.as_ref().unwrap().ty.id);
|
||||
assert_enum_variants(&call1_variants, &call2_variants);
|
||||
|
||||
// event: check variants and fields but ignore the type name which will be different
|
||||
let event1_variants = get_enum_variants(pallets[i].event.as_ref().unwrap().ty.id);
|
||||
let event2_variants = get_enum_variants(pallets[i + 1].event.as_ref().unwrap().ty.id);
|
||||
assert_enum_variants(&event1_variants, &event2_variants);
|
||||
|
||||
let err1 = get_enum_variants(pallets[i].error.as_ref().unwrap().ty.id)
|
||||
.iter()
|
||||
.filter(|v| v.name == "__Ignore")
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
let err2 = get_enum_variants(pallets[i + 1].error.as_ref().unwrap().ty.id)
|
||||
.iter()
|
||||
.filter(|v| v.name == "__Ignore")
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
assert_enum_variants(&err1, &err2);
|
||||
|
||||
pretty_assertions::assert_eq!(pallets[i].constants, pallets[i + 1].constants);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn types() {
|
||||
assert_eq!(
|
||||
pallet_old::Event::<Runtime>::decode(
|
||||
&mut &pallet::Event::<Runtime>::Dummy(10).encode()[..]
|
||||
)
|
||||
.unwrap(),
|
||||
pallet_old::Event::<Runtime>::Dummy(10),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
pallet_old::Call::<Runtime>::decode(
|
||||
&mut &pallet::Call::<Runtime>::set_dummy { new_value: 10 }.encode()[..]
|
||||
)
|
||||
.unwrap(),
|
||||
pallet_old::Call::<Runtime>::set_dummy { new_value: 10 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -18,21 +18,23 @@
|
||||
use frame_support::{
|
||||
dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays, UnfilteredDispatchable},
|
||||
pallet_prelude::ValueQuery,
|
||||
parameter_types,
|
||||
storage::unhashed,
|
||||
traits::{ConstU32, GetCallName, OnFinalize, OnGenesis, OnInitialize, OnRuntimeUpgrade},
|
||||
weights::Weight,
|
||||
};
|
||||
use sp_io::{
|
||||
hashing::{blake2_128, twox_128, twox_64},
|
||||
TestExternalities,
|
||||
};
|
||||
use sp_runtime::{DispatchError, ModuleError};
|
||||
use sp_std::any::TypeId;
|
||||
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
pub mod pallet {
|
||||
use codec::MaxEncodedLen;
|
||||
use frame_support::{pallet_prelude::*, parameter_types, scale_info};
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_system::pallet_prelude::*;
|
||||
use sp_std::any::TypeId;
|
||||
|
||||
type BalanceOf<T, I> = <T as Config<I>>::Balance;
|
||||
|
||||
@@ -346,8 +348,6 @@ frame_support::construct_runtime!(
|
||||
}
|
||||
);
|
||||
|
||||
use frame_support::weights::Weight;
|
||||
|
||||
#[test]
|
||||
fn call_expand() {
|
||||
let call_foo = pallet::Call::<Runtime>::foo { foo: 3 };
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
pub trait Trait: frame_system::Config {
|
||||
type Balance: frame_support::dispatch::Parameter;
|
||||
/// The overarching event type.
|
||||
type RuntimeEvent: From<Event<Self>> + Into<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Trait> as Example {
|
||||
Dummy get(fn dummy) config(): Option<u32>;
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_event!(
|
||||
pub enum Event<T>
|
||||
where
|
||||
B = <T as Trait>::Balance,
|
||||
{
|
||||
Dummy(B),
|
||||
}
|
||||
);
|
||||
|
||||
frame_support::decl_error!(
|
||||
pub enum Error for Module<T: Trait> {
|
||||
Dummy,
|
||||
}
|
||||
);
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::RuntimeOrigin {
|
||||
fn deposit_event() = default;
|
||||
type Error = Error<T>;
|
||||
const Foo: u32 = u32::MAX;
|
||||
|
||||
#[weight = 0]
|
||||
fn accumulate_dummy(_origin, _increase_by: T::Balance) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn on_initialize(_n: T::BlockNumber) -> frame_support::weights::Weight {
|
||||
frame_support::weights::Weight::zero()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> sp_runtime::traits::ValidateUnsigned for Module<T> {
|
||||
type Call = Call<T>;
|
||||
|
||||
fn validate_unsigned(
|
||||
_source: sp_runtime::transaction_validity::TransactionSource,
|
||||
_call: &Self::Call,
|
||||
) -> sp_runtime::transaction_validity::TransactionValidity {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
pub const INHERENT_IDENTIFIER: frame_support::inherent::InherentIdentifier = *b"12345678";
|
||||
|
||||
impl<T: Trait> frame_support::inherent::ProvideInherent for Module<T> {
|
||||
type Call = Call<T>;
|
||||
type Error = frame_support::inherent::MakeFatalError<()>;
|
||||
const INHERENT_IDENTIFIER: frame_support::inherent::InherentIdentifier = INHERENT_IDENTIFIER;
|
||||
|
||||
fn create_inherent(_data: &frame_support::inherent::InherentData) -> Option<Self::Call> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn check_inherent(
|
||||
_: &Self::Call,
|
||||
_: &frame_support::inherent::InherentData,
|
||||
) -> std::result::Result<(), Self::Error> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn is_inherent(_call: &Self::Call) -> bool {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate as pallet_test;
|
||||
|
||||
use frame_support::traits::ConstU64;
|
||||
|
||||
type SignedExtra = (
|
||||
frame_system::CheckEra<Runtime>,
|
||||
frame_system::CheckNonce<Runtime>,
|
||||
frame_system::CheckWeight<Runtime>,
|
||||
);
|
||||
type TestBlock = sp_runtime::generic::Block<TestHeader, TestUncheckedExtrinsic>;
|
||||
type TestHeader = sp_runtime::generic::Header<u64, sp_runtime::traits::BlakeTwo256>;
|
||||
type TestUncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<
|
||||
<Runtime as frame_system::Config>::AccountId,
|
||||
<Runtime as frame_system::Config>::RuntimeCall,
|
||||
(),
|
||||
SignedExtra,
|
||||
>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub struct Runtime where
|
||||
Block = TestBlock,
|
||||
NodeBlock = TestBlock,
|
||||
UncheckedExtrinsic = TestUncheckedExtrinsic
|
||||
{
|
||||
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
|
||||
PalletTest: pallet_test::{Pallet, Call, Storage, Event<T>, Config, ValidateUnsigned, Inherent},
|
||||
}
|
||||
);
|
||||
|
||||
impl frame_system::Config for Runtime {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Hash = sp_core::H256;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Hashing = sp_runtime::traits::BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
|
||||
type Header = TestHeader;
|
||||
type RuntimeEvent = ();
|
||||
type BlockHashCount = ConstU64<250>;
|
||||
type DbWeight = ();
|
||||
type BlockWeights = ();
|
||||
type BlockLength = ();
|
||||
type Version = ();
|
||||
type PalletInfo = PalletInfo;
|
||||
type AccountData = ();
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
type SystemWeightInfo = ();
|
||||
type SS58Prefix = ();
|
||||
type OnSetCode = ();
|
||||
type MaxConsumers = frame_support::traits::ConstU32<16>;
|
||||
}
|
||||
|
||||
impl pallet_test::Trait for Runtime {
|
||||
type Balance = u32;
|
||||
type RuntimeEvent = ();
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
use frame_support::{
|
||||
assert_noop, assert_ok, dispatch::DispatchResult, pallet_prelude::ConstU32,
|
||||
assert_noop, assert_ok, dispatch::DispatchResult, ensure, pallet_prelude::ConstU32,
|
||||
storage::with_storage_layer,
|
||||
};
|
||||
use pallet::*;
|
||||
@@ -24,7 +24,8 @@ use sp_io::TestExternalities;
|
||||
|
||||
#[frame_support::pallet(dev_mode)]
|
||||
pub mod pallet {
|
||||
use frame_support::{ensure, pallet_prelude::*};
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_system::pallet_prelude::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
@@ -56,48 +57,29 @@ pub mod pallet {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod decl_pallet {
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
|
||||
#[weight = 0]
|
||||
pub fn set_value(_origin, value: u32) {
|
||||
DeclValue::put(value);
|
||||
frame_support::ensure!(value != 1, "Revert!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as StorageTransactions {
|
||||
pub DeclValue: u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type BlockNumber = u64;
|
||||
pub type BlockNumber = u32;
|
||||
pub type Index = u64;
|
||||
pub type Header = sp_runtime::generic::Header<u32, sp_runtime::traits::BlakeTwo256>;
|
||||
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type AccountId = u64;
|
||||
pub type Header = sp_runtime::generic::Header<BlockNumber, sp_runtime::traits::BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, RuntimeCall, (), ()>;
|
||||
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
impl frame_system::Config for Runtime {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type BlockWeights = ();
|
||||
type BlockLength = ();
|
||||
type DbWeight = ();
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u32;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Index = Index;
|
||||
type BlockNumber = BlockNumber;
|
||||
type Hash = sp_runtime::testing::H256;
|
||||
type Hashing = sp_runtime::traits::BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type AccountId = AccountId;
|
||||
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type BlockHashCount = ConstU32<250>;
|
||||
type DbWeight = ();
|
||||
type Version = ();
|
||||
type PalletInfo = PalletInfo;
|
||||
type AccountData = ();
|
||||
@@ -109,19 +91,17 @@ impl frame_system::Config for Runtime {
|
||||
type MaxConsumers = ConstU32<16>;
|
||||
}
|
||||
|
||||
impl pallet::Config for Runtime {}
|
||||
|
||||
impl decl_pallet::Config for Runtime {}
|
||||
impl Config for Runtime {}
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub struct Runtime where
|
||||
pub struct Runtime
|
||||
where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: frame_system,
|
||||
MyPallet: pallet,
|
||||
DeclPallet: decl_pallet::{Call, Storage},
|
||||
}
|
||||
);
|
||||
|
||||
@@ -264,23 +244,3 @@ fn storage_layer_in_pallet_call() {
|
||||
assert_noop!(call2.dispatch(RuntimeOrigin::signed(0)), Error::<Runtime>::Revert);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_layer_in_decl_pallet_call() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
use frame_support::StorageValue;
|
||||
use sp_runtime::traits::Dispatchable;
|
||||
|
||||
let call1 = RuntimeCall::DeclPallet(decl_pallet::Call::set_value { value: 2 });
|
||||
assert_ok!(call1.dispatch(RuntimeOrigin::signed(0)));
|
||||
assert_eq!(decl_pallet::DeclValue::get(), 2);
|
||||
|
||||
let call2 = RuntimeCall::DeclPallet(decl_pallet::Call::set_value { value: 1 });
|
||||
assert_noop!(call2.dispatch(RuntimeOrigin::signed(0)), "Revert!");
|
||||
// Calling the function directly also works with storage layers.
|
||||
assert_noop!(
|
||||
decl_pallet::Module::<Runtime>::set_value(RuntimeOrigin::signed(1), 1),
|
||||
"Revert!"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -20,46 +20,86 @@
|
||||
|
||||
use frame_support::{
|
||||
assert_noop, assert_ok, assert_storage_noop,
|
||||
dispatch::{DispatchError, DispatchResult},
|
||||
dispatch::DispatchResult,
|
||||
storage::{with_transaction, TransactionOutcome::*},
|
||||
transactional, StorageMap, StorageValue,
|
||||
transactional,
|
||||
};
|
||||
use sp_core::sr25519;
|
||||
use sp_io::TestExternalities;
|
||||
use sp_runtime::TransactionOutcome;
|
||||
use sp_std::result;
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, Verify},
|
||||
TransactionOutcome,
|
||||
};
|
||||
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
pub use self::pallet::*;
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {
|
||||
#[weight = 0]
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use self::frame_system::pallet_prelude::*;
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::generate_store(pub (super) trait Store)]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
#[pallet::weight(0)]
|
||||
#[transactional]
|
||||
fn value_commits(_origin, v: u32) {
|
||||
Value::set(v);
|
||||
pub fn value_commits(_origin: OriginFor<T>, v: u32) -> DispatchResult {
|
||||
<Value<T>>::set(v);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[weight = 0]
|
||||
#[pallet::weight(0)]
|
||||
#[transactional]
|
||||
fn value_rollbacks(_origin, v: u32) -> DispatchResult {
|
||||
Value::set(v);
|
||||
pub fn value_rollbacks(_origin: OriginFor<T>, v: u32) -> DispatchResult {
|
||||
<Value<T>>::set(v);
|
||||
Err(DispatchError::Other("nah"))
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Value<T: Config> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::unbounded]
|
||||
pub type Map<T: Config> = StorageMap<_, Twox64Concat, String, u32, ValueQuery>;
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as StorageTransactions {
|
||||
pub Value: u32;
|
||||
pub Map: map hasher(twox_64_concat) String => u32;
|
||||
pub type BlockNumber = u32;
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Runtime
|
||||
where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: frame_support_test,
|
||||
MyPallet: pallet,
|
||||
}
|
||||
}
|
||||
|
||||
struct Runtime;
|
||||
);
|
||||
|
||||
impl frame_support_test::Config for Runtime {
|
||||
type RuntimeOrigin = u32;
|
||||
type BlockNumber = u32;
|
||||
type PalletInfo = frame_support_test::PanicPalletInfo;
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
@@ -68,6 +108,9 @@ impl Config for Runtime {}
|
||||
#[test]
|
||||
fn storage_transaction_basic_commit() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
type Value = pallet::Value<Runtime>;
|
||||
type Map = pallet::Map<Runtime>;
|
||||
|
||||
assert_eq!(Value::get(), 0);
|
||||
assert!(!Map::contains_key("val0"));
|
||||
|
||||
@@ -87,6 +130,9 @@ fn storage_transaction_basic_commit() {
|
||||
#[test]
|
||||
fn storage_transaction_basic_rollback() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
type Value = pallet::Value<Runtime>;
|
||||
type Map = pallet::Map<Runtime>;
|
||||
|
||||
assert_eq!(Value::get(), 0);
|
||||
assert_eq!(Map::get("val0"), 0);
|
||||
|
||||
@@ -119,6 +165,9 @@ fn storage_transaction_basic_rollback() {
|
||||
#[test]
|
||||
fn storage_transaction_rollback_then_commit() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
type Value = pallet::Value<Runtime>;
|
||||
type Map = pallet::Map<Runtime>;
|
||||
|
||||
Value::set(1);
|
||||
Map::insert("val1", 1);
|
||||
|
||||
@@ -162,6 +211,9 @@ fn storage_transaction_rollback_then_commit() {
|
||||
#[test]
|
||||
fn storage_transaction_commit_then_rollback() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
type Value = pallet::Value<Runtime>;
|
||||
type Map = pallet::Map<Runtime>;
|
||||
|
||||
Value::set(1);
|
||||
Map::insert("val1", 1);
|
||||
|
||||
@@ -204,19 +256,21 @@ fn storage_transaction_commit_then_rollback() {
|
||||
|
||||
#[test]
|
||||
fn transactional_annotation() {
|
||||
type Value = pallet::Value<Runtime>;
|
||||
|
||||
fn set_value(v: u32) -> DispatchResult {
|
||||
Value::set(v);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[transactional]
|
||||
fn value_commits(v: u32) -> result::Result<u32, &'static str> {
|
||||
fn value_commits(v: u32) -> Result<u32, &'static str> {
|
||||
set_value(v)?;
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
#[transactional]
|
||||
fn value_rollbacks(v: u32) -> result::Result<u32, &'static str> {
|
||||
fn value_rollbacks(v: u32) -> Result<u32, &'static str> {
|
||||
set_value(v)?;
|
||||
Err("nah")?;
|
||||
Ok(v)
|
||||
@@ -229,14 +283,3 @@ fn transactional_annotation() {
|
||||
assert_noop!(value_rollbacks(3), "nah");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transactional_annotation_in_decl_module() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
let origin = 0;
|
||||
assert_ok!(<Module<Runtime>>::value_commits(origin, 2));
|
||||
assert_eq!(Value::get(), 2);
|
||||
|
||||
assert_noop!(<Module<Runtime>>::value_rollbacks(origin, 3), "nah");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use frame_support::{
|
||||
codec::{Decode, Encode, EncodeLike},
|
||||
traits::Get,
|
||||
weights::RuntimeDbWeight,
|
||||
};
|
||||
|
||||
pub trait Config: 'static + Eq + Clone {
|
||||
type RuntimeOrigin: Into<Result<RawOrigin<Self::AccountId>, Self::RuntimeOrigin>>
|
||||
+ From<RawOrigin<Self::AccountId>>;
|
||||
|
||||
type BaseCallFilter: frame_support::traits::Contains<Self::RuntimeCall>;
|
||||
type BlockNumber: Decode + Encode + EncodeLike + Clone + Default + scale_info::TypeInfo;
|
||||
type Hash;
|
||||
type AccountId: Encode + EncodeLike + Decode + scale_info::TypeInfo;
|
||||
type RuntimeCall;
|
||||
type RuntimeEvent: From<Event<Self>>;
|
||||
type PalletInfo: frame_support::traits::PalletInfo;
|
||||
type DbWeight: Get<RuntimeDbWeight>;
|
||||
}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {
|
||||
#[weight = 0]
|
||||
fn noop(_origin) {}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> Module<T> {
|
||||
pub fn deposit_event(_event: impl Into<T::RuntimeEvent>) {}
|
||||
}
|
||||
|
||||
frame_support::decl_event!(
|
||||
pub enum Event<T>
|
||||
where
|
||||
BlockNumber = <T as Config>::BlockNumber,
|
||||
{
|
||||
ExtrinsicSuccess,
|
||||
ExtrinsicFailed,
|
||||
Ignore(BlockNumber),
|
||||
}
|
||||
);
|
||||
|
||||
frame_support::decl_error! {
|
||||
pub enum Error for Module<T: Config> {
|
||||
/// Test error documentation
|
||||
TestError,
|
||||
/// Error documentation
|
||||
/// with multiple lines
|
||||
AnotherError,
|
||||
// Required by construct_runtime
|
||||
CallFiltered,
|
||||
}
|
||||
}
|
||||
|
||||
pub use frame_support::dispatch::RawOrigin;
|
||||
pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'static str>
|
||||
where
|
||||
OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>,
|
||||
{
|
||||
o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin")
|
||||
}
|
||||
Reference in New Issue
Block a user