Skip to content

Commit

Permalink
chore: restructure validator pallet (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
sameh-farouk authored Jul 10, 2023
1 parent 215a12a commit 5140310
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 169 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use sp_runtime::traits::StaticLookup;
use crate::Pallet as ValidatorModule;
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_support::assert_ok;
Expand Down
185 changes: 19 additions & 166 deletions substrate-node/pallets/pallet-validator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@

#![cfg_attr(not(feature = "std"), no_std)]

use frame_support;
use frame_support::traits::Currency;
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
use sp_runtime::traits::StaticLookup;
use sp_std::convert::TryInto;
use sp_std::prelude::*;
use substrate_validator_set;
pub use pallet::*;

pub mod types;
pub use pallet::*;
mod validator;

#[cfg(test)]
mod tests;
Expand All @@ -29,9 +23,15 @@ pub mod weights;

#[frame_support::pallet]
pub mod pallet {
use super::weights::WeightInfo;
use super::*;
use super::{weights::WeightInfo, *};
use frame_support::{
dispatch::DispatchResultWithPostInfo, pallet_prelude::*, traits::Currency,
};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::StaticLookup;
use sp_std::{convert::TryInto, prelude::*};
use substrate_validator_set;

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

Expand Down Expand Up @@ -132,28 +132,14 @@ pub mod pallet {
info: Vec<u8>,
) -> DispatchResultWithPostInfo {
let address = ensure_signed(origin.clone())?;

// Request should not be a duplicate
ensure!(
!<Validator<T>>::contains_key(&address),
Error::<T>::DuplicateValidator
);

let request = types::Validator {
validator_node_account: validator_node_account.clone(),
Self::_create_validator_request(
address,
validator_node_account,
stash_account,
description,
tf_connect_id,
info,
state: types::ValidatorRequestState::Created,
};

// Create a validator request object
<Validator<T>>::insert(&address, &request);

Self::deposit_event(Event::ValidatorRequestCreated(address, request.clone()));

Ok(().into())
)
}

/// Start participating in consensus
Expand All @@ -164,32 +150,7 @@ pub mod pallet {
#[pallet::weight(<T as Config>::WeightInfo::activate_validator_node())]
pub fn activate_validator_node(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let address = ensure_signed(origin)?;

let mut validator = <Validator<T>>::get(&address)
.ok_or(DispatchError::from(Error::<T>::ValidatorNotFound))?;

ensure!(
validator.state != types::ValidatorRequestState::Validating,
Error::<T>::ValidatorValidatingAlready
);
ensure!(
validator.state == types::ValidatorRequestState::Approved,
Error::<T>::ValidatorNotApproved
);

// Update the validator request
validator.state = types::ValidatorRequestState::Validating;
<Validator<T>>::insert(address, &validator);

// Add the validator and rotate
substrate_validator_set::Pallet::<T>::add_validator(
frame_system::RawOrigin::Root.into(),
validator.validator_node_account.clone(),
)?;

Self::deposit_event(Event::ValidatorActivated(validator));

Ok(().into())
Self::_activate_validator_node(address)
}

/// Change validator node account
Expand All @@ -203,36 +164,7 @@ pub mod pallet {
new_node_validator_account: T::AccountId,
) -> DispatchResultWithPostInfo {
let address = ensure_signed(origin)?;

let mut validator = <Validator<T>>::get(&address)
.ok_or(DispatchError::from(Error::<T>::ValidatorNotFound))?;

// if validator is validating, also remove old one from consensus and add new one.
if validator.state == types::ValidatorRequestState::Validating {
// Remove the old validator and rotate session
substrate_validator_set::Pallet::<T>::remove_validator(
frame_system::RawOrigin::Root.into(),
validator.validator_node_account.clone(),
)?;
Self::deposit_event(Event::NodeValidatorRemoved(
validator.validator_node_account.clone(),
));

// Add the new validator and rotate session
substrate_validator_set::Pallet::<T>::add_validator(
frame_system::RawOrigin::Root.into(),
new_node_validator_account.clone(),
)?;
Self::deposit_event(Event::NodeValidatorChanged(
new_node_validator_account.clone(),
));
}

// Set the new validator node account on the validator struct
validator.validator_node_account = new_node_validator_account;
<Validator<T>>::insert(address, &validator);

Ok(().into())
Self::_change_validator_node_account(address, new_node_validator_account)
}

/// Bond an account to a validator account
Expand All @@ -244,18 +176,7 @@ pub mod pallet {
validator: <T::Lookup as StaticLookup>::Source,
) -> DispatchResultWithPostInfo {
let stash = ensure_signed(origin.clone())?;

if <Bonded<T>>::contains_key(&stash) {
Err(Error::<T>::AlreadyBonded)?
}
let validator = T::Lookup::lookup(validator)?;
// TOCHECK: enough to identify validator?

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

Self::deposit_event(Event::Bonded(stash.clone()));

Ok(().into())
Self::_bond(stash, validator)
}

/// Approve validator (council)
Expand All @@ -268,26 +189,7 @@ pub mod pallet {
validator_account: <T::Lookup as StaticLookup>::Source,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

Self::is_council_member(&who)?;

let v = T::Lookup::lookup(validator_account.clone())?;
let mut validator = <Validator<T>>::get(&v)
.ok_or(DispatchError::from(Error::<T>::ValidatorNotFound))?;

// Set state to approved
validator.state = types::ValidatorRequestState::Approved;
<Validator<T>>::insert(v.clone(), &validator);

// Add the validator as a council member
pallet_membership::Pallet::<T, pallet_membership::Instance1>::add_member(
frame_system::RawOrigin::Root.into(),
validator_account,
)?;

Self::deposit_event(Event::ValidatorRequestApproved(validator));

Ok(().into())
Self::_approve_validator(who, validator_account)
}

/// Remove validator
Expand All @@ -303,56 +205,7 @@ pub mod pallet {
validator_account: <T::Lookup as StaticLookup>::Source,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())?;

let v = T::Lookup::lookup(validator_account.clone())?;

if !(v == who || Self::is_council_member(&who).is_ok()) {
Err(Error::<T>::BadOrigin)?
}

let _ = <Validator<T>>::get(&v)
.ok_or(DispatchError::from(Error::<T>::ValidatorNotFound))?;

// Remove the validator as a council member
pallet_membership::Pallet::<T, pallet_membership::Instance1>::remove_member(
frame_system::RawOrigin::Root.into(),
validator_account,
)?;

// Remove the entry from the storage map
<Validator<T>>::remove(v);

// let node_validators = substrate_validator_set::Validators::<T>::get();

// match node_validators {
// Some(validators) => {
// for (_, val) in validators.clone().into_iter().enumerate() {
// if val == v.validator_node_account {
// // Remove the old validator and rotate session
// substrate_validator_set::Pallet::<T>::remove_validator(
// frame_system::RawOrigin::Root.into(),
// v.validator_node_account.clone(),
// )?;

// Self::deposit_event(Event::ValidatorRemoved(v.clone()));
// }
// }
// },
// None => ()
// }

Ok(().into())
Self::_remove_validator(who, validator_account)
}
}
}

impl<T: Config> Pallet<T> {
fn is_council_member(who: &T::AccountId) -> DispatchResultWithPostInfo {
let council_members =
pallet_membership::Pallet::<T, pallet_membership::Instance1>::members();

ensure!(council_members.contains(&who), Error::<T>::NotCouncilMember,);

Ok(().into())
}
}
5 changes: 2 additions & 3 deletions substrate-node/pallets/pallet-validator/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
use crate as pallet_validator;
use core::cell::RefCell;
use frame_support::{bounded_vec, construct_runtime, parameter_types, traits::ConstU32};
use frame_support::{bounded_vec, construct_runtime, parameter_types, traits::{ConstU32, GenesisBuild}};
use frame_system::EnsureRoot;
use pallet_session::{SessionHandler, ShouldEndSession};
use sp_core::{crypto::key_types::DUMMY, H256};
Expand Down Expand Up @@ -31,7 +30,7 @@ construct_runtime!(
}
);

use weights;
use crate::weights;
impl pallet_validator::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type CouncilOrigin = EnsureRoot<Self::AccountId>;
Expand Down
Loading

0 comments on commit 5140310

Please sign in to comment.