Skip to content

Commit

Permalink
feat: Added mandate pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox committed Sep 6, 2023
1 parent 5f83153 commit 1183b96
Show file tree
Hide file tree
Showing 11 changed files with 533 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

51 changes: 51 additions & 0 deletions pallets/mandate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[package]
name = "pallet-mandate"
version = "1.0.0"
edition = "2021"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
# Substrate
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
sp-io = { version = "7.0.0", default-features = false }
sp-runtime = { version = "7.0.0", default-features = false }
sp-std = { version = "5.0.0", default-features = false }

# Benchmarking
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true }

[dev-dependencies]
sp-core = { version = "7.0.0", default-features = false }
pallet-collective = { version = "4.0.0-dev", default-features = false }
avail-core = { version = "0.5", features = ["runtime"] }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"pallet-collective/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
18 changes: 18 additions & 0 deletions pallets/mandate/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![cfg(feature = "runtime-benchmarks")]

use frame_benchmarking::benchmarks;
use frame_system::RawOrigin;
use sp_std::prelude::*;

use super::*;
#[allow(unused)]
use crate::Pallet as Mandate;

benchmarks! {
where_clause { where <T as Config>::RuntimeCall: From<frame_system::Call<T>> }
mandate {
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();
}: _(RawOrigin::Root, Box::new(call))

impl_benchmark_test_suite!(Mandate, crate::mock::new_test_ext(), crate::mock::Test);
}
75 changes: 75 additions & 0 deletions pallets/mandate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
mod weights;

use frame_support::{
dispatch::{DispatchResultWithPostInfo, GetDispatchInfo, UnfilteredDispatchable},
pallet_prelude::*,
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use sp_std::prelude::*;
pub use weights::WeightInfo;

#[frame_support::pallet]
pub mod pallet {
use super::*;

#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// A sudo-able call.
type RuntimeCall: Parameter
+ UnfilteredDispatchable<RuntimeOrigin = Self::RuntimeOrigin>
+ GetDispatchInfo;

/// Type representing the weight of this pallet
type WeightInfo: WeightInfo;

/// Someone who can call the mandate extrinsic.
type ExternalOrigin: EnsureOrigin<Self::RuntimeOrigin>;
}

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

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight({
let dispatch_info = call.get_dispatch_info();
(
T::WeightInfo::mandate().saturating_add(dispatch_info.weight),
dispatch_info.class
)
})]
pub fn mandate(
origin: OriginFor<T>,
call: Box<<T as Config>::RuntimeCall>,
) -> DispatchResultWithPostInfo {
T::ExternalOrigin::ensure_origin(origin)?;

let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into());
Self::deposit_event(Event::RootOp {
result: res.map(|_| ()).map_err(|e| e.error),
});

// Sudo user does not pay a fee.
Ok(Pays::No.into())
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// A root operation was executed, show result
RootOp { result: DispatchResult },
}
}
116 changes: 116 additions & 0 deletions pallets/mandate/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use frame_support::{
pallet_prelude::Weight,
parameter_types,
traits::{EitherOf, EnsureOrigin},
};
use frame_system::EnsureRoot;
use sp_core::H256;
use sp_runtime::traits::{BlakeTwo256, ConstU32, IdentityLookup};

use crate::{self as pallet_mandate};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
type BlockNumber = u32;
type AccountId = u32;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system,
TechnicalCommittee: pallet_collective,
Mandate: pallet_mandate,
}
);

frame_support::parameter_types! {
pub const BlockHashCount: u32 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(Weight::from_ref_time(1_024));
pub static ExistentialDeposit: u64 = 0;
}

impl frame_system::Config for Test {
type AccountData = ();
type AccountId = AccountId;
type BaseCallFilter = frame_support::traits::Everything;
type BlockHashCount = BlockHashCount;
type BlockLength = ();
type BlockNumber = BlockNumber;
type BlockWeights = ();
type DbWeight = ();
type Hash = H256;
type Hashing = BlakeTwo256;
type Header = avail_core::header::Header<Self::BlockNumber, BlakeTwo256>;
type HeaderExtensionBuilder = frame_system::header_builder::da::HeaderExtensionBuilder<Test>;
type Index = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type MaxConsumers = ConstU32<16>;
type OnKilledAccount = ();
type OnNewAccount = ();
type OnSetCode = ();
type PalletInfo = PalletInfo;
type Randomness = frame_system::test_utils::TestRandomness<Test>;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type SS58Prefix = ();
type SubmittedDataExtractor = ();
type SystemWeightInfo = ();
type UncheckedExtrinsic = UncheckedExtrinsic;
type Version = ();
}

parameter_types! {
pub const CouncilMotionDuration: BlockNumber = 10;
pub const CouncilMaxProposals: u32 = 100;
pub const CouncilMaxMembers: u32 = 100;
}

impl pallet_collective::Config for Test {
type DefaultVote = pallet_collective::PrimeDefaultVote;
type MaxMembers = CouncilMaxMembers;
type MaxProposals = CouncilMaxProposals;
type MotionDuration = CouncilMotionDuration;
type Proposal = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type WeightInfo = ();
}

impl pallet_mandate::Config for Test {
type ExternalOrigin = EitherOf<EnsureRoot<AccountId>, HalfOfTechnicalCommittee>;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

pub struct HalfOfTechnicalCommittee;
impl<OuterOrigin> EnsureOrigin<OuterOrigin> for HalfOfTechnicalCommittee
where
OuterOrigin: Into<Result<pallet_collective::RawOrigin<AccountId, ()>, OuterOrigin>>
+ From<pallet_collective::RawOrigin<AccountId, ()>>,
{
type Success = ();

fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
o.into().and_then(|o| match o {
pallet_collective::RawOrigin::Members(n, m) if n * 2u32 >= 1u32 * m => Ok(()),
r => Err(OuterOrigin::from(r)),
})
}
}

/// Create new externalities for `Mandate` module tests.
#[allow(dead_code)]
pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
Loading

0 comments on commit 1183b96

Please sign in to comment.