|
| 1 | +import pytest |
| 2 | + |
| 3 | +from bittensor.core.chain_data.delegate_info import DelegateInfo, DelegatedInfo |
| 4 | +from bittensor.utils.balance import Balance |
| 5 | +from bittensor.utils.delegates_details import DelegatesDetails |
| 6 | +from tests.e2e_tests.utils.chain_interactions import ( |
| 7 | + decrease_take, |
| 8 | + increase_take, |
| 9 | + registry_set_identity, |
| 10 | + sudo_set_admin_utils, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +DEFAULT_DELEGATE_TAKE = 0.179995422293431 |
| 15 | + |
| 16 | + |
| 17 | +def test_identity(subtensor, alice_wallet, bob_wallet): |
| 18 | + """ |
| 19 | + Tests: |
| 20 | + - Check Delegate's default identity |
| 21 | + - Update Delegate's identity |
| 22 | + """ |
| 23 | + |
| 24 | + identities = subtensor.get_delegate_identities() |
| 25 | + |
| 26 | + assert alice_wallet.hotkey.ss58_address not in identities |
| 27 | + |
| 28 | + # Replace hotkey as it's the same as coldkey. |
| 29 | + # It's required to edit identity later. |
| 30 | + # Otherwise only subnet owner can do this. |
| 31 | + alice_wallet.set_hotkey( |
| 32 | + keypair=bob_wallet.hotkey, |
| 33 | + encrypt=False, |
| 34 | + overwrite=True, |
| 35 | + ) |
| 36 | + |
| 37 | + subtensor.root_register( |
| 38 | + alice_wallet, |
| 39 | + wait_for_inclusion=True, |
| 40 | + wait_for_finalization=True, |
| 41 | + ) |
| 42 | + |
| 43 | + identities = subtensor.get_delegate_identities() |
| 44 | + |
| 45 | + assert alice_wallet.hotkey.ss58_address not in identities |
| 46 | + |
| 47 | + success, error = registry_set_identity( |
| 48 | + subtensor, |
| 49 | + alice_wallet, |
| 50 | + alice_wallet.hotkey.ss58_address, |
| 51 | + display=b"Alice Display", |
| 52 | + web=b"https://bittensor.com/", |
| 53 | + ) |
| 54 | + |
| 55 | + assert error == "" |
| 56 | + assert success is True |
| 57 | + |
| 58 | + identities = subtensor.get_delegate_identities() |
| 59 | + |
| 60 | + assert alice_wallet.hotkey.ss58_address in identities |
| 61 | + |
| 62 | + alice_identity = identities[alice_wallet.hotkey.ss58_address] |
| 63 | + |
| 64 | + assert alice_identity == DelegatesDetails( |
| 65 | + additional=[], |
| 66 | + display="Alice Display", |
| 67 | + email="", |
| 68 | + image="", |
| 69 | + legal="", |
| 70 | + pgp_fingerprint=None, |
| 71 | + riot="", |
| 72 | + twitter="", |
| 73 | + web="https://bittensor.com/", |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def test_change_take(local_chain, subtensor, alice_wallet): |
| 78 | + """ |
| 79 | + Tests: |
| 80 | + - Get default Delegate's take once registered in root subnet |
| 81 | + - Increase and decreased Delegate's take |
| 82 | + - Try corner cases (increase/decrease beyond allowed min/max) |
| 83 | + """ |
| 84 | + |
| 85 | + success, error = decrease_take( |
| 86 | + subtensor, |
| 87 | + alice_wallet, |
| 88 | + 0.1, |
| 89 | + ) |
| 90 | + |
| 91 | + assert success is False |
| 92 | + assert "`HotKeyAccountNotExists(Module)`" in error |
| 93 | + |
| 94 | + subtensor.root_register( |
| 95 | + alice_wallet, |
| 96 | + wait_for_inclusion=True, |
| 97 | + wait_for_finalization=True, |
| 98 | + ) |
| 99 | + |
| 100 | + assert ( |
| 101 | + subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address) |
| 102 | + == DEFAULT_DELEGATE_TAKE |
| 103 | + ) |
| 104 | + |
| 105 | + success, error = increase_take( |
| 106 | + subtensor, |
| 107 | + alice_wallet, |
| 108 | + 0.5, |
| 109 | + ) |
| 110 | + |
| 111 | + assert success is False |
| 112 | + assert "`DelegateTakeTooHigh(Module)`" in error |
| 113 | + |
| 114 | + # increase_take but try to change from 0.18 to 0.1 |
| 115 | + success, error = increase_take( |
| 116 | + subtensor, |
| 117 | + alice_wallet, |
| 118 | + 0.1, |
| 119 | + ) |
| 120 | + |
| 121 | + assert "`DelegateTakeTooLow(Module)`" in error |
| 122 | + assert success is False |
| 123 | + |
| 124 | + success, error = decrease_take( |
| 125 | + subtensor, |
| 126 | + alice_wallet, |
| 127 | + 0.1, |
| 128 | + ) |
| 129 | + |
| 130 | + assert success is True |
| 131 | + assert error == "" |
| 132 | + |
| 133 | + take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address) |
| 134 | + |
| 135 | + assert take == 0.09999237048905166 |
| 136 | + |
| 137 | + success, error = increase_take( |
| 138 | + subtensor, |
| 139 | + alice_wallet, |
| 140 | + 0.15, |
| 141 | + ) |
| 142 | + |
| 143 | + assert success is False |
| 144 | + assert "`DelegateTxRateLimitExceeded(Module)`" in error |
| 145 | + |
| 146 | + take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address) |
| 147 | + |
| 148 | + assert take == 0.09999237048905166 |
| 149 | + |
| 150 | + sudo_set_admin_utils( |
| 151 | + local_chain, |
| 152 | + alice_wallet, |
| 153 | + call_function="sudo_set_tx_delegate_take_rate_limit", |
| 154 | + call_params={ |
| 155 | + "tx_rate_limit": 0, |
| 156 | + }, |
| 157 | + ) |
| 158 | + |
| 159 | + success, error = increase_take( |
| 160 | + subtensor, |
| 161 | + alice_wallet, |
| 162 | + 0.15, |
| 163 | + ) |
| 164 | + |
| 165 | + assert success is True |
| 166 | + assert error == "" |
| 167 | + |
| 168 | + take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address) |
| 169 | + |
| 170 | + assert take == 0.14999618524452582 |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.asyncio |
| 174 | +async def test_delegates(subtensor, alice_wallet, bob_wallet): |
| 175 | + """ |
| 176 | + Tests: |
| 177 | + - Check default Delegates |
| 178 | + - Register Delegates |
| 179 | + - Check if Hotkey is a Delegate |
| 180 | + - Nominator Staking |
| 181 | + """ |
| 182 | + |
| 183 | + assert subtensor.get_delegates() == [] |
| 184 | + assert subtensor.get_delegated(alice_wallet.coldkey.ss58_address) == [] |
| 185 | + assert subtensor.get_delegate_by_hotkey(alice_wallet.hotkey.ss58_address) is None |
| 186 | + assert subtensor.get_delegate_by_hotkey(bob_wallet.hotkey.ss58_address) is None |
| 187 | + |
| 188 | + assert subtensor.is_hotkey_delegate(alice_wallet.hotkey.ss58_address) is False |
| 189 | + assert subtensor.is_hotkey_delegate(bob_wallet.hotkey.ss58_address) is False |
| 190 | + |
| 191 | + subtensor.root_register( |
| 192 | + alice_wallet, |
| 193 | + wait_for_inclusion=True, |
| 194 | + wait_for_finalization=True, |
| 195 | + ) |
| 196 | + subtensor.root_register( |
| 197 | + bob_wallet, |
| 198 | + wait_for_inclusion=True, |
| 199 | + wait_for_finalization=True, |
| 200 | + ) |
| 201 | + |
| 202 | + assert subtensor.is_hotkey_delegate(alice_wallet.hotkey.ss58_address) is True |
| 203 | + assert subtensor.is_hotkey_delegate(bob_wallet.hotkey.ss58_address) is True |
| 204 | + |
| 205 | + alice_delegate = subtensor.get_delegate_by_hotkey(alice_wallet.hotkey.ss58_address) |
| 206 | + |
| 207 | + assert alice_delegate == DelegateInfo( |
| 208 | + hotkey_ss58=alice_wallet.hotkey.ss58_address, |
| 209 | + owner_ss58=alice_wallet.coldkey.ss58_address, |
| 210 | + take=DEFAULT_DELEGATE_TAKE, |
| 211 | + validator_permits=[], |
| 212 | + registrations=[0], |
| 213 | + return_per_1000=Balance(0), |
| 214 | + total_daily_return=Balance(0), |
| 215 | + total_stake={}, |
| 216 | + nominators={}, |
| 217 | + ) |
| 218 | + |
| 219 | + bob_delegate = subtensor.get_delegate_by_hotkey(bob_wallet.hotkey.ss58_address) |
| 220 | + |
| 221 | + assert bob_delegate == DelegateInfo( |
| 222 | + hotkey_ss58=bob_wallet.hotkey.ss58_address, |
| 223 | + owner_ss58=bob_wallet.coldkey.ss58_address, |
| 224 | + take=DEFAULT_DELEGATE_TAKE, |
| 225 | + validator_permits=[], |
| 226 | + registrations=[0], |
| 227 | + return_per_1000=Balance(0), |
| 228 | + total_daily_return=Balance(0), |
| 229 | + total_stake={}, |
| 230 | + nominators={}, |
| 231 | + ) |
| 232 | + |
| 233 | + delegates = subtensor.get_delegates() |
| 234 | + |
| 235 | + assert delegates == [ |
| 236 | + bob_delegate, |
| 237 | + alice_delegate, |
| 238 | + ] |
| 239 | + |
| 240 | + assert subtensor.get_delegated(bob_wallet.coldkey.ss58_address) == [] |
| 241 | + |
| 242 | + subtensor.add_stake( |
| 243 | + bob_wallet, |
| 244 | + alice_wallet.hotkey.ss58_address, |
| 245 | + netuid=0, |
| 246 | + amount=Balance.from_tao(10_000), |
| 247 | + wait_for_inclusion=True, |
| 248 | + wait_for_finalization=True, |
| 249 | + ) |
| 250 | + |
| 251 | + assert subtensor.get_delegated(bob_wallet.coldkey.ss58_address) == [ |
| 252 | + DelegatedInfo( |
| 253 | + hotkey_ss58=alice_wallet.hotkey.ss58_address, |
| 254 | + owner_ss58=alice_wallet.coldkey.ss58_address, |
| 255 | + take=DEFAULT_DELEGATE_TAKE, |
| 256 | + validator_permits=[], |
| 257 | + registrations=[0], |
| 258 | + return_per_1000=Balance(0), |
| 259 | + total_daily_return=Balance(0), |
| 260 | + netuid=0, |
| 261 | + stake=Balance.from_tao(9_999.99995), |
| 262 | + ), |
| 263 | + ] |
| 264 | + |
| 265 | + |
| 266 | +def test_nominator_min_required_stake(local_chain, subtensor, alice_wallet, bob_wallet): |
| 267 | + """ |
| 268 | + Tests: |
| 269 | + - Check default NominatorMinRequiredStake |
| 270 | + - Add Stake to Nominate |
| 271 | + - Update NominatorMinRequiredStake |
| 272 | + - Check Nominator is removed |
| 273 | + """ |
| 274 | + |
| 275 | + minimum_required_stake = subtensor.get_minimum_required_stake() |
| 276 | + |
| 277 | + assert minimum_required_stake == Balance(0) |
| 278 | + |
| 279 | + subtensor.root_register( |
| 280 | + alice_wallet, |
| 281 | + wait_for_inclusion=True, |
| 282 | + wait_for_finalization=True, |
| 283 | + ) |
| 284 | + subtensor.root_register( |
| 285 | + bob_wallet, |
| 286 | + wait_for_inclusion=True, |
| 287 | + wait_for_finalization=True, |
| 288 | + ) |
| 289 | + |
| 290 | + success = subtensor.add_stake( |
| 291 | + alice_wallet, |
| 292 | + bob_wallet.hotkey.ss58_address, |
| 293 | + netuid=0, |
| 294 | + amount=Balance.from_tao(10_000), |
| 295 | + wait_for_inclusion=True, |
| 296 | + wait_for_finalization=True, |
| 297 | + ) |
| 298 | + |
| 299 | + assert success is True |
| 300 | + |
| 301 | + stake = subtensor.get_stake( |
| 302 | + alice_wallet.coldkey.ss58_address, |
| 303 | + bob_wallet.hotkey.ss58_address, |
| 304 | + netuid=0, |
| 305 | + ) |
| 306 | + |
| 307 | + assert stake == Balance.from_tao(9_999.99995) |
| 308 | + |
| 309 | + # this will trigger clear_small_nominations |
| 310 | + sudo_set_admin_utils( |
| 311 | + local_chain, |
| 312 | + alice_wallet, |
| 313 | + call_function="sudo_set_nominator_min_required_stake", |
| 314 | + call_params={ |
| 315 | + "min_stake": "100000000000000", |
| 316 | + }, |
| 317 | + return_error_message=True, |
| 318 | + ) |
| 319 | + |
| 320 | + minimum_required_stake = subtensor.get_minimum_required_stake() |
| 321 | + |
| 322 | + assert minimum_required_stake == Balance.from_tao(100_000) |
| 323 | + |
| 324 | + stake = subtensor.get_stake( |
| 325 | + alice_wallet.coldkey.ss58_address, |
| 326 | + bob_wallet.hotkey.ss58_address, |
| 327 | + netuid=0, |
| 328 | + ) |
| 329 | + |
| 330 | + assert stake == Balance(0) |
0 commit comments