Beispiel #1
0
def test_spend():
    test_settings = settings["test_spend"]
    (root_dir, node, external_api,
     top) = setup_node_with_tokens(test_settings, "node")
    internal_api = common.internal_api(node)

    private_key = keys.new_private()
    public_key = keys.public_key(private_key)

    alice_address = keys.address(public_key)
    bob_address = test_settings["spend_tx"]["recipient"]

    test_settings["alice"]["pubkey"] = alice_address
    send_tokens_to_user("alice", test_settings, internal_api, external_api)

    spend_data_obj = SpendTx(sender=alice_address,
                             recipient_pubkey=bob_address,
                             amount=test_settings["spend_tx"]["amount"],
                             fee=test_settings["spend_tx"]["fee"])
    unsigned_spend_obj = external_api.post_spend(spend_data_obj)
    unsigned_spend_enc = unsigned_spend_obj.tx
    tx_hash = unsigned_spend_obj.tx_hash
    unsigned_tx = common.base58_decode(unsigned_spend_enc)
    unpacked_tx = common.unpack_tx(unsigned_tx)
    print("Tx " + str(common.parse_tx(unpacked_tx)))

    alice_balance0 = common.get_account_balance(internal_api,
                                                pub_key=alice_address).balance
    bob_balance0 = common.get_account_balance(internal_api,
                                              pub_key=bob_address).balance

    signed = keys.sign_verify_encode_tx(unsigned_tx, unpacked_tx, private_key,
                                        public_key)
    print("Signed transaction " + signed)
    print("Tx hash " + tx_hash)

    tx_object = Tx(tx=signed)
    external_api.post_tx(tx_object)

    top = external_api.get_top()
    common.wait_until_height(external_api, top.height + 3)
    alice_balance = common.get_account_balance(internal_api,
                                               pub_key=alice_address).balance
    bob_balance = common.get_account_balance(internal_api,
                                             pub_key=bob_address).balance
    tx = external_api.get_tx(
        tx_hash=tx_hash)  # it is there - either in mempool or in a block

    print("Alice balance now is " + str(alice_balance))
    print("Bob balance now is " + str(bob_balance))
    print("Balances are updated, transaction has been executed")

    assert_equals(
        alice_balance0, alice_balance + test_settings["spend_tx"]["fee"] +
        test_settings["spend_tx"]["amount"])

    assert_equals(bob_balance0,
                  bob_balance - test_settings["spend_tx"]["amount"])
    cleanup(node, root_dir)
Beispiel #2
0
def test_contract_create():
    test_settings = settings["test_contract_create"]
    (root_dir, node, external_api, top) = setup_node_with_tokens(test_settings, "node") 
    internal_api = common.internal_api(node)

    private_key = keys.new_private()
    public_key = keys.public_key(private_key)

    alice_address = keys.address(public_key)

    test_settings["alice"]["pubkey"] = alice_address
    send_tokens_to_user("alice", test_settings, internal_api, external_api)
    encoded_tx, contract_address = get_unsigned_contract_create(alice_address, test_settings["create_contract"], external_api)

    print("Unsigned encoded transaction: " + encoded_tx)
    print("Contract address: " + contract_address)
    unsigned_tx = common.base58_decode(encoded_tx)
    unpacked_tx = common.unpack_tx(unsigned_tx)
    tx = common.parse_tx(unpacked_tx)
    print("Unsigned decoded transaction: " + str(tx))

    # make sure same tx
    assert_equals(tx['type'], 'aect_create_tx')
    assert_equals(tx['owner'], common.base58_decode(test_settings["alice"]["pubkey"]))
    assert_equals(tx['vm_version'], test_settings["create_contract"]["vm_version"])
    assert_equals(tx['deposit'], test_settings["create_contract"]["deposit"])
    assert_equals(tx['amount'], test_settings["create_contract"]["amount"])
    assert_equals(tx['gas'], test_settings["create_contract"]["gas"])
    assert_equals(tx['gas_price'], test_settings["create_contract"]["gas_price"])
    assert_equals(tx['fee'], test_settings["create_contract"]["fee"])

    code = bytearray.fromhex(test_settings["create_contract"]["code"][2:]) # without 0x
    assert_equals(tx['code'], code)

    call_data = bytearray.fromhex(test_settings["create_contract"]["call_data"][2:]) # without 0x
    assert_equals(tx['call_data'], call_data)

    signed = keys.sign_verify_encode_tx(unsigned_tx, unpacked_tx, private_key, public_key)
    print("Signed transaction " + signed)

    alice_balance0 = common.get_account_balance(internal_api, pub_key=alice_address).balance
    tx_object = Tx(tx=signed)
    external_api.post_tx(tx_object)

    top = external_api.get_top()
    common.wait_until_height(external_api, top.height + 3)
    alice_balance = common.get_account_balance(internal_api, pub_key=alice_address).balance

    assert_equals(alice_balance0, alice_balance + test_settings["create_contract"]["fee"])
    print("Fee was consumed, transaction is part of the chain")

    cleanup(node, root_dir)
Beispiel #3
0
def test_contract_create():
    test_settings = settings["test_contract_create"]
    (root_dir, node, external_api, top) = setup_node_with_tokens(test_settings, "node") 
    internal_api = common.internal_api(node)

    send_tokens_to_user("alice", test_settings, internal_api, external_api)

    encoded_tx = get_unsigned_contract_create(test_settings["alice"]["pubkey"], test_settings["create_contract"], external_api)

    print("Unsigned encoded transaction: " + encoded_tx)
    unsigned_tx = common.base58_decode(encoded_tx)
    unpacked_tx = common.unpack_tx(unsigned_tx)
    tx = common.parse_tx(unpacked_tx)
    print("Unsigned decoded transaction: " + str(tx))

    # make sure same tx
    assert_equals(tx['type'], 'contract_create')
    assert_equals(tx['owner'], common.base58_decode(test_settings["alice"]["pubkey"]))
    assert_equals(tx['vm_version'], test_settings["create_contract"]["vm_version"])
    assert_equals(tx['deposit'], test_settings["create_contract"]["deposit"])
    assert_equals(tx['amount'], test_settings["create_contract"]["amount"])
    assert_equals(tx['gas'], test_settings["create_contract"]["gas"])
    assert_equals(tx['gas_price'], test_settings["create_contract"]["gas_price"])
    assert_equals(tx['fee'], test_settings["create_contract"]["fee"])

    code = bytearray.fromhex(test_settings["create_contract"]["code"][2:]) # without 0x
    assert_equals(tx['code'], code)

    call_data = bytearray.fromhex(test_settings["create_contract"]["call_data"][2:]) # without 0x
    assert_equals(tx['call_data'], call_data)

    signature = bytearray(list(map(int, test_settings["create_contract"]["signature"].split(","))))
    signed = common.encode_signed_tx(unpacked_tx, [signature]) 
    print("Signed transaction " + signed)

    alice_balance0 = common.get_account_balance(internal_api, pub_key=test_settings["alice"]["pubkey"]).balance
    tx_object = Tx(tx=signed)
    external_api.post_tx(tx_object)

    top = external_api.get_top()
    common.wait_until_height(external_api, top.height + 3)
    alice_balance = common.get_account_balance(internal_api, pub_key=test_settings["alice"]["pubkey"]).balance

    assert_equals(alice_balance0, alice_balance + test_settings["create_contract"]["fee"])

    cleanup(node, root_dir)
Beispiel #4
0
def test_contract_call():
    test_settings = settings["test_contract_call"]
    create_settings = settings["test_contract_create"]
    (root_dir, node, external_api, top) = setup_node_with_tokens(test_settings, "node") 
    internal_api = common.internal_api(node)

    private_key = keys.new_private()
    public_key = keys.public_key(private_key)

    alice_address = keys.address(public_key)

    test_settings["alice"]["pubkey"] = alice_address
    send_tokens_to_user("alice", test_settings, internal_api, external_api)

    ## create contract
    encoded_tx, contract_address = get_unsigned_contract_create(alice_address, create_settings["create_contract"], external_api)
    unsigned_tx = common.base58_decode(encoded_tx)
    unpacked_tx = common.unpack_tx(unsigned_tx)

    signed = keys.sign_verify_encode_tx(unsigned_tx, unpacked_tx, private_key, public_key)

    alice_balance0 = common.get_account_balance(internal_api, pub_key=alice_address).balance
    tx_object = Tx(tx=signed)
    external_api.post_tx(tx_object)

    top = external_api.get_top()
    common.wait_until_height(external_api, top.height + 3)
    alice_balance = common.get_account_balance(internal_api, pub_key=alice_address).balance

    # assert contract created:
    call_contract = test_settings["contract_call"]
    assert_equals(alice_balance0, alice_balance + create_settings["create_contract"]["fee"])

    call_input = ContractCallInput("ring", create_settings["create_contract"]["code"],\
                                           call_contract["data"]["function"],\
                                           call_contract["data"]["argument"])
    result = external_api.call_contract(call_input)
    contract_call_obj = ContractCallData(
        caller=test_settings["alice"]["pubkey"],
        contract=contract_address,
        vm_version=call_contract["vm_version"],
        fee=call_contract["fee"],
        amount=call_contract["amount"],
        gas=call_contract["gas"],
        gas_price=call_contract["gas_price"],
        call_data=result.out)


    call_tx_obj = external_api.post_contract_call(contract_call_obj)
    encoded_call_tx = call_tx_obj.tx

    print("Unsigned encoded transaction: " + encoded_call_tx)
    unsigned_call_tx = common.base58_decode(encoded_call_tx)
    unpacked_call_tx = common.unpack_tx(unsigned_call_tx)
    tx = common.parse_tx(unpacked_call_tx)
    print("Unsigned decoded transaction: " + str(tx))

    signed_call = keys.sign_verify_encode_tx(unsigned_call_tx, unpacked_call_tx, private_key, public_key)

    print("Signed transaction: " + signed_call)
    alice_balance0 = common.get_account_balance(internal_api, pub_key=alice_address).balance
    tx_object = Tx(tx=signed_call)
    external_api.post_tx(tx_object)

    top = external_api.get_top()
    common.wait_until_height(external_api, top.height + 3)
    alice_balance = common.get_account_balance(internal_api, pub_key=alice_address).balance

    # assert contract created:
    assert_equals(alice_balance0, alice_balance + test_settings["contract_call"]["fee"])
    print("Fee was consumed, transaction is part of the chain")

    cleanup(node, root_dir)
Beispiel #5
0
def sign_encode_tx(msgpacked_tx, private_key):
    unpacked_tx = common.unpack_tx(msgpacked_tx)
    signature = sign(msgpacked_tx, private_key)
    signed_encoded = common.encode_signed_tx(unpacked_tx, [bytearray(signature)]) 
    return signed_encoded
Beispiel #6
0
def test_spend():
    # 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.

    # Setup
    test_settings = settings["test_spend"]
    (root_dir, node, external_api, top) = setup_node_with_tokens(test_settings, "node") 
    internal_api = common.internal_api(node)

    private_key = keys.new_private()
    public_key = keys.public_key(private_key)

    alice_address = keys.address(public_key)
    bob_address = test_settings["spend_tx"]["recipient"]

    test_settings["alice"]["pubkey"] = alice_address
    send_tokens_to_user("alice", test_settings, internal_api)

    alice_balance0 = common.get_account_balance(internal_api, pub_key=alice_address).balance
    bob_balance0 = common.get_account_balance(internal_api, pub_key=bob_address).balance

    # Alice creates spend tx
    spend_data_obj = SpendTx(
            sender=alice_address,
            recipient_pubkey=bob_address,
            amount=test_settings["spend_tx"]["amount"],
            fee=test_settings["spend_tx"]["fee"])
    unsigned_spend_obj = external_api.post_spend(spend_data_obj)
    unsigned_spend_enc = unsigned_spend_obj.tx
    tx_hash = unsigned_spend_obj.tx_hash
    unsigned_tx = common.base58_decode(unsigned_spend_enc)
    unpacked_tx = common.unpack_tx(unsigned_tx)
    print("Tx " + str(common.parse_tx(unpacked_tx)))
    print("Tx hash " + tx_hash)

    # Alice signs spend tx
    signed = keys.sign_verify_encode_tx(unsigned_tx, unpacked_tx, private_key, public_key)
    print("Signed transaction " + signed)

    # Alice posts spend tx
    tx_object = Tx(tx=signed)
    external_api.post_tx(tx_object)
    _ = external_api.get_tx(tx_hash=tx_hash) # it is there - either in mempool or in a block

    # Wait until spend tx is mined
    wait(lambda: common.get_account_balance(internal_api, pub_key=alice_address).balance < alice_balance0,
         timeout_seconds=120, sleep_seconds=0.25)
    _ = external_api.get_tx(tx_hash=tx_hash) # it is there - shall be in a block
    print("Tx in chain ")

    # Check that Alice was debited and Bob was credited
    alice_balance = common.get_account_balance(internal_api, pub_key=alice_address).balance
    bob_balance = common.get_account_balance(internal_api, pub_key=bob_address).balance
    print("Alice balance now is " + str(alice_balance))
    print("Bob balance now is " + str(bob_balance))
    assert_equals(alice_balance0, alice_balance + test_settings["spend_tx"]["fee"] + test_settings["spend_tx"]["amount"])
    assert_equals(bob_balance0, bob_balance - test_settings["spend_tx"]["amount"])

    # Cleanup
    cleanup(node, root_dir)
Beispiel #7
0
def test_contract_call():
    test_settings = settings["test_contract_call"]
    create_settings = settings["test_contract_create"]
    (root_dir, node, external_api, top) = setup_node_with_tokens(test_settings, "node") 
    internal_api = common.internal_api(node)

    send_tokens_to_user("alice", test_settings, internal_api, external_api)

    ## create contract
    encoded_tx = get_unsigned_contract_create(test_settings["alice"]["pubkey"], create_settings["create_contract"], external_api)
    unsigned_tx = common.base58_decode(encoded_tx)
    unpacked_tx = common.unpack_tx(unsigned_tx)
    signature = bytearray(list(map(int, create_settings["create_contract"]["signature"].split(","))))
    signed = common.encode_signed_tx(unpacked_tx,[signature]) 

    alice_balance0 = common.get_account_balance(internal_api, pub_key=test_settings["alice"]["pubkey"]).balance
    tx_object = Tx(tx=signed)
    external_api.post_tx(tx_object)

    top = external_api.get_top()
    common.wait_until_height(external_api, top.height + 3)
    alice_balance = common.get_account_balance(internal_api, pub_key=test_settings["alice"]["pubkey"]).balance

    # assert contract created:
    call_contract = test_settings["contract_call"]
    assert_equals(alice_balance0, alice_balance + create_settings["create_contract"]["fee"])

    call_input = ContractCallInput("ring", create_settings["create_contract"]["code"],\
                                           call_contract["data"]["function"],\
                                           call_contract["data"]["argument"])
    result = external_api.call_contract(call_input)
    contract_call_obj = ContractCallData(
        caller=test_settings["alice"]["pubkey"],
        contract=call_contract["contract"],
        vm_version=call_contract["vm_version"],
        fee=call_contract["fee"],
        amount=call_contract["amount"],
        gas=call_contract["gas"],
        gas_price=call_contract["gas_price"],
        call_data=result.out)


    call_tx_obj = external_api.post_contract_call(contract_call_obj)
    encoded_call_tx = call_tx_obj.tx

    print("Unsigned encoded transaction: " + encoded_call_tx)
    unsigned_call_tx = common.base58_decode(encoded_call_tx)
    unpacked_call_tx = common.unpack_tx(unsigned_call_tx)
    tx = common.parse_tx(unpacked_call_tx)
    print("Unsigned decoded transaction: " + str(tx))

    signature = bytearray(list(map(int, test_settings["contract_call"]["signature"].split(","))))

    signed = common.encode_signed_tx(unpacked_call_tx,[signature]) 

    print("Signed transaction: " + signed)
    alice_balance0 = common.get_account_balance(internal_api, pub_key=test_settings["alice"]["pubkey"]).balance
    tx_object = Tx(tx=signed)
    external_api.post_tx(tx_object)

    top = external_api.get_top()
    common.wait_until_height(external_api, top.height + 3)
    alice_balance = common.get_account_balance(internal_api, pub_key=test_settings["alice"]["pubkey"]).balance

    print("BALANCE0 " + str(alice_balance0))
    print("BALANCE " + str(alice_balance))
    # assert contract created:
    assert_equals(alice_balance0, alice_balance + test_settings["contract_call"]["fee"])



    cleanup(node, root_dir)