Skip to content

Commit c13b7e9

Browse files
authored
test: add tests for pallet validator (#468)
related to #455 and #494
1 parent a5aefdb commit c13b7e9

File tree

6 files changed

+576
-23
lines changed

6 files changed

+576
-23
lines changed

substrate-node/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

substrate-node/pallets/pallet-dao/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn close_if_not_council_member_fails() {
256256
None
257257
));
258258

259-
let not_council_member = Origin::signed(4); // [1,2,3] are council members
259+
let not_council_member = Origin::signed(4); // [1, 2, 3] are council members
260260

261261
assert_noop!(
262262
DaoModule::close(not_council_member, hash.clone(), 0,),

substrate-node/pallets/pallet-validator/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polka
1515
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
1616
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
1717
pallet-membership = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
18+
pallet-collective = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
19+
pallet-session = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
1820
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
1921

2022
[dev-dependencies]
@@ -34,6 +36,8 @@ std = [
3436
'frame-system/std',
3537
'substrate-validator-set/std',
3638
'pallet-membership/std',
39+
'pallet-collective/std',
40+
'pallet-session/std',
3741
'sp-io/std',
3842
'scale-info/std'
3943
]

substrate-node/pallets/pallet-validator/src/lib.rs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ use frame_support;
99
use frame_support::traits::Currency;
1010
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
1111
use sp_runtime::traits::StaticLookup;
12+
use sp_std::convert::TryInto;
1213
use sp_std::prelude::*;
1314
use substrate_validator_set;
14-
use sp_std::convert::TryInto;
1515

1616
pub mod types;
1717
pub use pallet::*;
1818

19+
#[cfg(test)]
20+
mod tests;
21+
22+
#[cfg(test)]
23+
mod mock;
24+
1925
#[frame_support::pallet]
2026
pub mod pallet {
2127
use super::*;
@@ -53,8 +59,8 @@ pub mod pallet {
5359
#[pallet::generate_deposit(pub(super) fn deposit_event)]
5460
pub enum Event<T: Config> {
5561
Bonded(T::AccountId),
56-
ValidatorCreated(T::AccountId, types::Validator<T::AccountId>),
57-
ValidatorApproved(types::Validator<T::AccountId>),
62+
ValidatorRequestCreated(T::AccountId, types::Validator<T::AccountId>),
63+
ValidatorRequestApproved(types::Validator<T::AccountId>),
5864
ValidatorActivated(types::Validator<T::AccountId>),
5965
ValidatorRemoved(types::Validator<T::AccountId>),
6066
NodeValidatorChanged(T::AccountId),
@@ -64,16 +70,17 @@ pub mod pallet {
6470
#[pallet::error]
6571
pub enum Error<T> {
6672
BadOrigin,
73+
NotCouncilMember,
6774
AlreadyBonded,
68-
StashNotBonded,
69-
StashBondedWithWrongValidator,
70-
CannotBondWithSameAccount,
75+
StashNotBonded, // TOCHECK: Unused
76+
StashBondedWithWrongValidator, // TOCHECK: Unused
77+
CannotBondWithSameAccount, // TOCHECK: Unused
7178
DuplicateValidator,
7279
ValidatorNotFound,
7380
ValidatorNotApproved,
74-
UnauthorizedToActivateValidator,
81+
UnauthorizedToActivateValidator, // TOCHECK: Unused
7582
ValidatorValidatingAlready,
76-
ValidatorNotValidating,
83+
ValidatorNotValidating, // TOCHECK: Unused
7784
}
7885

7986
#[pallet::hooks]
@@ -105,11 +112,11 @@ pub mod pallet {
105112
/// Validator node account: the account that will validate on consensus layer
106113
/// Stash account: the "bank" account of the validator (where rewards should be sent to) the stash should be bonded to a validator
107114
/// Description: why someone wants to become a validator
108-
/// Tf Connect ID: the threefold connect ID of the persion who wants to become a validator
115+
/// Tf Connect ID: the threefold connect ID of the person who wants to become a validator
109116
/// Info: some public info about the validator (website link, blog link, ..)
110117
/// A user can only have 1 validator request at a time
111118
#[pallet::weight(100_000_000)]
112-
pub fn create_validator(
119+
pub fn create_validator_request(
113120
origin: OriginFor<T>,
114121
validator_node_account: T::AccountId,
115122
stash_account: T::AccountId,
@@ -137,7 +144,7 @@ pub mod pallet {
137144
// Create a validator request object
138145
<Validator<T>>::insert(&address, &request);
139146

140-
Self::deposit_event(Event::ValidatorCreated(address, request.clone()));
147+
Self::deposit_event(Event::ValidatorRequestCreated(address, request.clone()));
141148

142149
Ok(().into())
143150
}
@@ -191,10 +198,6 @@ pub mod pallet {
191198
let mut validator = <Validator<T>>::get(&address)
192199
.ok_or(DispatchError::from(Error::<T>::ValidatorNotFound))?;
193200

194-
// Set the new validator node account on the validator struct
195-
validator.validator_node_account = new_node_validator_account.clone();
196-
<Validator<T>>::insert(address, &validator);
197-
198201
// if validator is validating, also remove old one from consensus and add new one.
199202
if validator.state == types::ValidatorRequestState::Validating {
200203
// Remove the old validator and rotate session
@@ -211,13 +214,19 @@ pub mod pallet {
211214
frame_system::RawOrigin::Root.into(),
212215
new_node_validator_account.clone(),
213216
)?;
214-
Self::deposit_event(Event::NodeValidatorChanged(new_node_validator_account));
217+
Self::deposit_event(Event::NodeValidatorChanged(
218+
new_node_validator_account.clone(),
219+
));
215220
}
216221

222+
// Set the new validator node account on the validator struct
223+
validator.validator_node_account = new_node_validator_account;
224+
<Validator<T>>::insert(address, &validator);
225+
217226
Ok(().into())
218227
}
219228

220-
/// Bond an account to to a validator account
229+
/// Bond an account to a validator account
221230
/// Just proves that the stash account is indeed under control of the validator account
222231
#[pallet::weight(100_000_000)]
223232
pub fn bond(
@@ -230,6 +239,7 @@ pub mod pallet {
230239
Err(Error::<T>::AlreadyBonded)?
231240
}
232241
let validator = T::Lookup::lookup(validator)?;
242+
// TOCHECK: enough to identify validator?
233243

234244
<Bonded<T>>::insert(&stash, &validator);
235245

@@ -246,7 +256,9 @@ pub mod pallet {
246256
origin: OriginFor<T>,
247257
validator_account: T::AccountId,
248258
) -> DispatchResultWithPostInfo {
249-
T::CouncilOrigin::ensure_origin(origin)?;
259+
let who = ensure_signed(origin)?;
260+
261+
Self::is_council_member(who)?;
250262

251263
let mut validator = <Validator<T>>::get(&validator_account)
252264
.ok_or(DispatchError::from(Error::<T>::ValidatorNotFound))?;
@@ -261,7 +273,7 @@ pub mod pallet {
261273
validator_account.clone(),
262274
)?;
263275

264-
Self::deposit_event(Event::ValidatorApproved(validator));
276+
Self::deposit_event(Event::ValidatorRequestApproved(validator));
265277

266278
Ok(().into())
267279
}
@@ -277,9 +289,9 @@ pub mod pallet {
277289
origin: OriginFor<T>,
278290
validator: T::AccountId,
279291
) -> DispatchResultWithPostInfo {
280-
if !(ensure_signed(origin.clone())? == validator
281-
|| T::CouncilOrigin::ensure_origin(origin).is_ok())
282-
{
292+
let who = ensure_signed(origin.clone())?;
293+
294+
if !(who == validator || Self::is_council_member(who).is_ok()) {
283295
Err(Error::<T>::BadOrigin)?
284296
}
285297

@@ -318,3 +330,14 @@ pub mod pallet {
318330
}
319331
}
320332
}
333+
334+
impl<T: Config> Pallet<T> {
335+
fn is_council_member(who: T::AccountId) -> DispatchResultWithPostInfo {
336+
let council_members =
337+
pallet_membership::Pallet::<T, pallet_membership::Instance1>::members();
338+
339+
ensure!(council_members.contains(&who), Error::<T>::NotCouncilMember,);
340+
341+
Ok(().into())
342+
}
343+
}

0 commit comments

Comments
 (0)