|
3 | 3 | from bittensor.core.chain_data.stake_info import StakeInfo
|
4 | 4 | from bittensor.utils.balance import Balance
|
5 | 5 | from tests.e2e_tests.utils.chain_interactions import ANY_BALANCE
|
| 6 | +from bittensor import logging |
| 7 | + |
| 8 | +logging.enable_info() |
6 | 9 |
|
7 | 10 |
|
8 | 11 | def test_single_operation(subtensor, alice_wallet, bob_wallet):
|
@@ -245,3 +248,169 @@ def test_batch_operations(subtensor, alice_wallet, bob_wallet):
|
245 | 248 | bob_wallet.coldkey.ss58_address: Balance.from_tao(999_998),
|
246 | 249 | }
|
247 | 250 | assert balances[alice_wallet.coldkey.ss58_address] > alice_balance
|
| 251 | + |
| 252 | + |
| 253 | +def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet): |
| 254 | + """ |
| 255 | + Tests safe staking scenarios with different parameters. |
| 256 | +
|
| 257 | + For both staking and unstaking: |
| 258 | + 1. Fails with strict threshold (0.5%) and no partial staking |
| 259 | + 2. Succeeds with strict threshold (0.5%) and partial staking allowed |
| 260 | + 3. Succeeds with lenient threshold (10% and 30%) and no partial staking |
| 261 | + """ |
| 262 | + netuid = 2 |
| 263 | + # Register root as Alice - the subnet owner and validator |
| 264 | + assert subtensor.register_subnet(alice_wallet) |
| 265 | + |
| 266 | + # Verify subnet created successfully |
| 267 | + assert subtensor.subnet_exists(netuid), "Subnet wasn't created successfully" |
| 268 | + |
| 269 | + subtensor.burned_register( |
| 270 | + alice_wallet, |
| 271 | + netuid=netuid, |
| 272 | + wait_for_inclusion=True, |
| 273 | + wait_for_finalization=True, |
| 274 | + ) |
| 275 | + subtensor.burned_register( |
| 276 | + bob_wallet, |
| 277 | + netuid=netuid, |
| 278 | + wait_for_inclusion=True, |
| 279 | + wait_for_finalization=True, |
| 280 | + ) |
| 281 | + |
| 282 | + initial_stake = subtensor.get_stake( |
| 283 | + alice_wallet.coldkey.ss58_address, |
| 284 | + bob_wallet.hotkey.ss58_address, |
| 285 | + netuid=netuid, |
| 286 | + ) |
| 287 | + assert initial_stake == Balance(0) |
| 288 | + |
| 289 | + # Test Staking Scenarios |
| 290 | + stake_amount = Balance.from_tao(100) |
| 291 | + |
| 292 | + # 1. Strict params - should fail |
| 293 | + success = subtensor.add_stake( |
| 294 | + alice_wallet, |
| 295 | + bob_wallet.hotkey.ss58_address, |
| 296 | + netuid=netuid, |
| 297 | + amount=stake_amount, |
| 298 | + wait_for_inclusion=True, |
| 299 | + wait_for_finalization=True, |
| 300 | + safe_staking=True, |
| 301 | + rate_threshold=0.005, # 0.5% |
| 302 | + allow_partial_stake=False, |
| 303 | + ) |
| 304 | + assert success is False |
| 305 | + |
| 306 | + current_stake = subtensor.get_stake( |
| 307 | + alice_wallet.coldkey.ss58_address, |
| 308 | + bob_wallet.hotkey.ss58_address, |
| 309 | + netuid=netuid, |
| 310 | + ) |
| 311 | + assert current_stake == Balance(0), "Stake should not change after failed attempt" |
| 312 | + |
| 313 | + # 2. Partial allowed - should succeed partially |
| 314 | + success = subtensor.add_stake( |
| 315 | + alice_wallet, |
| 316 | + bob_wallet.hotkey.ss58_address, |
| 317 | + netuid=netuid, |
| 318 | + amount=stake_amount, |
| 319 | + wait_for_inclusion=True, |
| 320 | + wait_for_finalization=True, |
| 321 | + safe_staking=True, |
| 322 | + rate_threshold=0.005, # 0.5% |
| 323 | + allow_partial_stake=True, |
| 324 | + ) |
| 325 | + assert success is True |
| 326 | + |
| 327 | + partial_stake = subtensor.get_stake( |
| 328 | + alice_wallet.coldkey.ss58_address, |
| 329 | + bob_wallet.hotkey.ss58_address, |
| 330 | + netuid=netuid, |
| 331 | + ) |
| 332 | + assert partial_stake > Balance(0), "Partial stake should be added" |
| 333 | + assert ( |
| 334 | + partial_stake < stake_amount |
| 335 | + ), "Partial stake should be less than requested amount" |
| 336 | + |
| 337 | + # 3. Higher threshold - should succeed fully |
| 338 | + amount = Balance.from_tao(100) |
| 339 | + success = subtensor.add_stake( |
| 340 | + alice_wallet, |
| 341 | + bob_wallet.hotkey.ss58_address, |
| 342 | + netuid=netuid, |
| 343 | + amount=amount, |
| 344 | + wait_for_inclusion=True, |
| 345 | + wait_for_finalization=True, |
| 346 | + safe_staking=True, |
| 347 | + rate_threshold=0.1, # 10% |
| 348 | + allow_partial_stake=False, |
| 349 | + ) |
| 350 | + assert success is True |
| 351 | + |
| 352 | + full_stake = subtensor.get_stake( |
| 353 | + alice_wallet.coldkey.ss58_address, |
| 354 | + bob_wallet.hotkey.ss58_address, |
| 355 | + netuid=netuid, |
| 356 | + ) |
| 357 | + assert full_stake >= stake_amount, "Full stake amount should be added" |
| 358 | + |
| 359 | + # Test Unstaking Scenarios |
| 360 | + # 1. Strict params - should fail |
| 361 | + success = subtensor.unstake( |
| 362 | + alice_wallet, |
| 363 | + bob_wallet.hotkey.ss58_address, |
| 364 | + netuid=netuid, |
| 365 | + amount=stake_amount, |
| 366 | + wait_for_inclusion=True, |
| 367 | + wait_for_finalization=True, |
| 368 | + safe_staking=True, |
| 369 | + rate_threshold=0.005, # 0.5% |
| 370 | + allow_partial_stake=False, |
| 371 | + ) |
| 372 | + assert success is False |
| 373 | + |
| 374 | + current_stake = subtensor.get_stake( |
| 375 | + alice_wallet.coldkey.ss58_address, |
| 376 | + bob_wallet.hotkey.ss58_address, |
| 377 | + netuid=netuid, |
| 378 | + ) |
| 379 | + assert ( |
| 380 | + current_stake == full_stake |
| 381 | + ), "Stake should not change after failed unstake attempt" |
| 382 | + |
| 383 | + # 2. Partial allowed - should succeed partially |
| 384 | + success = subtensor.unstake( |
| 385 | + alice_wallet, |
| 386 | + bob_wallet.hotkey.ss58_address, |
| 387 | + netuid=netuid, |
| 388 | + amount=stake_amount, |
| 389 | + wait_for_inclusion=True, |
| 390 | + wait_for_finalization=True, |
| 391 | + safe_staking=True, |
| 392 | + rate_threshold=0.005, # 0.5% |
| 393 | + allow_partial_stake=True, |
| 394 | + ) |
| 395 | + assert success is True |
| 396 | + |
| 397 | + partial_unstake = subtensor.get_stake( |
| 398 | + alice_wallet.coldkey.ss58_address, |
| 399 | + bob_wallet.hotkey.ss58_address, |
| 400 | + netuid=netuid, |
| 401 | + ) |
| 402 | + assert partial_unstake > Balance(0), "Some stake should remain" |
| 403 | + |
| 404 | + # 3. Higher threshold - should succeed fully |
| 405 | + success = subtensor.unstake( |
| 406 | + alice_wallet, |
| 407 | + bob_wallet.hotkey.ss58_address, |
| 408 | + netuid=netuid, |
| 409 | + amount=partial_unstake, |
| 410 | + wait_for_inclusion=True, |
| 411 | + wait_for_finalization=True, |
| 412 | + safe_staking=True, |
| 413 | + rate_threshold=0.3, # 30% |
| 414 | + allow_partial_stake=False, |
| 415 | + ) |
| 416 | + assert success is True |
0 commit comments