Skip to content

Commit 60c4a5c

Browse files
committed
Revert back to ema-prices
1 parent e846eba commit 60c4a5c

File tree

4 files changed

+461
-429
lines changed

4 files changed

+461
-429
lines changed

pallets/subtensor/src/coinbase/subnet_emissions.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl<T: Config> Pallet<T> {
5151

5252
// Update SubnetEmaTaoFlow if needed and return its value for
5353
// the current block
54+
#[allow(dead_code)]
5455
fn get_ema_flow(netuid: NetUid) -> I64F64 {
5556
let current_block: u64 = Self::get_current_block_as_u64();
5657

@@ -87,6 +88,7 @@ impl<T: Config> Pallet<T> {
8788
// Either the minimal EMA flow L = min{Si}, or an artificial
8889
// cut off at some higher value A (TaoFlowCutoff)
8990
// L = max {A, min{min{S[i], 0}}}
91+
#[allow(dead_code)]
9092
fn get_lower_limit(ema_flows: &BTreeMap<NetUid, I64F64>) -> I64F64 {
9193
let zero = I64F64::saturating_from_num(0);
9294
let min_flow = ema_flows
@@ -178,6 +180,7 @@ impl<T: Config> Pallet<T> {
178180
}
179181

180182
// Implementation of shares that uses TAO flow
183+
#[allow(dead_code)]
181184
fn get_shares_flow(subnets_to_emit_to: &[NetUid]) -> BTreeMap<NetUid, U64F64> {
182185
// Get raw flows
183186
let ema_flows = subnets_to_emit_to
@@ -210,8 +213,37 @@ impl<T: Config> Pallet<T> {
210213
offset_flows
211214
}
212215

216+
// DEPRECATED: Implementation of shares that uses EMA prices will be gradually deprecated
217+
fn get_shares_price_ema(subnets_to_emit_to: &[NetUid]) -> BTreeMap<NetUid, U64F64> {
218+
// Get sum of alpha moving prices
219+
let total_moving_prices = subnets_to_emit_to
220+
.iter()
221+
.map(|netuid| U64F64::saturating_from_num(Self::get_moving_alpha_price(*netuid)))
222+
.fold(U64F64::saturating_from_num(0.0), |acc, ema| {
223+
acc.saturating_add(ema)
224+
});
225+
log::debug!("total_moving_prices: {total_moving_prices:?}");
226+
227+
// Calculate shares.
228+
subnets_to_emit_to
229+
.iter()
230+
.map(|netuid| {
231+
let moving_price =
232+
U64F64::saturating_from_num(Self::get_moving_alpha_price(*netuid));
233+
log::debug!("moving_price_i: {moving_price:?}");
234+
235+
let share = moving_price
236+
.checked_div(total_moving_prices)
237+
.unwrap_or(U64F64::saturating_from_num(0));
238+
239+
(*netuid, share)
240+
})
241+
.collect::<BTreeMap<NetUid, U64F64>>()
242+
}
243+
213244
// Combines ema price method and tao flow method linearly over FlowHalfLife blocks
214245
pub(crate) fn get_shares(subnets_to_emit_to: &[NetUid]) -> BTreeMap<NetUid, U64F64> {
215-
Self::get_shares_flow(subnets_to_emit_to)
246+
// Self::get_shares_flow(subnets_to_emit_to)
247+
Self::get_shares_price_ema(subnets_to_emit_to)
216248
}
217249
}

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 100 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,28 @@ fn test_coinbase_tao_issuance_base_low() {
115115
}
116116

117117
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_tao_issuance_base_low_flow --exact --show-output --nocapture
118-
#[test]
119-
fn test_coinbase_tao_issuance_base_low_flow() {
120-
new_test_ext(1).execute_with(|| {
121-
let emission = TaoCurrency::from(1_234_567);
122-
let subnet_owner_ck = U256::from(1001);
123-
let subnet_owner_hk = U256::from(1002);
124-
let netuid = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
125-
let emission = TaoCurrency::from(1);
126-
127-
// 100% tao flow method
128-
let block_num = FlowHalfLife::<Test>::get();
129-
SubnetEmaTaoFlow::<Test>::insert(netuid, (block_num, I64F64::from_num(1_000_000_000)));
130-
System::set_block_number(block_num);
131-
132-
let tao_in_before = SubnetTAO::<Test>::get(netuid);
133-
let total_stake_before = TotalStake::<Test>::get();
134-
SubtensorModule::run_coinbase(U96F32::from_num(emission));
135-
assert_eq!(SubnetTAO::<Test>::get(netuid), tao_in_before + emission);
136-
assert_eq!(TotalIssuance::<Test>::get(), emission);
137-
assert_eq!(TotalStake::<Test>::get(), total_stake_before + emission);
138-
});
139-
}
118+
// #[test]
119+
// fn test_coinbase_tao_issuance_base_low_flow() {
120+
// new_test_ext(1).execute_with(|| {
121+
// let emission = TaoCurrency::from(1_234_567);
122+
// let subnet_owner_ck = U256::from(1001);
123+
// let subnet_owner_hk = U256::from(1002);
124+
// let netuid = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
125+
// let emission = TaoCurrency::from(1);
126+
127+
// // 100% tao flow method
128+
// let block_num = FlowHalfLife::<Test>::get();
129+
// SubnetEmaTaoFlow::<Test>::insert(netuid, (block_num, I64F64::from_num(1_000_000_000)));
130+
// System::set_block_number(block_num);
131+
132+
// let tao_in_before = SubnetTAO::<Test>::get(netuid);
133+
// let total_stake_before = TotalStake::<Test>::get();
134+
// SubtensorModule::run_coinbase(U96F32::from_num(emission));
135+
// assert_eq!(SubnetTAO::<Test>::get(netuid), tao_in_before + emission);
136+
// assert_eq!(TotalIssuance::<Test>::get(), emission);
137+
// assert_eq!(TotalStake::<Test>::get(), total_stake_before + emission);
138+
// });
139+
// }
140140

141141
// Test emission distribution across multiple subnets.
142142
// This test verifies that:
@@ -260,85 +260,85 @@ fn test_coinbase_tao_issuance_different_prices() {
260260
}
261261

262262
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_tao_issuance_different_flows --exact --show-output --nocapture
263-
#[test]
264-
fn test_coinbase_tao_issuance_different_flows() {
265-
new_test_ext(1).execute_with(|| {
266-
let subnet_owner_ck = U256::from(1001);
267-
let subnet_owner_hk = U256::from(1002);
268-
let netuid1 = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
269-
let netuid2 = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
270-
let emission = 100_000_000;
271-
272-
// Setup prices 0.1 and 0.2
273-
let initial_tao: u64 = 100_000_u64;
274-
let initial_alpha1: u64 = initial_tao * 10;
275-
let initial_alpha2: u64 = initial_tao * 5;
276-
mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha1.into());
277-
mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha2.into());
278-
279-
// Force the swap to initialize
280-
SubtensorModule::swap_tao_for_alpha(
281-
netuid1,
282-
TaoCurrency::ZERO,
283-
1_000_000_000_000.into(),
284-
false,
285-
)
286-
.unwrap();
287-
SubtensorModule::swap_tao_for_alpha(
288-
netuid2,
289-
TaoCurrency::ZERO,
290-
1_000_000_000_000.into(),
291-
false,
292-
)
293-
.unwrap();
294-
295-
// Set subnet prices to reversed proportion to ensure they don't affect emissions.
296-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(2));
297-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(1));
298-
299-
// Set subnet tao flow ema.
300-
let block_num = FlowHalfLife::<Test>::get();
301-
SubnetEmaTaoFlow::<Test>::insert(netuid1, (block_num, I64F64::from_num(1)));
302-
SubnetEmaTaoFlow::<Test>::insert(netuid2, (block_num, I64F64::from_num(2)));
303-
System::set_block_number(block_num);
304-
305-
// Set normalization exponent to 1 for simplicity
306-
FlowNormExponent::<Test>::set(U64F64::from(1_u64));
307-
308-
// Assert initial TAO reserves.
309-
assert_eq!(SubnetTAO::<Test>::get(netuid1), initial_tao.into());
310-
assert_eq!(SubnetTAO::<Test>::get(netuid2), initial_tao.into());
311-
let total_stake_before = TotalStake::<Test>::get();
312-
313-
// Run the coinbase with the emission amount.
314-
SubtensorModule::run_coinbase(U96F32::from_num(emission));
315-
316-
// Assert tao emission is split evenly.
317-
assert_abs_diff_eq!(
318-
SubnetTAO::<Test>::get(netuid1),
319-
TaoCurrency::from(initial_tao + emission / 3),
320-
epsilon = 10.into(),
321-
);
322-
assert_abs_diff_eq!(
323-
SubnetTAO::<Test>::get(netuid2),
324-
TaoCurrency::from(initial_tao + 2 * emission / 3),
325-
epsilon = 10.into(),
326-
);
263+
// #[test]
264+
// fn test_coinbase_tao_issuance_different_flows() {
265+
// new_test_ext(1).execute_with(|| {
266+
// let subnet_owner_ck = U256::from(1001);
267+
// let subnet_owner_hk = U256::from(1002);
268+
// let netuid1 = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
269+
// let netuid2 = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
270+
// let emission = 100_000_000;
271+
272+
// // Setup prices 0.1 and 0.2
273+
// let initial_tao: u64 = 100_000_u64;
274+
// let initial_alpha1: u64 = initial_tao * 10;
275+
// let initial_alpha2: u64 = initial_tao * 5;
276+
// mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha1.into());
277+
// mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha2.into());
278+
279+
// // Force the swap to initialize
280+
// SubtensorModule::swap_tao_for_alpha(
281+
// netuid1,
282+
// TaoCurrency::ZERO,
283+
// 1_000_000_000_000.into(),
284+
// false,
285+
// )
286+
// .unwrap();
287+
// SubtensorModule::swap_tao_for_alpha(
288+
// netuid2,
289+
// TaoCurrency::ZERO,
290+
// 1_000_000_000_000.into(),
291+
// false,
292+
// )
293+
// .unwrap();
294+
295+
// // Set subnet prices to reversed proportion to ensure they don't affect emissions.
296+
// SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(2));
297+
// SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(1));
298+
299+
// // Set subnet tao flow ema.
300+
// let block_num = FlowHalfLife::<Test>::get();
301+
// SubnetEmaTaoFlow::<Test>::insert(netuid1, (block_num, I64F64::from_num(1)));
302+
// SubnetEmaTaoFlow::<Test>::insert(netuid2, (block_num, I64F64::from_num(2)));
303+
// System::set_block_number(block_num);
304+
305+
// // Set normalization exponent to 1 for simplicity
306+
// FlowNormExponent::<Test>::set(U64F64::from(1_u64));
307+
308+
// // Assert initial TAO reserves.
309+
// assert_eq!(SubnetTAO::<Test>::get(netuid1), initial_tao.into());
310+
// assert_eq!(SubnetTAO::<Test>::get(netuid2), initial_tao.into());
311+
// let total_stake_before = TotalStake::<Test>::get();
312+
313+
// // Run the coinbase with the emission amount.
314+
// SubtensorModule::run_coinbase(U96F32::from_num(emission));
315+
316+
// // Assert tao emission is split evenly.
317+
// assert_abs_diff_eq!(
318+
// SubnetTAO::<Test>::get(netuid1),
319+
// TaoCurrency::from(initial_tao + emission / 3),
320+
// epsilon = 10.into(),
321+
// );
322+
// assert_abs_diff_eq!(
323+
// SubnetTAO::<Test>::get(netuid2),
324+
// TaoCurrency::from(initial_tao + 2 * emission / 3),
325+
// epsilon = 10.into(),
326+
// );
327327

328-
// Prices are low => we limit tao issued (buy alpha with it)
329-
let tao_issued = TaoCurrency::from(((0.1 + 0.2) * emission as f64) as u64);
330-
assert_abs_diff_eq!(
331-
TotalIssuance::<Test>::get(),
332-
tao_issued,
333-
epsilon = 10.into()
334-
);
335-
assert_abs_diff_eq!(
336-
TotalStake::<Test>::get(),
337-
total_stake_before + emission.into(),
338-
epsilon = 10.into()
339-
);
340-
});
341-
}
328+
// // Prices are low => we limit tao issued (buy alpha with it)
329+
// let tao_issued = TaoCurrency::from(((0.1 + 0.2) * emission as f64) as u64);
330+
// assert_abs_diff_eq!(
331+
// TotalIssuance::<Test>::get(),
332+
// tao_issued,
333+
// epsilon = 10.into()
334+
// );
335+
// assert_abs_diff_eq!(
336+
// TotalStake::<Test>::get(),
337+
// total_stake_before + emission.into(),
338+
// epsilon = 10.into()
339+
// );
340+
// });
341+
// }
342342

343343
// Test moving price updates with different alpha values.
344344
// This test verifies that:

0 commit comments

Comments
 (0)