Skip to content

Commit 969730d

Browse files
authored
Merge pull request #2108 from opentensor/migrate-network-lock-cost-2500
Migrate Reset Network Burn Cost to 2500
2 parents a60ebab + 269e51f commit 969730d

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ mod hooks {
147147
// Migrate Lock Reduction Interval
148148
.saturating_add(migrations::migrate_network_lock_reduction_interval::migrate_network_lock_reduction_interval::<T>())
149149
// Migrate subnet locked balances
150-
.saturating_add(migrations::migrate_subnet_locked::migrate_restore_subnet_locked::<T>());
150+
.saturating_add(migrations::migrate_subnet_locked::migrate_restore_subnet_locked::<T>())
151+
// Migrate subnet burn cost to 2500
152+
.saturating_add(migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::<T>());
151153
weight
152154
}
153155

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use super::*;
2+
use frame_support::{traits::Get, weights::Weight};
3+
use log;
4+
use scale_info::prelude::string::String;
5+
6+
pub fn migrate_network_lock_cost_2500<T: Config>() -> Weight {
7+
const RAO_PER_TAO: u64 = 1_000_000_000;
8+
const TARGET_COST_TAO: u64 = 2_500;
9+
const NEW_LAST_LOCK_RAO: u64 = (TARGET_COST_TAO / 2) * RAO_PER_TAO; // 1,250 TAO
10+
11+
let migration_name = b"migrate_network_lock_cost_2500".to_vec();
12+
let mut weight = T::DbWeight::get().reads(1);
13+
14+
// Skip if already executed
15+
if HasMigrationRun::<T>::get(&migration_name) {
16+
log::info!(
17+
target: "runtime",
18+
"Migration '{}' already run - skipping.",
19+
String::from_utf8_lossy(&migration_name)
20+
);
21+
return weight;
22+
}
23+
24+
// Use the current block; ensure it's non-zero so mult == 2 in get_network_lock_cost()
25+
let current_block = Pallet::<T>::get_current_block_as_u64();
26+
let block_to_set = if current_block == 0 { 1 } else { current_block };
27+
28+
// Set last_lock so that price = 2 * last_lock = 2,500 TAO at this block
29+
Pallet::<T>::set_network_last_lock(TaoCurrency::from(NEW_LAST_LOCK_RAO));
30+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
31+
32+
// Start decay from "now" (no backdated decay)
33+
Pallet::<T>::set_network_last_lock_block(block_to_set);
34+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
35+
36+
// Mark migration done
37+
HasMigrationRun::<T>::insert(&migration_name, true);
38+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
39+
40+
log::info!(
41+
target: "runtime",
42+
"Migration '{}' completed. lock_cost set to 2,500 TAO at block {}.",
43+
String::from_utf8_lossy(&migration_name),
44+
block_to_set
45+
);
46+
47+
weight
48+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod migrate_fix_root_tao_and_alpha_in;
2121
pub mod migrate_identities_v2;
2222
pub mod migrate_init_total_issuance;
2323
pub mod migrate_network_immunity_period;
24+
pub mod migrate_network_lock_cost_2500;
2425
pub mod migrate_network_lock_reduction_interval;
2526
pub mod migrate_orphaned_storage_items;
2627
pub mod migrate_populate_owned_hotkeys;

pallets/subtensor/src/tests/migration.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,3 +2044,115 @@ fn test_migrate_restore_subnet_locked_65_128() {
20442044
);
20452045
});
20462046
}
2047+
2048+
#[test]
2049+
fn test_migrate_network_lock_cost_2500_sets_price_and_decay() {
2050+
new_test_ext(0).execute_with(|| {
2051+
// ── constants ───────────────────────────────────────────────────────
2052+
const RAO_PER_TAO: u64 = 1_000_000_000;
2053+
const TARGET_COST_TAO: u64 = 2_500;
2054+
const TARGET_COST_RAO: u64 = TARGET_COST_TAO * RAO_PER_TAO;
2055+
const NEW_LAST_LOCK_RAO: u64 = (TARGET_COST_TAO / 2) * RAO_PER_TAO;
2056+
2057+
let migration_key = b"migrate_network_lock_cost_2500".to_vec();
2058+
2059+
// ── pre ──────────────────────────────────────────────────────────────
2060+
assert!(
2061+
!HasMigrationRun::<Test>::get(migration_key.clone()),
2062+
"HasMigrationRun should be false before migration"
2063+
);
2064+
2065+
// Ensure current_block > 0 so mult == 2 in get_network_lock_cost()
2066+
step_block(1);
2067+
let current_block_before = Pallet::<Test>::get_current_block_as_u64();
2068+
2069+
// Snapshot interval to ensure migration doesn't change it
2070+
let interval_before = NetworkLockReductionInterval::<Test>::get();
2071+
2072+
// ── run migration ────────────────────────────────────────────────────
2073+
let weight = crate::migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::<Test>();
2074+
assert!(!weight.is_zero(), "migration weight should be > 0");
2075+
2076+
// ── asserts: params & flags ─────────────────────────────────────────
2077+
assert_eq!(
2078+
Pallet::<Test>::get_network_last_lock(),
2079+
NEW_LAST_LOCK_RAO.into(),
2080+
"last_lock should be set to 1,250 TAO (in rao)"
2081+
);
2082+
assert_eq!(
2083+
Pallet::<Test>::get_network_last_lock_block(),
2084+
current_block_before,
2085+
"last_lock_block should be set to the current block"
2086+
);
2087+
2088+
// Lock cost should be exactly 2,500 TAO immediately after migration
2089+
let lock_cost_now = Pallet::<Test>::get_network_lock_cost();
2090+
assert_eq!(
2091+
lock_cost_now,
2092+
TARGET_COST_RAO.into(),
2093+
"lock cost should be 2,500 TAO right after migration"
2094+
);
2095+
2096+
// Interval should be unchanged by this migration
2097+
assert_eq!(
2098+
NetworkLockReductionInterval::<Test>::get(),
2099+
interval_before,
2100+
"lock reduction interval should not be modified by this migration"
2101+
);
2102+
2103+
assert!(
2104+
HasMigrationRun::<Test>::get(migration_key.clone()),
2105+
"HasMigrationRun should be true after migration"
2106+
);
2107+
2108+
// ── decay check (1 block later) ─────────────────────────────────────
2109+
// Expected: cost = max(min_lock, 2*L - floor(L / eff_interval) * delta_blocks)
2110+
let eff_interval = Pallet::<Test>::get_lock_reduction_interval();
2111+
let per_block_decrement: u64 = if eff_interval == 0 {
2112+
0
2113+
} else {
2114+
NEW_LAST_LOCK_RAO / eff_interval
2115+
};
2116+
2117+
let min_lock_rao: u64 = Pallet::<Test>::get_network_min_lock().to_u64();
2118+
2119+
step_block(1);
2120+
let expected_after_1: u64 = core::cmp::max(
2121+
min_lock_rao,
2122+
TARGET_COST_RAO.saturating_sub(per_block_decrement),
2123+
);
2124+
let lock_cost_after_1 = Pallet::<Test>::get_network_lock_cost();
2125+
assert_eq!(
2126+
lock_cost_after_1,
2127+
expected_after_1.into(),
2128+
"lock cost should decay by one per-block step after 1 block"
2129+
);
2130+
2131+
// ── idempotency: running the migration again should do nothing ──────
2132+
let last_lock_before_rerun = Pallet::<Test>::get_network_last_lock();
2133+
let last_lock_block_before_rerun = Pallet::<Test>::get_network_last_lock_block();
2134+
let cost_before_rerun = Pallet::<Test>::get_network_lock_cost();
2135+
2136+
let _weight2 = crate::migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::<Test>();
2137+
2138+
assert!(
2139+
HasMigrationRun::<Test>::get(migration_key.clone()),
2140+
"HasMigrationRun remains true on second run"
2141+
);
2142+
assert_eq!(
2143+
Pallet::<Test>::get_network_last_lock(),
2144+
last_lock_before_rerun,
2145+
"second run should not modify last_lock"
2146+
);
2147+
assert_eq!(
2148+
Pallet::<Test>::get_network_last_lock_block(),
2149+
last_lock_block_before_rerun,
2150+
"second run should not modify last_lock_block"
2151+
);
2152+
assert_eq!(
2153+
Pallet::<Test>::get_network_lock_cost(),
2154+
cost_before_rerun,
2155+
"second run should not change current lock cost"
2156+
);
2157+
});
2158+
}

0 commit comments

Comments
 (0)