Skip to content

Commit d3e2f9e

Browse files
committed
simple root monitoring solution
1 parent be47139 commit d3e2f9e

File tree

2 files changed

+6
-270
lines changed

2 files changed

+6
-270
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ impl<T: Config> Pallet<T> {
7878
);
7979
if let Ok(buy_swap_result_ok) = buy_swap_result {
8080
let bought_alpha: AlphaCurrency = buy_swap_result_ok.amount_paid_out.into();
81-
Self::recycle_subnet_alpha(*netuid_i, bought_alpha);
81+
// Self::recycle_subnet_alpha(*netuid_i, bought_alpha);
82+
PendingRootAlphaDivs::<T>::mutate(*netuid_i, |total| {
83+
*total = total.saturating_add(bought_alpha);
84+
});
8285
}
8386
}
8487

@@ -148,8 +151,8 @@ impl<T: Config> Pallet<T> {
148151
let mut alpha_in_i: U96F32 = tao_emission_i.safe_div_or(price_i, U96F32::from_num(0.0));
149152

150153
let alpha_injection_cap: U96F32 = alpha_emission_i.min(tao_block_emission);
151-
if alpha_in_i > alpha_injection_cap {
152-
alpha_in_i = alpha_injection_cap;
154+
if alpha_in_i > Self::root_proportion(netuid_i) {
155+
alpha_in_i = Self::root_proportion(netuid_i) ;
153156
tao_in_i = alpha_in_i.saturating_mul(price_i);
154157
}
155158

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 0 additions & 267 deletions
Original file line numberDiff line numberDiff line change
@@ -588,81 +588,6 @@ fn test_coinbase_alpha_issuance_with_cap_trigger() {
588588
});
589589
}
590590

591-
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission --exact --show-output --nocapture
592-
#[test]
593-
fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() {
594-
new_test_ext(1).execute_with(|| {
595-
let netuid1 = NetUid::from(1);
596-
let netuid2 = NetUid::from(2);
597-
let emission: u64 = 1_000_000;
598-
add_network(netuid1, 1, 0);
599-
add_network(netuid2, 1, 0);
600-
601-
// Make subnets dynamic.
602-
SubnetMechanism::<Test>::insert(netuid1, 1);
603-
SubnetMechanism::<Test>::insert(netuid2, 1);
604-
605-
// Setup prices 0.000001
606-
let initial_tao: u64 = 10_000_u64;
607-
let initial_alpha: u64 = initial_tao * 100_000_u64;
608-
mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha.into());
609-
mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha.into());
610-
611-
// Enable emission
612-
FirstEmissionBlockNumber::<Test>::insert(netuid1, 0);
613-
FirstEmissionBlockNumber::<Test>::insert(netuid2, 0);
614-
// Set subnet TAO flows to non-zero and 1:2 ratio
615-
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
616-
SubnetTaoFlow::<Test>::insert(netuid2, 200_000_000_i64);
617-
618-
// Force the swap to initialize
619-
SubtensorModule::swap_tao_for_alpha(
620-
netuid1,
621-
TaoCurrency::ZERO,
622-
1_000_000_000_000.into(),
623-
false,
624-
)
625-
.unwrap();
626-
SubtensorModule::swap_tao_for_alpha(
627-
netuid2,
628-
TaoCurrency::ZERO,
629-
1_000_000_000_000.into(),
630-
false,
631-
)
632-
.unwrap();
633-
634-
// Get the prices before the run_coinbase
635-
let price_1_before = <Test as pallet::Config>::SwapInterface::current_alpha_price(netuid1);
636-
let price_2_before = <Test as pallet::Config>::SwapInterface::current_alpha_price(netuid2);
637-
638-
// Set issuance at 21M
639-
SubnetAlphaOut::<Test>::insert(netuid1, AlphaCurrency::from(21_000_000_000_000_000)); // Set issuance above 21M
640-
SubnetAlphaOut::<Test>::insert(netuid2, AlphaCurrency::from(21_000_000_000_000_000)); // Set issuance above 21M
641-
642-
// Run coinbase
643-
SubtensorModule::run_coinbase(U96F32::from_num(emission));
644-
645-
// Get the prices after the run_coinbase
646-
let price_1_after = <Test as pallet::Config>::SwapInterface::current_alpha_price(netuid1);
647-
let price_2_after = <Test as pallet::Config>::SwapInterface::current_alpha_price(netuid2);
648-
649-
// AlphaIn gets decreased beacuse of a buy
650-
assert!(u64::from(SubnetAlphaIn::<Test>::get(netuid1)) < initial_alpha);
651-
assert_eq!(
652-
u64::from(SubnetAlphaOut::<Test>::get(netuid2)),
653-
21_000_000_000_000_000_u64
654-
);
655-
assert!(u64::from(SubnetAlphaIn::<Test>::get(netuid2)) < initial_alpha);
656-
assert_eq!(
657-
u64::from(SubnetAlphaOut::<Test>::get(netuid2)),
658-
21_000_000_000_000_000_u64
659-
);
660-
661-
assert!(price_1_after > price_1_before);
662-
assert!(price_2_after > price_2_before);
663-
});
664-
}
665-
666591
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_owner_cut_base --exact --show-output --nocapture
667592
#[test]
668593
fn test_owner_cut_base() {
@@ -2976,198 +2901,6 @@ fn test_zero_shares_zero_emission() {
29762901
});
29772902
}
29782903

2979-
#[test]
2980-
fn test_mining_emission_distribution_with_no_root_sell() {
2981-
new_test_ext(1).execute_with(|| {
2982-
let validator_coldkey = U256::from(1);
2983-
let validator_hotkey = U256::from(2);
2984-
let validator_miner_coldkey = U256::from(3);
2985-
let validator_miner_hotkey = U256::from(4);
2986-
let miner_coldkey = U256::from(5);
2987-
let miner_hotkey = U256::from(6);
2988-
let netuid = NetUid::from(1);
2989-
let subnet_tempo = 10;
2990-
let stake: u64 = 100_000_000_000;
2991-
let root_stake: u64 = 200_000_000_000; // 200 TAO
2992-
2993-
// Create root network
2994-
SubtensorModule::set_tao_weight(0); // Start tao weight at 0
2995-
SubtokenEnabled::<Test>::insert(NetUid::ROOT, true);
2996-
NetworksAdded::<Test>::insert(NetUid::ROOT, true);
2997-
2998-
// Add network, register hotkeys, and setup network parameters
2999-
add_network(netuid, subnet_tempo, 0);
3000-
SubnetMechanism::<Test>::insert(netuid, 1); // Set mechanism to 1
3001-
3002-
// Setup large LPs to prevent slippage
3003-
SubnetTAO::<Test>::insert(netuid, TaoCurrency::from(1_000_000_000_000_000));
3004-
SubnetAlphaIn::<Test>::insert(netuid, AlphaCurrency::from(1_000_000_000_000_000));
3005-
3006-
register_ok_neuron(netuid, validator_hotkey, validator_coldkey, 0);
3007-
register_ok_neuron(netuid, validator_miner_hotkey, validator_miner_coldkey, 1);
3008-
register_ok_neuron(netuid, miner_hotkey, miner_coldkey, 2);
3009-
SubtensorModule::add_balance_to_coldkey_account(
3010-
&validator_coldkey,
3011-
stake + ExistentialDeposit::get(),
3012-
);
3013-
SubtensorModule::add_balance_to_coldkey_account(
3014-
&validator_miner_coldkey,
3015-
stake + ExistentialDeposit::get(),
3016-
);
3017-
SubtensorModule::add_balance_to_coldkey_account(
3018-
&miner_coldkey,
3019-
stake + ExistentialDeposit::get(),
3020-
);
3021-
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
3022-
step_block(subnet_tempo);
3023-
SubnetOwnerCut::<Test>::set(u16::MAX / 10);
3024-
// There are two validators and three neurons
3025-
MaxAllowedUids::<Test>::set(netuid, 3);
3026-
SubtensorModule::set_max_allowed_validators(netuid, 2);
3027-
3028-
// Setup stakes:
3029-
// Stake from validator
3030-
// Stake from valiminer
3031-
assert_ok!(SubtensorModule::add_stake(
3032-
RuntimeOrigin::signed(validator_coldkey),
3033-
validator_hotkey,
3034-
netuid,
3035-
stake.into()
3036-
));
3037-
assert_ok!(SubtensorModule::add_stake(
3038-
RuntimeOrigin::signed(validator_miner_coldkey),
3039-
validator_miner_hotkey,
3040-
netuid,
3041-
stake.into()
3042-
));
3043-
3044-
// Setup YUMA so that it creates emissions
3045-
Weights::<Test>::insert(NetUidStorageIndex::from(netuid), 0, vec![(1, 0xFFFF)]);
3046-
Weights::<Test>::insert(NetUidStorageIndex::from(netuid), 1, vec![(2, 0xFFFF)]);
3047-
BlockAtRegistration::<Test>::set(netuid, 0, 1);
3048-
BlockAtRegistration::<Test>::set(netuid, 1, 1);
3049-
BlockAtRegistration::<Test>::set(netuid, 2, 1);
3050-
LastUpdate::<Test>::set(NetUidStorageIndex::from(netuid), vec![2, 2, 2]);
3051-
Kappa::<Test>::set(netuid, u16::MAX / 5);
3052-
ActivityCutoff::<Test>::set(netuid, u16::MAX); // makes all stake active
3053-
ValidatorPermit::<Test>::insert(netuid, vec![true, true, false]);
3054-
3055-
// Run run_coinbase until emissions are drained
3056-
step_block(subnet_tempo);
3057-
3058-
// Add stake to validator so it has root stake
3059-
SubtensorModule::add_balance_to_coldkey_account(&validator_coldkey, root_stake.into());
3060-
// init root
3061-
assert_ok!(SubtensorModule::add_stake(
3062-
RuntimeOrigin::signed(validator_coldkey),
3063-
validator_hotkey,
3064-
NetUid::ROOT,
3065-
root_stake.into()
3066-
));
3067-
// Set tao weight non zero
3068-
SubtensorModule::set_tao_weight(u64::MAX / 10);
3069-
3070-
// Make root sell NOT happen
3071-
// set price very low, e.g. a lot of alpha in
3072-
//SubnetAlphaIn::<Test>::insert(netuid, AlphaCurrency::from(1_000_000_000_000_000));
3073-
pallet_subtensor_swap::AlphaSqrtPrice::<Test>::insert(
3074-
netuid,
3075-
U64F64::saturating_from_num(0.01),
3076-
);
3077-
3078-
// Make sure we ARE NOT root selling, so we do not have root alpha divs.
3079-
let root_sell_flag = SubtensorModule::get_network_root_sell_flag(&[netuid]);
3080-
assert!(!root_sell_flag, "Root sell flag should be false");
3081-
3082-
// Run run_coinbase until emissions are drained
3083-
step_block(subnet_tempo);
3084-
3085-
let old_root_alpha_divs = PendingRootAlphaDivs::<Test>::get(netuid);
3086-
let per_block_emission = SubtensorModule::get_block_emission_for_issuance(
3087-
SubtensorModule::get_alpha_issuance(netuid).into(),
3088-
)
3089-
.unwrap_or(0);
3090-
3091-
// step by one block
3092-
step_block(1);
3093-
// Verify that root alpha divs
3094-
let new_root_alpha_divs = PendingRootAlphaDivs::<Test>::get(netuid);
3095-
// Check that we are indeed NOT root selling, i.e. that root alpha divs are NOT increasing
3096-
assert_eq!(
3097-
new_root_alpha_divs, old_root_alpha_divs,
3098-
"Root alpha divs should not increase"
3099-
);
3100-
// Check root divs are zero
3101-
assert_eq!(
3102-
new_root_alpha_divs,
3103-
AlphaCurrency::ZERO,
3104-
"Root alpha divs should be zero"
3105-
);
3106-
let miner_stake_before_epoch = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
3107-
&miner_hotkey,
3108-
&miner_coldkey,
3109-
netuid,
3110-
);
3111-
// Run again but with some root stake
3112-
step_block(subnet_tempo - 2);
3113-
assert_abs_diff_eq!(
3114-
PendingServerEmission::<Test>::get(netuid).to_u64(),
3115-
U96F32::saturating_from_num(per_block_emission)
3116-
.saturating_mul(U96F32::saturating_from_num(subnet_tempo as u64))
3117-
.saturating_mul(U96F32::saturating_from_num(0.5)) // miner cut
3118-
.saturating_mul(U96F32::saturating_from_num(0.90))
3119-
.saturating_to_num::<u64>(),
3120-
epsilon = 100_000_u64.into()
3121-
);
3122-
step_block(1);
3123-
assert!(
3124-
BlocksSinceLastStep::<Test>::get(netuid) == 0,
3125-
"Blocks since last step should be 0"
3126-
);
3127-
3128-
let miner_uid = Uids::<Test>::get(netuid, miner_hotkey).unwrap_or(0);
3129-
log::info!("Miner uid: {miner_uid:?}");
3130-
let miner_incentive: AlphaCurrency = {
3131-
let miner_incentive = Incentive::<Test>::get(NetUidStorageIndex::from(netuid))
3132-
.get(miner_uid as usize)
3133-
.copied();
3134-
3135-
assert!(miner_incentive.is_some());
3136-
3137-
(miner_incentive.unwrap_or_default() as u64).into()
3138-
};
3139-
log::info!("Miner incentive: {miner_incentive:?}");
3140-
3141-
// Miner emissions
3142-
let miner_emission_1: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
3143-
&miner_hotkey,
3144-
&miner_coldkey,
3145-
netuid,
3146-
)
3147-
.to_u64()
3148-
- miner_stake_before_epoch.to_u64();
3149-
3150-
assert_abs_diff_eq!(
3151-
Incentive::<Test>::get(NetUidStorageIndex::from(netuid))
3152-
.iter()
3153-
.sum::<u16>(),
3154-
u16::MAX,
3155-
epsilon = 10
3156-
);
3157-
3158-
assert_abs_diff_eq!(
3159-
miner_emission_1,
3160-
U96F32::saturating_from_num(miner_incentive)
3161-
.saturating_div(u16::MAX.into())
3162-
.saturating_mul(U96F32::saturating_from_num(per_block_emission))
3163-
.saturating_mul(U96F32::saturating_from_num(subnet_tempo + 1))
3164-
.saturating_mul(U96F32::saturating_from_num(0.45)) // miner cut
3165-
.saturating_to_num::<u64>(),
3166-
epsilon = 1_000_000_u64
3167-
);
3168-
});
3169-
}
3170-
31712904
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_mining_emission_distribution_with_root_sell --exact --show-output --nocapture
31722905
#[test]
31732906
fn test_mining_emission_distribution_with_root_sell() {

0 commit comments

Comments
 (0)