Skip to content

Disallow subnet owners to set AdjustmentAlpha greater than 0.97 #1606

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

Open
wants to merge 6 commits into
base: devnet-ready
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 15 additions & 14 deletions evm-tests/test/subnet.precompile.hyperparameter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 +163,27 @@ describe("Test the Subnet precompile contract", () => {
// assert.equal(valueFromContract, onchainValue);
// })

it("Can set adjustmentAlpha parameter", async () => {
// need sudo as origin now
// it("Can set adjustmentAlpha parameter", async () => {

const totalNetwork = await api.query.SubtensorModule.TotalNetworks.getValue()
const contract = new ethers.Contract(ISUBNET_ADDRESS, ISubnetABI, wallet);
const netuid = totalNetwork - 1;
// const totalNetwork = await api.query.SubtensorModule.TotalNetworks.getValue()
// const contract = new ethers.Contract(ISUBNET_ADDRESS, ISubnetABI, wallet);
// const netuid = totalNetwork - 1;

const newValue = 105;
const tx = await contract.setAdjustmentAlpha(netuid, newValue);
await tx.wait();
// const newValue = 105;
// const tx = await contract.setAdjustmentAlpha(netuid, newValue);
// await tx.wait();

let onchainValue = await api.query.SubtensorModule.AdjustmentAlpha.getValue(netuid)
// let onchainValue = await api.query.SubtensorModule.AdjustmentAlpha.getValue(netuid)


let valueFromContract = Number(
await contract.getAdjustmentAlpha(netuid)
);
// let valueFromContract = Number(
// await contract.getAdjustmentAlpha(netuid)
// );

assert.equal(valueFromContract, newValue)
assert.equal(valueFromContract, onchainValue);
})
// assert.equal(valueFromContract, newValue)
// assert.equal(valueFromContract, onchainValue);
// })

it("Can set maxWeightLimit parameter", async () => {

Expand Down
2 changes: 1 addition & 1 deletion pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ pub mod pallet {
netuid: u16,
adjustment_alpha: u64,
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
ensure_root(origin)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ mod hooks {
// Reset max burn
.saturating_add(migrations::migrate_reset_max_burn::migrate_reset_max_burn::<T>())
// Migrate ColdkeySwapScheduled structure to new format
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>());
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>())
// Reset adjustment alpha
.saturating_add(migrations::migrate_reset_adjustment_alpha::migrate_reset_adjustment_alpha::<T>());
weight
}

Expand Down
59 changes: 59 additions & 0 deletions pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::*;
use frame_support::{traits::Get, weights::Weight};
use log;
use scale_info::prelude::string::String;

pub fn migrate_reset_adjustment_alpha<T: Config>() -> Weight {
let migration_name = b"migrate_reset_adjustment_alpha".to_vec();
let mut weight = T::DbWeight::get().reads(1);

// ------------------------------
// Step 0: Check if already run
// ------------------------------
if HasMigrationRun::<T>::get(&migration_name) {
log::info!(
"Migration '{:?}' has already run. Skipping.",
migration_name
);
return weight;
}

log::info!(
"Running migration '{}'",
String::from_utf8_lossy(&migration_name)
);

// ------------------------------
// Step 1: Reset all subnet's adjustment alpha to 0.5 if the value exceeds 0.5
// ------------------------------

let mut reset_entries_count = 0u64;

for netuid in AdjustmentAlpha::<T>::iter_keys() {
AdjustmentAlpha::<T>::mutate(netuid, |adjustment| {
*adjustment = (*adjustment).min(32768); // 0.5
});
reset_entries_count = reset_entries_count.saturating_add(1);
}

weight = weight
.saturating_add(T::DbWeight::get().reads_writes(reset_entries_count, reset_entries_count));

log::info!(
"Reset {} subnets from AdjustmentAlpha.",
reset_entries_count
);

// ------------------------------
// Step 2: Mark Migration as Completed
// ------------------------------
HasMigrationRun::<T>::insert(&migration_name, true);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

log::info!(
"Migration '{:?}' completed successfully.",
String::from_utf8_lossy(&migration_name)
);

weight
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod migrate_remove_stake_map;
pub mod migrate_remove_total_hotkey_coldkey_stakes_this_interval;
pub mod migrate_remove_unused_maps_and_values;
pub mod migrate_remove_zero_total_hotkey_alpha;
pub mod migrate_reset_adjustment_alpha;
pub mod migrate_reset_bonds_moving_average;
pub mod migrate_reset_max_burn;
pub mod migrate_set_first_emission_block_number;
Expand Down
17 changes: 5 additions & 12 deletions precompiles/src/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,12 @@ where
#[precompile::public("setAdjustmentAlpha(uint16,uint64)")]
#[precompile::payable]
fn set_adjustment_alpha(
handle: &mut impl PrecompileHandle,
netuid: u16,
adjustment_alpha: u64,
_handle: &mut impl PrecompileHandle,
_netuid: u16,
_adjustment_alpha: u64,
) -> EvmResult<()> {
let call = pallet_admin_utils::Call::<R>::sudo_set_adjustment_alpha {
netuid,
adjustment_alpha,
};

handle.try_dispatch_runtime_call::<R, _>(
call,
RawOrigin::Signed(handle.caller_account_id::<R>()),
)
// DEPRECATED. Subnet owner cannot set adjustment alpha
Ok(())
}

#[precompile::public("getMaxWeightLimit(uint16)")]
Expand Down
Loading