Exemplo n.º 1
0
def test_not_enough_tokens():
    # Bob should not be able to send more tokens than he has
    #
    # Let's say Bob has 100 tokens. He should not be able to send more than
    # 100 tokens to Alice.
    #
    # If there's an incoming but unconfirmed deposit into Bob's account then Bob
    # should not be able to use the incoming tokens until the spend transaction
    # they are in is confirmed.
    test_settings = settings["test_not_enough_tokens"]
    coinbase_reward = common.coinbase_reward()
    (root_dir, bob_node, bob_api,
     bob_top) = setup_node_with_tokens(test_settings, "bob")
    bob_internal_api = common.internal_api(bob_node)

    def get_bob_balance(height):
        return common.get_account_balance_at_height(bob_api, bob_internal_api,
                                                    height)

    def get_alice_balance(height):
        k = test_settings["spend_tx"]["alice_pubkey"]
        return common.get_account_balance_at_height(bob_api,
                                                    bob_internal_api,
                                                    height,
                                                    pub_key=k)

    spend_tx_amt = test_settings["spend_tx"]["amount"]
    spend_tx_fee = test_settings["spend_tx"]["fee"]
    bob_balance = get_bob_balance(bob_top.height)
    bob_has_not_enough_tokens = bob_balance.balance < spend_tx_amt + spend_tx_fee
    assert_equals(bob_has_not_enough_tokens, True)
    print("Bob initial balance is " + str(bob_balance.balance) +
          " and he will try to spend " + str(spend_tx_amt + spend_tx_fee))

    alice_balance0 = get_alice_balance(bob_top.height)

    # Bob tries to send some tokens to Alice
    spend_tx_obj = SpendTx(
        recipient_pubkey=test_settings["spend_tx"]["alice_pubkey"],
        amount=spend_tx_amt,
        fee=spend_tx_fee)
    print("Bob's spend_tx is " + str(spend_tx_obj))
    bob_internal_api.post_spend_tx(spend_tx_obj)
    print("Transaction sent")
    bob_top_after_tx = bob_api.get_top()

    print("Waiting for a few blocks to be mined")
    checked_height = bob_top_after_tx.height + 3
    common.wait_until_height(bob_api, checked_height)

    # ensure Alice balance had not changed
    alice_balance1 = get_alice_balance(checked_height)
    print("Alice's balance is now " + str(alice_balance1.balance))
    assert_equals(alice_balance1.balance, alice_balance0.balance)

    # ensure Bob not debited
    # Since Bob had mined the block he is receiving the coinbase_reward
    blocks_mined = checked_height - bob_top.height
    print("Coinbase reward is " + str(coinbase_reward) + ", had mined " +
          str(blocks_mined) + " blocks")
    expected_balance = bob_balance.balance + coinbase_reward * blocks_mined
    bob_new_balance = get_bob_balance(checked_height)
    print("Bob's balance (with coinbase rewards) is now " +
          str(bob_new_balance.balance))
    assert_equals(bob_new_balance.balance, expected_balance)

    # stop node
    common.stop_node(bob_node)
    shutil.rmtree(root_dir)
Exemplo n.º 2
0
def test_send_by_name():
    # Bob registers a name 'bob.aet'
    # Alice should be able to send tokens to Bob using that name
    test_settings = settings["test_send_by_name"]
    coinbase_reward = common.coinbase_reward()
    (root_dir, node, ext_api,
     top) = setup_node_with_tokens(test_settings, "miner")
    int_api = common.internal_api(node)

    alice_private_key = keys.new_private()
    alice_public_key = keys.public_key(alice_private_key)
    alice_address = keys.address(alice_public_key)

    bob_private_key = keys.new_private()
    bob_public_key = keys.public_key(bob_private_key)
    bob_address = keys.address(bob_public_key)

    # initial balances - amounts that the miner should send them
    alice_init_balance = test_settings["send_tokens"]["alice"]
    bob_init_balance = test_settings["send_tokens"]["bob"]

    # populate accounts with tokens
    miner_send_tokens(alice_address, alice_init_balance, int_api, ext_api)
    miner_send_tokens(bob_address, bob_init_balance, int_api, ext_api)

    # validate balances
    alice_balance0 = common.get_account_balance(ext_api,
                                                int_api,
                                                pub_key=alice_address).balance
    bob_balance0 = common.get_account_balance(ext_api,
                                              int_api,
                                              pub_key=bob_address).balance

    assert_equals(alice_balance0, alice_init_balance)
    assert_equals(bob_balance0, bob_init_balance)
    print("Alice balance is " + str(alice_balance0))
    print("Bob balance is " + str(bob_balance0))
    print("Bob address is " + bob_address)

    bob_name = test_settings["name_register"]["name"]
    register_name(bob_name, bob_address, ext_api, bob_private_key)

    print("Bob has registered " + bob_name)
    bob_balance1 = common.get_account_balance(ext_api,
                                              int_api,
                                              pub_key=bob_address).balance
    print("Bob balance is " + str(bob_balance1))

    tokens_to_send = test_settings["spend_tx"]["amount"]
    print("Alice is about to send " + str(tokens_to_send) + " to " + bob_name)
    send_tokens_to_name(bob_name, tokens_to_send, alice_address,
                        alice_private_key, ext_api)

    # validate balances
    alice_balance2 = common.get_account_balance(ext_api,
                                                int_api,
                                                pub_key=alice_address).balance
    bob_balance2 = common.get_account_balance(ext_api,
                                              int_api,
                                              pub_key=bob_address).balance

    print("Alice balance is " + str(alice_balance2))
    print("Bob balance is " + str(bob_balance2))

    # Alice's balance should be decresed by the amount being send and the fee (1)
    assert_equals(alice_balance2, alice_balance0 - tokens_to_send - 1)

    # Bob's balance should be incresed by the amount being send
    assert_equals(bob_balance2, bob_balance1 + tokens_to_send)

    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Exemplo n.º 3
0
def test_successful():
    # Alice should be able to create a spend transaction to send tokens to
    # Bob. In a controlled environment, Alice should see her transaction
    # appear in the next block or two that are added to the blockchain.
    #
    # Once that happens, Alice should see her account debited and Bob's
    # account credited with the same number of tokens.
    #
    # The debit/credit should not happen until the transaction is confirmed,
    # e.g. included in at least one block in the chain.
    test_settings = settings["test_successful"]
    coinbase_reward = common.coinbase_reward()
    (root_dir, alice_node, alice_api,
     alice_top) = setup_node_with_tokens(test_settings, "alice")
    alice_internal_api = common.internal_api(alice_node)

    spend_tx_amt = test_settings["spend_tx"]["amount"]
    spend_tx_fee = test_settings["spend_tx"]["fee"]
    alice_balance = common.get_account_balance(alice_internal_api)
    alice_has_enough_tokens = alice_balance.balance >= spend_tx_amt + spend_tx_fee
    assert_equals(alice_has_enough_tokens, True)
    print("Alice initial balance is " + str(alice_balance.balance))

    bob_balance0 = common.get_account_balance(
        alice_internal_api, pub_key=test_settings["spend_tx"]["bob_pubkey"])
    # Alice sends some tokens to Bob
    alice_internal_api = common.internal_api(alice_node)
    spend_tx_obj = SpendTx(
        recipient_pubkey=test_settings["spend_tx"]["bob_pubkey"],
        amount=spend_tx_amt,
        fee=spend_tx_fee)
    print("Alice's spend_tx is " + str(spend_tx_obj))
    alice_internal_api.post_spend_tx(spend_tx_obj)
    print("Transaction sent")

    # ensure Alice balance had not changed
    alice_balance1 = common.get_account_balance(alice_internal_api)
    assert_equals(alice_balance.balance, alice_balance1.balance)

    # ensure Bob balance had not changed
    bob_balance1 = common.get_account_balance(
        alice_internal_api, pub_key=test_settings["spend_tx"]["bob_pubkey"])
    assert_equals(bob_balance1.balance, bob_balance0.balance)

    # wait for a block to be mined
    print("Waiting for a next block to be mined")
    common.wait_until_height(alice_api, alice_top.height + 1)

    alice_new_top = alice_api.get_top()
    alice_new_balance = common.get_account_balance(alice_internal_api)

    blocks_mined = alice_new_top.height - alice_top.height

    # Since Alice had mined the block she is receiving the fee and the
    # coinbase_reward
    # Alice should have
    # tokens = old_balance - spent_amt - spend_tx + spent_fee + coinbase_reward
    expected_balance = alice_balance.balance - spend_tx_amt + coinbase_reward * blocks_mined
    assert_equals(alice_new_balance.balance, expected_balance)

    bob_balance = common.get_account_balance(
        alice_internal_api, pub_key=test_settings["spend_tx"]["bob_pubkey"])
    print("Coinbase reward is " + str(coinbase_reward) + ", had mined " +
          str(blocks_mined) + " blocks")
    print("Alice's balance (with a coinbase reward) is now " +
          str(alice_new_balance.balance))
    print("Bob's balance is now " + str(bob_balance.balance))
    assert_equals(bob_balance.balance, spend_tx_amt)
    # stop node
    common.stop_node(alice_node)
    shutil.rmtree(root_dir)