Skip to content

Commit 950d8d6

Browse files
authored
Merge pull request #3153 from opentensor/fix/roman/root-claim-e2e-tests
[v10] Bringing RootClam tests to life
2 parents a7d1d3f + 5f98297 commit 950d8d6

File tree

2 files changed

+178
-15
lines changed

2 files changed

+178
-15
lines changed

tests/e2e_tests/test_root_claim.py

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from bittensor.utils.balance import Balance
22
from bittensor.utils.btlogging import logging
3+
from tests.e2e_tests.utils.set_subnet_moving_price import (
4+
increase_subnet_ema,
5+
async_increase_subnet_ema,
6+
)
37
from tests.e2e_tests.utils import (
48
AdminUtils,
59
NETUID,
@@ -17,7 +21,6 @@
1721
PROOF_COUNTER = 2
1822

1923

20-
@pytest.mark.skip(reason="we need to figure out how to emulate behavior")
2124
def test_root_claim_swap(subtensor, alice_wallet, bob_wallet, charlie_wallet):
2225
"""Tests root claim Swap logic.
2326
@@ -51,6 +54,9 @@ def test_root_claim_swap(subtensor, alice_wallet, bob_wallet, charlie_wallet):
5154
]
5255
)
5356

57+
# Here is the damn magic with EMA
58+
assert increase_subnet_ema(subtensor=subtensor, sudo_wallet=alice_wallet)
59+
5460
stake_balance = Balance.from_tao(10)
5561

5662
# Stake to Alice in ROOT
@@ -112,7 +118,6 @@ def test_root_claim_swap(subtensor, alice_wallet, bob_wallet, charlie_wallet):
112118
proof_counter -= 1
113119

114120

115-
@pytest.mark.skip(reason="we need to figure out how to emulate behavior")
116121
@pytest.mark.asyncio
117122
async def test_root_claim_swap_async(
118123
async_subtensor, alice_wallet, bob_wallet, charlie_wallet
@@ -149,6 +154,11 @@ async def test_root_claim_swap_async(
149154
]
150155
)
151156

157+
# Here is the damn magic with EMA
158+
assert await async_increase_subnet_ema(
159+
subtensor=async_subtensor, sudo_wallet=alice_wallet
160+
)
161+
152162
stake_balance = Balance.from_tao(10)
153163

154164
# Stake to Alice in ROOT
@@ -214,7 +224,6 @@ async def test_root_claim_swap_async(
214224
proof_counter -= 1
215225

216226

217-
@pytest.mark.skip(reason="we need to figure out how to emulate behavior")
218227
def test_root_claim_keep_with_zero_num_root_auto_claims(
219228
subtensor, alice_wallet, bob_wallet, charlie_wallet
220229
):
@@ -280,6 +289,9 @@ def test_root_claim_keep_with_zero_num_root_auto_claims(
280289
== "Keep"
281290
)
282291

292+
# Here is the damn magic with EMA
293+
assert increase_subnet_ema(subtensor=subtensor, sudo_wallet=alice_wallet)
294+
283295
stake_balance = Balance.from_tao(1000) # just a dream - stake 1000 TAO to SN0 :D
284296

285297
# Stake from Charlie to Alice in ROOT
@@ -376,7 +388,6 @@ def test_root_claim_keep_with_zero_num_root_auto_claims(
376388
logging.console.info(f"SN2 Stake: {stake_after_charlie}")
377389

378390

379-
@pytest.mark.skip(reason="we need to figure out how to emulate behavior")
380391
@pytest.mark.asyncio
381392
async def test_root_claim_keep_with_zero_num_root_auto_claims_async(
382393
async_subtensor, alice_wallet, bob_wallet, charlie_wallet
@@ -445,6 +456,11 @@ async def test_root_claim_keep_with_zero_num_root_auto_claims_async(
445456
== "Keep"
446457
)
447458

459+
# Here is the damn magic with EMA
460+
assert await async_increase_subnet_ema(
461+
subtensor=async_subtensor, sudo_wallet=alice_wallet
462+
)
463+
448464
stake_balance = Balance.from_tao(1000) # just a dream - stake 1000 TAO to SN0 :D
449465

450466
# Stake from Charlie to Alice in ROOT
@@ -547,7 +563,6 @@ async def test_root_claim_keep_with_zero_num_root_auto_claims_async(
547563
logging.console.info(f"SN2 Stake: {stake_after_charlie}")
548564

549565

550-
@pytest.mark.skip(reason="we need to figure out how to emulate behavior")
551566
def test_root_claim_keep_with_random_auto_claims(
552567
subtensor, alice_wallet, bob_wallet, charlie_wallet, dave_wallet
553568
):
@@ -610,7 +625,10 @@ def test_root_claim_keep_with_random_auto_claims(
610625
== "Keep"
611626
)
612627

613-
stake_balance = Balance.from_tao(1000) # just a dream - stake 1000 TAO to SN0 :D
628+
# Set EMA to enable root_sell_flag
629+
assert increase_subnet_ema(subtensor=subtensor, sudo_wallet=alice_wallet)
630+
631+
stake_balance = Balance.from_tao(1000)
614632

615633
# Stake from Charlie to Alice in ROOT
616634
response = subtensor.staking.add_stake(
@@ -621,6 +639,13 @@ def test_root_claim_keep_with_random_auto_claims(
621639
)
622640
assert response.success, response.message
623641

642+
# Skip the epoch in which stake was installed. Emission doesn't occur in the same epoch as stake installation
643+
logging.console.info("Skipping stake epoch")
644+
next_epoch_start_block = subtensor.subnets.get_next_epoch_start_block(
645+
netuid=root_sn.netuid
646+
)
647+
subtensor.wait_for_block(next_epoch_start_block)
648+
624649
proof_counter = PROOF_COUNTER
625650

626651
prev_claimed_stake_charlie = subtensor.staking.get_stake(
@@ -635,8 +660,7 @@ def test_root_claim_keep_with_random_auto_claims(
635660
netuid=sn2.netuid,
636661
)
637662

638-
# proof that ROOT stake increases each epoch even RootClaimType is Keep bc of random auto claim takes min 5 coldkeys
639-
# to do release emissions
663+
# Wait for epochs and check that stake increases due to random auto claims
640664
while proof_counter > 0:
641665
next_epoch_start_block = subtensor.subnets.get_next_epoch_start_block(
642666
root_sn.netuid
@@ -649,21 +673,24 @@ def test_root_claim_keep_with_random_auto_claims(
649673
hotkey_ss58=alice_wallet.hotkey.ss58_address,
650674
netuid=sn2.netuid,
651675
)
652-
assert claimed_stake_charlie > prev_claimed_stake_charlie
676+
assert claimed_stake_charlie > prev_claimed_stake_charlie, (
677+
f"Stake did not increase: {claimed_stake_charlie} <= {prev_claimed_stake_charlie}"
678+
)
653679
prev_claimed_stake_charlie = claimed_stake_charlie
654680

655681
root_claimed_charlie = subtensor.staking.get_root_claimed(
656682
coldkey_ss58=charlie_wallet.coldkey.ss58_address,
657683
hotkey_ss58=alice_wallet.hotkey.ss58_address,
658684
netuid=sn2.netuid,
659685
)
660-
assert root_claimed_charlie > prev_root_claimed_charlie
686+
assert root_claimed_charlie > prev_root_claimed_charlie, (
687+
f"Root claimed did not increase: {root_claimed_charlie} <= {prev_root_claimed_charlie}"
688+
)
661689
prev_root_claimed_charlie = root_claimed_charlie
662690

663691
proof_counter -= 1
664692

665693

666-
@pytest.mark.skip(reason="we need to figure out how to emulate behavior")
667694
@pytest.mark.asyncio
668695
async def test_root_claim_keep_with_random_auto_claims_async(
669696
async_subtensor, alice_wallet, bob_wallet, charlie_wallet, dave_wallet
@@ -729,6 +756,11 @@ async def test_root_claim_keep_with_random_auto_claims_async(
729756
== "Keep"
730757
)
731758

759+
# Here is the damn magic with EMA
760+
assert await async_increase_subnet_ema(
761+
subtensor=async_subtensor, sudo_wallet=alice_wallet
762+
)
763+
732764
stake_balance = Balance.from_tao(1000) # just a dream - stake 1000 TAO to SN0 :D
733765

734766
# Stake from Charlie to Alice in ROOT
@@ -740,6 +772,13 @@ async def test_root_claim_keep_with_random_auto_claims_async(
740772
)
741773
assert response.success, response.message
742774

775+
# Skip the epoch in which stake was installed. Emission doesn't occur in the same epoch as stake installation
776+
logging.console.info("Skipping stake epoch")
777+
next_epoch_start_block = await async_subtensor.subnets.get_next_epoch_start_block(
778+
netuid=root_sn.netuid
779+
)
780+
await async_subtensor.wait_for_block(next_epoch_start_block)
781+
743782
proof_counter = PROOF_COUNTER
744783

745784
prev_claimed_stake_charlie = await async_subtensor.staking.get_stake(
@@ -754,8 +793,7 @@ async def test_root_claim_keep_with_random_auto_claims_async(
754793
netuid=sn2.netuid,
755794
)
756795

757-
# proof that ROOT stake increases each epoch even RootClaimType is Keep bc of random auto claim takes min 5 coldkeys
758-
# to do release emissions
796+
# Wait for epochs and check that stake increases due to random auto claims
759797
while proof_counter > 0:
760798
next_epoch_start_block = (
761799
await async_subtensor.subnets.get_next_epoch_start_block(root_sn.netuid)
@@ -768,15 +806,19 @@ async def test_root_claim_keep_with_random_auto_claims_async(
768806
hotkey_ss58=alice_wallet.hotkey.ss58_address,
769807
netuid=sn2.netuid,
770808
)
771-
assert claimed_stake_charlie > prev_claimed_stake_charlie
809+
assert claimed_stake_charlie > prev_claimed_stake_charlie, (
810+
f"Stake did not increase: {claimed_stake_charlie} <= {prev_claimed_stake_charlie}"
811+
)
772812
prev_claimed_stake_charlie = claimed_stake_charlie
773813

774814
root_claimed_charlie = await async_subtensor.staking.get_root_claimed(
775815
coldkey_ss58=charlie_wallet.coldkey.ss58_address,
776816
hotkey_ss58=alice_wallet.hotkey.ss58_address,
777817
netuid=sn2.netuid,
778818
)
779-
assert root_claimed_charlie > prev_root_claimed_charlie
819+
assert root_claimed_charlie > prev_root_claimed_charlie, (
820+
f"Root claimed did not increase: {root_claimed_charlie} <= {prev_root_claimed_charlie}"
821+
)
780822
prev_root_claimed_charlie = root_claimed_charlie
781823

782824
proof_counter -= 1
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from bittensor_wallet import Wallet
2+
3+
from bittensor.core.extrinsics.utils import sudo_call_extrinsic
4+
from bittensor.core.extrinsics.asyncex.utils import (
5+
sudo_call_extrinsic as async_sudo_call_extrinsic,
6+
)
7+
from bittensor.extras.subtensor_api import SubtensorApi
8+
9+
10+
def check_root_sell_flag(subtensor: "SubtensorApi") -> tuple[bool, int]:
11+
"""Checks the current state of root_sell_flag."""
12+
# Get all non-root subnets
13+
subnets = [
14+
netuid for netuid in subtensor.subnets.get_all_subnets_netuid() if netuid != 0
15+
]
16+
total_ema = 0
17+
for netuid in subnets:
18+
# Get moving_alpha_price through subnet's Dynamic info data
19+
moving_price = subtensor.subnets.subnet(netuid).moving_price
20+
total_ema += moving_price
21+
root_sell_flag = total_ema > 1.0
22+
return root_sell_flag, total_ema
23+
24+
25+
async def async_check_root_sell_flag(subtensor: "SubtensorApi") -> tuple[bool, int]:
26+
"""Checks the current state of root_sell_flag."""
27+
# Get all non-root subnets
28+
subnets = [
29+
netuid
30+
for netuid in await subtensor.subnets.get_all_subnets_netuid()
31+
if netuid != 0
32+
]
33+
total_ema = 0
34+
for netuid in subnets:
35+
# Get moving_alpha_price through subnet's Dynamic info data
36+
moving_price = (await subtensor.subnets.subnet(netuid)).moving_price
37+
total_ema += moving_price
38+
root_sell_flag = total_ema > 1.0
39+
return root_sell_flag, total_ema
40+
41+
42+
def set_huge_ema_before_test(subtensor: "SubtensorApi", sudo_wallet: "Wallet"):
43+
"""Sets huge EMA value into SubnetMovingPrice storage."""
44+
45+
encoded_storage_key = (
46+
"0x658faa385070e074c85bf6b568cf05551abf1b0f4fd14f7b72ee50f9d91d59150200"
47+
)
48+
encoded_storage_value = b"003665c4ffc99a3b0000000000000000"
49+
items = [encoded_storage_key, encoded_storage_value]
50+
51+
return sudo_call_extrinsic(
52+
subtensor=subtensor.inner_subtensor,
53+
wallet=sudo_wallet,
54+
call_function="set_storage",
55+
call_params={"items": [items]},
56+
call_module="System",
57+
)
58+
59+
60+
async def async_set_huge_ema_before_test(
61+
subtensor: "SubtensorApi", sudo_wallet: "Wallet"
62+
):
63+
"""Sets huge EMA value into SubnetMovingPrice storage."""
64+
65+
encoded_storage_key = (
66+
"0x658faa385070e074c85bf6b568cf05551abf1b0f4fd14f7b72ee50f9d91d59150200"
67+
)
68+
encoded_storage_value = b"003665c4ffc99a3b0000000000000000"
69+
items = [encoded_storage_key, encoded_storage_value]
70+
71+
return await async_sudo_call_extrinsic(
72+
subtensor=subtensor.inner_subtensor,
73+
wallet=sudo_wallet,
74+
call_function="set_storage",
75+
call_params={"items": [items]},
76+
call_module="System",
77+
)
78+
79+
80+
def increase_subnet_ema(subtensor: "SubtensorApi", sudo_wallet: "Wallet") -> bool:
81+
"""Increases EMA value in SubnetMovingPrice storage."""
82+
try:
83+
root_sell_flag, sns_sum_of_ema = check_root_sell_flag(subtensor)
84+
assert not root_sell_flag
85+
assert sns_sum_of_ema < 1.0
86+
87+
response = set_huge_ema_before_test(
88+
subtensor=subtensor, sudo_wallet=sudo_wallet
89+
)
90+
assert response.success, response.message
91+
subtensor.wait_for_block()
92+
93+
root_sell_flag, sns_sum_of_ema = check_root_sell_flag(subtensor)
94+
assert root_sell_flag, "Root sell still false"
95+
assert sns_sum_of_ema > 1.0, "SNs EMA sum wasn't increased greater than 1.0"
96+
return True
97+
except AssertionError:
98+
return False
99+
100+
101+
async def async_increase_subnet_ema(
102+
subtensor: "SubtensorApi", sudo_wallet: "Wallet"
103+
) -> bool:
104+
"""Increases EMA value in SubnetMovingPrice storage."""
105+
try:
106+
root_sell_flag, sns_sum_of_ema = await async_check_root_sell_flag(subtensor)
107+
assert not root_sell_flag
108+
assert sns_sum_of_ema < 1.0
109+
110+
response = await async_set_huge_ema_before_test(
111+
subtensor=subtensor, sudo_wallet=sudo_wallet
112+
)
113+
assert response.success, response.message
114+
await subtensor.wait_for_block()
115+
116+
root_sell_flag, sns_sum_of_ema = await async_check_root_sell_flag(subtensor)
117+
assert root_sell_flag, "Root sell still false"
118+
assert sns_sum_of_ema > 1.0, "SNs EMA sum wasn't increased greater than 1.0"
119+
return True
120+
except AssertionError:
121+
return False

0 commit comments

Comments
 (0)