Skip to content

Commit

Permalink
fix(yuma): fill stake vec if stake is missing (#73)
Browse files Browse the repository at this point in the history
* fix(yuma): fill stake vec if stake is missing

* chore: bump to 1.8.1

* chore: upload hash to artifact
  • Loading branch information
saiintbrisson authored Jul 24, 2024
1 parent 143285c commit 324ee6c
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 54 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ jobs:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Build node
run: cargo build --release --package node-subspace-runtime

run: |
cargo build --release --package node-subspace-runtime
export SHA256SUM=$(sha256sum target/release/wbuild/node-subspace-runtime/node_subspace_runtime.compact.compressed.wasm)
echo $SHA256SUM
touch $SHA256SUM
- uses: actions/upload-artifact@v4
with:
name: node_subspace_runtime.compact.compressed.wasm
path: target/release/wbuild/node-subspace-runtime/node_subspace_runtime.compact.compressed.wasm
name:
path: target/release/wbuild/node-subspace-runtime/
if-no-files-found: error
overwrite: true
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 pallets/subnet_emission/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-subnet-emission"
version = "1.0.0"
version = "1.0.1"
description = "FRAME pallet for runtime logic of Subspace Blockchain."
authors = ["Commune Community"]
homepage = "https://commune.com"
Expand Down
2 changes: 1 addition & 1 deletion pallets/subnet_emission/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pallet_subnet_emission_api::SubnetConsensus;
use pallet_subspace::{Pallet as PalletSubspace, Vec};

#[derive(Default)]
pub struct InitialMigration<T>(PhantomData<T>);
struct InitialMigration<T>(PhantomData<T>);

impl<T: Config + pallet_subspace::Config> OnRuntimeUpgrade for InitialMigration<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
Expand Down
17 changes: 6 additions & 11 deletions pallets/subnet_emission/src/subnet_consensus/yuma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use frame_support::{ensure, DebugNoBound};
use pallet_subspace::{
math::*, Active, Bonds, BondsMovingAverage, Config, Consensus, Dividends, Emission, Founder,
Incentive, Kappa, Keys, LastUpdate, MaxAllowedValidators, MaxWeightAge,
Pallet as PalletSubspace, PruningScores, Rank, StakeFrom, Trust, Uids, ValidatorPermits,
ValidatorTrust, Vec, Weights, N,
Pallet as PalletSubspace, PruningScores, Rank, Trust, Uids, ValidatorPermits, ValidatorTrust,
Vec, Weights, N,
};
use sp_std::vec;
use substrate_fixed::types::{I32F32, I64F64, I96F32};
Expand Down Expand Up @@ -323,17 +323,12 @@ impl<T: Config> YumaEpoch<T> {
}

fn compute_stake(&self) -> Result<StakeVal, &'static str> {
let keys_map: BTreeMap<_, _> = Uids::<T>::iter_prefix(self.netuid).collect();
let mut stake_map: BTreeMap<u16, I64F64> = BTreeMap::new();
for (k1, _, value) in StakeFrom::<T>::iter() {
if let Some(uid) = keys_map.get(&k1) {
let stake = stake_map.entry(*uid).or_default();
*stake = stake.saturating_add(I64F64::from_num(value));
}
let mut stake = vec![I64F64::from(0); self.module_count as usize];
for (module, uid) in Uids::<T>::iter_prefix(self.netuid) {
let stake = stake.get_mut(uid as usize).ok_or("uids is bigger than N module count")?;
*stake = I64F64::from_num(PalletSubspace::<T>::get_delegated_stake(&module));
}

let mut stake: Vec<_> = stake_map.into_values().collect();

ensure!(
stake.len() == self.module_count as usize,
"unequal number of stakes and modules"
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.8.0"
version = "1.8.1"
description = "FRAME pallet for runtime logic of Subspace Blockchain."
authors = ["Commune Community"]
homepage = "https://commune.com"
Expand Down
49 changes: 25 additions & 24 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,36 +1038,37 @@ pub mod pallet {
Self::do_delegate_rootnet_control(origin, target)
}
}
}

// ---- Subspace helper functions.
impl<T: Config> Pallet<T> {
/// used to get account total value staked to modules
pub fn get_owned_stake(key: &T::AccountId) -> u64 {
StakeTo::<T>::iter_prefix(key).map(|(_, stake)| stake).sum()
}
impl<T: Config> Pallet<T> {
/// Returns the total amount staked by the given key to other keys.
#[inline]
pub fn get_owned_stake(staker: &T::AccountId) -> u64 {
StakeTo::<T>::iter_prefix_values(staker).sum()
}

/// used to get modules total value staked from accounts
pub fn get_delegated_stake(key: &T::AccountId) -> u64 {
StakeFrom::<T>::iter_prefix(key).map(|(_, stake)| stake).sum()
}
/// Returns the total amount staked into the given key by other keys.
#[inline]
pub fn get_delegated_stake(staked: &T::AccountId) -> u64 {
StakeFrom::<T>::iter_prefix_values(staked).sum()
}

// --- Returns the transaction priority for setting weights.
pub fn get_priority_set_weights(key: &T::AccountId, netuid: u16) -> u64 {
if let Some(uid) = Uids::<T>::get(netuid, key) {
let last_update = Self::get_last_update_for_uid(netuid, uid);
Self::get_current_block_number().saturating_add(last_update)
} else {
0
}
}
// --- Returns the transaction priority for setting weights.
pub fn get_priority_stake(key: &T::AccountId, netuid: u16) -> u64 {
if Uids::<T>::contains_key(netuid, key) {
return Self::get_delegated_stake(key);
}
// --- Returns the transaction priority for setting weights.
pub fn get_priority_set_weights(key: &T::AccountId, netuid: u16) -> u64 {
if let Some(uid) = Uids::<T>::get(netuid, key) {
let last_update = Self::get_last_update_for_uid(netuid, uid);
Self::get_current_block_number().saturating_add(last_update)
} else {
0
}
}
// --- Returns the transaction priority for setting weights.
pub fn get_priority_stake(key: &T::AccountId, netuid: u16) -> u64 {
if Uids::<T>::contains_key(netuid, key) {
return Self::get_delegated_stake(key);
}
0
}
}

#[derive(Debug, PartialEq, Default)]
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.8.0"
version = "1.8.1"
description = "A Substrate node for commune-ai"
authors = ["Commune Community"]
homepage = "https://substrate.io/"
Expand Down
11 changes: 3 additions & 8 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,8 @@ pub mod opaque {
}
}

pub type Migrations = (
// ! Make sure that this migration in Subspace pallet runs first everytime
pallet_subspace::migrations::v12::MigrateToV12<Runtime>,
// Initialize the new pallet
pallet_subnet_emission::migrations::InitialMigration<Runtime>,
pallet_governance::migrations::MigrationV1<Runtime>,
);
pub type Migrations = ();

// To learn more about runtime versioning, see:
// https://docs.substrate.io/main-docs/build/upgrade#runtime-versioning
#[sp_version::runtime_version]
Expand All @@ -130,7 +125,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: 118,
spec_version: 119,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
18 changes: 18 additions & 0 deletions tests/src/subnet_emission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,21 @@ fn test_subnet_deregistration_based_on_emission() {
SubnetEmission::<Test>::insert(4, 500);
});
}

#[test]
fn yuma_does_not_fail_if_module_does_not_have_stake() {
new_test_ext().execute_with(|| {
zero_min_burn();
MinimumAllowedStake::<Test>::set(0);

let netuid: u16 = 1;
let key = 0;

let stake: u64 = 1;

assert_ok!(register_module(netuid, key, stake, false));
assert_ok!(SubspaceMod::do_remove_stake(get_origin(key), key, stake));

assert_ok!(YumaEpoch::<Test>::new(netuid, ONE).run());
});
}

0 comments on commit 324ee6c

Please sign in to comment.