Skip to content

Commit

Permalink
fix: migrations, bumb spec version 114 -> 115, update crates v
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme committed May 28, 2024
1 parent b1dfe86 commit 8c8beb9
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 38 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node-subspace"
version = "1.7.4"
version = "1.7.5"
description = "A fresh FRAME-based Substrate node, ready for hacking."
authors = ["Substrate DevHub <https://github.com/substrate-developer-hub>"]
homepage = "https://substrate.io/"
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-subspace"
version = "1.7.4"
version = "1.7.5"
description = "FRAME pallet for runtime logic of Subspace Blockchain."
authors = ["Commune Nucleus Team"]
homepage = "https://commune.com"
Expand Down
11 changes: 8 additions & 3 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use frame_support::{
use codec::{Decode, Encode};
use frame_support::sp_runtime::transaction_validity::ValidTransaction;
use sp_runtime::{
traits::{ConstU16, DispatchInfoOf, Dispatchable, PostDispatchInfoOf, SignedExtension},
traits::{DispatchInfoOf, Dispatchable, PostDispatchInfoOf, SignedExtension},
transaction_validity::{TransactionValidity, TransactionValidityError},
};
use sp_std::marker::PhantomData;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub mod pallet {
use sp_arithmetic::per_things::Percent;
pub use sp_std::{vec, vec::Vec};

const STORAGE_VERSION: StorageVersion = StorageVersion::new(8);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(9);

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
Expand Down Expand Up @@ -464,9 +464,14 @@ pub mod pallet {
#[pallet::storage] // --- MAP ( netuid ) --> trarget_registrations_interval
pub type TargetRegistrationsInterval<T> =
StorageMap<_, Identity, u16, u16, ValueQuery, DefaultTargetRegistrationsInterval<T>>;

#[pallet::type_value]
pub fn DefaultMaxRegistrationsPerInterval<T: Config>() -> u16 {
42
}
#[pallet::storage] // --- MAP ( netuid ) --> trarget_registrations_interval
pub type MaxRegistrationsPerInterval<T> =
StorageMap<_, Identity, u16, u16, ValueQuery, ConstU16<42>>;
StorageMap<_, Identity, u16, u16, ValueQuery, DefaultMaxRegistrationsPerInterval<T>>;

#[pallet::type_value]
pub fn DefaultMaxStake<T: Config>() -> u64 {
Expand Down
39 changes: 30 additions & 9 deletions pallets/subspace/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,58 @@ pub fn ss58_to_account_id<T: Config>(
Ok(T::AccountId::decode(&mut &account_id_vec[..]).unwrap())
}

pub mod v8 {
pub mod v9 {
use super::*;

pub struct MigrateToV8<T>(sp_std::marker::PhantomData<T>);
pub struct MigrateToV9<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for MigrateToV8<T> {
impl<T: Config> OnRuntimeUpgrade for MigrateToV9<T> {
fn on_runtime_upgrade() -> Weight {
let on_chain_version = StorageVersion::get::<Pallet<T>>();

if on_chain_version != 7 {
log::info!("Storage v8 already updated");
if on_chain_version != 8 {
log::info!("Storage v9 already updated");
return Weight::zero();
}

// Clear the old registration storages
// ====================================

let _ = TargetRegistrationsInterval::<T>::clear(u32::MAX, None);
let _ = TargetRegistrationsPerInterval::<T>::clear(u32::MAX, None);
let _ = MaxRegistrationsPerInterval::<T>::clear(u32::MAX, None);

let new_registration_interval = 142;
let new_target_registrations_per_interval = 4;
let max_registration_per_interval = 42;

for netuid in N::<T>::iter_keys() {
TargetRegistrationsInterval::<T>::insert(netuid, new_registration_interval);
TargetRegistrationsPerInterval::<T>::insert(
netuid,
new_target_registrations_per_interval,
);
MaxRegistrationsPerInterval::<T>::insert(netuid, max_registration_per_interval);
}
log::info!("Migrated Registration Intervals to V8");

MaxRegistrationsPerBlock::<T>::put(3);
log::info!("Migrated Registration Intervals to V8");
log::info!(
"Target registration per interval are {:?}",
TargetRegistrationsPerInterval::<T>::iter().collect::<Vec<_>>()
);

StorageVersion::new(8).put::<Pallet<T>>();
log::info!("Migrated Registration Intervals to V8");
log::info!(
"Target registration interval are {:?}",
TargetRegistrationsInterval::<T>::iter().collect::<Vec<_>>()
);

log::info!(
"Max registration per interval are {:?}",
MaxRegistrationsPerInterval::<T>::iter().collect::<Vec<_>>()
);

StorageVersion::new(9).put::<Pallet<T>>();
log::info!("Migrated Registration Intervals to V9");

T::DbWeight::get().writes(1)
}
Expand Down
11 changes: 11 additions & 0 deletions pallets/subspace/src/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl<T: Config> SubnetChangeset<T> {
pub fn apply(self, netuid: u16) -> Result<(), sp_runtime::DispatchError> {
Self::validate_params(Some(netuid), &self.params)?;

// TODO: implement check that all params are inserted
SubnetNames::<T>::insert(netuid, &self.params.name);
Founder::<T>::insert(netuid, &self.params.founder);
FounderShare::<T>::insert(netuid, self.params.founder_share);
Expand All @@ -43,6 +44,16 @@ impl<T: Config> SubnetChangeset<T> {
TrustRatio::<T>::insert(netuid, self.params.trust_ratio);
IncentiveRatio::<T>::insert(netuid, self.params.incentive_ratio);
VoteModeSubnet::<T>::insert(netuid, self.params.vote_mode);
BondsMovingAverage::<T>::insert(netuid, self.params.bonds_ma);
TargetRegistrationsInterval::<T>::insert(netuid, self.params.target_registrations_interval);
TargetRegistrationsPerInterval::<T>::insert(
netuid,
self.params.target_registrations_per_interval,
);
MaxRegistrationsPerInterval::<T>::insert(
netuid,
self.params.max_registrations_per_interval,
);

if self.params.maximum_set_weight_calls_per_epoch == 0 {
MaximumSetWeightCallsPerEpoch::<T>::remove(netuid);
Expand Down
5 changes: 4 additions & 1 deletion pallets/subspace/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use frame_support::{
traits::{Everything, Hooks},
};
use frame_system as system;
use pallet_subspace::MaxRegistrationsPerInterval;
use sp_core::{H256, U256};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
Expand Down Expand Up @@ -195,7 +196,9 @@ pub fn register_module(netuid: u16, key: U256, stake: u64) -> DispatchResult {

add_balance(key, stake + 1);

SubspaceModule::register(origin, network, name, address, stake, key, None)
let result = SubspaceModule::register(origin, network, name, address, stake, key, None);
MaxRegistrationsPerInterval::<Test>::set(netuid, 1000);
result
}

#[allow(dead_code)]
Expand Down
4 changes: 1 addition & 3 deletions pallets/subspace/tests/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sp_core::U256;
use log::info;
use pallet_subspace::{
voting::ApplicationStatus, CuratorApplications, Emission, Error, MaxAllowedModules,
MaxAllowedUids, MaxRegistrationsPerInterval, MinStake, RemovedSubnets, Stake, SubnetNames,
MaxAllowedUids, MinStake, RemovedSubnets, Stake, SubnetNames,
TotalSubnets, N,
};
use sp_runtime::{DispatchResult, Percent};
Expand Down Expand Up @@ -65,7 +65,6 @@ fn test_max_registration() {
let max_registrations_per_block = 100;
// make sure that the results won´t get affected by burn
SubspaceModule::set_min_burn(0);
MaxRegistrationsPerInterval::<Test>::set(netuid, 1000);

assert_eq!(SubspaceModule::get_registrations_this_block(), 0);
SubspaceModule::set_max_registrations_per_block(1000);
Expand Down Expand Up @@ -144,7 +143,6 @@ fn test_many_registrations() {
SubspaceModule::set_min_burn(0);

SubspaceModule::set_max_registrations_per_block(n);
MaxRegistrationsPerInterval::<Test>::set(netuid, 1000);
for i in 0..n {
register_module(netuid, U256::from(i), stake).unwrap_or_else(|_| {
panic!("Failed to register module with key: {i:?} and stake: {stake:?}",)
Expand Down
5 changes: 1 addition & 4 deletions pallets/subspace/tests/step_linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use frame_support::assert_ok;
use log::info;
use mock::*;
use pallet_subspace::{
DaoTreasuryDistribution, GlobalDaoTreasury, MaxAllowedWeights, MaxRegistrationsPerInterval,
DaoTreasuryDistribution, GlobalDaoTreasury, MaxAllowedWeights,
MinAllowedWeights, MinBurn, SubnetStakeThreshold, Tempo,
};
use sp_core::U256;
Expand Down Expand Up @@ -256,7 +256,6 @@ fn test_pruning() {
let stake_per_module: u64 = 10_000;
let tempo: u16 = 100;

MaxRegistrationsPerInterval::<Test>::insert(netuid, 1000);
// make sure that the results won´t get affected by burn
SubspaceModule::set_min_burn(0);
SubspaceModule::set_max_registrations_per_block(1000);
Expand Down Expand Up @@ -328,7 +327,6 @@ fn test_lowest_priority_mechanism() {
// make sure that the results won´t get affected by burn
SubspaceModule::set_min_burn(0);
SubspaceModule::set_max_registrations_per_block(1000);
MaxRegistrationsPerInterval::<Test>::insert(netuid, 1000);
// SETUP NETWORK
register_n_modules(netuid, n, stake_per_module);

Expand Down Expand Up @@ -924,7 +922,6 @@ fn test_dynamic_burn() {
// Create the subnet
let subnet_key = U256::from(2050);
assert_ok!(register_module(netuid, subnet_key, initial_stake));
MaxRegistrationsPerInterval::<Test>::set(netuid, 1000);
// Using the default GlobalParameters:
// - registration target interval = 2 * tempo (200 blocks)
// - registration target for interval = registration_target_interval / 2
Expand Down
7 changes: 1 addition & 6 deletions pallets/subspace/tests/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use frame_support::{assert_err, assert_ok};
use log::info;
use mock::*;
use pallet_subspace::{
Dividends, Error, FounderShare, MaxRegistrationsPerInterval, MaximumSetWeightCallsPerEpoch,
Dividends, Error, FounderShare, MaximumSetWeightCallsPerEpoch,
SubnetNames, Tempo, N,
};
use sp_core::U256;
Expand Down Expand Up @@ -135,7 +135,6 @@ fn test_set_max_allowed_uids_growing() {
SubspaceModule::set_min_burn(0);

assert_ok!(register_module(netuid, U256::from(0), stake));
MaxRegistrationsPerInterval::<Test>::insert(netuid, 9999);
SubspaceModule::set_max_registrations_per_block(max_uids + extra_uids * rounds);
for i in 1..max_uids {
assert_ok!(register_module(netuid, U256::from(i), stake));
Expand Down Expand Up @@ -185,7 +184,6 @@ fn test_set_max_allowed_uids_shrinking() {
let max_uids: u16 = 100;
let extra_uids: u16 = 20;

MaxRegistrationsPerInterval::<Test>::insert(netuid, 9999);
// make sure that the results won´t get affected by burn
SubspaceModule::set_min_burn(0);

Expand Down Expand Up @@ -284,8 +282,6 @@ fn test_set_max_allowed_modules() {
SubspaceModule::set_max_registrations_per_block(1000);
SubspaceModule::set_max_allowed_modules(max_allowed_modules);
// set max_total modules
MaxRegistrationsPerInterval::<Test>::insert(netuid, 9999);

for i in 1..(2 * max_allowed_modules) {
assert_ok!(register_module(netuid, U256::from(i), stake));
let n = SubspaceModule::get_subnet_n(netuid);
Expand Down Expand Up @@ -586,7 +582,6 @@ fn test_parasite_subnet_registrations() {
let expected_module_amount: u16 = 5;
SubspaceModule::set_max_allowed_modules(expected_module_amount);
SubspaceModule::set_max_registrations_per_block(1000);
MaxRegistrationsPerInterval::<Test>::insert(0, 9999);

let main_subnet_netuid: u16 = 0;
let main_subnet_stake = to_nano(500_000);
Expand Down
5 changes: 1 addition & 4 deletions pallets/subspace/tests/weights.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod mock;
use frame_support::{assert_err, assert_ok};

use pallet_subspace::{Error, FloorFounderShare, MaxRegistrationsPerInterval};
use pallet_subspace::{Error, FloorFounderShare};
use sp_core::U256;
use sp_runtime::DispatchError;

Expand Down Expand Up @@ -109,7 +109,6 @@ fn test_set_weight_not_enough_values() {
new_test_ext().execute_with(|| {
let netuid: u16 = 0;
let n = 100;
MaxRegistrationsPerInterval::<Test>::insert(netuid, 9999);
SubspaceModule::set_max_registrations_per_block(n);
let account_id = U256::from(0);
// make sure that the results won´t get affected by burn
Expand Down Expand Up @@ -164,7 +163,6 @@ fn test_set_max_allowed_uids() {
new_test_ext().execute_with(|| {
let netuid: u16 = 0;
let n = 100;
MaxRegistrationsPerInterval::<Test>::insert(netuid, 9999);
SubspaceModule::set_max_registrations_per_block(n);
let account_id = U256::from(0);
// make sure that the results won´t get affected by burn
Expand Down Expand Up @@ -270,7 +268,6 @@ fn test_weight_age() {
const ACTIVE_VOTER: u16 = 1;
SubspaceModule::set_max_registrations_per_block(1000);
FloorFounderShare::<Test>::put(0);
MaxRegistrationsPerInterval::<Test>::insert(NETUID, 1000);
// Register modules
(0..MODULE_COUNT).for_each(|i| {
assert_ok!(register_module(NETUID, U256::from(i), to_nano(10)));
Expand Down
2 changes: 1 addition & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node-subspace-runtime"
version = "1.7.4"
version = "1.7.5"
description = "A Substrate node for commune-ai"
authors = [
"Sal Vivona <https://github.com/salvivona>",
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub mod opaque {
}
}

pub type Migrations = (pallet_subspace::migrations::v8::MigrateToV8<Runtime>,);
pub type Migrations = (pallet_subspace::migrations::v9::MigrateToV9<Runtime>,);
// To learn more about runtime versioning, see:
// https://docs.substrate.io/main-docs/build/upgrade#runtime-versioning
#[sp_version::runtime_version]
Expand All @@ -136,7 +136,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 114,
spec_version: 115,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down

0 comments on commit 8c8beb9

Please sign in to comment.