Skip to content

Commit

Permalink
feat: adding DAO whitelist to subnet 0 (#42)
Browse files Browse the repository at this point in the history
* Update global_params function to use generic type

* fix: manually impl debug for GlobalParams

* added whitelist

* refac: updated whitelist calls

* refac: turning LegitWhitelist to Vec<T::AccountId>

* refac: reverting back to the mutlisig

* fix: test cases

* refac: change whitelist to a map

---------

Co-authored-by: saiintbrisson <[email protected]>
  • Loading branch information
Supremesource and saiintbrisson authored Mar 24, 2024
1 parent b5e9e81 commit 67a74cd
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 27 deletions.
2 changes: 1 addition & 1 deletion node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub fn generate_config(network: String) -> Result<ChainSpec, String> {
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
],
// Sudo account
// Sudo account, this is multisig of 5
Ss58Codec::from_ss58check("5FXymAnjbb7p57pNyfdLb6YCdzm73ZhVq6oFF1AdCEPEg8Uw")
.unwrap(),
// Pre-funded a
Expand Down
44 changes: 33 additions & 11 deletions pallets/subspace/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sp_arithmetic::per_things::Percent;
use system::ensure_root;

impl<T: Config> Pallet<T> {
pub fn global_params() -> GlobalParams {
pub fn global_params() -> GlobalParams<T> {
GlobalParams {
max_name_length: Self::get_global_max_name_length(),
max_allowed_subnets: Self::get_global_max_allowed_subnets(),
Expand All @@ -20,6 +20,7 @@ impl<T: Config> Pallet<T> {
vote_threshold: Self::get_global_vote_threshold(),
max_proposals: Self::get_max_proposals(),
vote_mode: Self::get_vote_mode_global(),
nominator: Self::get_nominator(),
burn_rate: Self::get_burn_rate(),
min_burn: Self::get_min_burn(),
max_burn: Self::get_max_burn(),
Expand All @@ -31,7 +32,7 @@ impl<T: Config> Pallet<T> {
}
}

pub fn check_global_params(params: &GlobalParams) -> DispatchResult {
pub fn check_global_params(params: &GlobalParams<T>) -> DispatchResult {
// checks if params are valid
let old_params = Self::global_params();

Expand Down Expand Up @@ -107,7 +108,7 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub fn set_global_params(params: GlobalParams) {
pub fn set_global_params(params: GlobalParams<T>) {
// Check if the params are valid
Self::check_global_params(&params).expect("global params are invalid");

Expand All @@ -129,6 +130,15 @@ impl<T: Config> Pallet<T> {
Self::set_min_weight_stake(params.min_weight_stake);
Self::set_min_stake_global(params.min_stake);
Self::set_floor_delegation_fee(params.floor_delegation_fee);
Self::set_nominator(params.nominator);
}

pub fn get_nominator() -> T::AccountId {
Nominator::<T>::get()
}

pub fn set_nominator(nominator: T::AccountId) {
Nominator::<T>::put(nominator)
}

pub fn get_registrations_this_interval() -> u16 {
Expand Down Expand Up @@ -179,6 +189,9 @@ impl<T: Config> Pallet<T> {
pub fn get_burn_rate() -> u16 {
BurnRate::<T>::get()
}
pub fn set_burn_rate(burn_rate: u16) {
BurnRate::<T>::put(burn_rate.min(100));
}

pub fn get_burn() -> u64 {
Burn::<T>::get()
Expand All @@ -190,18 +203,13 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::RegistrationBurnChanged(burn));
}

pub fn set_burn_rate(burn_rate: u16) {
BurnRate::<T>::put(burn_rate.min(100));
pub fn get_max_proposals() -> u64 {
MaxProposals::<T>::get()
}

pub fn set_max_proposals(max_proposals: u64) {
MaxProposals::<T>::put(max_proposals);
}

pub fn get_max_proposals() -> u64 {
MaxProposals::<T>::get()
}

pub fn get_global_vote_threshold() -> u16 {
GlobalVoteThreshold::<T>::get()
}
Expand Down Expand Up @@ -234,7 +242,7 @@ impl<T: Config> Pallet<T> {
MaxNameLength::<T>::put(max_name_length)
}

pub fn do_update_global(origin: T::RuntimeOrigin, params: GlobalParams) -> DispatchResult {
pub fn do_update_global(origin: T::RuntimeOrigin, params: GlobalParams<T>) -> DispatchResult {
ensure_root(origin)?;

// TODO, once decentralization is reached, remove this
Expand Down Expand Up @@ -301,4 +309,18 @@ impl<T: Config> Pallet<T> {
pub fn set_adjustment_alpha(adjustment_alpha: u64) {
AdjustmentAlpha::<T>::put(adjustment_alpha);
}

// Whitelist management

pub fn is_in_legit_whitelist(account_id: &T::AccountId) -> bool {
LegitWhitelist::<T>::contains_key(account_id)
}

pub fn insert_to_whitelist(module_key: T::AccountId) {
LegitWhitelist::<T>::insert(module_key, ());
}

pub fn rm_from_whitelist(module_key: &T::AccountId) {
LegitWhitelist::<T>::remove(&module_key);
}
}
93 changes: 85 additions & 8 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ pub mod pallet {
pub controller: T::AccountId,
}

#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
// skip
pub struct GlobalParams {
#[derive(Decode, Encode, PartialEq, Eq, Clone, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct GlobalParams<T: Config> {
pub burn_rate: u16,
// max
pub max_name_length: u16, // max length of a network name
Expand All @@ -299,10 +299,50 @@ pub mod pallet {
pub tx_rate_limit: u64, // tx rate limit
pub vote_threshold: u16, // out of 100
pub vote_mode: Vec<u8>, // out of 100
pub nominator: T::AccountId,
}

impl<T: Config> core::fmt::Debug for GlobalParams<T>
where
T::AccountId: core::fmt::Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct("GlobalParams")
.field("burn_rate", &self.burn_rate)
.field("max_name_length", &self.max_name_length)
.field("max_allowed_subnets", &self.max_allowed_subnets)
.field("max_allowed_modules", &self.max_allowed_modules)
.field(
"max_registrations_per_block",
&self.max_registrations_per_block,
)
.field("max_allowed_weights", &self.max_allowed_weights)
.field("max_proposals", &self.max_proposals)
.field("min_burn", &self.min_burn)
.field("max_burn", &self.max_burn)
.field("min_stake", &self.min_stake)
.field("floor_delegation_fee", &self.floor_delegation_fee)
.field("min_weight_stake", &self.min_weight_stake)
.field(
"target_registrations_per_interval",
&self.target_registrations_per_interval,
)
.field(
"target_registrations_interval",
&self.target_registrations_interval,
)
.field("adjustment_alpha", &self.adjustment_alpha)
.field("unit_emission", &self.unit_emission)
.field("tx_rate_limit", &self.tx_rate_limit)
.field("vote_threshold", &self.vote_threshold)
.field("vote_mode", &self.vote_mode)
.field("nominator", &self.nominator)
.finish()
}
}

#[pallet::type_value]
pub fn DefaultGlobalParams<T: Config>() -> GlobalParams {
pub fn DefaultGlobalParams<T: Config>() -> GlobalParams<T> {
GlobalParams {
burn_rate: DefaultBurnRate::<T>::get(),
max_allowed_subnets: DefaultMaxAllowedSubnets::<T>::get(),
Expand All @@ -323,6 +363,7 @@ pub mod pallet {
tx_rate_limit: DefaultTxRateLimit::<T>::get(),
vote_threshold: DefaultVoteThreshold::<T>::get(),
vote_mode: DefaultVoteMode::<T>::get(),
nominator: DefaultNominator::<T>::get(),
}
}

Expand Down Expand Up @@ -522,6 +563,13 @@ pub mod pallet {
#[pallet::storage] // --- MAP ( netuid ) --> epoch
pub type GlobalVoteThreshold<T> = StorageValue<_, u16, ValueQuery, DefaultVoteThreshold<T>>;

#[pallet::type_value]
pub fn DefaultNominator<T: Config>() -> T::AccountId {
T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap()
}
#[pallet::storage]
pub type Nominator<T: Config> = StorageValue<_, T::AccountId, ValueQuery, DefaultNominator<T>>;

// VOTING MODE
// OPTIONS -> [stake, authority, quadratic]
#[pallet::type_value]
Expand Down Expand Up @@ -588,7 +636,6 @@ pub mod pallet {
#[pallet::storage] // --- DMAP ( netuid, uid ) --> module_key
pub(super) type Keys<T: Config> =
StorageDoubleMap<_, Identity, u16, Identity, u16, T::AccountId, ValueQuery, DefaultKey<T>>;

#[pallet::type_value]
pub fn DefaultName<T: Config>() -> Vec<u8> {
vec![]
Expand Down Expand Up @@ -622,7 +669,6 @@ pub mod pallet {
>;

// STATE OF THE MODULE

#[pallet::type_value]
pub fn DefaultBlockAtRegistration<T: Config>() -> u64 {
0
Expand Down Expand Up @@ -746,6 +792,11 @@ pub mod pallet {
DefaultWeights<T>,
>;

// whitelist for the base subnet (netuid 0)
#[pallet::storage]
pub(super) type LegitWhitelist<T: Config> =
StorageMap<_, Identity, T::AccountId, (), ValueQuery, GetDefault>;

// ========================================================
// ==== Voting System to Update Global and Subnet ====
// ========================================================
Expand All @@ -754,7 +805,7 @@ pub mod pallet {
pub struct Proposal<T: Config> {
// --- parameters
pub subnet_params: SubnetParams<T>,
pub global_params: GlobalParams,
pub global_params: GlobalParams<T>,
pub netuid: u16, // FOR SUBNET PROPOSAL ONLY
pub votes: u64,
pub participants: Vec<T::AccountId>,
Expand Down Expand Up @@ -807,6 +858,10 @@ pub mod pallet {
* account has been registered to the chain. */
ModuleDeregistered(u16, u16, T::AccountId), /* --- Event created when a module account
* has been deregistered from the chain. */
WhitelistModuleAdded(T::AccountId), /* --- Event created when a module account has been
* added to the whitelist. */
WhitelistModuleRemoved(T::AccountId), /* --- Event created when a module account has
* been removed from the whitelist. */
BulkModulesRegistered(u16, u16), /* --- Event created when multiple uids have been
* concurrently registered. */
BulkBalancesSet(u16, u16),
Expand All @@ -827,7 +882,8 @@ pub mod pallet {
MaxAllowedModulesSet(u16), // --- Event created when setting the maximum allowed modules
MaxRegistrationsPerBlockSet(u16), // --- Event created when we set max registrations
target_registrations_intervalSet(u16), // --- Event created when we set target registrations
GlobalParamsUpdated(GlobalParams), // --- Event created when global parameters are updated
GlobalParamsUpdated(GlobalParams<T>), /* --- Event created when global parameters are
* updated */
SubnetParamsUpdated(u16), // --- Event created when subnet parameters are updated
GlobalProposalAccepted(u64), // (id)
CustomProposalAccepted(u64), // (id)
Expand Down Expand Up @@ -902,6 +958,12 @@ pub mod pallet {
ModuleNameDoesNotExist, /* --- Thrown when the user tries to remove a module name that
* does not exist. */
EmptyKeys,
NotNominator, /* --- Thrown when the user tries to set the nominator and is not the
* nominator */
AlreadyWhitelisted, /* --- Thrown when the user tries to whitelist an account that is
* already whitelisted. */
NotWhitelisted, /* --- Thrown when the user tries to remove an account from the
* whitelist that is not whitelisted. */
InvalidShares,
ProfitSharesNotAdded,
NotFounder,
Expand Down Expand Up @@ -1169,6 +1231,19 @@ pub mod pallet {
Self::do_add_profit_shares(origin, keys, shares)
}

#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))]
pub fn add_to_whitelist(origin: OriginFor<T>, module_key: T::AccountId) -> DispatchResult {
Self::do_add_to_whitelist(origin, module_key)
}

#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))]
pub fn remove_from_whitelist(
origin: OriginFor<T>,
module_key: T::AccountId,
) -> DispatchResult {
Self::do_remove_from_whitelist(origin, module_key)
}

#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))]
pub fn update_global(
origin: OriginFor<T>,
Expand All @@ -1190,6 +1265,7 @@ pub mod pallet {
floor_delegation_fee: Percent,
target_registrations_per_interval: u16,
target_registrations_interval: u16,
nominator: T::AccountId,
) -> DispatchResult {
let mut params = Self::global_params();

Expand All @@ -1211,6 +1287,7 @@ pub mod pallet {
params.floor_delegation_fee = floor_delegation_fee;
params.target_registrations_per_interval = target_registrations_per_interval;
params.target_registrations_interval = target_registrations_interval;
params.nominator = nominator;

// Check if the parameters are valid
Self::check_global_params(&params)?;
Expand Down
52 changes: 52 additions & 0 deletions pallets/subspace/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,58 @@ use frame_system::ensure_signed;
use sp_std::vec::Vec;

impl<T: Config> Pallet<T> {
pub fn do_add_to_whitelist(
origin: T::RuntimeOrigin,
module_key: T::AccountId,
) -> DispatchResult {
// --- 1. Check that the caller has signed the transaction.
let key = ensure_signed(origin)?;

// --- 2. Ensure that the key is the nominator multisig.
ensure!(Self::get_nominator() == key, Error::<T>::NotNominator);

// --- 3. Ensure that the module_key is not already in the whitelist.
ensure!(
!Self::is_in_legit_whitelist(&module_key),
Error::<T>::AlreadyWhitelisted
);

// --- 4. Insert the module_key into the whitelist.
Self::insert_to_whitelist(module_key.clone());

// -- deposit event
Self::deposit_event(Event::WhitelistModuleAdded(module_key));

// --- 5. Ok and done.
Ok(())
}

pub fn do_remove_from_whitelist(
origin: T::RuntimeOrigin,
module_key: T::AccountId,
) -> DispatchResult {
// --- 1. Check that the caller has signed the transaction.
let key = ensure_signed(origin)?;

// --- 2. Ensure that the key is the nominator multisig.
ensure!(Self::get_nominator() == key, Error::<T>::NotNominator);

// --- 3. Ensure that the module_key is in the whitelist.
ensure!(
Self::is_in_legit_whitelist(&module_key),
Error::<T>::NotWhitelisted
);

// --- 4. Remove the module_key from the whitelist.
Self::rm_from_whitelist(&module_key);

// -- deposit event
Self::deposit_event(Event::WhitelistModuleRemoved(module_key));

// --- 5. Ok and done.
Ok(())
}

// TODO:
//- check ip
// - add ability to set delegaiton fee, straight in registration
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl<T: Config> Pallet<T> {
fn process_weights(
netuid: u16,
n: u16,
global_params: &GlobalParams,
global_params: &GlobalParams<T>,
subnet_params: &SubnetParams<T>,
current_block: u64,
stake_f64: &[I64F64],
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<T: Config> Pallet<T> {
pub fn do_add_global_proposal(
origin: T::RuntimeOrigin,
// params
params: GlobalParams,
params: GlobalParams<T>,
) -> DispatchResult {
let mut proposal = Self::default_proposal();
proposal.global_params = params;
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub fn delegate_register_module(
let result =
SubspaceModule::register(origin, network, name.clone(), address, stake, module_key);

log::info!("Register ok neuron: network: {name:?}, module_key: {module_key:?} key: {key:?}",);
log::info!("Register ok module: network: {name:?}, module_key: {module_key:?} key: {key:?}",);

result
}
Expand Down
Loading

0 comments on commit 67a74cd

Please sign in to comment.