-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathtest_staking.py
666 lines (571 loc) · 18.4 KB
/
test_staking.py
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
from bittensor import logging
from bittensor.core.chain_data.stake_info import StakeInfo
from bittensor.utils.balance import Balance
from tests.e2e_tests.utils.chain_interactions import ANY_BALANCE
from tests.helpers.helpers import ApproxBalance
logging.enable_info()
def test_single_operation(subtensor, alice_wallet, bob_wallet):
"""
Tests:
- Staking using `add_stake`
- Unstaking using `unstake`
- Checks StakeInfo
"""
subtensor.burned_register(
alice_wallet,
netuid=1,
wait_for_inclusion=True,
wait_for_finalization=True,
)
subtensor.burned_register(
bob_wallet,
netuid=1,
wait_for_inclusion=True,
wait_for_finalization=True,
)
stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=1,
)
assert stake == Balance(0)
success = subtensor.add_stake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=1,
amount=Balance.from_tao(10_000),
wait_for_inclusion=True,
wait_for_finalization=True,
)
assert success is True
stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=1,
)
assert stake > Balance(0)
stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)
assert stakes == [
StakeInfo(
hotkey_ss58=bob_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=1,
stake=stake,
locked=Balance(0),
emission=Balance(0),
drain=0,
is_registered=True,
),
]
stakes = subtensor.get_stake_info_for_coldkey(alice_wallet.coldkey.ss58_address)
assert stakes == [
StakeInfo(
hotkey_ss58=bob_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=1,
stake=stake,
locked=Balance(0),
emission=Balance(0),
drain=0,
is_registered=True,
),
]
stakes = subtensor.get_stake_for_coldkey_and_hotkey(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
)
assert stakes == {
0: StakeInfo(
hotkey_ss58=bob_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=0,
stake=Balance(0),
locked=Balance(0),
emission=Balance(0),
drain=0,
is_registered=False,
),
1: StakeInfo(
hotkey_ss58=bob_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=1,
stake=stake,
locked=Balance.from_tao(0, netuid=1),
emission=Balance.from_tao(0, netuid=1),
drain=0,
is_registered=True,
),
}
success = subtensor.unstake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=1,
amount=stake,
wait_for_inclusion=True,
wait_for_finalization=True,
)
assert success is True
stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=1,
)
assert stake == Balance(0)
def test_batch_operations(subtensor, alice_wallet, bob_wallet):
"""
Tests:
- Staking using `add_stake_multiple`
- Unstaking using `unstake_multiple`
- Checks StakeInfo
- Checks Accounts Balance
"""
netuids = [
2,
3,
]
for _ in netuids:
subtensor.register_subnet(
alice_wallet,
wait_for_inclusion=True,
wait_for_finalization=True,
)
for netuid in netuids:
subtensor.burned_register(
bob_wallet,
netuid,
wait_for_inclusion=True,
wait_for_finalization=True,
)
for netuid in netuids:
stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
assert stake == Balance(0), f"netuid={netuid} stake={stake}"
balances = subtensor.get_balances(
alice_wallet.coldkey.ss58_address,
bob_wallet.coldkey.ss58_address,
)
assert balances == {
alice_wallet.coldkey.ss58_address: ANY_BALANCE,
bob_wallet.coldkey.ss58_address: Balance.from_tao(999_998),
}
alice_balance = balances[alice_wallet.coldkey.ss58_address]
success = subtensor.add_stake_multiple(
alice_wallet,
hotkey_ss58s=[bob_wallet.hotkey.ss58_address for _ in netuids],
netuids=netuids,
amounts=[Balance.from_tao(10_000) for _ in netuids],
)
assert success is True
stakes = [
subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
for netuid in netuids
]
for netuid, stake in zip(netuids, stakes):
assert stake > Balance(0), f"netuid={netuid} stake={stake}"
alice_balance -= len(netuids) * Balance.from_tao(10_000)
balances = subtensor.get_balances(
alice_wallet.coldkey.ss58_address,
bob_wallet.coldkey.ss58_address,
)
assert balances == {
alice_wallet.coldkey.ss58_address: ApproxBalance(alice_balance.rao),
bob_wallet.coldkey.ss58_address: Balance.from_tao(999_998),
}
success = subtensor.unstake_multiple(
alice_wallet,
hotkey_ss58s=[bob_wallet.hotkey.ss58_address for _ in netuids],
netuids=netuids,
amounts=[Balance.from_tao(100) for _ in netuids],
)
assert success is True
for netuid, old_stake in zip(netuids, stakes):
stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
assert stake < old_stake, f"netuid={netuid} stake={stake}"
balances = subtensor.get_balances(
alice_wallet.coldkey.ss58_address,
bob_wallet.coldkey.ss58_address,
)
assert balances == {
alice_wallet.coldkey.ss58_address: ANY_BALANCE,
bob_wallet.coldkey.ss58_address: Balance.from_tao(999_998),
}
assert balances[alice_wallet.coldkey.ss58_address] > alice_balance
def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
"""
Tests safe staking scenarios with different parameters.
For both staking and unstaking:
1. Fails with strict threshold (0.5%) and no partial staking
2. Succeeds with strict threshold (0.5%) and partial staking allowed
3. Succeeds with lenient threshold (10% and 30%) and no partial staking
"""
netuid = 2
# Register root as Alice - the subnet owner and validator
assert subtensor.register_subnet(alice_wallet)
# Verify subnet created successfully
assert subtensor.subnet_exists(netuid), "Subnet wasn't created successfully"
subtensor.burned_register(
alice_wallet,
netuid=netuid,
wait_for_inclusion=True,
wait_for_finalization=True,
)
subtensor.burned_register(
bob_wallet,
netuid=netuid,
wait_for_inclusion=True,
wait_for_finalization=True,
)
initial_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
assert initial_stake == Balance(0)
# Test Staking Scenarios
stake_amount = Balance.from_tao(100)
# 1. Strict params - should fail
success = subtensor.add_stake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
amount=stake_amount,
wait_for_inclusion=True,
wait_for_finalization=True,
safe_staking=True,
rate_tolerance=0.005, # 0.5%
allow_partial_stake=False,
)
assert success is False
current_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
assert current_stake == Balance(0), "Stake should not change after failed attempt"
# 2. Partial allowed - should succeed partially
success = subtensor.add_stake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
amount=stake_amount,
wait_for_inclusion=True,
wait_for_finalization=True,
safe_staking=True,
rate_tolerance=0.005, # 0.5%
allow_partial_stake=True,
)
assert success is True
partial_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
assert partial_stake > Balance(0), "Partial stake should be added"
assert (
partial_stake < stake_amount
), "Partial stake should be less than requested amount"
# 3. Higher threshold - should succeed fully
amount = Balance.from_tao(100)
success = subtensor.add_stake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
amount=amount,
wait_for_inclusion=True,
wait_for_finalization=True,
safe_staking=True,
rate_tolerance=0.1, # 10%
allow_partial_stake=False,
)
assert success is True
full_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
# Test Unstaking Scenarios
# 1. Strict params - should fail
success = subtensor.unstake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
amount=stake_amount,
wait_for_inclusion=True,
wait_for_finalization=True,
safe_staking=True,
rate_tolerance=0.005, # 0.5%
allow_partial_stake=False,
)
assert success is False
current_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
assert (
current_stake == full_stake
), "Stake should not change after failed unstake attempt"
# 2. Partial allowed - should succeed partially
success = subtensor.unstake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
amount=current_stake,
wait_for_inclusion=True,
wait_for_finalization=True,
safe_staking=True,
rate_tolerance=0.005, # 0.5%
allow_partial_stake=True,
)
assert success is True
partial_unstake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
)
assert partial_unstake > Balance(0), "Some stake should remain"
# 3. Higher threshold - should succeed fully
success = subtensor.unstake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=netuid,
amount=partial_unstake,
wait_for_inclusion=True,
wait_for_finalization=True,
safe_staking=True,
rate_tolerance=0.3, # 30%
allow_partial_stake=False,
)
assert success is True
def test_safe_swap_stake_scenarios(subtensor, alice_wallet, bob_wallet):
"""
Tests safe swap stake scenarios with different parameters.
Tests:
1. Fails with strict threshold (0.5%)
2. Succeeds with lenient threshold (10%)
"""
# Create new subnet (netuid 2) and register Alice
origin_netuid = 2
assert subtensor.register_subnet(bob_wallet)
assert subtensor.subnet_exists(origin_netuid), "Subnet wasn't created successfully"
dest_netuid = 3
assert subtensor.register_subnet(bob_wallet)
assert subtensor.subnet_exists(dest_netuid), "Subnet wasn't created successfully"
# Register Alice on both subnets
subtensor.burned_register(
alice_wallet,
netuid=origin_netuid,
wait_for_inclusion=True,
wait_for_finalization=True,
)
subtensor.burned_register(
alice_wallet,
netuid=dest_netuid,
wait_for_inclusion=True,
wait_for_finalization=True,
)
# Add initial stake to swap from
initial_stake_amount = Balance.from_tao(10_000)
success = subtensor.add_stake(
alice_wallet,
alice_wallet.hotkey.ss58_address,
netuid=origin_netuid,
amount=initial_stake_amount,
wait_for_inclusion=True,
wait_for_finalization=True,
)
assert success is True
origin_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
alice_wallet.hotkey.ss58_address,
netuid=origin_netuid,
)
assert origin_stake > Balance(0), "Origin stake should be non-zero"
stake_swap_amount = Balance.from_tao(10_000)
# 1. Try swap with strict threshold and big amount- should fail
success = subtensor.swap_stake(
wallet=alice_wallet,
hotkey_ss58=alice_wallet.hotkey.ss58_address,
origin_netuid=origin_netuid,
destination_netuid=dest_netuid,
amount=stake_swap_amount,
wait_for_inclusion=True,
wait_for_finalization=True,
safe_staking=True,
rate_tolerance=0.005, # 0.5%
allow_partial_stake=False,
)
assert success is False
# Verify no stake was moved
dest_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
alice_wallet.hotkey.ss58_address,
netuid=dest_netuid,
)
assert dest_stake == Balance(
0
), "Destination stake should remain 0 after failed swap"
# 2. Try swap with higher threshold and less amount - should succeed
stake_swap_amount = Balance.from_tao(100)
success = subtensor.swap_stake(
wallet=alice_wallet,
hotkey_ss58=alice_wallet.hotkey.ss58_address,
origin_netuid=origin_netuid,
destination_netuid=dest_netuid,
amount=stake_swap_amount,
wait_for_inclusion=True,
wait_for_finalization=True,
safe_staking=True,
rate_tolerance=0.3, # 30%
allow_partial_stake=True,
)
assert success is True
# Verify stake was moved
origin_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
alice_wallet.hotkey.ss58_address,
netuid=origin_netuid,
)
dest_stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
alice_wallet.hotkey.ss58_address,
netuid=dest_netuid,
)
assert dest_stake > Balance(
0
), "Destination stake should be non-zero after successful swap"
def test_move_stake(subtensor, alice_wallet, bob_wallet):
"""
Tests:
- Adding stake
- Moving stake from one hotkey-subnet pair to another
"""
subtensor.burned_register(
alice_wallet,
netuid=1,
wait_for_inclusion=True,
wait_for_finalization=True,
)
assert subtensor.add_stake(
alice_wallet,
alice_wallet.hotkey.ss58_address,
netuid=1,
amount=Balance.from_tao(1_000),
wait_for_inclusion=True,
wait_for_finalization=True,
)
stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)
assert stakes == [
StakeInfo(
hotkey_ss58=alice_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=1,
stake=ANY_BALANCE,
locked=Balance(0),
emission=ANY_BALANCE,
drain=0,
is_registered=True,
),
]
subtensor.register_subnet(bob_wallet)
assert subtensor.move_stake(
alice_wallet,
origin_hotkey=alice_wallet.hotkey.ss58_address,
origin_netuid=1,
destination_hotkey=bob_wallet.hotkey.ss58_address,
destination_netuid=2,
amount=stakes[0].stake,
wait_for_finalization=True,
wait_for_inclusion=True,
)
stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)
assert stakes == [
StakeInfo(
hotkey_ss58=bob_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=2,
stake=ANY_BALANCE,
locked=Balance(0),
emission=ANY_BALANCE,
drain=0,
is_registered=True,
),
]
def test_transfer_stake(subtensor, alice_wallet, bob_wallet, dave_wallet):
"""
Tests:
- Adding stake
- Transfering stake from one coldkey-subnet pair to another
"""
subtensor.burned_register(
alice_wallet,
netuid=1,
wait_for_inclusion=True,
wait_for_finalization=True,
)
assert subtensor.add_stake(
alice_wallet,
alice_wallet.hotkey.ss58_address,
netuid=1,
amount=Balance.from_tao(1_000),
wait_for_inclusion=True,
wait_for_finalization=True,
)
alice_stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)
assert alice_stakes == [
StakeInfo(
hotkey_ss58=alice_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=1,
stake=ANY_BALANCE,
locked=Balance(0),
emission=ANY_BALANCE,
drain=0,
is_registered=True,
),
]
bob_stakes = subtensor.get_stake_for_coldkey(bob_wallet.coldkey.ss58_address)
assert bob_stakes == []
subtensor.register_subnet(dave_wallet)
subtensor.burned_register(
bob_wallet,
netuid=2,
wait_for_inclusion=True,
wait_for_finalization=True,
)
assert subtensor.transfer_stake(
alice_wallet,
destination_coldkey_ss58=bob_wallet.coldkey.ss58_address,
hotkey_ss58=alice_wallet.hotkey.ss58_address,
origin_netuid=1,
destination_netuid=2,
amount=alice_stakes[0].stake,
wait_for_inclusion=True,
wait_for_finalization=True,
)
alice_stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)
assert alice_stakes == []
bob_stakes = subtensor.get_stake_for_coldkey(bob_wallet.coldkey.ss58_address)
assert bob_stakes == [
StakeInfo(
hotkey_ss58=alice_wallet.hotkey.ss58_address,
coldkey_ss58=bob_wallet.coldkey.ss58_address,
netuid=2,
stake=ANY_BALANCE,
locked=Balance(0),
emission=ANY_BALANCE,
drain=0,
is_registered=False,
),
]