-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathcoinbase.rs
1441 lines (1361 loc) · 59.7 KB
/
coinbase.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![allow(unused, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)]
use super::mock::*;
use crate::*;
use approx::assert_abs_diff_eq;
use frame_support::assert_ok;
use sp_core::U256;
use substrate_fixed::types::I64F64;
use substrate_fixed::types::I96F32;
#[allow(clippy::arithmetic_side_effects)]
fn close(value: u64, target: u64, eps: u64) {
assert!(
(value as i64 - target as i64).abs() < eps as i64,
"Assertion failed: value = {}, target = {}, eps = {}",
value,
target,
eps
)
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_hotkey_take --exact --show-output --nocapture
#[test]
fn test_hotkey_take() {
new_test_ext(1).execute_with(|| {
let hotkey = U256::from(1);
Delegates::<Test>::insert(hotkey, u16::MAX / 2);
log::info!(
"expected: {:?}",
SubtensorModule::get_hotkey_take_float(&hotkey)
);
log::info!(
"expected: {:?}",
SubtensorModule::get_hotkey_take_float(&hotkey)
);
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_dynamic_function_various_values --exact --show-output --nocapture
#[test]
fn test_dynamic_function_various_values() {
new_test_ext(1).execute_with(|| {
let price_values: [f64; 9] = [0.001, 0.1, 0.5, 1.0, 2.0, 10.0, 100.0, 200.0, 1000.0];
let tao_in_values: [u64; 9] = [0, 1, 10, 100, 1_000, 1_000_000, 1_000_000_000, 1_000_000_000_000, 1_000_000_000_000_000 ];
let alpha_emission_values: [u64; 9] = [0, 1, 10, 100, 1_000, 1_000_000, 1_000_000_000, 1_000_000_000_000, 1_000_000_000_000_000 ];
for &price in price_values.iter() {
for &tao_in in tao_in_values.iter() {
for &alpha_emission in alpha_emission_values.iter() {
// Set the price.
SubnetMechanism::<Test>::insert(1, 1);
SubnetTAO::<Test>::insert(1, (price * 1_000_000_000.0) as u64);
SubnetAlphaIn::<Test>::insert(1, 1_000_000_000);
let (tao_in_emission, alpha_in_emission, alpha_out_emission) = SubtensorModule::get_dynamic_tao_emission( 1, tao_in, alpha_emission);
assert!(tao_in_emission <= tao_in, "tao_in_emission is greater than tao_in");
assert!(alpha_in_emission <= alpha_emission, "alpha_in_emission is greater than alpha_emission");
assert!(alpha_out_emission <= 2 * alpha_emission, "alpha_out_emission is greater than 2 * alpha_emission");
assert!((alpha_in_emission + alpha_out_emission) <= 2 * alpha_emission, "Sum of alpha_in_emission and alpha_out_emission is less than or equal to. 2 * alpha_emission");
close( alpha_in_emission + alpha_out_emission, alpha_in_emission + alpha_emission, 10 );
// if alpha_in_emission > 0 || tao_in_emission > 0 {
// assert!((tao_in_emission as f64 / alpha_in_emission as f64 - price).abs() < 1e-1, "Ratio of tao_in_emission to alpha_in_emission is not equal to price");
// }
}
}
}
});
}
// Test the base case of running coinbase with zero emission.
// This test verifies that the coinbase mechanism can handle the edge case
// of zero emission without errors or unexpected behavior.
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_basecase --exact --show-output --nocapture
#[test]
fn test_coinbase_basecase() {
new_test_ext(1).execute_with(|| {
SubtensorModule::run_coinbase(I96F32::from_num(0.0));
});
}
// Test the emission distribution for a single subnet.
// This test verifies that:
// - A single subnet receives the full emission amount
// - The emission is correctly reflected in SubnetTAO
// - Total issuance and total stake are updated appropriately
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_tao_issuance_base --exact --show-output --nocapture
#[test]
fn test_coinbase_tao_issuance_base() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let emission: u64 = 1_234_567;
add_network(netuid, 1, 0);
assert_eq!(SubnetTAO::<Test>::get(netuid), 0);
SubtensorModule::run_coinbase(I96F32::from_num(emission));
assert_eq!(SubnetTAO::<Test>::get(netuid), emission);
assert_eq!(TotalIssuance::<Test>::get(), emission);
assert_eq!(TotalStake::<Test>::get(), emission);
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_tao_issuance_base_low --exact --show-output --nocapture
#[test]
fn test_coinbase_tao_issuance_base_low() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let emission: u64 = 1;
add_network(netuid, 1, 0);
assert_eq!(SubnetTAO::<Test>::get(netuid), 0);
SubtensorModule::run_coinbase(I96F32::from_num(emission));
assert_eq!(SubnetTAO::<Test>::get(netuid), emission);
assert_eq!(TotalIssuance::<Test>::get(), emission);
assert_eq!(TotalStake::<Test>::get(), emission);
});
}
// Test emission distribution across multiple subnets.
// This test verifies that:
// - Multiple subnets receive equal portions of the total emission
// - Each subnet's TAO balance is updated correctly
// - Total issuance and total stake reflect the full emission amount
// - The emission is split evenly between all subnets
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_tao_issuance_multiple --exact --show-output --nocapture
#[test]
fn test_coinbase_tao_issuance_multiple() {
new_test_ext(1).execute_with(|| {
let netuid1: u16 = 1;
let netuid2: u16 = 2;
let netuid3: u16 = 3;
let emission: u64 = 3_333_333;
add_network(netuid1, 1, 0);
add_network(netuid2, 1, 0);
add_network(netuid3, 1, 0);
assert_eq!(SubnetTAO::<Test>::get(netuid1), 0);
assert_eq!(SubnetTAO::<Test>::get(netuid2), 0);
assert_eq!(SubnetTAO::<Test>::get(netuid3), 0);
SubtensorModule::run_coinbase(I96F32::from_num(emission));
assert_eq!(SubnetTAO::<Test>::get(netuid1), emission / 3);
assert_eq!(SubnetTAO::<Test>::get(netuid2), emission / 3);
assert_eq!(SubnetTAO::<Test>::get(netuid3), emission / 3);
assert_eq!(TotalIssuance::<Test>::get(), emission);
assert_eq!(TotalStake::<Test>::get(), emission);
});
}
// Test emission distribution with different subnet prices.
// This test verifies that:
// - Subnets with different prices receive proportional emission shares
// - A subnet with double the price receives double the emission
// - Total issuance and total stake reflect the full emission amount
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_tao_issuance_different_prices --exact --show-output --nocapture
#[test]
fn test_coinbase_tao_issuance_different_prices() {
new_test_ext(1).execute_with(|| {
let netuid1: u16 = 1;
let netuid2: u16 = 2;
let emission: u64 = 100_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);
// Set subnet prices.
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
// Assert initial TAO reserves.
assert_eq!(SubnetTAO::<Test>::get(netuid1), 0);
assert_eq!(SubnetTAO::<Test>::get(netuid2), 0);
// Run the coinbase with the emission amount.
SubtensorModule::run_coinbase(I96F32::from_num(emission));
// Assert tao emission is split evenly.
assert_eq!(SubnetTAO::<Test>::get(netuid1), emission / 3);
assert_eq!(SubnetTAO::<Test>::get(netuid2), emission / 3 + emission / 3);
close(TotalIssuance::<Test>::get(), emission, 2);
close(TotalStake::<Test>::get(), emission, 2);
});
}
// Test moving price updates with different alpha values.
// This test verifies that:
// - Moving price stays constant when alpha is 1.0
// - Moving price converges to real price at expected rate with alpha 0.1
// - Moving price updates correctly over multiple iterations
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_moving_prices --exact --show-output --nocapture
#[test]
fn test_coinbase_moving_prices() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
// Set price to 1.0
SubnetTAO::<Test>::insert(netuid, 1_000_000);
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
SubnetMechanism::<Test>::insert(netuid, 1);
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(1));
NetworkRegisteredAt::<Test>::insert(netuid, 1);
// Updating the moving price keeps it the same.
assert_eq!(
SubtensorModule::get_moving_alpha_price(netuid),
I96F32::from_num(1)
);
// Skip some blocks so that EMA price is not slowed down
System::set_block_number(7_200_000);
SubtensorModule::update_moving_price(netuid);
assert_eq!(
SubtensorModule::get_moving_alpha_price(netuid),
I96F32::from_num(1)
);
// Check alpha of 1.
// Set price to zero.
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
SubnetMovingAlpha::<Test>::set(I96F32::from_num(1.0));
// Run moving 1 times.
SubtensorModule::update_moving_price(netuid);
// Assert price is ~ 100% of the real price.
assert!(I96F32::from_num(1.0) - SubtensorModule::get_moving_alpha_price(netuid) < 0.05);
// Set price to zero.
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.1));
// EMA price 14 days after registration
System::set_block_number(7_200 * 14);
// Run moving 14 times.
for _ in 0..14 {
SubtensorModule::update_moving_price(netuid);
}
// Assert price is > 50% of the real price.
assert!(
(I96F32::from_num(0.512325) - SubtensorModule::get_moving_alpha_price(netuid)).abs()
< 0.001
);
});
}
// Test moving price updates slow down at the beginning.
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_update_moving_price_initial --exact --show-output --nocapture
#[test]
fn test_update_moving_price_initial() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
// Set current price to 1.0
SubnetTAO::<Test>::insert(netuid, 1_000_000);
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
SubnetMechanism::<Test>::insert(netuid, 1);
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.5));
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
// Registered recently
System::set_block_number(510);
NetworkRegisteredAt::<Test>::insert(netuid, 500);
SubtensorModule::update_moving_price(netuid);
let new_price = SubnetMovingPrice::<Test>::get(netuid);
assert!(new_price.to_num::<f64>() < 0.001);
});
}
// Test moving price updates slow down at the beginning.
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_update_moving_price_after_time --exact --show-output --nocapture
#[test]
fn test_update_moving_price_after_time() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
// Set current price to 1.0
SubnetTAO::<Test>::insert(netuid, 1_000_000);
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
SubnetMechanism::<Test>::insert(netuid, 1);
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.5));
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
// Registered long time ago
System::set_block_number(72_000_500);
NetworkRegisteredAt::<Test>::insert(netuid, 500);
SubtensorModule::update_moving_price(netuid);
let new_price = SubnetMovingPrice::<Test>::get(netuid);
assert!((new_price.to_num::<f64>() - 0.5).abs() < 0.001);
});
}
// Test basic alpha issuance in coinbase mechanism.
// This test verifies that:
// - Alpha issuance is initialized to 0 for new subnets
// - Alpha issuance is split evenly between subnets during coinbase
// - Each subnet receives the expected fraction of total emission
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_alpha_issuance_base --exact --show-output --nocapture
#[test]
fn test_coinbase_alpha_issuance_base() {
new_test_ext(1).execute_with(|| {
let netuid1: u16 = 1;
let netuid2: u16 = 2;
let emission: u64 = 1_000_000;
add_network(netuid1, 1, 0);
add_network(netuid2, 1, 0);
// Set up prices 1 and 1
let initial: u64 = 1_000_000;
SubnetTAO::<Test>::insert(netuid1, initial);
SubnetAlphaIn::<Test>::insert(netuid1, initial);
SubnetTAO::<Test>::insert(netuid2, initial);
SubnetAlphaIn::<Test>::insert(netuid2, initial);
// Check initial
SubtensorModule::run_coinbase(I96F32::from_num(emission));
// tao_in = 500_000
// alpha_in = 500_000/price = 500_000
assert_eq!(SubnetAlphaIn::<Test>::get(netuid1), initial + emission / 2);
assert_eq!(SubnetAlphaIn::<Test>::get(netuid2), initial + emission / 2);
});
}
// Test alpha issuance with different subnet prices.
// This test verifies that:
// - Alpha issuance is proportional to subnet prices
// - Higher priced subnets receive more TAO emission
// - Alpha issuance is correctly calculated based on price ratios
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_alpha_issuance_different --exact --show-output --nocapture
#[test]
fn test_coinbase_alpha_issuance_different() {
new_test_ext(1).execute_with(|| {
let netuid1: u16 = 1;
let netuid2: u16 = 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 1 and 1
let initial: u64 = 1_000_000;
SubnetTAO::<Test>::insert(netuid1, initial);
SubnetAlphaIn::<Test>::insert(netuid1, initial);
SubnetTAO::<Test>::insert(netuid2, initial);
SubnetAlphaIn::<Test>::insert(netuid2, initial);
// Set subnet prices.
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
// Run coinbase
SubtensorModule::run_coinbase(I96F32::from_num(emission));
// tao_in = 333_333
// alpha_in = 333_333/price = 333_333 + initial
assert_eq!(SubnetAlphaIn::<Test>::get(netuid1), initial + emission / 3);
// tao_in = 666_666
// alpha_in = 666_666/price = 666_666 + initial
assert_eq!(
SubnetAlphaIn::<Test>::get(netuid2),
initial + emission / 3 + emission / 3
);
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_alpha_issuance_with_cap_trigger --exact --show-output --nocapture
#[test]
fn test_coinbase_alpha_issuance_with_cap_trigger() {
new_test_ext(1).execute_with(|| {
let netuid1: u16 = 1;
let netuid2: u16 = 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 1000000
let initial: u64 = 1_000;
let initial_alpha: u64 = initial * 1000000;
SubnetTAO::<Test>::insert(netuid1, initial);
SubnetAlphaIn::<Test>::insert(netuid1, initial_alpha); // Make price extremely low.
SubnetTAO::<Test>::insert(netuid2, initial);
SubnetAlphaIn::<Test>::insert(netuid2, initial_alpha); // Make price extremely low.
// Set subnet prices.
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
// Run coinbase
SubtensorModule::run_coinbase(I96F32::from_num(emission));
// tao_in = 333_333
// alpha_in = 333_333/price > 1_000_000_000 --> 1_000_000_000 + initial_alpha
assert_eq!(
SubnetAlphaIn::<Test>::get(netuid1),
initial_alpha + 1_000_000_000
);
assert_eq!(SubnetAlphaOut::<Test>::get(netuid2), 1_000_000_000);
// tao_in = 666_666
// alpha_in = 666_666/price > 1_000_000_000 --> 1_000_000_000 + initial_alpha
assert_eq!(
SubnetAlphaIn::<Test>::get(netuid2),
initial_alpha + 1_000_000_000
);
assert_eq!(SubnetAlphaOut::<Test>::get(netuid2), 1_000_000_000); // Gets full block emission.
});
}
// 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: u16 = 1;
let netuid2: u16 = 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 1000000
let initial: u64 = 1_000;
let initial_alpha: u64 = initial * 1000000;
SubnetTAO::<Test>::insert(netuid1, initial);
SubnetAlphaIn::<Test>::insert(netuid1, initial_alpha); // Make price extremely low.
SubnetTAO::<Test>::insert(netuid2, initial);
SubnetAlphaIn::<Test>::insert(netuid2, initial_alpha); // Make price extremely low.
// Set issuance to greater than 21M
SubnetAlphaOut::<Test>::insert(netuid1, 22_000_000_000_000_000); // Set issuance above 21M
SubnetAlphaOut::<Test>::insert(netuid2, 22_000_000_000_000_000); // Set issuance above 21M
// Set subnet prices.
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
// Run coinbase
SubtensorModule::run_coinbase(I96F32::from_num(emission));
// tao_in = 333_333
// alpha_in = 333_333/price > 1_000_000_000 --> 0 + initial_alpha
assert_eq!(SubnetAlphaIn::<Test>::get(netuid1), initial_alpha);
assert_eq!(SubnetAlphaOut::<Test>::get(netuid2), 22_000_000_000_000_000);
// tao_in = 666_666
// alpha_in = 666_666/price > 1_000_000_000 --> 0 + initial_alpha
assert_eq!(SubnetAlphaIn::<Test>::get(netuid2), initial_alpha);
assert_eq!(SubnetAlphaOut::<Test>::get(netuid2), 22_000_000_000_000_000);
// No emission.
});
}
// 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() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
SubtensorModule::set_tempo(netuid, 10000); // Large number (dont drain)
SubtensorModule::set_subnet_owner_cut(0);
SubtensorModule::run_coinbase(I96F32::from_num(0));
assert_eq!(PendingOwnerCut::<Test>::get(netuid), 0); // No cut
SubtensorModule::set_subnet_owner_cut(u16::MAX);
SubtensorModule::run_coinbase(I96F32::from_num(0));
assert_eq!(PendingOwnerCut::<Test>::get(netuid), 1_000_000_000); // Full cut.
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_pending_swapped --exact --show-output --nocapture
#[test]
fn test_pending_swapped() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let emission: u64 = 1_000_000;
add_network(netuid, 1, 0);
SubtensorModule::run_coinbase(I96F32::from_num(0));
assert_eq!(PendingAlphaSwapped::<Test>::get(netuid), 0); // Zero tao weight and no root.
SubnetTAO::<Test>::insert(0, 1_000_000_000); // Add root weight.
SubtensorModule::run_coinbase(I96F32::from_num(0));
assert_eq!(PendingAlphaSwapped::<Test>::get(netuid), 0); // Zero tao weight with 1 root.
SubtensorModule::set_tempo(netuid, 10000); // Large number (dont drain)
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
SubtensorModule::run_coinbase(I96F32::from_num(0));
assert_eq!(PendingAlphaSwapped::<Test>::get(netuid), 125000000); // 1 TAO / ( 1 + 3 ) = 0.25 * 1 / 2 = 125000000
assert_eq!(
PendingEmission::<Test>::get(netuid),
1_000_000_000 - 125000000
); // 1 - swapped.
assert_eq!(PendingRootDivs::<Test>::get(netuid), 125000000); // swapped * (price = 1)
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base --exact --show-output --nocapture
#[test]
fn test_drain_base() {
new_test_ext(1).execute_with(|| SubtensorModule::drain_pending_emission(0, 0, 0, 0, 0));
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet --exact --show-output --nocapture
#[test]
fn test_drain_base_with_subnet() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
SubtensorModule::drain_pending_emission(netuid, 0, 0, 0, 0)
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_single_staker_not_registered --exact --show-output --nocapture
#[test]
fn test_drain_base_with_subnet_with_single_staker_not_registered() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
let hotkey = U256::from(1);
let coldkey = U256::from(2);
let stake_before: u64 = 1_000_000_000;
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey,
&coldkey,
netuid,
stake_before,
);
let pending_alpha: u64 = 1_000_000_000;
SubtensorModule::drain_pending_emission(netuid, pending_alpha, 0, 0, 0);
let stake_after =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
assert_eq!(stake_before, stake_after); // Not registered.
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_single_staker_registered --exact --show-output --nocapture
#[test]
fn test_drain_base_with_subnet_with_single_staker_registered() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
let hotkey = U256::from(1);
let coldkey = U256::from(2);
let stake_before: u64 = 1_000_000_000;
register_ok_neuron(netuid, hotkey, coldkey, 0);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey,
&coldkey,
netuid,
stake_before,
);
let pending_alpha: u64 = 1_000_000_000;
SubtensorModule::drain_pending_emission(netuid, pending_alpha, 0, 0, 0);
let stake_after =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
close(stake_before + pending_alpha, stake_after, 10); // Registered gets all emission.
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_single_staker_registered_root_weight --exact --show-output --nocapture
#[test]
fn test_drain_base_with_subnet_with_single_staker_registered_root_weight() {
new_test_ext(1).execute_with(|| {
let root: u16 = 0;
let netuid: u16 = 1;
add_network(netuid, 1, 0);
let hotkey = U256::from(1);
let coldkey = U256::from(2);
let stake_before: u64 = 1_000_000_000;
// register_ok_neuron(root, hotkey, coldkey, 0);
register_ok_neuron(netuid, hotkey, coldkey, 0);
Delegates::<Test>::insert(hotkey, 0);
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey,
&coldkey,
root,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey,
&coldkey,
netuid,
stake_before,
);
let pending_tao: u64 = 1_000_000_000;
let pending_alpha: u64 = 1_000_000_000;
SubtensorModule::drain_pending_emission(netuid, pending_alpha, pending_tao, 0, 0);
let stake_after =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
let root_after =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, root);
close(stake_before + pending_alpha / 2, stake_after, 10); // Registered gets all alpha emission.
close(stake_before + pending_tao, root_after, 10); // Registered gets all tao emission
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_two_stakers_registered --exact --show-output --nocapture
#[test]
fn test_drain_base_with_subnet_with_two_stakers_registered() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
let hotkey1 = U256::from(1);
let hotkey2 = U256::from(2);
let coldkey = U256::from(3);
let stake_before: u64 = 1_000_000_000;
register_ok_neuron(netuid, hotkey1, coldkey, 0);
register_ok_neuron(netuid, hotkey2, coldkey, 0);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey1,
&coldkey,
netuid,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey2,
&coldkey,
netuid,
stake_before,
);
let pending_alpha: u64 = 1_000_000_000;
SubtensorModule::drain_pending_emission(netuid, pending_alpha, 0, 0, 0);
let stake_after1 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, netuid);
let stake_after2 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, netuid);
close(stake_before + pending_alpha / 2, stake_after1, 10); // Registered gets 1/2 emission
close(stake_before + pending_alpha / 2, stake_after2, 10); // Registered gets 1/2 emission.
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_two_stakers_registered_and_root --exact --show-output --nocapture
#[test]
fn test_drain_base_with_subnet_with_two_stakers_registered_and_root() {
new_test_ext(1).execute_with(|| {
let root: u16 = 0;
let netuid: u16 = 1;
add_network(netuid, 1, 0);
let hotkey1 = U256::from(1);
let hotkey2 = U256::from(2);
let coldkey = U256::from(3);
let stake_before: u64 = 1_000_000_000;
register_ok_neuron(netuid, hotkey1, coldkey, 0);
register_ok_neuron(netuid, hotkey2, coldkey, 0);
Delegates::<Test>::insert(hotkey1, 0);
Delegates::<Test>::insert(hotkey2, 0);
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey1,
&coldkey,
netuid,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey1,
&coldkey,
root,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey2,
&coldkey,
netuid,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey2,
&coldkey,
root,
stake_before,
);
let pending_tao: u64 = 1_000_000_000;
let pending_alpha: u64 = 1_000_000_000;
SubtensorModule::drain_pending_emission(netuid, pending_alpha, pending_tao, 0, 0);
let stake_after1 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, netuid);
let root_after1 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, root);
let stake_after2 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, netuid);
let root_after2 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, root);
close(stake_before + pending_alpha / 4, stake_after1, 10); // Registered gets 1/2 emission
close(stake_before + pending_alpha / 4, stake_after2, 10); // Registered gets 1/2 emission.
close(stake_before + pending_tao / 2, root_after1, 10); // Registered gets 1/2 tao emission
close(stake_before + pending_tao / 2, root_after2, 10); // Registered gets 1/2 tao emission
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_amounts --exact --show-output --nocapture
#[test]
fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_amounts() {
new_test_ext(1).execute_with(|| {
let root: u16 = 0;
let netuid: u16 = 1;
add_network(netuid, 1, 0);
let hotkey1 = U256::from(1);
let hotkey2 = U256::from(2);
let coldkey = U256::from(3);
let stake_before: u64 = 1_000_000_000;
Delegates::<Test>::insert(hotkey1, 0);
Delegates::<Test>::insert(hotkey2, 0);
register_ok_neuron(netuid, hotkey1, coldkey, 0);
register_ok_neuron(netuid, hotkey2, coldkey, 0);
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey1,
&coldkey,
netuid,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey1,
&coldkey,
root,
2 * stake_before, // Hotkey 1 has twice as much root weight.
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey2,
&coldkey,
netuid,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey2,
&coldkey,
root,
stake_before,
);
let pending_tao: u64 = 1_000_000_000;
let pending_alpha: u64 = 1_000_000_000;
SubtensorModule::drain_pending_emission(netuid, pending_alpha, pending_tao, 0, 0);
let stake_after1 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, netuid);
let root_after1 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, root);
let stake_after2 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, netuid);
let root_after2 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, root);
let expected_stake = I96F32::from_num(stake_before)
+ (I96F32::from_num(pending_alpha)
* I96F32::from_num(3.0 / 5.0)
* I96F32::from_num(1.0 / 3.0));
close(expected_stake.to_num::<u64>(), stake_after1, 10); // Registered gets 60% of emission
let expected_stake2 = I96F32::from_num(stake_before)
+ I96F32::from_num(pending_alpha)
* I96F32::from_num(2.0 / 5.0)
* I96F32::from_num(1.0 / 2.0);
close(expected_stake2.to_num::<u64>(), stake_after2, 10); // Registered gets 40% emission
let expected_root1 = I96F32::from_num(2 * stake_before)
+ I96F32::from_num(pending_tao) * I96F32::from_num(2.0 / 3.0);
close(expected_root1.to_num::<u64>(), root_after1, 10); // Registered gets 2/3 tao emission
let expected_root2 = I96F32::from_num(stake_before)
+ I96F32::from_num(pending_tao) * I96F32::from_num(1.0 / 3.0);
close(expected_root2.to_num::<u64>(), root_after2, 10); // Registered gets 1/3 tao emission
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_amounts_half_tao_weight --exact --show-output --nocapture
#[test]
fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_amounts_half_tao_weight()
{
new_test_ext(1).execute_with(|| {
let root: u16 = 0;
let netuid: u16 = 1;
add_network(netuid, 1, 0);
let hotkey1 = U256::from(1);
let hotkey2 = U256::from(2);
let coldkey = U256::from(3);
let stake_before: u64 = 1_000_000_000;
Delegates::<Test>::insert(hotkey1, 0);
Delegates::<Test>::insert(hotkey2, 0);
register_ok_neuron(netuid, hotkey1, coldkey, 0);
register_ok_neuron(netuid, hotkey2, coldkey, 0);
SubtensorModule::set_tao_weight(u64::MAX / 2); // Set TAO weight to 0.5
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey1,
&coldkey,
netuid,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey1,
&coldkey,
root,
2 * stake_before, // Hotkey 1 has twice as much root weight.
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey2,
&coldkey,
netuid,
stake_before,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey2,
&coldkey,
root,
stake_before,
);
let pending_tao: u64 = 1_000_000_000;
let pending_alpha: u64 = 1_000_000_000;
SubtensorModule::drain_pending_emission(netuid, pending_alpha, pending_tao, 0, 0);
let stake_after1 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, netuid);
let root_after1 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, root);
let stake_after2 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, netuid);
let root_after2 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, root);
// hotkey 1 has (1 + (2 * 0.5))/( 1 + 1*0.5 + 1 + (2 * 0.5)) = 0.5714285714 of the hotkey emission.
let expected_stake = I96F32::from_num(stake_before)
+ I96F32::from_num(pending_alpha)
* I96F32::from_num(0.5714285714)
* I96F32::from_num(1.0 / 2.0);
close(expected_stake.to_num::<u64>(), stake_after1, 10);
// hotkey 2 has (1 + 1*0.5)/( 1 + 1*0.5 + 1 + (2 * 0.5)) = 0.4285714286 of the hotkey emission.
let expected_stake2 = I96F32::from_num(stake_before)
+ I96F32::from_num(pending_alpha)
* I96F32::from_num(0.4285714286)
* I96F32::from_num(2.0 / 3.0);
close(expected_stake2.to_num::<u64>(), stake_after2, 10);
// hotkey 1 has 2 / 3 root tao
let expected_root1 = I96F32::from_num(2 * stake_before)
+ I96F32::from_num(pending_tao) * I96F32::from_num(2.0 / 3.0);
close(expected_root1.to_num::<u64>(), root_after1, 10);
// hotkey 1 has 1 / 3 root tao
let expected_root2 = I96F32::from_num(stake_before)
+ I96F32::from_num(pending_tao) * I96F32::from_num(1.0 / 3.0);
close(expected_root2.to_num::<u64>(), root_after2, 10);
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_alpha_childkey_parentkey --exact --show-output --nocapture
#[test]
fn test_drain_alpha_childkey_parentkey() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
let parent = U256::from(1);
let child = U256::from(2);
let coldkey = U256::from(3);
let stake_before: u64 = 1_000_000_000;
register_ok_neuron(netuid, child, coldkey, 0);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&parent,
&coldkey,
netuid,
stake_before,
);
mock_set_children_no_epochs(netuid, &parent, &[(u64::MAX, child)]);
// Childkey take is 10%
ChildkeyTake::<Test>::insert(child, netuid, u16::MAX / 10);
let pending_alpha: u64 = 1_000_000_000;
SubtensorModule::drain_pending_emission(netuid, pending_alpha, 0, 0, 0);
let parent_stake_after = SubtensorModule::get_stake_for_hotkey_on_subnet(&parent, netuid);
let child_stake_after = SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid);
// Child gets 10%, parent gets 90%
let expected = I96F32::from_num(stake_before)
+ I96F32::from_num(pending_alpha) * I96F32::from_num(9.0 / 10.0);
log::info!(
"expected: {:?}, parent_stake_after: {:?}",
expected.to_num::<u64>(),
parent_stake_after
);
close(expected.to_num::<u64>(), parent_stake_after, 10_000);
let expected = I96F32::from_num(pending_alpha) / I96F32::from_num(10);
close(expected.to_num::<u64>(), child_stake_after, 10_000);
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_get_root_children --exact --show-output --nocapture
#[test]
fn test_get_root_children() {
new_test_ext(1).execute_with(|| {
// Init netuid 1
let root: u16 = 0;
let alpha: u16 = 1;
add_network(root, 1, 0);
add_network(alpha, 1, 0);
// Set TAO weight to 1.
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.
// Create keys.
let cold = U256::from(0);
let alice = U256::from(1);
let bob = U256::from(2);
// Register Alice and Bob to the root network and alpha subnet.
register_ok_neuron(alpha, alice, cold, 0);
register_ok_neuron(alpha, bob, cold, 0);
assert_ok!(SubtensorModule::root_register(
RuntimeOrigin::signed(cold).clone(),
alice,
));
assert_ok!(SubtensorModule::root_register(
RuntimeOrigin::signed(cold).clone(),
bob,
));
// Add stake for Alice and Bob on root.
let alice_root_stake: u64 = 1_000_000_000;
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&alice,
&cold,
root,
alice_root_stake,
);
let bob_root_stake: u64 = 1_000_000_000;
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&bob,
&cold,
root,
alice_root_stake,
);
// Add stake for Alice and Bob on netuid.
let alice_alpha_stake: u64 = 1_000_000_000;
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&alice,
&cold,
alpha,
alice_alpha_stake,
);
let bob_alpha_stake: u64 = 1_000_000_000;
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&bob,
&cold,
alpha,
bob_alpha_stake,
);
// Set Bob as 100% child of Alice on root.
// mock_set_children_no_epochs( root, &alice, &[(u64::MAX, bob)]);
mock_set_children_no_epochs(alpha, &alice, &[(u64::MAX, bob)]);
// Assert Alice and Bob stake on root and netuid
assert_eq!(
SubtensorModule::get_stake_for_hotkey_on_subnet(&alice, root),
alice_root_stake
);
assert_eq!(
SubtensorModule::get_stake_for_hotkey_on_subnet(&bob, root),
bob_root_stake
);
assert_eq!(
SubtensorModule::get_stake_for_hotkey_on_subnet(&alice, alpha),
alice_alpha_stake
);
assert_eq!(
SubtensorModule::get_stake_for_hotkey_on_subnet(&bob, alpha),
bob_alpha_stake
);
// Assert Alice and Bob inherited stakes
assert_eq!(
SubtensorModule::get_inherited_for_hotkey_on_subnet(&alice, root),
alice_root_stake
);
assert_eq!(
SubtensorModule::get_inherited_for_hotkey_on_subnet(&alice, alpha),
0
);
assert_eq!(
SubtensorModule::get_inherited_for_hotkey_on_subnet(&bob, root),
bob_root_stake
);
assert_eq!(
SubtensorModule::get_inherited_for_hotkey_on_subnet(&bob, alpha),
bob_alpha_stake + alice_alpha_stake
);
// Assert Alice and Bob TAO inherited stakes
assert_eq!(
SubtensorModule::get_tao_inherited_for_hotkey_on_subnet(&alice, alpha),
0
);
assert_eq!(
SubtensorModule::get_tao_inherited_for_hotkey_on_subnet(&bob, alpha),
bob_root_stake + alice_root_stake
);
// Get Alice stake amounts on subnet alpha.
let (alice_total, alice_alpha, alice_tao): (I64F64, I64F64, I64F64) =
SubtensorModule::get_stake_weights_for_hotkey_on_subnet(&alice, alpha);
assert_eq!(alice_total, I64F64::from_num(0));
// Get Bob stake amounts on subnet alpha.
let (bob_total, bob_alpha, bob_tao): (I64F64, I64F64, I64F64) =
SubtensorModule::get_stake_weights_for_hotkey_on_subnet(&bob, alpha);
assert_eq!(bob_total, I64F64::from_num(4 * bob_root_stake));
});
}
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_get_root_children_drain --exact --show-output --nocapture
#[test]
fn test_get_root_children_drain() {
new_test_ext(1).execute_with(|| {
// Init netuid 1
let root: u16 = 0;
let alpha: u16 = 1;
add_network(root, 1, 0);
add_network(alpha, 1, 0);
// Set TAO weight to 1.
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.
// Create keys.
let cold_alice = U256::from(0);
let cold_bob = U256::from(1);
let alice = U256::from(2);
let bob = U256::from(3);
// Register Alice and Bob to the root network and alpha subnet.
register_ok_neuron(alpha, alice, cold_alice, 0);
register_ok_neuron(alpha, bob, cold_bob, 0);
assert_ok!(SubtensorModule::root_register(
RuntimeOrigin::signed(cold_alice).clone(),
alice,