Skip to content

Commit

Permalink
Migrate remaining old decl_* macros to the new pallet attribute macros (
Browse files Browse the repository at this point in the history
paritytech#12271)

* Migrate remaining old decl_* macros to the new pallet attribute macros

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Apply review suggestions

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Apply review suggestions

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* use pallet::storage

* Fix dev rpc test

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix service tests

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro authored and ark0f committed Feb 27, 2023
1 parent 67672d1 commit 3d1d774
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 107 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions client/rpc/src/dev/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ async fn block_stats_work() {
.await
.unwrap(),
Some(BlockStats {
witness_len: 597,
witness_compact_len: 500,
witness_len: 630,
witness_compact_len: 534,
block_len: 99,
num_extrinsics: 0,
}),
Expand Down
9 changes: 5 additions & 4 deletions client/service/test/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ fn storage_keys_iter_works() {
let res: Vec<_> = client
.storage_keys_iter(&BlockId::Number(0), Some(&prefix), None)
.unwrap()
.take(8)
.take(9)
.map(|x| array_bytes::bytes2hex("", &x.0))
.collect();
assert_eq!(
Expand All @@ -1675,6 +1675,7 @@ fn storage_keys_iter_works() {
"1a560ecfd2a62c2b8521ef149d0804eb621050e3988ed97dca55f0d7c3e6aa34",
"1d66850d32002979d67dd29dc583af5b2ae2a1f71c1f35ad90fff122be7a3824",
"237498b98d8803334286e9f0483ef513098dd3c1c22ca21c4dc155b4ef6cc204",
"26aa394eea5630e07c48ae0c9558cef75e0621c4869aa60c02be9adcc98a0d1d",
"29b9db10ec5bf7907d8f74b5e60aa8140c4fbdd8127a1ee5600cb98e5ec01729",
"3a636f6465",
]
Expand All @@ -1699,7 +1700,7 @@ fn storage_keys_iter_works() {
"5c2d5fda66373dabf970e4fb13d277ce91c5233473321129d32b5a8085fa8133",
"6644b9b8bc315888ac8e41a7968dc2b4141a5403c58acdf70b7e8f7e07bf5081",
"66484000ed3f75c95fc7b03f39c20ca1e1011e5999278247d3b2f5e3c3273808",
"79c07e2b1d2e2abfd4855b936617eeff5e0621c4869aa60c02be9adcc98a0d1d",
"7d5007603a7f5dd729d51d93cf695d6465789443bb967c0d1fe270e388c96eaa",
]
);

Expand All @@ -1708,7 +1709,7 @@ fn storage_keys_iter_works() {
&BlockId::Number(0),
Some(&prefix),
Some(&StorageKey(array_bytes::hex2bytes_unchecked(
"79c07e2b1d2e2abfd4855b936617eeff5e0621c4869aa60c02be9adcc98a0d1d",
"7d5007603a7f5dd729d51d93cf695d6465789443bb967c0d1fe270e388c96eaa",
))),
)
.unwrap()
Expand All @@ -1718,11 +1719,11 @@ fn storage_keys_iter_works() {
assert_eq!(
res,
[
"7d5007603a7f5dd729d51d93cf695d6465789443bb967c0d1fe270e388c96eaa",
"811ecfaadcf5f2ee1d67393247e2f71a1662d433e8ce7ff89fb0d4aa9561820b",
"a93d74caa7ec34ea1b04ce1e5c090245f867d333f0f88278a451e45299654dc5",
"a9ee1403384afbfc13f13be91ff70bfac057436212e53b9733914382ac942892",
"cf722c0832b5231d35e29f319ff27389f5032bfc7bfc3ba5ed7839f2042fb99f",
"e3b47b6c84c0493481f97c5197d2554f",
]
);
}
Expand Down
8 changes: 4 additions & 4 deletions frame/benchmarking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod pallet_test {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
pub struct Pallet<T>(PhantomData<T>);

#[pallet::config]
pub trait Config: frame_system::Config {
Expand All @@ -46,21 +46,21 @@ mod pallet_test {
}

#[pallet::storage]
#[pallet::getter(fn heartbeat_after)]
#[pallet::getter(fn value)]
pub(crate) type Value<T: Config> = StorageValue<_, u32, OptionQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(0)]
pub fn set_value(origin: OriginFor<T>, n: u32) -> DispatchResult {
let _sender = frame_system::ensure_signed(origin)?;
let _sender = ensure_signed(origin)?;
Value::<T>::put(n);
Ok(())
}

#[pallet::weight(0)]
pub fn dummy(origin: OriginFor<T>, _n: u32) -> DispatchResult {
let _sender = frame_system::ensure_none(origin)?;
let _sender = ensure_none(origin)?;
Ok(())
}

Expand Down
83 changes: 43 additions & 40 deletions frame/benchmarking/src/tests_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,52 @@ use sp_runtime::{
};
use sp_std::prelude::*;

#[frame_support::pallet]
mod pallet_test {
use frame_support::pallet_prelude::Get;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

frame_support::decl_storage! {
trait Store for Module<T: Config<I>, I: Instance = DefaultInstance> as Test where
<T as OtherConfig>::OtherEvent: Into<<T as Config<I>>::RuntimeEvent>
{
pub Value get(fn value): Option<u32>;
}
}

frame_support::decl_module! {
pub struct Module<T: Config<I>, I: Instance = DefaultInstance> for enum Call where
origin: T::RuntimeOrigin, <T as OtherConfig>::OtherEvent: Into<<T as Config<I>>::RuntimeEvent>
{
#[weight = 0]
fn set_value(origin, n: u32) -> frame_support::dispatch::DispatchResult {
let _sender = frame_system::ensure_signed(origin)?;
Value::<I>::put(n);
Ok(())
}

#[weight = 0]
fn dummy(origin, _n: u32) -> frame_support::dispatch::DispatchResult {
let _sender = frame_system::ensure_none(origin)?;
Ok(())
}
}
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

pub trait OtherConfig {
type OtherEvent;
}

pub trait Config<I: Instance = DefaultInstance>: frame_system::Config + OtherConfig
where
Self::OtherEvent: Into<<Self as Config<I>>::RuntimeEvent>,
{
type RuntimeEvent;
#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config + OtherConfig {
type RuntimeEvent: From<Event<Self, I>>
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
type LowerBound: Get<u32>;
type UpperBound: Get<u32>;
}

#[pallet::storage]
#[pallet::getter(fn value)]
pub(crate) type Value<T: Config<I>, I: 'static = ()> = StorageValue<_, u32, OptionQuery>;

#[pallet::event]
pub enum Event<T: Config<I>, I: 'static = ()> {}

#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I>
where
<T as OtherConfig>::OtherEvent: Into<<T as Config<I>>::RuntimeEvent>,
{
#[pallet::weight(0)]
pub fn set_value(origin: OriginFor<T>, n: u32) -> DispatchResult {
let _sender = ensure_signed(origin)?;
Value::<T, I>::put(n);
Ok(())
}

#[pallet::weight(0)]
pub fn dummy(origin: OriginFor<T>, _n: u32) -> DispatchResult {
let _sender = ensure_none(origin)?;
Ok(())
}
}
}

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
Expand All @@ -82,26 +86,26 @@ frame_support::construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
TestPallet: pallet_test::{Pallet, Call, Storage},
TestPallet: pallet_test::{Pallet, Call, Storage, Event<T>},
}
);

impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type RuntimeCall = RuntimeCall;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ();
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
Expand All @@ -110,7 +114,7 @@ impl frame_system::Config for Test {
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type MaxConsumers = ConstU32<16>;
}

impl pallet_test::Config for Test {
Expand All @@ -130,15 +134,14 @@ fn new_test_ext() -> sp_io::TestExternalities {
mod benchmarks {
use super::pallet_test::{self, Value};
use crate::account;
use frame_support::{ensure, StorageValue};
use frame_support::ensure;
use frame_system::RawOrigin;
use sp_std::prelude::*;

// Additional used internally by the benchmark macro.
use super::pallet_test::{Call, Config, Pallet};
use frame_support::traits::Instance;

crate::benchmarks_instance! {
crate::benchmarks_instance_pallet! {
where_clause {
where
<T as pallet_test::OtherConfig>::OtherEvent: Clone
Expand All @@ -151,7 +154,7 @@ mod benchmarks {
let caller = account::<T::AccountId>("caller", 0, 0);
}: _ (RawOrigin::Signed(caller), b.into())
verify {
assert_eq!(Value::<pallet_test::DefaultInstance>::get(), Some(b));
assert_eq!(Value::<T, I>::get(), Some(b));
}

other_name {
Expand Down
12 changes: 7 additions & 5 deletions test-utils/runtime/src/genesismap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

//! Tool for creating the genesis block.

use super::{system, wasm_binary_unwrap, AccountId, AuthorityId};
use super::{system, wasm_binary_unwrap, AccountId, AuthorityId, Runtime};
use codec::{Encode, Joiner, KeyedVec};
use frame_support::traits::GenesisBuild;
use sc_service::client::genesis;
use sp_core::{
map,
Expand Down Expand Up @@ -80,10 +81,11 @@ impl GenesisConfig {
// Assimilate the system genesis config.
let mut storage =
Storage { top: map, children_default: self.extra_storage.children_default.clone() };
let config = system::GenesisConfig { authorities: self.authorities.clone() };
config
.assimilate_storage(&mut storage)
.expect("Adding `system::GensisConfig` to the genesis");
<system::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&system::GenesisConfig { authorities: self.authorities.clone() },
&mut storage,
)
.expect("Adding `system::GensisConfig` to the genesis");

storage
}
Expand Down
2 changes: 2 additions & 0 deletions test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ impl frame_system::Config for Runtime {
type MaxConsumers = ConstU32<16>;
}

impl system::Config for Runtime {}

impl pallet_timestamp::Config for Runtime {
/// A timestamp: milliseconds since the unix epoch.
type Moment = u64;
Expand Down
Loading

0 comments on commit 3d1d774

Please sign in to comment.