Skip to content

Commit 064a074

Browse files
Merge pull request #2277 from opentensor/root-prop-storage-map
Root_prop storage map
2 parents ee967f6 + abbf449 commit 064a074

File tree

4 files changed

+102
-18
lines changed

4 files changed

+102
-18
lines changed

pallets/subtensor/src/coinbase/block_step.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
2525
Self::run_coinbase(block_emission);
2626
// --- 5. Update moving prices AFTER using them for emissions.
2727
Self::update_moving_prices();
28-
// --- 6. Set pending children on the epoch; but only after the coinbase has been run.
28+
// --- 6. Update roop prop AFTER using them for emissions.
29+
Self::update_root_prop();
30+
// --- 7. Set pending children on the epoch; but only after the coinbase has been run.
2931
Self::try_set_pending_children(block_number);
30-
// --- 7. Run auto-claim root divs.
32+
// --- 8. Run auto-claim root divs.
3133
Self::run_auto_claim_root_divs(last_block_hash);
32-
// --- 8. Populate root coldkey maps.
34+
// --- 9. Populate root coldkey maps.
3335
Self::populate_root_coldkey_staking_maps();
3436

3537
// Return ok.
@@ -277,6 +279,29 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
277279
}
278280
}
279281

282+
pub fn update_root_prop() {
283+
let subnets_to_emit_to: Vec<NetUid> =
284+
Self::get_subnets_to_emit_to(&Self::get_all_subnet_netuids());
285+
// Only root_prop for subnets that we emit to.
286+
for netuid_i in subnets_to_emit_to.iter() {
287+
let root_prop = Self::root_proportion(*netuid_i);
288+
289+
RootProp::<T>::insert(netuid_i, root_prop);
290+
}
291+
}
292+
293+
pub fn root_proportion(netuid: NetUid) -> U96F32 {
294+
let alpha_issuance = U96F32::from_num(Self::get_alpha_issuance(netuid));
295+
let root_tao: U96F32 = U96F32::from_num(SubnetTAO::<T>::get(NetUid::ROOT));
296+
let tao_weight: U96F32 = root_tao.saturating_mul(Self::get_tao_weight());
297+
298+
let root_proportion: U96F32 = tao_weight
299+
.checked_div(tao_weight.saturating_add(alpha_issuance))
300+
.unwrap_or(U96F32::from_num(0.0));
301+
302+
root_proportion
303+
}
304+
280305
pub fn reveal_crv3_commits() {
281306
let netuids: Vec<NetUid> = Self::get_all_subnet_netuids();
282307
for netuid in netuids.into_iter().filter(|netuid| *netuid != NetUid::ROOT) {

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,6 @@ impl<T: Config> Pallet<T> {
178178
// --- 3. Inject ALPHA for participants.
179179
let cut_percent: U96F32 = Self::get_float_subnet_owner_cut();
180180

181-
// Get total TAO on root.
182-
let root_tao: U96F32 = asfloat!(SubnetTAO::<T>::get(NetUid::ROOT));
183-
log::debug!("root_tao: {root_tao:?}");
184-
// Get tao_weight
185-
let tao_weight: U96F32 = root_tao.saturating_mul(Self::get_tao_weight());
186-
log::debug!("tao_weight: {tao_weight:?}");
187-
188181
for netuid_i in subnets_to_emit_to.iter() {
189182
// Get alpha_out for this block.
190183
let mut alpha_out_i: U96F32 = *alpha_out.get(netuid_i).unwrap_or(&asfloat!(0));
@@ -205,14 +198,8 @@ impl<T: Config> Pallet<T> {
205198
*total = total.saturating_add(tou64!(owner_cut_i).into());
206199
});
207200

208-
// Get ALPHA issuance.
209-
let alpha_issuance: U96F32 = asfloat!(Self::get_alpha_issuance(*netuid_i));
210-
log::debug!("alpha_issuance: {alpha_issuance:?}");
211-
212201
// Get root proportional dividends.
213-
let root_proportion: U96F32 = tao_weight
214-
.checked_div(tao_weight.saturating_add(alpha_issuance))
215-
.unwrap_or(asfloat!(0.0));
202+
let root_proportion = Self::root_proportion(*netuid_i);
216203
log::debug!("root_proportion: {root_proportion:?}");
217204

218205
// Get root alpha from root prop.

pallets/subtensor/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub mod pallet {
9595
use sp_std::collections::vec_deque::VecDeque;
9696
use sp_std::vec;
9797
use sp_std::vec::Vec;
98-
use substrate_fixed::types::{I64F64, I96F32, U64F64};
98+
use substrate_fixed::types::{I64F64, I96F32, U64F64, U96F32};
9999
use subtensor_macros::freeze_struct;
100100
use subtensor_runtime_common::{
101101
AlphaCurrency, Currency, MechId, NetUid, NetUidStorageIndex, TaoCurrency,
@@ -994,6 +994,12 @@ pub mod pallet {
994994
I96F32::saturating_from_num(0.0)
995995
}
996996

997+
/// Default subnet root proportion.
998+
#[pallet::type_value]
999+
pub fn DefaultRootProp<T: Config>() -> U96F32 {
1000+
U96F32::saturating_from_num(0.0)
1001+
}
1002+
9971003
/// Default subnet root claimable
9981004
#[pallet::type_value]
9991005
pub fn DefaultRootClaimable<T: Config>() -> BTreeMap<NetUid, I96F32> {
@@ -1284,6 +1290,11 @@ pub mod pallet {
12841290
pub type SubnetMovingPrice<T: Config> =
12851291
StorageMap<_, Identity, NetUid, I96F32, ValueQuery, DefaultMovingPrice<T>>;
12861292

1293+
/// --- MAP ( netuid ) --> root_prop | The subnet root proportion.
1294+
#[pallet::storage]
1295+
pub type RootProp<T: Config> =
1296+
StorageMap<_, Identity, NetUid, U96F32, ValueQuery, DefaultRootProp<T>>;
1297+
12871298
/// --- MAP ( netuid ) --> total_volume | The total amount of TAO bought and sold since the start of the network.
12881299
#[pallet::storage]
12891300
pub type SubnetVolume<T: Config> =

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3919,3 +3919,64 @@ fn test_pending_emission_start_call_not_done() {
39193919
);
39203920
});
39213921
}
3922+
3923+
#[test]
3924+
fn test_root_prop_filled_on_block_step() {
3925+
new_test_ext(1).execute_with(|| {
3926+
let hotkey = U256::from(10);
3927+
let coldkey = U256::from(11);
3928+
let netuid1 = add_dynamic_network(&hotkey, &coldkey);
3929+
let netuid2 = add_dynamic_network(&hotkey, &coldkey);
3930+
3931+
SubnetTAO::<Test>::insert(NetUid::ROOT, TaoCurrency::from(1_000_000_000_000u64));
3932+
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
3933+
3934+
let tao_reserve = TaoCurrency::from(50_000_000_000);
3935+
let alpha_in = AlphaCurrency::from(100_000_000_000);
3936+
SubnetTAO::<Test>::insert(netuid1, tao_reserve);
3937+
SubnetAlphaIn::<Test>::insert(netuid1, alpha_in);
3938+
SubnetTAO::<Test>::insert(netuid2, tao_reserve);
3939+
SubnetAlphaIn::<Test>::insert(netuid2, alpha_in);
3940+
3941+
assert!(!RootProp::<Test>::contains_key(netuid1));
3942+
assert!(!RootProp::<Test>::contains_key(netuid2));
3943+
3944+
run_to_block(2);
3945+
3946+
assert!(RootProp::<Test>::get(netuid1) > U96F32::from_num(0));
3947+
assert!(RootProp::<Test>::get(netuid2) > U96F32::from_num(0));
3948+
});
3949+
}
3950+
3951+
#[test]
3952+
fn test_root_proportion() {
3953+
new_test_ext(1).execute_with(|| {
3954+
let hotkey = U256::from(10);
3955+
let coldkey = U256::from(11);
3956+
let netuid = add_dynamic_network(&hotkey, &coldkey);
3957+
3958+
let root_tao_reserve = 1_000_000_000_000u64;
3959+
SubnetTAO::<Test>::insert(NetUid::ROOT, TaoCurrency::from(root_tao_reserve));
3960+
3961+
let tao_weight = 3_320_413_933_267_719_290u64;
3962+
SubtensorModule::set_tao_weight(tao_weight);
3963+
3964+
let alpha_in = 100_000_000_000u64;
3965+
SubnetAlphaIn::<Test>::insert(netuid, AlphaCurrency::from(alpha_in));
3966+
3967+
let actual_root_proportion = SubtensorModule::root_proportion(netuid);
3968+
let expected_root_prop = {
3969+
let tao_weight = SubtensorModule::get_tao_weight();
3970+
let root_tao = U96F32::from_num(root_tao_reserve);
3971+
let alpha_in = {
3972+
let alpha: u64 = SubtensorModule::get_alpha_issuance(netuid).into();
3973+
3974+
U96F32::from_num(alpha)
3975+
};
3976+
3977+
tao_weight * root_tao / (tao_weight * root_tao + alpha_in)
3978+
};
3979+
3980+
assert_eq!(actual_root_proportion, expected_root_prop);
3981+
});
3982+
}

0 commit comments

Comments
 (0)