Skip to content

Commit 5b6d261

Browse files
authored
V1.8.5 (#231)
* feat: enforcing whitelist * Update CHANGELOG.md
1 parent ef959bd commit 5b6d261

File tree

10 files changed

+79
-33
lines changed

10 files changed

+79
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
spec version `124`
4+
5+
enforcing whitelist on genral subnet, lowering the min weights to 1
6+
37
spec version `123`
48

59
fix metadata removal upon module registration

Cargo.lock

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

pallets/subspace/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pallet-subspace"
3-
version = "1.8.4"
3+
version = "1.8.5"
44
description = "FRAME pallet for runtime logic of Subspace Blockchain."
55
authors = ["Commune Community"]
66
homepage = "https://commune.com"

pallets/subspace/src/lib.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub mod pallet {
6565
use sp_core::{ConstU16, ConstU64, ConstU8};
6666
pub use sp_std::{vec, vec::Vec};
6767

68-
const STORAGE_VERSION: StorageVersion = StorageVersion::new(13);
68+
const STORAGE_VERSION: StorageVersion = StorageVersion::new(14);
6969

7070
#[pallet::pallet]
7171
#[pallet::storage_version(STORAGE_VERSION)]
@@ -102,6 +102,8 @@ pub mod pallet {
102102

103103
/// The weight information of this pallet.
104104
type WeightInfo: WeightInfo;
105+
/// Should enforce legit whitelist on general subnet
106+
type EnforceWhitelist: Get<bool>;
105107
}
106108

107109
pub type BalanceOf<T> =
@@ -706,6 +708,8 @@ pub mod pallet {
706708
InvalidMinValidatorStake,
707709
/// The maximum allowed validators value is invalid, minimum is 10.
708710
InvalidMaxAllowedValidators,
711+
/// Uid is not in general subnet legit whitelist
712+
UidNotWhitelisted,
709713
}
710714

711715
// ---------------------------------
@@ -1047,21 +1051,4 @@ impl<T: Config> Pallet<T> {
10471051
pub fn get_delegated_stake(staked: &T::AccountId) -> u64 {
10481052
StakeFrom::<T>::iter_prefix_values(staked).sum()
10491053
}
1050-
1051-
// --- Returns the transaction priority for setting weights.
1052-
pub fn get_priority_set_weights(key: &T::AccountId, netuid: u16) -> u64 {
1053-
if let Some(uid) = Uids::<T>::get(netuid, key) {
1054-
let last_update = Self::get_last_update_for_uid(netuid, uid);
1055-
Self::get_current_block_number().saturating_add(last_update)
1056-
} else {
1057-
0
1058-
}
1059-
}
1060-
// --- Returns the transaction priority for setting weights.
1061-
pub fn get_priority_stake(key: &T::AccountId, netuid: u16) -> u64 {
1062-
if Uids::<T>::contains_key(netuid, key) {
1063-
return Self::get_delegated_stake(key);
1064-
}
1065-
0
1066-
}
10671054
}

pallets/subspace/src/migrations.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::*;
22

33
use frame_support::traits::{StorageInstance, StorageVersion};
4+
use pallet_subnet_emission_api::SubnetConsensus;
45

56
impl<T: Config> StorageInstance for Pallet<T> {
67
fn pallet_prefix() -> &'static str {
@@ -33,6 +34,29 @@ pub mod v14 {
3334
log::info!("Storage v14 already updated");
3435
return Weight::zero();
3536
}
37+
38+
let general_subnet_netuid = 2;
39+
let onchain_netuid = T::get_consensus_netuid(SubnetConsensus::Linear).unwrap_or(2);
40+
41+
// return early if there is not a match
42+
if general_subnet_netuid != onchain_netuid {
43+
log::info!("General subnet netuid does not match onchain netuid");
44+
return Weight::zero();
45+
}
46+
47+
// Clear all of the current weights on subnet 2
48+
let _ = Weights::<T>::clear_prefix(general_subnet_netuid, u32::MAX, None);
49+
log::info!("Cleared all weights for subnet 2");
50+
51+
// Make sure we allow just one weight for the general subnet
52+
MinAllowedWeights::<T>::set(general_subnet_netuid, 1);
53+
log::info!("Set min allowed weights for subnet 2");
54+
55+
// Make sure max allowed weights are same as max allowed uids
56+
let max_allowed_uids = MaxAllowedUids::<T>::get(general_subnet_netuid);
57+
MaxAllowedWeights::<T>::set(general_subnet_netuid, max_allowed_uids);
58+
log::info!("Set max allowed weights for subnet 2");
59+
3660
log::info!("Migrating storage to v14");
3761
StorageVersion::new(14).put::<Pallet<T>>();
3862
Weight::zero()

pallets/subspace/src/set_weights.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use super::*;
22
use frame_support::pallet_prelude::DispatchResult;
33
use pallet_subnet_emission_api::SubnetConsensus;
4+
use sp_core::Get;
45

56
impl<T: Config> Pallet<T> {
6-
/// Sets weights for a node in a specific subnet.
7+
/// Sets weights for a node in a specific subnet.
78
/// # Arguments
89
///
910
/// * `origin` - The origin of the call, must be a signed account.
@@ -60,6 +61,7 @@ impl<T: Config> Pallet<T> {
6061
Self::validate_input(uid, &uids, &values, netuid)?;
6162
Self::handle_rate_limiting(uid, netuid, &key)?;
6263
Self::validate_stake(&key, uids.len())?;
64+
Self::check_whitelisted(netuid, &uids)?;
6365
Self::finalize_weights(netuid, uid, &uids, &values)?;
6466
Self::remove_rootnet_delegation(netuid, key);
6567
Ok(())
@@ -106,6 +108,33 @@ impl<T: Config> Pallet<T> {
106108
Ok(())
107109
}
108110

111+
fn check_whitelisted(netuid: u16, uids: &[u16]) -> DispatchResult {
112+
// Only perform the whitelist check if EnforceWhitelist is true
113+
if T::EnforceWhitelist::get() {
114+
let consensus_netuid = T::get_consensus_netuid(SubnetConsensus::Linear);
115+
116+
// Early return if consensus_netuid is None or doesn't match the given netuid
117+
if consensus_netuid.map_or(true, |cn| cn != netuid) {
118+
return Ok(());
119+
}
120+
121+
let whitelisted = T::whitelisted_keys();
122+
123+
uids.iter().try_for_each(|&uid| {
124+
let key = Self::get_key_for_uid(netuid, uid).ok_or(Error::<T>::InvalidUid)?;
125+
126+
if !whitelisted.contains(&key) {
127+
return Err(Error::<T>::UidNotWhitelisted.into());
128+
}
129+
130+
Ok(())
131+
})
132+
} else {
133+
// If EnforceWhitelist is false, always return Ok
134+
Ok(())
135+
}
136+
}
137+
109138
fn finalize_weights(netuid: u16, uid: u16, uids: &[u16], values: &[u16]) -> DispatchResult {
110139
let normalized_values = Self::normalize_weights(values);
111140
let zipped_weights: Vec<(u16, u16)> = uids.iter().copied().zip(normalized_values).collect();
@@ -116,17 +145,15 @@ impl<T: Config> Pallet<T> {
116145
Self::deposit_event(Event::WeightsSet(netuid, uid));
117146
Ok(())
118147
}
119-
148+
// ----------
149+
// Utils
150+
// ----------
120151
fn remove_rootnet_delegation(netuid: u16, key: T::AccountId) {
121152
if Self::is_rootnet(netuid) {
122153
RootnetControlDelegation::<T>::remove(key);
123154
}
124155
}
125156

126-
// ----------
127-
// Utils
128-
// ----------
129-
130157
fn contains_duplicates(items: &[u16]) -> bool {
131158
let mut seen = sp_std::collections::btree_set::BTreeSet::new();
132159
items.iter().any(|item| !seen.insert(item))

pallets/subspace/src/subnet.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,13 @@ impl<T: Config> Pallet<T> {
410410
pub fn get_netuid_for_name(name: &[u8]) -> Option<u16> {
411411
SubnetNames::<T>::iter().find(|(_, n)| n == name).map(|(id, _)| id)
412412
}
413-
// Returs the key under the network uid as a Result. Ok if the uid is taken.
413+
/// Returs the key under the network uid as a Result. Ok if the uid is taken.
414+
#[inline]
414415
pub fn get_key_for_uid(netuid: u16, module_uid: u16) -> Option<T::AccountId> {
415416
Keys::<T>::get(netuid, module_uid)
416417
}
417-
// Returns the uid of the key in the network as a Result. Ok if the key has a slot.
418+
/// Returns the uid of the key in the network as a Result. Ok if the key has a slot.
419+
#[inline]
418420
pub fn get_uid_for_key(netuid: u16, key: &T::AccountId) -> Option<u16> {
419421
Uids::<T>::get(netuid, key)
420422
}

runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "node-subspace-runtime"
3-
version = "1.8.4"
3+
version = "1.8.5"
44
description = "A Substrate node for commune-ai"
55
authors = ["Commune Community"]
66
homepage = "https://substrate.io/"

runtime/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub mod opaque {
162162
}
163163
}
164164

165-
pub type Migrations = pallet_governance::migrations::v2::MigrateToV2<Runtime>;
165+
pub type Migrations = pallet_subspace::migrations::v14::MigrateToV14<Runtime>;
166166

167167
// To learn more about runtime versioning, see:
168168
// https://docs.substrate.io/main-docs/build/upgrade#runtime-versioning
@@ -176,7 +176,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
176176
// `spec_version`, and `authoring_version` are the same between Wasm and native.
177177
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
178178
// the compatible custom types.
179-
spec_version: 123,
179+
spec_version: 124,
180180
impl_version: 1,
181181
apis: RUNTIME_API_VERSIONS,
182182
transaction_version: 1,
@@ -415,6 +415,7 @@ impl pallet_subspace::Config for Runtime {
415415
type DefaultSubnetMinBurn = ConstU64<2_000_000_000_000>;
416416
type WeightInfo = pallet_subspace::weights::SubstrateWeight<Runtime>;
417417
type DefaultMinValidatorStake = ConstU64<50_000_000_000_000>;
418+
type EnforceWhitelist = ConstBool<true>;
418419
}
419420

420421
impl pallet_governance::Config for Runtime {

tests/src/mock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use frame_support::{
44
ensure, parameter_types,
5-
traits::{Currency, Everything, Get, Hooks},
5+
traits::{ConstBool, Currency, Everything, Get, Hooks},
66
PalletId,
77
};
88
use frame_system as system;
@@ -121,6 +121,7 @@ impl pallet_subspace::Config for Test {
121121
type DefaultSubnetMinBurn = SubnetMinBurnConfig;
122122
type DefaultMinValidatorStake = MinValidatorStake;
123123
type PalletId = SubspacePalletId;
124+
type EnforceWhitelist = ConstBool<false>;
124125
}
125126

126127
impl GovernanceApi<<Test as frame_system::Config>::AccountId> for Test {

0 commit comments

Comments
 (0)