@@ -190,11 +190,16 @@ fn test_coinbase_moving_prices() {
190
190
SubnetAlphaIn :: < Test > :: insert ( netuid, 1_000_000 ) ;
191
191
SubnetMechanism :: < Test > :: insert ( netuid, 1 ) ;
192
192
SubnetMovingPrice :: < Test > :: insert ( netuid, I96F32 :: from_num ( 1 ) ) ;
193
+ NetworkRegisteredAt :: < Test > :: insert ( netuid, 1 ) ;
194
+
193
195
// Updating the moving price keeps it the same.
194
196
assert_eq ! (
195
197
SubtensorModule :: get_moving_alpha_price( netuid) ,
196
198
I96F32 :: from_num( 1 )
197
199
) ;
200
+ // Skip some blocks so that EMA price is not slowed down
201
+ System :: set_block_number ( 7_200_000 ) ;
202
+
198
203
SubtensorModule :: update_moving_price ( netuid) ;
199
204
assert_eq ! (
200
205
SubtensorModule :: get_moving_alpha_price( netuid) ,
@@ -206,29 +211,78 @@ fn test_coinbase_moving_prices() {
206
211
SubnetMovingAlpha :: < Test > :: set ( I96F32 :: from_num ( 1.0 ) ) ;
207
212
// Run moving 1 times.
208
213
SubtensorModule :: update_moving_price ( netuid) ;
209
- // Assert price is == 100% of the real price.
210
- assert_eq ! (
211
- SubtensorModule :: get_moving_alpha_price( netuid) ,
212
- I96F32 :: from_num( 1.0 )
213
- ) ;
214
+ // Assert price is ~ 100% of the real price.
215
+ assert ! ( I96F32 :: from_num( 1.0 ) - SubtensorModule :: get_moving_alpha_price( netuid) < 0.05 ) ;
214
216
// Set price to zero.
215
217
SubnetMovingPrice :: < Test > :: insert ( netuid, I96F32 :: from_num ( 0 ) ) ;
216
218
SubnetMovingAlpha :: < Test > :: set ( I96F32 :: from_num ( 0.1 ) ) ;
217
- // Run moving 6 times.
218
- SubtensorModule :: update_moving_price ( netuid) ;
219
- SubtensorModule :: update_moving_price ( netuid) ;
220
- SubtensorModule :: update_moving_price ( netuid) ;
221
- SubtensorModule :: update_moving_price ( netuid) ;
222
- SubtensorModule :: update_moving_price ( netuid) ;
223
- SubtensorModule :: update_moving_price ( netuid) ;
219
+
220
+ // EMA price 14 days after registration
221
+ System :: set_block_number ( 7_200 * 14 ) ;
222
+
223
+ // Run moving 14 times.
224
+ for _ in 0 ..14 {
225
+ SubtensorModule :: update_moving_price ( netuid) ;
226
+ }
227
+
224
228
// Assert price is > 50% of the real price.
225
- assert_eq ! (
226
- SubtensorModule :: get_moving_alpha_price( netuid) ,
227
- I96F32 :: from_num ( 0.468559 )
229
+ assert ! (
230
+ ( I96F32 :: from_num ( 0.512325 ) - SubtensorModule :: get_moving_alpha_price( netuid) ) . abs ( )
231
+ < 0.001
228
232
) ;
229
233
} ) ;
230
234
}
231
235
236
+ // Test moving price updates slow down at the beginning.
237
+ // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_update_moving_price_initial --exact --show-output --nocapture
238
+ #[ test]
239
+ fn test_update_moving_price_initial ( ) {
240
+ new_test_ext ( 1 ) . execute_with ( || {
241
+ let netuid: u16 = 1 ;
242
+ add_network ( netuid, 1 , 0 ) ;
243
+ // Set current price to 1.0
244
+ SubnetTAO :: < Test > :: insert ( netuid, 1_000_000 ) ;
245
+ SubnetAlphaIn :: < Test > :: insert ( netuid, 1_000_000 ) ;
246
+ SubnetMechanism :: < Test > :: insert ( netuid, 1 ) ;
247
+ SubnetMovingAlpha :: < Test > :: set ( I96F32 :: from_num ( 0.5 ) ) ;
248
+ SubnetMovingPrice :: < Test > :: insert ( netuid, I96F32 :: from_num ( 0 ) ) ;
249
+
250
+ // Registered recently
251
+ System :: set_block_number ( 510 ) ;
252
+ NetworkRegisteredAt :: < Test > :: insert ( netuid, 500 ) ;
253
+
254
+ SubtensorModule :: update_moving_price ( netuid) ;
255
+
256
+ let new_price = SubnetMovingPrice :: < Test > :: get ( netuid) ;
257
+ assert ! ( new_price. to_num:: <f64 >( ) < 0.001 ) ;
258
+ } ) ;
259
+ }
260
+
261
+ // Test moving price updates slow down at the beginning.
262
+ // 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
263
+ #[ test]
264
+ fn test_update_moving_price_after_time ( ) {
265
+ new_test_ext ( 1 ) . execute_with ( || {
266
+ let netuid: u16 = 1 ;
267
+ add_network ( netuid, 1 , 0 ) ;
268
+ // Set current price to 1.0
269
+ SubnetTAO :: < Test > :: insert ( netuid, 1_000_000 ) ;
270
+ SubnetAlphaIn :: < Test > :: insert ( netuid, 1_000_000 ) ;
271
+ SubnetMechanism :: < Test > :: insert ( netuid, 1 ) ;
272
+ SubnetMovingAlpha :: < Test > :: set ( I96F32 :: from_num ( 0.5 ) ) ;
273
+ SubnetMovingPrice :: < Test > :: insert ( netuid, I96F32 :: from_num ( 0 ) ) ;
274
+
275
+ // Registered long time ago
276
+ System :: set_block_number ( 72_000_500 ) ;
277
+ NetworkRegisteredAt :: < Test > :: insert ( netuid, 500 ) ;
278
+
279
+ SubtensorModule :: update_moving_price ( netuid) ;
280
+
281
+ let new_price = SubnetMovingPrice :: < Test > :: get ( netuid) ;
282
+ assert ! ( ( new_price. to_num:: <f64 >( ) - 0.5 ) . abs( ) < 0.001 ) ;
283
+ } ) ;
284
+ }
285
+
232
286
// Test basic alpha issuance in coinbase mechanism.
233
287
// This test verifies that:
234
288
// - Alpha issuance is initialized to 0 for new subnets
0 commit comments