Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 5038c3a

Browse files
emostovshawntabrizibkchr4meta5gnunicorn
authored
Make Pallet ModuleId and LockIdentifier Configurable (#5695)
* transition treasury to configurable moduleids * make election module id configurable * convert runtime and pallet to accept module id config elections-phragmen * add ModuleId to evm pallet * change society pallet to configurable module id * delete commented out module_id * delete commented out code and merge in upstream master * try and convert 4 whitespace to tab * fix remaining space to tab conversions * trivial cleaning * delete comment from elections-phragrems tests * trivial * Update frame/elections-phragmen/src/lib.rs * add docs for elections and elections phragmen * make has_lock test get moduleid dynamically * Apply suggestions from code review Co-Authored-By: Amar Singh <[email protected]> * make sure get is imported to evm Co-authored-by: Shawn Tabrizi <[email protected]> Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Amar Singh <[email protected]> Co-authored-by: Benjamin Kampmann <[email protected]>
1 parent a9c1b75 commit 5038c3a

File tree

9 files changed

+63
-29
lines changed

9 files changed

+63
-29
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// GNU General Public License for more details.
1313

1414
// You should have received a copy of the GNU General Public License
15-
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
1616

1717
//! The Substrate runtime. This can be compiled with ``#[no_std]`, ready for Wasm.
1818
@@ -24,15 +24,15 @@ use sp_std::prelude::*;
2424
use frame_support::{
2525
construct_runtime, parameter_types, debug,
2626
weights::{Weight, RuntimeDbWeight},
27-
traits::{Currency, Randomness, OnUnbalanced, Imbalance},
27+
traits::{Currency, Randomness, OnUnbalanced, Imbalance, LockIdentifier},
2828
};
2929
use sp_core::u32_trait::{_1, _2, _3, _4};
3030
pub use node_primitives::{AccountId, Signature};
3131
use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Index, Moment};
3232
use sp_api::impl_runtime_apis;
3333
use sp_runtime::{
3434
Permill, Perbill, Perquintill, Percent, ApplyExtrinsicResult,
35-
impl_opaque_keys, generic, create_runtime_str,
35+
impl_opaque_keys, generic, create_runtime_str, ModuleId,
3636
};
3737
use sp_runtime::curve::PiecewiseLinear;
3838
use sp_runtime::transaction_validity::{TransactionValidity, TransactionSource, TransactionPriority};
@@ -387,9 +387,11 @@ parameter_types! {
387387
pub const TermDuration: BlockNumber = 7 * DAYS;
388388
pub const DesiredMembers: u32 = 13;
389389
pub const DesiredRunnersUp: u32 = 7;
390+
pub const ElectionsPhragmenModuleId: LockIdentifier = *b"phrelect";
390391
}
391392

392393
impl pallet_elections_phragmen::Trait for Runtime {
394+
type ModuleId = ElectionsPhragmenModuleId;
393395
type Event = Event;
394396
type Currency = Balances;
395397
type ChangeMembers = Council;
@@ -439,6 +441,7 @@ parameter_types! {
439441
pub const TipFindersFee: Percent = Percent::from_percent(20);
440442
pub const TipReportDepositBase: Balance = 1 * DOLLARS;
441443
pub const TipReportDepositPerByte: Balance = 1 * CENTS;
444+
pub const TreasuryModuleId: ModuleId = ModuleId(*b"py/trsry");
442445
}
443446

444447
impl pallet_treasury::Trait for Runtime {
@@ -456,6 +459,7 @@ impl pallet_treasury::Trait for Runtime {
456459
type ProposalBondMinimum = ProposalBondMinimum;
457460
type SpendPeriod = SpendPeriod;
458461
type Burn = Burn;
462+
type ModuleId = TreasuryModuleId;
459463
}
460464

461465
parameter_types! {
@@ -625,6 +629,7 @@ parameter_types! {
625629
pub const PeriodSpend: Balance = 500 * DOLLARS;
626630
pub const MaxLockDuration: BlockNumber = 36 * 30 * DAYS;
627631
pub const ChallengePeriod: BlockNumber = 7 * DAYS;
632+
pub const SocietyModuleId: ModuleId = ModuleId(*b"py/socie");
628633
}
629634

630635
impl pallet_society::Trait for Runtime {
@@ -641,6 +646,7 @@ impl pallet_society::Trait for Runtime {
641646
type FounderSetOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
642647
type SuspensionJudgementOrigin = pallet_society::EnsureFounder<Runtime>;
643648
type ChallengePeriod = ChallengePeriod;
649+
type ModuleId = SocietyModuleId;
644650
}
645651

646652
parameter_types! {

frame/elections-phragmen/src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ use frame_support::{
9898
use sp_phragmen::{build_support_map, ExtendedBalance, VoteWeight, PhragmenResult};
9999
use frame_system::{self as system, ensure_signed, ensure_root};
100100

101-
const MODULE_ID: LockIdentifier = *b"phrelect";
102-
103101
/// The maximum votes allowed per voter.
104102
pub const MAXIMUM_VOTE: usize = 16;
105103

@@ -111,6 +109,9 @@ pub trait Trait: frame_system::Trait {
111109
/// The overarching event type.c
112110
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
113111

112+
/// Identifier for the elections-phragmen pallet's lock
113+
type ModuleId: Get<LockIdentifier>;
114+
114115
/// The currency that people are electing with.
115116
type Currency:
116117
LockableCurrency<Self::AccountId, Moment=Self::BlockNumber> +
@@ -276,6 +277,7 @@ decl_module! {
276277
const DesiredMembers: u32 = T::DesiredMembers::get();
277278
const DesiredRunnersUp: u32 = T::DesiredRunnersUp::get();
278279
const TermDuration: T::BlockNumber = T::TermDuration::get();
280+
const ModuleId: LockIdentifier = T::ModuleId::get();
279281

280282
/// Vote for a set of candidates for the upcoming round of election.
281283
///
@@ -321,7 +323,7 @@ decl_module! {
321323

322324
// lock
323325
T::Currency::set_lock(
324-
MODULE_ID,
326+
T::ModuleId::get(),
325327
&who,
326328
locked_balance,
327329
WithdrawReasons::except(WithdrawReason::TransactionPayment),
@@ -650,7 +652,7 @@ impl<T: Trait> Module<T> {
650652
fn do_remove_voter(who: &T::AccountId, unreserve: bool) {
651653
// remove storage and lock.
652654
Voting::<T>::remove(who);
653-
T::Currency::remove_lock(MODULE_ID, who);
655+
T::Currency::remove_lock(T::ModuleId::get(), who);
654656

655657
if unreserve {
656658
T::Currency::unreserve(who, T::VotingBond::get());
@@ -924,7 +926,7 @@ mod tests {
924926

925927
parameter_types! {
926928
pub const ExistentialDeposit: u64 = 1;
927-
}
929+
}
928930

929931
impl pallet_balances::Trait for Test {
930932
type Balance = u64;
@@ -1021,7 +1023,12 @@ mod tests {
10211023
}
10221024
}
10231025

1026+
parameter_types!{
1027+
pub const ElectionsPhragmenModuleId: LockIdentifier = *b"phrelect";
1028+
}
1029+
10241030
impl Trait for Test {
1031+
type ModuleId = ElectionsPhragmenModuleId;
10251032
type Event = Event;
10261033
type Currency = Balances;
10271034
type CurrencyToVote = CurrencyToVoteHandler;
@@ -1125,7 +1132,7 @@ mod tests {
11251132

11261133
fn has_lock(who: &u64) -> u64 {
11271134
let lock = Balances::locks(who)[0].clone();
1128-
assert_eq!(lock.id, MODULE_ID);
1135+
assert_eq!(lock.id, ElectionsPhragmenModuleId::get());
11291136
lock.amount
11301137
}
11311138

frame/elections/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use frame_support::{
3333
weights::{Weight, MINIMUM_WEIGHT, DispatchClass},
3434
traits::{
3535
Currency, ExistenceRequirement, Get, LockableCurrency, LockIdentifier, BalanceStatus,
36-
OnUnbalanced, ReservableCurrency, WithdrawReason, WithdrawReasons, ChangeMembers
36+
OnUnbalanced, ReservableCurrency, WithdrawReason, WithdrawReasons, ChangeMembers,
3737
}
3838
};
3939
use codec::{Encode, Decode};
@@ -126,8 +126,6 @@ pub enum CellStatus {
126126
Hole,
127127
}
128128

129-
const MODULE_ID: LockIdentifier = *b"py/elect";
130-
131129
/// Number of voters grouped in one chunk.
132130
pub const VOTER_SET_SIZE: usize = 64;
133131
/// NUmber of approvals grouped in one chunk.
@@ -149,6 +147,9 @@ const APPROVAL_FLAG_LEN: usize = 32;
149147
pub trait Trait: frame_system::Trait {
150148
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
151149

150+
/// Identifier for the elections pallet's lock
151+
type ModuleId: Get<LockIdentifier>;
152+
152153
/// The currency that people are electing with.
153154
type Currency:
154155
LockableCurrency<Self::AccountId, Moment=Self::BlockNumber>
@@ -379,6 +380,8 @@ decl_module! {
379380
/// The chunk size of the approval vector.
380381
const APPROVAL_SET_SIZE: u32 = APPROVAL_SET_SIZE as u32;
381382

383+
const ModuleId: LockIdentifier = T::ModuleId::get();
384+
382385
fn deposit_event() = default;
383386

384387
/// Set candidate approvals. Approval slots stay valid as long as candidates in those slots
@@ -494,7 +497,7 @@ decl_module! {
494497
);
495498

496499
T::Currency::remove_lock(
497-
MODULE_ID,
500+
T::ModuleId::get(),
498501
if valid { &who } else { &reporter }
499502
);
500503

@@ -532,7 +535,7 @@ decl_module! {
532535

533536
Self::remove_voter(&who, index);
534537
T::Currency::unreserve(&who, T::VotingBond::get());
535-
T::Currency::remove_lock(MODULE_ID, &who);
538+
T::Currency::remove_lock(T::ModuleId::get(), &who);
536539
}
537540

538541
/// Submit oneself for candidacy.
@@ -892,7 +895,7 @@ impl<T: Trait> Module<T> {
892895
}
893896

894897
T::Currency::set_lock(
895-
MODULE_ID,
898+
T::ModuleId::get(),
896899
&who,
897900
locked_balance,
898901
WithdrawReasons::all(),

frame/elections/src/mock.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use std::cell::RefCell;
2222
use frame_support::{
2323
StorageValue, StorageMap, parameter_types, assert_ok,
24-
traits::{Get, ChangeMembers, Currency},
24+
traits::{Get, ChangeMembers, Currency, LockIdentifier},
2525
weights::Weight,
2626
};
2727
use sp_core::H256;
@@ -122,6 +122,10 @@ impl ChangeMembers<u64> for TestChangeMembers {
122122
}
123123
}
124124

125+
parameter_types!{
126+
pub const ElectionModuleId: LockIdentifier = *b"py/elect";
127+
}
128+
125129
impl elections::Trait for Test {
126130
type Event = Event;
127131
type Currency = Balances;
@@ -139,6 +143,7 @@ impl elections::Trait for Test {
139143
type InactiveGracePeriod = InactiveGracePeriod;
140144
type VotingPeriod = VotingPeriod;
141145
type DecayRatio = DecayRatio;
146+
type ModuleId = ElectionModuleId;
142147
}
143148

144149
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;

frame/evm/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use crate::backend::{Account, Log, Vicinity, Backend};
2626
use sp_std::{vec::Vec, marker::PhantomData};
2727
use frame_support::{ensure, decl_module, decl_storage, decl_event, decl_error};
2828
use frame_support::weights::{Weight, MINIMUM_WEIGHT, DispatchClass, FunctionOf, Pays};
29-
use frame_support::traits::{Currency, WithdrawReason, ExistenceRequirement};
29+
use frame_support::traits::{Currency, WithdrawReason, ExistenceRequirement, Get};
3030
use frame_system::{self as system, ensure_signed};
3131
use sp_runtime::ModuleId;
3232
use sp_core::{U256, H256, H160, Hasher};
@@ -38,8 +38,6 @@ use evm::{ExitReason, ExitSucceed, ExitError, Config};
3838
use evm::executor::StackExecutor;
3939
use evm::backend::ApplyBackend;
4040

41-
const MODULE_ID: ModuleId = ModuleId(*b"py/ethvm");
42-
4341
/// Type alias for currency balance.
4442
pub type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
4543

@@ -119,6 +117,8 @@ static ISTANBUL_CONFIG: Config = Config::istanbul();
119117

120118
/// EVM module trait
121119
pub trait Trait: frame_system::Trait + pallet_timestamp::Trait {
120+
/// The EVM's module id
121+
type ModuleId: Get<ModuleId>;
122122
/// Calculator for current gas price.
123123
type FeeCalculator: FeeCalculator;
124124
/// Convert account ID to H160;
@@ -188,6 +188,8 @@ decl_module! {
188188
type Error = Error<T>;
189189

190190
fn deposit_event() = default;
191+
192+
const ModuleId: ModuleId = T::ModuleId::get();
191193

192194
/// Deposit balance from currency/balances module into EVM.
193195
#[weight = MINIMUM_WEIGHT]
@@ -361,7 +363,7 @@ impl<T: Trait> Module<T> {
361363
/// This actually does computation. If you need to keep using it, then make sure you cache the
362364
/// value and only call this once.
363365
pub fn account_id() -> T::AccountId {
364-
MODULE_ID.into_account()
366+
T::ModuleId::get().into_account()
365367
}
366368

367369
/// Check whether an account is empty.

frame/society/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,14 @@ use frame_system::{self as system, ensure_signed, ensure_root};
269269

270270
type BalanceOf<T, I> = <<T as Trait<I>>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;
271271

272-
const MODULE_ID: ModuleId = ModuleId(*b"py/socie");
273-
274272
/// The module's configuration trait.
275273
pub trait Trait<I=DefaultInstance>: system::Trait {
276274
/// The overarching event type.
277275
type Event: From<Event<Self, I>> + Into<<Self as system::Trait>::Event>;
278276

277+
/// The societies's module id
278+
type ModuleId: Get<ModuleId>;
279+
279280
/// The currency type used for bidding.
280281
type Currency: ReservableCurrency<Self::AccountId>;
281282

@@ -491,6 +492,9 @@ decl_module! {
491492
/// The number of blocks between membership challenges.
492493
const ChallengePeriod: T::BlockNumber = T::ChallengePeriod::get();
493494

495+
/// The societies's module id
496+
const ModuleId: ModuleId = T::ModuleId::get();
497+
494498
// Used for handling module events.
495499
fn deposit_event() = default;
496500

@@ -1570,15 +1574,15 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
15701574
/// This actually does computation. If you need to keep using it, then make sure you cache the
15711575
/// value and only call this once.
15721576
pub fn account_id() -> T::AccountId {
1573-
MODULE_ID.into_account()
1577+
T::ModuleId::get().into_account()
15741578
}
15751579

15761580
/// The account ID of the payouts pot. This is where payouts are made from.
15771581
///
15781582
/// This actually does computation. If you need to keep using it, then make sure you cache the
15791583
/// value and only call this once.
15801584
pub fn payouts() -> T::AccountId {
1581-
MODULE_ID.into_sub_account(b"payouts")
1585+
T::ModuleId::get().into_sub_account(b"payouts")
15821586
}
15831587

15841588
/// Return the duration of the lock, in blocks, with the given number of members.

frame/society/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ parameter_types! {
5555
pub const AvailableBlockRatio: Perbill = Perbill::one();
5656

5757
pub const ExistentialDeposit: u64 = 1;
58+
pub const SocietyModuleId: ModuleId = ModuleId(*b"py/socie");
5859
}
5960

6061
ord_parameter_types! {
@@ -107,6 +108,7 @@ impl Trait for Test {
107108
type FounderSetOrigin = EnsureSignedBy<FounderSetAccount, u128>;
108109
type SuspensionJudgementOrigin = EnsureSignedBy<SuspensionJudgementSetAccount, u128>;
109110
type ChallengePeriod = ChallengePeriod;
111+
type ModuleId = SocietyModuleId;
110112
}
111113

112114
pub type Society = Module<Test>;

frame/treasury/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trai
110110
type PositiveImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::PositiveImbalance;
111111
type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
112112

113-
/// The treasury's module id, used for deriving its sovereign account ID.
114-
const MODULE_ID: ModuleId = ModuleId(*b"py/trsry");
115-
116113
pub trait Trait: frame_system::Trait {
114+
/// The treasury's module id, used for deriving its sovereign account ID.
115+
type ModuleId: Get<ModuleId>;
116+
117117
/// The staking balance.
118118
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
119119

@@ -313,6 +313,9 @@ decl_module! {
313313

314314
/// The amount held on deposit per byte within the tip report reason.
315315
const TipReportDepositPerByte: BalanceOf<T> = T::TipReportDepositPerByte::get();
316+
317+
/// The treasury's module id, used for deriving its sovereign account ID.
318+
const ModuleId: ModuleId = T::ModuleId::get();
316319

317320
type Error = Error<T>;
318321

@@ -571,7 +574,7 @@ impl<T: Trait> Module<T> {
571574
/// This actually does computation. If you need to keep using it, then make sure you cache the
572575
/// value and only call this once.
573576
pub fn account_id() -> T::AccountId {
574-
MODULE_ID.into_account()
577+
T::ModuleId::get().into_account()
575578
}
576579

577580
/// The needed bond for a proposal whose spend is `value`.

frame/treasury/src/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use frame_support::{
2626
};
2727
use sp_core::H256;
2828
use sp_runtime::{
29-
Perbill,
29+
Perbill, ModuleId,
3030
testing::Header,
3131
traits::{BlakeTwo256, IdentityLookup, BadOrigin},
3232
};
@@ -118,8 +118,10 @@ parameter_types! {
118118
pub const TipFindersFee: Percent = Percent::from_percent(20);
119119
pub const TipReportDepositBase: u64 = 1;
120120
pub const TipReportDepositPerByte: u64 = 1;
121+
pub const TreasuryModuleId: ModuleId = ModuleId(*b"py/trsry");
121122
}
122123
impl Trait for Test {
124+
type ModuleId = TreasuryModuleId;
123125
type Currency = pallet_balances::Module<Test>;
124126
type ApproveOrigin = frame_system::EnsureRoot<u64>;
125127
type RejectOrigin = frame_system::EnsureRoot<u64>;

0 commit comments

Comments
 (0)