Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ impl<T: Config> Pallet<T> {
);
if let Ok(buy_swap_result_ok) = buy_swap_result {
let bought_alpha: AlphaCurrency = buy_swap_result_ok.amount_paid_out.into();
Self::recycle_subnet_alpha(*netuid_i, bought_alpha);
// Self::recycle_subnet_alpha(*netuid_i, bought_alpha);
PendingRootAlphaDivs::<T>::mutate(*netuid_i, |total| {
*total = total.saturating_add(bought_alpha);
});
}
}

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

let alpha_injection_cap: U96F32 = alpha_emission_i.min(tao_block_emission);
if alpha_in_i > alpha_injection_cap {
alpha_in_i = alpha_injection_cap;
if alpha_in_i > Self::root_proportion(netuid_i) {
alpha_in_i = Self::root_proportion(netuid_i) ;
tao_in_i = alpha_in_i.saturating_mul(price_i);
}

Expand Down
267 changes: 0 additions & 267 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,81 +588,6 @@ fn test_coinbase_alpha_issuance_with_cap_trigger() {
});
}

// 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
#[test]
fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() {
new_test_ext(1).execute_with(|| {
let netuid1 = NetUid::from(1);
let netuid2 = NetUid::from(2);
let emission: u64 = 1_000_000;
add_network(netuid1, 1, 0);
add_network(netuid2, 1, 0);

// Make subnets dynamic.
SubnetMechanism::<Test>::insert(netuid1, 1);
SubnetMechanism::<Test>::insert(netuid2, 1);

// Setup prices 0.000001
let initial_tao: u64 = 10_000_u64;
let initial_alpha: u64 = initial_tao * 100_000_u64;
mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha.into());
mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha.into());

// Enable emission
FirstEmissionBlockNumber::<Test>::insert(netuid1, 0);
FirstEmissionBlockNumber::<Test>::insert(netuid2, 0);
// Set subnet TAO flows to non-zero and 1:2 ratio
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
SubnetTaoFlow::<Test>::insert(netuid2, 200_000_000_i64);

// Force the swap to initialize
SubtensorModule::swap_tao_for_alpha(
netuid1,
TaoCurrency::ZERO,
1_000_000_000_000.into(),
false,
)
.unwrap();
SubtensorModule::swap_tao_for_alpha(
netuid2,
TaoCurrency::ZERO,
1_000_000_000_000.into(),
false,
)
.unwrap();

// Get the prices before the run_coinbase
let price_1_before = <Test as pallet::Config>::SwapInterface::current_alpha_price(netuid1);
let price_2_before = <Test as pallet::Config>::SwapInterface::current_alpha_price(netuid2);

// Set issuance at 21M
SubnetAlphaOut::<Test>::insert(netuid1, AlphaCurrency::from(21_000_000_000_000_000)); // Set issuance above 21M
SubnetAlphaOut::<Test>::insert(netuid2, AlphaCurrency::from(21_000_000_000_000_000)); // Set issuance above 21M

// Run coinbase
SubtensorModule::run_coinbase(U96F32::from_num(emission));

// Get the prices after the run_coinbase
let price_1_after = <Test as pallet::Config>::SwapInterface::current_alpha_price(netuid1);
let price_2_after = <Test as pallet::Config>::SwapInterface::current_alpha_price(netuid2);

// AlphaIn gets decreased beacuse of a buy
assert!(u64::from(SubnetAlphaIn::<Test>::get(netuid1)) < initial_alpha);
assert_eq!(
u64::from(SubnetAlphaOut::<Test>::get(netuid2)),
21_000_000_000_000_000_u64
);
assert!(u64::from(SubnetAlphaIn::<Test>::get(netuid2)) < initial_alpha);
assert_eq!(
u64::from(SubnetAlphaOut::<Test>::get(netuid2)),
21_000_000_000_000_000_u64
);

assert!(price_1_after > price_1_before);
assert!(price_2_after > price_2_before);
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_owner_cut_base --exact --show-output --nocapture
#[test]
fn test_owner_cut_base() {
Expand Down Expand Up @@ -2976,198 +2901,6 @@ fn test_zero_shares_zero_emission() {
});
}

#[test]
fn test_mining_emission_distribution_with_no_root_sell() {
new_test_ext(1).execute_with(|| {
let validator_coldkey = U256::from(1);
let validator_hotkey = U256::from(2);
let validator_miner_coldkey = U256::from(3);
let validator_miner_hotkey = U256::from(4);
let miner_coldkey = U256::from(5);
let miner_hotkey = U256::from(6);
let netuid = NetUid::from(1);
let subnet_tempo = 10;
let stake: u64 = 100_000_000_000;
let root_stake: u64 = 200_000_000_000; // 200 TAO

// Create root network
SubtensorModule::set_tao_weight(0); // Start tao weight at 0
SubtokenEnabled::<Test>::insert(NetUid::ROOT, true);
NetworksAdded::<Test>::insert(NetUid::ROOT, true);

// Add network, register hotkeys, and setup network parameters
add_network(netuid, subnet_tempo, 0);
SubnetMechanism::<Test>::insert(netuid, 1); // Set mechanism to 1

// Setup large LPs to prevent slippage
SubnetTAO::<Test>::insert(netuid, TaoCurrency::from(1_000_000_000_000_000));
SubnetAlphaIn::<Test>::insert(netuid, AlphaCurrency::from(1_000_000_000_000_000));

register_ok_neuron(netuid, validator_hotkey, validator_coldkey, 0);
register_ok_neuron(netuid, validator_miner_hotkey, validator_miner_coldkey, 1);
register_ok_neuron(netuid, miner_hotkey, miner_coldkey, 2);
SubtensorModule::add_balance_to_coldkey_account(
&validator_coldkey,
stake + ExistentialDeposit::get(),
);
SubtensorModule::add_balance_to_coldkey_account(
&validator_miner_coldkey,
stake + ExistentialDeposit::get(),
);
SubtensorModule::add_balance_to_coldkey_account(
&miner_coldkey,
stake + ExistentialDeposit::get(),
);
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
step_block(subnet_tempo);
SubnetOwnerCut::<Test>::set(u16::MAX / 10);
// There are two validators and three neurons
MaxAllowedUids::<Test>::set(netuid, 3);
SubtensorModule::set_max_allowed_validators(netuid, 2);

// Setup stakes:
// Stake from validator
// Stake from valiminer
assert_ok!(SubtensorModule::add_stake(
RuntimeOrigin::signed(validator_coldkey),
validator_hotkey,
netuid,
stake.into()
));
assert_ok!(SubtensorModule::add_stake(
RuntimeOrigin::signed(validator_miner_coldkey),
validator_miner_hotkey,
netuid,
stake.into()
));

// Setup YUMA so that it creates emissions
Weights::<Test>::insert(NetUidStorageIndex::from(netuid), 0, vec![(1, 0xFFFF)]);
Weights::<Test>::insert(NetUidStorageIndex::from(netuid), 1, vec![(2, 0xFFFF)]);
BlockAtRegistration::<Test>::set(netuid, 0, 1);
BlockAtRegistration::<Test>::set(netuid, 1, 1);
BlockAtRegistration::<Test>::set(netuid, 2, 1);
LastUpdate::<Test>::set(NetUidStorageIndex::from(netuid), vec![2, 2, 2]);
Kappa::<Test>::set(netuid, u16::MAX / 5);
ActivityCutoff::<Test>::set(netuid, u16::MAX); // makes all stake active
ValidatorPermit::<Test>::insert(netuid, vec![true, true, false]);

// Run run_coinbase until emissions are drained
step_block(subnet_tempo);

// Add stake to validator so it has root stake
SubtensorModule::add_balance_to_coldkey_account(&validator_coldkey, root_stake.into());
// init root
assert_ok!(SubtensorModule::add_stake(
RuntimeOrigin::signed(validator_coldkey),
validator_hotkey,
NetUid::ROOT,
root_stake.into()
));
// Set tao weight non zero
SubtensorModule::set_tao_weight(u64::MAX / 10);

// Make root sell NOT happen
// set price very low, e.g. a lot of alpha in
//SubnetAlphaIn::<Test>::insert(netuid, AlphaCurrency::from(1_000_000_000_000_000));
pallet_subtensor_swap::AlphaSqrtPrice::<Test>::insert(
netuid,
U64F64::saturating_from_num(0.01),
);

// Make sure we ARE NOT root selling, so we do not have root alpha divs.
let root_sell_flag = SubtensorModule::get_network_root_sell_flag(&[netuid]);
assert!(!root_sell_flag, "Root sell flag should be false");

// Run run_coinbase until emissions are drained
step_block(subnet_tempo);

let old_root_alpha_divs = PendingRootAlphaDivs::<Test>::get(netuid);
let per_block_emission = SubtensorModule::get_block_emission_for_issuance(
SubtensorModule::get_alpha_issuance(netuid).into(),
)
.unwrap_or(0);

// step by one block
step_block(1);
// Verify that root alpha divs
let new_root_alpha_divs = PendingRootAlphaDivs::<Test>::get(netuid);
// Check that we are indeed NOT root selling, i.e. that root alpha divs are NOT increasing
assert_eq!(
new_root_alpha_divs, old_root_alpha_divs,
"Root alpha divs should not increase"
);
// Check root divs are zero
assert_eq!(
new_root_alpha_divs,
AlphaCurrency::ZERO,
"Root alpha divs should be zero"
);
let miner_stake_before_epoch = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&miner_hotkey,
&miner_coldkey,
netuid,
);
// Run again but with some root stake
step_block(subnet_tempo - 2);
assert_abs_diff_eq!(
PendingServerEmission::<Test>::get(netuid).to_u64(),
U96F32::saturating_from_num(per_block_emission)
.saturating_mul(U96F32::saturating_from_num(subnet_tempo as u64))
.saturating_mul(U96F32::saturating_from_num(0.5)) // miner cut
.saturating_mul(U96F32::saturating_from_num(0.90))
.saturating_to_num::<u64>(),
epsilon = 100_000_u64.into()
);
step_block(1);
assert!(
BlocksSinceLastStep::<Test>::get(netuid) == 0,
"Blocks since last step should be 0"
);

let miner_uid = Uids::<Test>::get(netuid, miner_hotkey).unwrap_or(0);
log::info!("Miner uid: {miner_uid:?}");
let miner_incentive: AlphaCurrency = {
let miner_incentive = Incentive::<Test>::get(NetUidStorageIndex::from(netuid))
.get(miner_uid as usize)
.copied();

assert!(miner_incentive.is_some());

(miner_incentive.unwrap_or_default() as u64).into()
};
log::info!("Miner incentive: {miner_incentive:?}");

// Miner emissions
let miner_emission_1: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&miner_hotkey,
&miner_coldkey,
netuid,
)
.to_u64()
- miner_stake_before_epoch.to_u64();

assert_abs_diff_eq!(
Incentive::<Test>::get(NetUidStorageIndex::from(netuid))
.iter()
.sum::<u16>(),
u16::MAX,
epsilon = 10
);

assert_abs_diff_eq!(
miner_emission_1,
U96F32::saturating_from_num(miner_incentive)
.saturating_div(u16::MAX.into())
.saturating_mul(U96F32::saturating_from_num(per_block_emission))
.saturating_mul(U96F32::saturating_from_num(subnet_tempo + 1))
.saturating_mul(U96F32::saturating_from_num(0.45)) // miner cut
.saturating_to_num::<u64>(),
epsilon = 1_000_000_u64
);
});
}

// 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
#[test]
fn test_mining_emission_distribution_with_root_sell() {
Expand Down
Loading