Ejemplo n.º 1
0
def generate_wallet(coldkey_pair: 'Keypair' = None,
                    hotkey_pair: 'Keypair' = None):
    if not coldkey_pair:
        coldkey_pair = Keypair.create_from_mnemonic(
            Keypair.generate_mnemonic())

    if not hotkey_pair:
        hotkey_pair = Keypair.create_from_mnemonic(Keypair.generate_mnemonic())

    return WalletStub(coldkey_pair=coldkey_pair, hotkey_pair=hotkey_pair)
Ejemplo n.º 2
0
    def test_sign_and_verify_ed25519(self):
        mnemonic = Keypair.generate_mnemonic()
        keypair = Keypair.create_from_mnemonic(mnemonic,
                                               crypto_type=KeypairType.ED25519)
        signature = keypair.sign("Test123")

        self.assertTrue(keypair.verify("Test123", signature))
Ejemplo n.º 3
0
def test_unstake_success(setup_chain):
    coldkeypair = Keypair.create_from_uri('//Alice')
    hotkey_pair = Keypair.create_from_mnemonic(Keypair.generate_mnemonic())
    wallet = generate_wallet(coldkey_pair=coldkeypair, hotkey_pair=hotkey_pair)

    subtensor = connect(setup_chain)

    subscribe(subtensor, wallet)

    # Get the balance for the cold key, we use this for later comparison
    balance = subtensor.get_balance(coldkeypair.public_key)

    add_stake(subtensor, wallet, Balance(4000))

    result = subtensor.unstake(amount=Balance(3000),
                               wallet=wallet,
                               hotkey_id=hotkey_pair.public_key,
                               wait_for_finalization=True,
                               timeout=30)
    assert result is True

    # We have staked 4000, but unstaked 3000, so the balance should be 1000 less than before the staking operation
    new_balance = subtensor.get_balance(coldkeypair.ss58_address)
    assert int(new_balance) == int(balance) - 1000

    uid = subtensor.get_uid_for_pubkey(hotkey_pair.public_key)
    stake = subtensor.get_stake_for_uid(uid)

    # When staked, this node will receive the full block reward.
    # We need to ignore this effect, hence the mod operator
    assert int(stake) % BLOCK_REWARD == 1000
Ejemplo n.º 4
0
def test_add_stake_success(setup_chain):
    coldkeypair = Keypair.create_from_uri("//Alice")
    hotkeypair = Keypair.create_from_mnemonic(Keypair.generate_mnemonic())

    wallet = generate_wallet(coldkey_pair=coldkeypair, hotkey_pair=hotkeypair)
    subtensor = connect(setup_chain)

    # Subscibe the hotkey using Alice's cold key, which has TAO
    subscribe(subtensor, wallet)

    uid = subtensor.get_uid_for_pubkey(hotkeypair.public_key)
    assert uid is not None

    result = subtensor.get_stake_for_uid(uid)
    assert int(result) == int(Balance(0))

    # Get balance
    balance_pre = subtensor.get_balance(coldkeypair.ss58_address)

    # Timeout is 30, because 3 * blocktime does not work.
    result = subtensor.add_stake(wallet,
                                 Balance(4000),
                                 hotkeypair.public_key,
                                 wait_for_finalization=True,
                                 timeout=30)
    assert result == True

    # Check if the amount of stake end up in the hotkey account
    result = subtensor.get_stake_for_uid(uid)
    assert int(result) == int(Balance(4000))

    # Check if the balances had reduced by the amount of stake
    balance_post = subtensor.get_balance(coldkeypair.ss58_address)
    assert int(balance_post) == int(balance_pre) - 4000
Ejemplo n.º 5
0
    def test_sign_and_verify_scale_bytes(self):
        mnemonic = Keypair.generate_mnemonic()
        keypair = Keypair.create_from_mnemonic(mnemonic)

        data = ScaleBytes('0x1234')

        signature = keypair.sign(data)
        self.assertTrue(keypair.verify(data, signature))
Ejemplo n.º 6
0
def test_get_stake_for_uid___has_no_stake(setup_chain):
    hotkeypair = Keypair.create_from_mnemonic(Keypair.generate_mnemonic())
    coldkeypair = Keypair.create_from_uri('//Alice')

    wallet = generate_wallet(coldkey_pair=coldkeypair, hotkey_pair=hotkeypair)
    subtensor = connect(setup_chain)

    subscribe(subtensor, wallet)
    uid = subtensor.get_uid_for_pubkey(hotkeypair.public_key)

    result = subtensor.get_stake_for_uid(uid)
    assert int(result) == 0
Ejemplo n.º 7
0
def test_unstake_success(setup_chain):
    coldkeypair = Keypair.create_from_uri('//Alice')
    hotkey_pair = Keypair.create_from_mnemonic(Keypair.generate_mnemonic())
    wallet = generate_wallet(coldkey_pair=coldkeypair, hotkey_pair=hotkey_pair)

    subtensor = connect(setup_chain)
    subtensor.is_connected()

    subscribe(subtensor, wallet)

    # Get the balance for the cold key, we use this for later comparison
    balance_pre = int(subtensor.get_balance(coldkeypair.public_key))

    add_stake(subtensor, wallet, Balance(4000))

    # Determine the cost of the add_stake transaction
    balance_post = int(subtensor.get_balance(coldkeypair.public_key))
    transaction_fee_add_stake = balance_pre - balance_post - 4000

    logger.error("Trans_fee add_stake: {}", transaction_fee_add_stake)

    # unstake incurs a transaction fee that is added to the block reward
    result = subtensor.unstake(amount=Balance(3000),
                               wallet=wallet,
                               hotkey_id=hotkey_pair.public_key,
                               wait_for_finalization=True,
                               timeout=30)
    assert result is True

    transaction_fee_unstake = balance_post - int(
        subtensor.get_balance(coldkeypair.public_key)) + 3000
    logger.error("Trans_fee add_stake: {}", transaction_fee_unstake)

    assert int(transaction_fee_unstake) == TRANSACTION_FEE_UNSTAKE

    # At this point, the unstake transaction fee is in the transaction_fee_pool, and will make it into the block
    # reward the next block. However, in order to get this reward into the hotkey account of the neuron,
    # and emit needs to take place. This is why the expectation does not include the unstake transaction fee

    uid = subtensor.get_uid_for_pubkey(hotkey_pair.public_key)
    stake = subtensor.get_stake_for_uid(uid)
    expectation = 1000 + (3 * BLOCK_REWARD) + TRANSACTION_FEE_ADD_STAKE

    assert int(stake) == expectation
Ejemplo n.º 8
0
 def test_sign_and_verify_hex_data(self):
     mnemonic = Keypair.generate_mnemonic()
     keypair = Keypair.create_from_mnemonic(mnemonic)
     signature = keypair.sign("0x1234")
     self.assertTrue(keypair.verify("0x1234", signature))
Ejemplo n.º 9
0
 def test_generate_mnemonic(self):
     mnemonic = Keypair.generate_mnemonic()
     self.assertTrue(bip39_validate(mnemonic))
Ejemplo n.º 10
0
 def test_sign_and_verify_invalid_signature_ed25519(self):
     mnemonic = Keypair.generate_mnemonic()
     keypair = Keypair.create_from_mnemonic(mnemonic,
                                            crypto_type=KeypairType.ED25519)
     signature = "0x4c291bfb0bb9c1274e86d4b666d13b2ac99a0bacc04a4846fb8ea50bda114677f83c1f164af58fc184451e5140cc8160c4de626163b11451d3bbb208a1889f8a"
     self.assertFalse(keypair.verify("Test123", signature))
Ejemplo n.º 11
0
 def test_sign_and_verify_invalid_message(self):
     mnemonic = Keypair.generate_mnemonic()
     keypair = Keypair.create_from_mnemonic(mnemonic)
     signature = keypair.sign("Test123")
     self.assertFalse(keypair.verify("OtherMessage", signature))
Ejemplo n.º 12
0
 def test_sign_and_verify_invalid_signature(self):
     mnemonic = Keypair.generate_mnemonic()
     keypair = Keypair.create_from_mnemonic(mnemonic)
     signature = "Test"
     self.assertRaises(TypeError, keypair.verify, "Test123", signature)