Skip to content

Commit 7f67188

Browse files
committed
test total issuance after coinbase
1 parent a4046a0 commit 7f67188

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use super::mock::*;
33

44
use crate::*;
5+
use approx::assert_abs_diff_eq;
56
use frame_support::assert_ok;
67
use sp_core::U256;
78
use substrate_fixed::types::I64F64;
@@ -72,3 +73,94 @@ fn test_dynamic_function_price_equal_emission() {
7273
close(alpha_out, 2 * alpha_block_emission - expected_alpha_in, 10);
7374
});
7475
}
76+
77+
// Verifies that the total issuance after the coinbase is only increased by the coinbase emission.
78+
// Includes TAO weight.
79+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_total_issuance_after_coinbase --exact --show-output --nocapture
80+
#[test]
81+
fn test_total_issuance_after_coinbase() {
82+
new_test_ext(1).execute_with(|| {
83+
let netuid: u16 = 1;
84+
add_network(netuid, 1, 0);
85+
// Set TAO weight to 18%
86+
SubtensorModule::set_tao_weight(I96F32::from_num(0.18).saturating_to_num::<u64>());
87+
// Set owner cut to ~11.11%
88+
SubtensorModule::set_subnet_owner_cut(u16::MAX / 9);
89+
let total_coinbase_emission: I96F32 = I96F32::from_num(1_123_456_789);
90+
let epsilon: u64 = 100;
91+
92+
// Define hotkeys and coldkeys
93+
let hotkey_a: U256 = U256::from(1);
94+
let hotkey_b: U256 = U256::from(2);
95+
let hotkey_c: U256 = U256::from(3);
96+
let coldkey_a: U256 = U256::from(100);
97+
let coldkey_b: U256 = U256::from(101);
98+
let coldkey_c: U256 = U256::from(102);
99+
100+
// Register neurons with decreasing stakes
101+
register_ok_neuron(netuid, hotkey_a, coldkey_a, 0);
102+
register_ok_neuron(netuid, hotkey_b, coldkey_b, 0);
103+
register_ok_neuron(netuid, hotkey_c, coldkey_c, 0);
104+
105+
// Add initial stakes
106+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_a, 1_000);
107+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_b, 1_000);
108+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_c, 1_000);
109+
110+
// Swap to alpha
111+
let total_tao: I96F32 = I96F32::from_num(300_000 + 100_000 + 50_000);
112+
let total_alpha: I96F32 = I96F32::from_num(SubtensorModule::swap_tao_for_alpha(
113+
netuid,
114+
total_tao.saturating_to_num::<u64>(),
115+
));
116+
117+
// Set the stakes directly
118+
// This avoids needing to swap tao to alpha, impacting the initial stake distribution.
119+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
120+
&hotkey_a,
121+
&coldkey_a,
122+
netuid,
123+
(total_alpha * I96F32::from_num(300_000) / total_tao).saturating_to_num::<u64>(),
124+
);
125+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
126+
&hotkey_b,
127+
&coldkey_b,
128+
netuid,
129+
(total_alpha * I96F32::from_num(100_000) / total_tao).saturating_to_num::<u64>(),
130+
);
131+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
132+
&hotkey_c,
133+
&coldkey_c,
134+
netuid,
135+
(total_alpha * I96F32::from_num(50_000) / total_tao).saturating_to_num::<u64>(),
136+
);
137+
138+
// Stake some to root
139+
let stake_to_root: u64 = 10_000_000;
140+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_a, stake_to_root);
141+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
142+
&hotkey_a,
143+
&coldkey_a,
144+
netuid,
145+
stake_to_root,
146+
);
147+
148+
let alpha_price = SubtensorModule::get_alpha_price(netuid);
149+
log::info!("alpha_price: {:?}", alpha_price);
150+
151+
// Get the total issuance
152+
let mut total_issuance_before = TotalIssuance::<Test>::get();
153+
log::info!("total_issuance_before: {:?}", total_issuance_before);
154+
155+
// Run the coinbase
156+
SubtensorModule::run_coinbase(total_coinbase_emission);
157+
158+
// Compare
159+
let total_issuance_after = TotalIssuance::<Test>::get();
160+
assert_abs_diff_eq!(
161+
total_issuance_after,
162+
total_issuance_before + total_coinbase_emission.saturating_to_num::<u64>(),
163+
epsilon = epsilon
164+
);
165+
});
166+
}

0 commit comments

Comments
 (0)