Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate v1 #31

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,23 @@ pub fn testnet_config() -> Result<ChainSpec, String> {
generate_config("test".to_string())
}

type ModuleData = (AccountId, Vec<u8>, Vec<u8>, Vec<(u16, u16)>);
type Modules = Vec<Vec<ModuleData>>;
type SubnetData = (Vec<u8>, u16, u16, u16, u16, u16, u16, u64, AccountId);
type Subnets = Vec<SubnetData>;
type StakeToData = (AccountId, Vec<(AccountId, u64)>);
type StakeToVec = Vec<Vec<StakeToData>>;

// Configure initial storage state for FRAME modules.
#[allow(clippy::too_many_arguments)]
fn network_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
balances: Vec<(AccountId, u64)>,
modules: Vec<Vec<(AccountId, Vec<u8>, Vec<u8>, Vec<(u16, u16)>)>>,
subnets: Vec<(Vec<u8>, u16, u16, u16, u16, u16, u16, u64, AccountId)>,
stake_to: Vec<Vec<(AccountId, Vec<(AccountId, u64)>)>>,
modules: Modules,
subnets: Subnets,
stake_to: StakeToVec,
block: u32,
) -> RuntimeGenesisConfig {
use node_subspace_runtime::EVMConfig;
Expand Down
28 changes: 18 additions & 10 deletions pallets/subspace/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<T: Config> Pallet<T> {
max_burn: Self::get_max_burn(),
adjustment_alpha: Self::get_adjustment_alpha(),
min_stake: Self::get_min_stake_global(),
min_delegation_fee: Self::get_min_deleg_fee_global(),
floor_delegation_fee: Self::get_floor_delegation_fee(),
min_weight_stake: Self::get_min_weight_stake(),
max_allowed_weights: Self::get_max_allowed_weights_global(),
}
Expand All @@ -35,13 +35,17 @@ impl<T: Config> Pallet<T> {
// TODO: make sure there are checks for all values
pub fn check_global_params(params: GlobalParams) -> DispatchResult {
// checks if params are valid
let og_params = Self::global_params();
let old_params = Self::global_params();

// check if the name already exists
ensure!(params.max_name_length > 0, Error::<T>::InvalidMaxNameLength);

// we need to ensure that the delegation fee floor is only moven up, moving it down would
// require a storage migration
ensure!(
params.min_delegation_fee.deconstruct() <= 100,
params.floor_delegation_fee.deconstruct() <= 100
&& params.floor_delegation_fee.deconstruct()
>= old_params.floor_delegation_fee.deconstruct(),
Error::<T>::InvalidMinDelegationFee
);

Expand Down Expand Up @@ -73,7 +77,7 @@ impl<T: Config> Pallet<T> {
ensure!(params.max_proposals > 0, Error::<T>::InvalidMaxProposals);

ensure!(
params.unit_emission <= og_params.unit_emission,
params.unit_emission <= old_params.unit_emission,
Error::<T>::InvalidUnitEmission
);

Expand Down Expand Up @@ -113,7 +117,7 @@ impl<T: Config> Pallet<T> {
Self::set_max_burn(params.max_burn);
Self::set_min_weight_stake(params.min_weight_stake);
Self::set_min_stake_global(params.min_stake);
Self::set_min_deleg_fee_global(params.min_delegation_fee);
Self::set_floor_delegation_fee(params.floor_delegation_fee);
}

pub fn get_registrations_this_interval() -> u16 {
Expand Down Expand Up @@ -146,12 +150,12 @@ impl<T: Config> Pallet<T> {
MinStakeGlobal::<T>::put(min_stake)
}

pub fn get_min_deleg_fee_global() -> Percent {
MinDelegationFeeGlobal::<T>::get()
pub fn get_floor_delegation_fee() -> Percent {
FloorDelegationFee::<T>::get()
}

pub fn set_min_deleg_fee_global(delegation_fee: Percent) {
MinDelegationFeeGlobal::<T>::put(delegation_fee)
pub fn set_floor_delegation_fee(delegation_fee: Percent) {
FloorDelegationFee::<T>::put(delegation_fee)
}

pub fn set_vote_mode_global(vote_mode: Vec<u8>) {
Expand Down Expand Up @@ -223,7 +227,11 @@ impl<T: Config> Pallet<T> {
Self::get_vote_mode_global() == AUTHORITY_MODE,
Error::<T>::InvalidVoteMode
);
Self::set_global_params(params);
Self::set_global_params(params.clone());

// event
Self::deposit_event(Event::GlobalParamsUpdated(params));

Ok(())
}

Expand Down
43 changes: 30 additions & 13 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub mod pallet {
}

#[pallet::storage]
pub type MinDelegationFeeGlobal<T> =
pub type FloorDelegationFee<T> =
StorageValue<_, Percent, ValueQuery, DefaultMinDelegationFeeGlobal<T>>;

#[pallet::type_value]
Expand Down Expand Up @@ -289,11 +289,11 @@ pub mod pallet {
pub max_proposals: u64, // max number of proposals per block

// mins
pub min_burn: u64, // min burn required
pub max_burn: u64, // max burn allowed
pub min_stake: u64, // min stake required
pub min_delegation_fee: Percent, // min delegation fee
pub min_weight_stake: u64, // min weight stake required
pub min_burn: u64, // min burn required
pub max_burn: u64, // max burn allowed
pub min_stake: u64, // min stake required
pub floor_delegation_fee: Percent, // min delegation fee
pub min_weight_stake: u64, // min weight stake required

// other
pub target_registrations_per_interval: u16, // desired number of registrations per interval
Expand Down Expand Up @@ -321,7 +321,7 @@ pub mod pallet {
min_burn: DefaultMinBurn::<T>::get(),
max_burn: DefaultMaxBurn::<T>::get(),
min_stake: DefaultMinStakeGlobal::<T>::get(),
min_delegation_fee: DefaultMinDelegationFeeGlobal::<T>::get(),
floor_delegation_fee: DefaultMinDelegationFeeGlobal::<T>::get(),
min_weight_stake: DefaultMinWeightStake::<T>::get(),
adjustment_alpha: DefaultAdjustmentAlpha::<T>::get(),
unit_emission: DefaultUnitEmission::<T>::get(),
Expand Down Expand Up @@ -822,9 +822,10 @@ 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
GlobalUpdate(u16, u16, u16, u16, u64, u64),
GlobalProposalAccepted(u64), // (id)
CustomProposalAccepted(u64), // (id)
GlobalParamsUpdated(GlobalParams), // --- Event created when global parameters are updated
SubnetParamsUpdated(u16), // --- Event created when subnet parameters are updated
GlobalProposalAccepted(u64), // (id)
CustomProposalAccepted(u64), // (id)
SubnetProposalAccepted(u64, u16), // (id, netuid)
}

Expand Down Expand Up @@ -1182,7 +1183,7 @@ pub mod pallet {
params.address = address;
if let Some(delegation_fee) = delegation_fee {
ensure!(
delegation_fee >= Self::get_min_deleg_fee_global(),
delegation_fee >= Self::get_floor_delegation_fee(),
Error::<T>::InvalidMinDelegationFee
);
params.delegation_fee = delegation_fee;
Expand Down Expand Up @@ -1225,14 +1226,18 @@ pub mod pallet {
max_name_length: u16,
max_proposals: u64,
max_registrations_per_block: u16,
target_registrations_interval: u16,
min_burn: u64,
max_burn: u64,
min_stake: u64,
min_weight_stake: u64,
tx_rate_limit: u64,
unit_emission: u64,
vote_mode: Vec<u8>,
vote_threshold: u16,
adjustment_alpha: u64,
floor_delegation_fee: Percent,
target_registrations_per_interval: u16,
target_registrations_interval: u16,
) -> DispatchResult {
let mut params = Self::global_params();

Expand All @@ -1242,15 +1247,23 @@ pub mod pallet {
params.max_name_length = max_name_length;
params.max_proposals = max_proposals;
params.max_registrations_per_block = max_registrations_per_block;
params.target_registrations_interval = target_registrations_interval;
params.min_burn = min_burn;
params.max_burn = max_burn;
params.min_stake = min_stake;
params.min_weight_stake = min_weight_stake;
params.tx_rate_limit = tx_rate_limit;
params.unit_emission = unit_emission;
params.vote_mode = vote_mode;
params.vote_threshold = vote_threshold;
params.adjustment_alpha = adjustment_alpha;
params.floor_delegation_fee = floor_delegation_fee;
params.target_registrations_per_interval = target_registrations_per_interval;
params.target_registrations_interval = target_registrations_interval;

// Check if the parameters are valid
Self::check_global_params(params.clone())?;

// if so update them
Self::do_update_global(origin, params)
}

Expand Down Expand Up @@ -1331,6 +1344,10 @@ pub mod pallet {
params.vote_mode = vote_mode;
params.vote_threshold = vote_threshold;

// Check if subnet parameters are valid
Self::check_subnet_params(params.clone())?;

// if so update them
Self::do_update_subnet(origin, netuid, params)
}

Expand Down
64 changes: 11 additions & 53 deletions pallets/subspace/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
use super::*;

use frame_support::{
codec::{Decode, Encode},
pallet_prelude::StorageValue,
traits::{Get, OnRuntimeUpgrade, StorageInstance, StorageVersion},
weights::Weight,
};
use scale_info::TypeInfo;

#[derive(Decode, Encode, TypeInfo, Default)]
pub struct OldGlobalParams {
pub burn_rate: u16,
pub max_name_length: u16,
pub max_allowed_subnets: u16,
pub max_allowed_modules: u16,
pub max_registrations_per_block: u16,
pub max_allowed_weights: u16,
pub max_proposals: u64,
pub min_burn: u64,
pub min_stake: u64,
pub min_weight_stake: u64,
pub unit_emission: u64,
pub tx_rate_limit: u64,
pub vote_threshold: u16,
pub vote_mode: Vec<u8>,
}

impl<T: Config> StorageInstance for Pallet<T> {
fn pallet_prefix() -> &'static str {
Expand All @@ -43,44 +22,23 @@ pub mod v1 {
fn on_runtime_upgrade() -> Weight {
let on_chain_version = StorageVersion::get::<Pallet<T>>();

// Migrate GlobalParams to v1
// Migrate DelegationFee to v1
if on_chain_version == 0 {
let encoded =
StorageValue::<Pallet<T>, OldGlobalParams>::get().unwrap_or_default().encode();
let old_global_params = OldGlobalParams::decode(&mut encoded.as_slice())
.expect("Decoding old global params failed");
let min_deleg_fee_global = Pallet::<T>::global_params().floor_delegation_fee;

let new_global_params = GlobalParams {
burn_rate: old_global_params.burn_rate,
max_name_length: old_global_params.max_name_length,
max_allowed_subnets: old_global_params.max_allowed_subnets,
max_allowed_modules: old_global_params.max_allowed_modules,
max_registrations_per_block: old_global_params.max_registrations_per_block,
max_allowed_weights: old_global_params.max_allowed_weights,
max_proposals: old_global_params.max_proposals,
min_burn: old_global_params.min_burn,
min_stake: old_global_params.min_stake,
min_weight_stake: old_global_params.min_weight_stake,
unit_emission: old_global_params.unit_emission,
tx_rate_limit: old_global_params.tx_rate_limit,
vote_threshold: old_global_params.vote_threshold,
vote_mode: old_global_params.vote_mode,
max_burn: DefaultMaxBurn::<T>::get(),
min_delegation_fee: DefaultMinDelegationFeeGlobal::<T>::get(),
target_registrations_per_interval:
DefaultTargetRegistrationsPerInterval::<T>::get(),
target_registrations_interval: DefaultTargetRegistrationsInterval::<T>::get(),
adjustment_alpha: DefaultAdjustmentAlpha::<T>::get(),
};
// Iterate through all entries in the DelegationFee storage map
for (netuid, account_id, delegation_fee) in DelegationFee::<T>::iter() {
if delegation_fee < min_deleg_fee_global {
// Update the delegation fee to the minimum value
DelegationFee::<T>::insert(netuid, account_id, min_deleg_fee_global);
}
}

StorageValue::<Pallet<T>, GlobalParams>::put(&new_global_params);
StorageVersion::new(1).put::<Pallet<T>>();

log::info!("Migrated GlobalParams to v1");

log::info!("Migrated DelegationFee to v1");
T::DbWeight::get().writes(1)
} else {
log::info!("GlobalParams already updated");
log::info!("DelegationFee already updated");
Weight::zero()
}
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl<T: Config> Pallet<T> {

// Returns the delegation fee of a module
pub fn get_delegation_fee(netuid: u16, module_key: &T::AccountId) -> Percent {
let min_deleg_fee_global = Self::get_min_deleg_fee_global();
let min_deleg_fee_global = Self::get_floor_delegation_fee();
let delegation_fee = DelegationFee::<T>::get(netuid, module_key);

delegation_fee.max(min_deleg_fee_global)
Expand Down
5 changes: 4 additions & 1 deletion pallets/subspace/src/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ impl<T: Config> Pallet<T> {
Self::is_subnet_founder(netuid, &key),
Error::<T>::NotFounder
);
Self::check_subnet_params(params.clone())?;

Self::set_subnet_params(netuid, params);

Self::deposit_event(Event::SubnetParamsUpdated(netuid));

// --- 16. Ok and done.
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions pallets/subspace/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl<T: Config> Pallet<T> {
Self::check_subnet_params(proposal.subnet_params.clone())?;
// check if vote mode is valid
let subnet_params: SubnetParams<T> = Self::subnet_params(proposal.netuid);
// TODO: once decentralization is achieved, remove this check
ensure!(
subnet_params.vote_mode == STAKE_MODE,
Error::<T>::InvalidVoteMode
Expand Down
1 change: 1 addition & 0 deletions pallets/subspace/tests/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use frame_support::assert_ok;

use mock::*;
use sp_arithmetic::per_things::Percent;

Check warning on line 5 in pallets/subspace/tests/voting.rs

View workflow job for this annotation

GitHub Actions / check

unused import: `sp_arithmetic::per_things::Percent`
use sp_core::U256;
use sp_std::vec;

Expand Down
Loading