Skip to content

Commit c84db66

Browse files
authored
Merge pull request #1203 from opentensor/devnet
testnet deploy 1/27/2025
2 parents 0631940 + 84c9434 commit c84db66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2442
-993
lines changed

Cargo.lock

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pallets/collective/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ pub mod pallet {
549549
);
550550

551551
let threshold = T::GetVotingMembers::get_count()
552-
.saturating_div(2)
552+
.checked_div(2)
553+
.unwrap_or(0)
553554
.saturating_add(1);
554555

555556
let members = Self::members();

pallets/subtensor/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pallet-utility = { workspace = true }
4141
ndarray = { workspace = true }
4242
hex = { workspace = true }
4343
share-pool = { default-features = false, path = "../../primitives/share-pool" }
44+
safe-math = { default-features = false, path = "../../primitives/safe-math" }
4445
approx = { workspace = true }
4546

4647
pallet-collective = { version = "4.0.0-dev", default-features = false, path = "../collective" }
@@ -104,6 +105,7 @@ std = [
104105
"ark-serialize/std",
105106
"w3f-bls/std",
106107
"rand_chacha/std",
108+
"safe-math/std",
107109
"sha2/std",
108110
"share-pool/std"
109111
]

pallets/subtensor/src/coinbase/block_emission.rs

+27-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use frame_support::traits::Get;
3+
use safe_math::*;
34
use substrate_fixed::{transcendental::log2, types::I96F32};
45

56
impl<T: Config> Pallet<T> {
@@ -30,15 +31,15 @@ impl<T: Config> Pallet<T> {
3031
alpha_block_emission: u64,
3132
) -> (u64, u64, u64) {
3233
// Init terms.
33-
let mut tao_in_emission: I96F32 = I96F32::from_num(tao_emission);
34-
let float_alpha_block_emission: I96F32 = I96F32::from_num(alpha_block_emission);
34+
let mut tao_in_emission: I96F32 = I96F32::saturating_from_num(tao_emission);
35+
let float_alpha_block_emission: I96F32 = I96F32::saturating_from_num(alpha_block_emission);
3536

3637
// Get alpha price for subnet.
3738
let alpha_price: I96F32 = Self::get_alpha_price(netuid);
3839
log::debug!("{:?} - alpha_price: {:?}", netuid, alpha_price);
3940

4041
// Get initial alpha_in
41-
let mut alpha_in_emission: I96F32 = I96F32::from_num(tao_emission)
42+
let mut alpha_in_emission: I96F32 = I96F32::saturating_from_num(tao_emission)
4243
.checked_div(alpha_price)
4344
.unwrap_or(float_alpha_block_emission);
4445

@@ -59,15 +60,15 @@ impl<T: Config> Pallet<T> {
5960
}
6061

6162
// Avoid rounding errors.
62-
if tao_in_emission < I96F32::from_num(1) || alpha_in_emission < I96F32::from_num(1) {
63-
alpha_in_emission = I96F32::from_num(0);
64-
tao_in_emission = I96F32::from_num(0);
63+
if tao_in_emission < I96F32::saturating_from_num(1)
64+
|| alpha_in_emission < I96F32::saturating_from_num(1)
65+
{
66+
alpha_in_emission = I96F32::saturating_from_num(0);
67+
tao_in_emission = I96F32::saturating_from_num(0);
6568
}
6669

6770
// Set Alpha in emission.
68-
let alpha_out_emission = I96F32::from_num(2)
69-
.saturating_mul(float_alpha_block_emission)
70-
.saturating_sub(alpha_in_emission);
71+
let alpha_out_emission = float_alpha_block_emission;
7172

7273
// Log results.
7374
log::debug!("{:?} - tao_in_emission: {:?}", netuid, tao_in_emission);
@@ -80,9 +81,9 @@ impl<T: Config> Pallet<T> {
8081

8182
// Return result.
8283
(
83-
tao_in_emission.to_num::<u64>(),
84-
alpha_in_emission.to_num::<u64>(),
85-
alpha_out_emission.to_num::<u64>(),
84+
tao_in_emission.saturating_to_num::<u64>(),
85+
alpha_in_emission.saturating_to_num::<u64>(),
86+
alpha_out_emission.saturating_to_num::<u64>(),
8687
)
8788
}
8889

@@ -105,23 +106,22 @@ impl<T: Config> Pallet<T> {
105106
/// Returns the block emission for an issuance value.
106107
pub fn get_block_emission_for_issuance(issuance: u64) -> Result<u64, &'static str> {
107108
// Convert issuance to a float for calculations below.
108-
let total_issuance: I96F32 = I96F32::from_num(issuance);
109+
let total_issuance: I96F32 = I96F32::saturating_from_num(issuance);
109110
// Check to prevent division by zero when the total supply is reached
110111
// and creating an issuance greater than the total supply.
111-
if total_issuance >= I96F32::from_num(TotalSupply::<T>::get()) {
112+
if total_issuance >= I96F32::saturating_from_num(TotalSupply::<T>::get()) {
112113
return Ok(0);
113114
}
114115
// Calculate the logarithmic residual of the issuance against half the total supply.
115116
let residual: I96F32 = log2(
116-
I96F32::from_num(1.0)
117+
I96F32::saturating_from_num(1.0)
117118
.checked_div(
118-
I96F32::from_num(1.0)
119+
I96F32::saturating_from_num(1.0)
119120
.checked_sub(
120121
total_issuance
121-
.checked_div(
122-
I96F32::from_num(2.0)
123-
.saturating_mul(I96F32::from_num(10_500_000_000_000_000.0)),
124-
)
122+
.checked_div(I96F32::saturating_from_num(2.0).saturating_mul(
123+
I96F32::saturating_from_num(10_500_000_000_000_000.0),
124+
))
125125
.ok_or("Logarithm calculation failed")?,
126126
)
127127
.ok_or("Logarithm calculation failed")?,
@@ -133,18 +133,19 @@ impl<T: Config> Pallet<T> {
133133
let floored_residual: I96F32 = residual.floor();
134134
// Calculate the final emission rate using the floored residual.
135135
// Convert floored_residual to an integer
136-
let floored_residual_int: u64 = floored_residual.to_num::<u64>();
136+
let floored_residual_int: u64 = floored_residual.saturating_to_num::<u64>();
137137
// Multiply 2.0 by itself floored_residual times to calculate the power of 2.
138-
let mut multiplier: I96F32 = I96F32::from_num(1.0);
138+
let mut multiplier: I96F32 = I96F32::saturating_from_num(1.0);
139139
for _ in 0..floored_residual_int {
140-
multiplier = multiplier.saturating_mul(I96F32::from_num(2.0));
140+
multiplier = multiplier.saturating_mul(I96F32::saturating_from_num(2.0));
141141
}
142-
let block_emission_percentage: I96F32 = I96F32::from_num(1.0).saturating_div(multiplier);
142+
let block_emission_percentage: I96F32 =
143+
I96F32::saturating_from_num(1.0).safe_div(multiplier);
143144
// Calculate the actual emission based on the emission rate
144145
let block_emission: I96F32 = block_emission_percentage
145-
.saturating_mul(I96F32::from_num(DefaultBlockEmission::<T>::get()));
146+
.saturating_mul(I96F32::saturating_from_num(DefaultBlockEmission::<T>::get()));
146147
// Convert to u64
147-
let block_emission_u64: u64 = block_emission.to_num::<u64>();
148+
let block_emission_u64: u64 = block_emission.saturating_to_num::<u64>();
148149
if BlockEmission::<T>::get() != block_emission_u64 {
149150
BlockEmission::<T>::put(block_emission_u64);
150151
}

pallets/subtensor/src/coinbase/block_step.rs

+37-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use frame_support::storage::IterableStorageMap;
3+
use safe_math::*;
34
use substrate_fixed::types::{I110F18, I96F32};
45

56
impl<T: Config + pallet_drand::Config> Pallet<T> {
@@ -10,14 +11,29 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
1011
// --- 1. Adjust difficulties.
1112
Self::adjust_registration_terms_for_networks();
1213
// --- 2. Get the current coinbase emission.
13-
let block_emission: I96F32 = I96F32::from_num(Self::get_block_emission().unwrap_or(0));
14+
let block_emission: I96F32 =
15+
I96F32::saturating_from_num(Self::get_block_emission().unwrap_or(0));
1416
log::debug!("Block emission: {:?}", block_emission);
1517
// --- 3. Run emission through network.
1618
Self::run_coinbase(block_emission);
19+
20+
// --- 4. Set pending children on the epoch; but only after the coinbase has been run.
21+
Self::try_set_pending_children(block_number);
22+
1723
// Return ok.
1824
Ok(())
1925
}
2026

27+
fn try_set_pending_children(block_number: u64) {
28+
let subnets: Vec<u16> = Self::get_all_subnet_netuids();
29+
for &netuid in subnets.iter() {
30+
if Self::should_run_epoch(netuid, block_number) {
31+
// Set pending children on the epoch.
32+
Self::do_set_pending_children(netuid);
33+
}
34+
}
35+
}
36+
2137
/// Adjusts the network difficulties/burns of every active network. Resetting state parameters.
2238
///
2339
pub fn adjust_registration_terms_for_networks() {
@@ -184,28 +200,28 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
184200
registrations_this_interval: u16,
185201
target_registrations_per_interval: u16,
186202
) -> u64 {
187-
let updated_difficulty: I110F18 = I110F18::from_num(current_difficulty)
188-
.saturating_mul(I110F18::from_num(
203+
let updated_difficulty: I110F18 = I110F18::saturating_from_num(current_difficulty)
204+
.saturating_mul(I110F18::saturating_from_num(
189205
registrations_this_interval.saturating_add(target_registrations_per_interval),
190206
))
191-
.saturating_div(I110F18::from_num(
207+
.safe_div(I110F18::saturating_from_num(
192208
target_registrations_per_interval.saturating_add(target_registrations_per_interval),
193209
));
194-
let alpha: I110F18 = I110F18::from_num(Self::get_adjustment_alpha(netuid))
195-
.saturating_div(I110F18::from_num(u64::MAX));
210+
let alpha: I110F18 = I110F18::saturating_from_num(Self::get_adjustment_alpha(netuid))
211+
.safe_div(I110F18::saturating_from_num(u64::MAX));
196212
let next_value: I110F18 = alpha
197-
.saturating_mul(I110F18::from_num(current_difficulty))
213+
.saturating_mul(I110F18::saturating_from_num(current_difficulty))
198214
.saturating_add(
199-
I110F18::from_num(1.0)
215+
I110F18::saturating_from_num(1.0)
200216
.saturating_sub(alpha)
201217
.saturating_mul(updated_difficulty),
202218
);
203-
if next_value >= I110F18::from_num(Self::get_max_difficulty(netuid)) {
219+
if next_value >= I110F18::saturating_from_num(Self::get_max_difficulty(netuid)) {
204220
Self::get_max_difficulty(netuid)
205-
} else if next_value <= I110F18::from_num(Self::get_min_difficulty(netuid)) {
221+
} else if next_value <= I110F18::saturating_from_num(Self::get_min_difficulty(netuid)) {
206222
return Self::get_min_difficulty(netuid);
207223
} else {
208-
return next_value.to_num::<u64>();
224+
return next_value.saturating_to_num::<u64>();
209225
}
210226
}
211227

@@ -218,28 +234,28 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
218234
registrations_this_interval: u16,
219235
target_registrations_per_interval: u16,
220236
) -> u64 {
221-
let updated_burn: I110F18 = I110F18::from_num(current_burn)
222-
.saturating_mul(I110F18::from_num(
237+
let updated_burn: I110F18 = I110F18::saturating_from_num(current_burn)
238+
.saturating_mul(I110F18::saturating_from_num(
223239
registrations_this_interval.saturating_add(target_registrations_per_interval),
224240
))
225-
.saturating_div(I110F18::from_num(
241+
.safe_div(I110F18::saturating_from_num(
226242
target_registrations_per_interval.saturating_add(target_registrations_per_interval),
227243
));
228-
let alpha: I110F18 = I110F18::from_num(Self::get_adjustment_alpha(netuid))
229-
.saturating_div(I110F18::from_num(u64::MAX));
244+
let alpha: I110F18 = I110F18::saturating_from_num(Self::get_adjustment_alpha(netuid))
245+
.safe_div(I110F18::saturating_from_num(u64::MAX));
230246
let next_value: I110F18 = alpha
231-
.saturating_mul(I110F18::from_num(current_burn))
247+
.saturating_mul(I110F18::saturating_from_num(current_burn))
232248
.saturating_add(
233-
I110F18::from_num(1.0)
249+
I110F18::saturating_from_num(1.0)
234250
.saturating_sub(alpha)
235251
.saturating_mul(updated_burn),
236252
);
237-
if next_value >= I110F18::from_num(Self::get_max_burn_as_u64(netuid)) {
253+
if next_value >= I110F18::saturating_from_num(Self::get_max_burn_as_u64(netuid)) {
238254
Self::get_max_burn_as_u64(netuid)
239-
} else if next_value <= I110F18::from_num(Self::get_min_burn_as_u64(netuid)) {
255+
} else if next_value <= I110F18::saturating_from_num(Self::get_min_burn_as_u64(netuid)) {
240256
return Self::get_min_burn_as_u64(netuid);
241257
} else {
242-
return next_value.to_num::<u64>();
258+
return next_value.saturating_to_num::<u64>();
243259
}
244260
}
245261
}

pallets/subtensor/src/coinbase/root.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use super::*;
1919
use frame_support::dispatch::Pays;
2020
use frame_support::storage::IterableStorageDoubleMap;
2121
use frame_support::weights::Weight;
22+
use safe_math::*;
2223
use sp_core::Get;
2324
use sp_std::vec;
2425
use substrate_fixed::types::I64F64;
@@ -112,7 +113,7 @@ impl<T: Config> Pallet<T> {
112113

113114
// --- 2. Initialize a 2D vector with zeros to store the weights. The dimensions are determined
114115
// by `n` (number of validators) and `k` (total number of subnets).
115-
let mut weights: Vec<Vec<I64F64>> = vec![vec![I64F64::from_num(0.0); k]; n];
116+
let mut weights: Vec<Vec<I64F64>> = vec![vec![I64F64::saturating_from_num(0.0); k]; n];
116117
log::debug!("weights:\n{:?}\n", weights);
117118

118119
let subnet_list = Self::get_all_subnet_netuids();
@@ -134,7 +135,7 @@ impl<T: Config> Pallet<T> {
134135
.zip(&subnet_list)
135136
.find(|(_, subnet)| *subnet == netuid)
136137
{
137-
*w = I64F64::from_num(*weight_ij);
138+
*w = I64F64::saturating_from_num(*weight_ij);
138139
}
139140
}
140141
}
@@ -624,7 +625,7 @@ impl<T: Config> Pallet<T> {
624625

625626
let mut lock_cost = last_lock.saturating_mul(mult).saturating_sub(
626627
last_lock
627-
.saturating_div(lock_reduction_interval)
628+
.safe_div(lock_reduction_interval)
628629
.saturating_mul(current_block.saturating_sub(last_lock_block)),
629630
);
630631

0 commit comments

Comments
 (0)