Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate pallet-utility to umbrella crate #6512

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion substrate/frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub mod prelude {

/// Dispatch types from `frame-support`, other fundamental traits
#[doc(no_inline)]
pub use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
pub use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo, extract_actual_weight, WithPostDispatchInfo};
pub use frame_support::traits::{Contains, IsSubType, OnRuntimeUpgrade};

/// Pallet prelude of `frame-system`.
Expand Down
24 changes: 4 additions & 20 deletions substrate/frame/utility/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,35 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
frame = { workspace = true, features = ["experimental", "runtime"] }

[dev-dependencies]
pallet-balances = { workspace = true, default-features = true }
pallet-root-testing = { workspace = true, default-features = true }
pallet-collective = { workspace = true, default-features = true }
pallet-timestamp = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"frame/std",
"pallet-balances/std",
"pallet-collective/std",
"pallet-root-testing/std",
"pallet-timestamp/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"frame/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame/try-runtime",
"pallet-balances/try-runtime",
"pallet-collective/try-runtime",
"pallet-root-testing/try-runtime",
"pallet-timestamp/try-runtime",
"sp-runtime/try-runtime",
]
4 changes: 3 additions & 1 deletion substrate/frame/utility/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
#![cfg(feature = "runtime-benchmarks")]

use alloc::vec;
/*
use frame_benchmarking::{benchmarking::add_to_whitelist, v2::*};
use frame_system::RawOrigin;

*/
use frame::benchmarking::prelude::*;
use crate::*;

const SEED: u32 = 0;
Expand Down
16 changes: 14 additions & 2 deletions substrate/frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,35 @@ pub mod weights;
extern crate alloc;

use alloc::{boxed::Box, vec::Vec};
/*
use codec::{Decode, Encode};
use frame_support::{
dispatch::{extract_actual_weight, GetDispatchInfo, PostDispatchInfo},
traits::{IsSubType, OriginTrait, UnfilteredDispatchable},
};
*/
use frame::{
prelude::*,
traits::{UnfilteredDispatchable, BadOrigin, OriginTrait},
deps::sp_core::{self, TypeId},
};
/*
use sp_core::TypeId;
use sp_io::hashing::blake2_256;
use sp_runtime::traits::{BadOrigin, Dispatchable, TrailingZeroInput};
*/
pub use weights::WeightInfo;

pub use pallet::*;

#[frame_support::pallet]
// #[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
use super::*;
/*
use frame_support::{dispatch::DispatchClass, pallet_prelude::*};
use frame_system::pallet_prelude::*;
*/

#[pallet::pallet]
pub struct Pallet<T>(_);
Expand All @@ -98,7 +110,7 @@ pub mod pallet {
/// The caller origin, overarching type of all pallets origins.
type PalletsOrigin: Parameter +
Into<<Self as frame_system::Config>::RuntimeOrigin> +
IsType<<<Self as frame_system::Config>::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin>;
IsType<<<Self as frame_system::Config>::RuntimeOrigin as frame::traits::OriginTrait>::PalletsOrigin>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
Expand Down
91 changes: 54 additions & 37 deletions substrate/frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,36 @@
use super::*;

use crate as utility;
use frame::{testing_prelude::*, arithmetic::Perbill}; // deps::frame_system, traits::{IsType, EnsureOrigin}, runtime::prelude::{OriginFor, DispatchResult, DispatchClass}, arithmetic::Perbill};
/*
use frame_support::{
assert_err_ignore_postinfo, assert_noop, assert_ok, derive_impl,
dispatch::{DispatchErrorWithPostInfo, Pays},
parameter_types, storage,
traits::{ConstU64, Contains},
weights::Weight,
};
use frame_system::EnsureRoot;
*/
// use frame_system::EnsureRoot;
use pallet_collective::{EnsureProportionAtLeast, Instance1};
/*
use sp_runtime::{
traits::{BadOrigin, BlakeTwo256, Dispatchable, Hash},
BuildStorage, DispatchError, TokenError,
};

*/
type BlockNumber = u64;

// example module to test behaviors.
#[frame_support::pallet(dev_mode)]
// #[frame_support::pallet(dev_mode)]
#[frame::pallet]
pub mod example {
//use super::*;
/*
use frame_support::{dispatch::WithPostDispatchInfo, pallet_prelude::*};
use frame_system::pallet_prelude::*;
*/
use frame::prelude::*;

#[pallet::pallet]
pub struct Pallet<T>(_);
Expand Down Expand Up @@ -87,46 +96,54 @@ pub mod example {
}
}

mod mock_democracy {
pub use pallet::*;
#[frame_support::pallet(dev_mode)]
pub mod pallet {
#[frame::pallet]
pub mod mock_democracy {
//pub use pallet::*;
//use super::*;
// #[frame_support::pallet(dev_mode)]
use frame::prelude::*;

//pub mod pallet {

/*
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
*/

#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config + Sized {
type RuntimeEvent: From<Event<Self>>
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
type ExternalMajorityOrigin: EnsureOrigin<Self::RuntimeOrigin>;
}
#[pallet::config]
pub trait Config: frame_system::Config + Sized {
type RuntimeEvent: From<Event<Self>>
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
type ExternalMajorityOrigin: EnsureOrigin<Self::RuntimeOrigin>;
}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(3)]
#[pallet::weight(0)]
pub fn external_propose_majority(origin: OriginFor<T>) -> DispatchResult {
T::ExternalMajorityOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::<T>::ExternalProposed);
Ok(())
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(3)]
#[pallet::weight(0)]
pub fn external_propose_majority(origin: OriginFor<T>) -> DispatchResult {
T::ExternalMajorityOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::<T>::ExternalProposed);
Ok(())
}
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
ExternalProposed,
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
ExternalProposed,
}
//}
}

type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test
//frame_support::construct_runtime!(
construct_runtime!(
pub struct Test
{
System: frame_system,
Timestamp: pallet_timestamp,
Expand Down Expand Up @@ -175,7 +192,7 @@ parameter_types! {
pub const MotionDuration: BlockNumber = MOTION_DURATION_IN_BLOCKS;
pub const MaxProposals: u32 = 100;
pub const MaxMembers: u32 = 100;
pub MaxProposalWeight: Weight = sp_runtime::Perbill::from_percent(50) * BlockWeights::get().max_block;
pub MaxProposalWeight: Weight = Perbill::from_percent(50) * BlockWeights::get().max_block;
}

type CouncilCollective = pallet_collective::Instance1;
Expand Down Expand Up @@ -233,7 +250,7 @@ use pallet_balances::Call as BalancesCall;
use pallet_root_testing::Call as RootTestingCall;
use pallet_timestamp::Call as TimestampCall;

pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 10), (3, 10), (4, 10), (5, 2)],
Expand All @@ -248,7 +265,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
let mut ext = TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
Expand Down Expand Up @@ -451,7 +468,7 @@ fn batch_early_exit_works() {

#[test]
fn batch_weight_calculation_doesnt_overflow() {
use sp_runtime::Perbill;
//use sp_runtime::Perbill;
new_test_ext().execute_with(|| {
let big_call = RuntimeCall::RootTesting(RootTestingCall::fill_block {
ratio: Perbill::from_percent(50),
Expand Down Expand Up @@ -899,7 +916,7 @@ fn with_weight_works() {
);
assert_eq!(
upgrade_code_call.get_dispatch_info().class,
frame_support::dispatch::DispatchClass::Operational
DispatchClass::Operational
);

let with_weight_call = Call::<Test>::with_weight {
Expand All @@ -910,7 +927,7 @@ fn with_weight_works() {
assert_eq!(with_weight_call.get_dispatch_info().call_weight, Weight::from_parts(123, 456));
assert_eq!(
with_weight_call.get_dispatch_info().class,
frame_support::dispatch::DispatchClass::Operational
DispatchClass::Operational
);
})
}
4 changes: 3 additions & 1 deletion substrate/frame/utility/src/weights.rs

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