def test_hdkd_path_gt_32_bytes(self): derivation_address = '5GR5pfZeNs1uQiSWVxZaQiZou3wdZiX894eqgvfNfHbEh7W2' derivation_path = '//PathNameLongerThan32BytesWhichShouldBeHashed' derived_keypair = Keypair.create_from_uri(derivation_path) self.assertEqual(derivation_address, derived_keypair.ss58_address)
async def test_get_current_block(setup_chain): keypair = Keypair.create_from_uri("//Alice") client = connect(keypair, setup_chain) await client.is_connected() result = await client.get_current_block() assert result >= 0
def test_hdkd_nested_soft_hard_path(self): derivation_address = '5Cwc8tShrshDJUp1P1M21dKUTcYQpV9GcfSa4hUBNmMdV3Cx' derivation_path = '/Bob//test' derived_keypair = Keypair.create_from_uri(derivation_path) self.assertEqual(derivation_address, derived_keypair.ss58_address)
def test_hdkd_nested_hard_soft_path(self): derivation_address = '5CJGwWiKXSE16WJaxBdPZhWqUYkotgenLUALv7ZvqQ4TXeqf' derivation_path = '//Bob/test' derived_keypair = Keypair.create_from_uri(derivation_path) self.assertEqual(derivation_address, derived_keypair.ss58_address)
def test_hdkd_default_to_dev_mnemonic(self): derivation_address = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY' derivation_path = '//Alice' derived_keypair = Keypair.create_from_uri(derivation_path) self.assertEqual(derivation_address, derived_keypair.ss58_address)
async def test_get_balance_success(setup_chain): hotkeypair = Keypair.create_from_uri("//Alice") client = connect(hotkeypair, setup_chain) await client.is_connected() result = await client.get_balance(hotkeypair.ss58_address) assert int(result) == pow(10, 9)
def test_hdkd_soft_path(self): mnemonic = 'old leopard transfer rib spatial phone calm indicate online fire caution review' derivation_address = '5GNXbA46ma5dg19GXdiKi5JH3mnkZ8Yea3bBtZAvj7t99P9i' derivation_path = '/Alice' derived_keypair = Keypair.create_from_uri(mnemonic + derivation_path) self.assertEqual(derivation_address, derived_keypair.ss58_address)
async def test_connect_success(): socket = "localhost:9944" keypair = Keypair.create_from_uri('//Alice') client = WSClient(socket, keypair) client.connect() result = await client.is_connected() assert result == True
async def test_connect_failed(): socket = 'localhost:9999' keypair = Keypair.create_from_uri('//Alice') client = WSClient(socket, keypair) client.connect() result = await client.is_connected() assert result == False
def test_hdkd_hard_path(self): mnemonic = 'old leopard transfer rib spatial phone calm indicate online fire caution review' derivation_address = '5FEiH8iuDUw271xbqWTWuB6WrDjv5dnCeDX1CyHubAniXDNN' derivation_path = '//Alice' derived_keypair = Keypair.create_from_uri(mnemonic + derivation_path) self.assertEqual(derivation_address, derived_keypair.ss58_address)
async def test_transfer_success(setup_chain): coldkey_alice = Keypair.create_from_uri("//Alice") coldkey_bob = Keypair.create_from_uri("//Bob") client = connect(coldkey_alice, setup_chain) await client.is_connected() balance_alice = await client.get_balance(coldkey_alice.ss58_address) balance_bob = await client.get_balance(coldkey_bob.ss58_address) result = await client.transfer(coldkey_bob.public_key, Balance(pow(10, 4))) assert result is not None assert 'extrinsic_hash' in result # Wait until extrinsic is processed await asyncio.sleep(10) balance_alice_new = await client.get_balance(coldkey_alice.ss58_address) balance_bob_new = await client.get_balance(coldkey_bob.ss58_address) assert balance_alice_new < balance_alice assert balance_bob_new > balance_bob
async def test_subscribe_success(setup_chain): hotkeypair = generate_keypair() client = connect(hotkeypair, setup_chain) coldkeypair = Keypair.create_from_uri("//Alice") await client.is_connected() await client.subscribe("8.8.8.8", 6666, 0, coldkeypair.public_key) await asyncio.sleep( 10 ) # Sleep for at least one block to give extrinsic the chance to be put onto the blockchain uid = await client.get_uid_for_pubkey(hotkeypair.public_key) assert uid is not None
async def test_get_stake_for_uid___has_no_stake(setup_chain): hotkeyp = generate_keypair() coldkeyp = Keypair.create_from_uri("//Alice") client = connect(hotkeyp, setup_chain) await client.is_connected() await client.subscribe("8.8.8.8", 667, 0, coldkeyp.public_key) await asyncio.sleep(10) uid = await client.get_uid_for_pubkey(hotkeyp.public_key) result = await client.get_stake_for_uid(uid) assert int(result) == 0
async def test_set_weights_success(setup_chain): hotkeypair_alice = Keypair.create_from_uri("//Alice") hotkeypair_bob = Keypair.create_from_uri("//Bob") coldkeypair = generate_keypair() client_alice = connect(hotkeypair_alice, setup_chain) client_bob = connect(hotkeypair_bob, setup_chain) await client_alice.is_connected() await client_bob.is_connected() # Subscribe both alice and bob await client_alice.subscribe("8.8.8.8", 666, 0, coldkeypair.public_key) await client_bob.subscribe("8.8.8.8", 666, 0, coldkeypair.public_key) await asyncio.sleep(10) alice_uid = await client_alice.get_uid_for_pubkey( hotkeypair_alice.public_key) bob_uid = await client_bob.get_uid_for_pubkey(hotkeypair_bob.public_key) w_uids = [alice_uid, bob_uid] w_vals = [pow(2, 31) - 1, pow(2, 31) - 1] # 50/50 distro result = await client_alice.set_weights(w_uids, w_vals, wait_for_inclusion=False) assert result is not None assert "extrinsic_hash" in result await asyncio.sleep(10) result = await client_alice.weight_uids_for_uid(alice_uid) assert result == w_uids result = await client_alice.weight_vals_for_uid(alice_uid) assert result == w_vals
async def test_get_active(setup_chain): keypair = Keypair.create_from_uri("//Alice") client = connect(keypair, setup_chain) await client.is_connected() # Subscribe at least one await client.subscribe("8.8.8.8", 666, 0, keypair.public_key) await asyncio.sleep(10) result = await client.get_active() assert isinstance(result, List) assert len(result) > 0 elem = result[0] assert isinstance(elem[0], str) assert elem[0][:2] == "0x" assert len(elem[0][2:]) == 64 assert isinstance(elem[1], int)
async def test_add_stake_success(setup_chain): coldkeypair = Keypair.create_from_uri("//Alice") hotkeypair = generate_keypair() client = connect(hotkeypair, setup_chain) await client.is_connected() # Subscribe a new neuron with the hotkey await client.subscribe("8.8.8.8", 6666, 0, coldkeypair.public_key) await asyncio.sleep(10) # Now switch the connection the use the coldkey client = connect(coldkeypair, setup_chain) await client.is_connected() # Get the uid of the new neuron uid = await client.get_uid_for_pubkey(hotkeypair.public_key) assert uid is not None # Get the amount of stake, should be 0 result = await client.get_stake_for_uid(uid) assert int(result) == int(Balance(0)) # Add stake to new neuron result = await client.add_stake(Balance(4000), hotkeypair.public_key) assert result is not None assert 'extrinsic_hash' in result # Wait for the extrinsic to complete await asyncio.sleep(10) # Get the amount of stake result = await client.get_stake_for_uid(uid) assert int(result) == int(Balance(4000))
async def test_connect_success(setup_chain): logger.error(setup_chain) hotkeypair = Keypair.create_from_uri("//Alice") client = connect(hotkeypair, setup_chain) result = await client.is_connected() assert result is True
from bittensor.subtensor.client import WSClient from bittensor.subtensor.interface import Keypair from loguru import logger import pytest import asyncio logger.remove() # Shut up loguru socket = "localhost:9944" keypair = Keypair.create_from_uri('//Alice') client = WSClient(socket, keypair) @pytest.mark.asyncio async def test_subscribe(): client.connect() await client.is_connected() await client.subscribe("127.0.0.1", 666, 0, keypair.public_key) await asyncio.sleep(10) uid = await client.get_uid_for_pubkey(keypair.public_key) assert uid is not None @pytest.mark.asyncio async def get_uid_for_pubkey__does_not_exist(): client.connect() await client.is_connected() random = Keypair.create_from_mnemonic(Keypair.generate_mnemonic()) uid = await client.get_uid_for_pubkey(random.public_key)
async def test_unstake_success(setup_chain): coldkeypair = Keypair.create_from_uri("//Alice") hotkeypair = generate_keypair() hotkey_client = connect(hotkeypair, setup_chain) await hotkey_client.is_connected() # Subscribe a new neuron with the hotkey await hotkey_client.subscribe("8.8.8.8", 6666, 0, coldkeypair.public_key) await asyncio.sleep(10) # Now switch the connection the use the coldkey coldkey_client = connect(coldkeypair, setup_chain) await coldkey_client.is_connected() # Get the uid of the new neuron uid = await coldkey_client.get_uid_for_pubkey(hotkeypair.public_key) logger.error(uid) assert uid is not None # Get the amount of stake, should be 0 result = await coldkey_client.get_stake_for_uid(uid) assert int(result) == int(Balance(0)) # Get the balance for the cold key, we use this for later comparison balance = await coldkey_client.get_balance(coldkeypair.public_key) # Add stake to new neuron result = await coldkey_client.add_stake(Balance(4000), hotkeypair.public_key) logger.info(result) assert result is not None assert 'extrinsic_hash' in result # Wait for the extrinsic to complete await asyncio.sleep(10) # Get current balance, should be 4000 less than first balance result = await coldkey_client.get_balance(coldkeypair.ss58_address) assert int(result) == int(balance) - 4000 # Get the amount of stake, should be 4000 result = await coldkey_client.get_stake_for_uid(uid) assert int(result) == int(Balance(4000)) # Now do the actual unstake # Reconnect with coldkey account coldkey_client = connect(coldkeypair, setup_chain) await coldkey_client.is_connected() # Do unstake result = await coldkey_client.unstake(Balance(4000), hotkeypair.public_key) assert result is not None assert 'extrinsic_hash' in result await asyncio.sleep(10) # Check if balance is the same as what we started with new_balance = await coldkey_client.get_balance(coldkeypair.ss58_address) assert int(new_balance) == int(balance)