Beispiel #1
0
def get_unsigned_contract_create(owner_id, contract, external_api, internal_api):
    bytecode = read_id_contract(internal_api)
    call_input = ContractCallInput("sophia",
                                   bytecode,
                                   contract["function"],
                                   contract["argument"])
    print("Call input:", call_input)
    result = internal_api.encode_calldata(call_input)
    call_data = result.calldata

    print("OWNERID", owner_id)
    contract_create_tx_obj = ContractCreateTx(
        owner_id=owner_id,
        code=bytecode,
        vm_version=contract["vm_version"],
        abi_version=contract["abi_version"],
        deposit=contract["deposit"],
        amount=contract["amount"],
        gas=contract["gas"],
        gas_price=contract["gas_price"],
        fee=contract["fee"],
        ttl=100,
        call_data=call_data)
    tx_obj = internal_api.post_contract_create(contract_create_tx_obj)
    return (tx_obj.tx, tx_obj.contract_id)
Beispiel #2
0
def test_contract_on_chain_call_off_chain():
    test_settings = settings["test_contract_call"]
    create_settings = settings["test_contract_create"]
    beneficiary = common.setup_beneficiary()
    (node, (root_dir, external_api, internal_api, top)) = setup_node_with_tokens(test_settings, beneficiary, "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(beneficiary, "alice", test_settings, external_api, internal_api)

    ## create contract
    encoded_tx, encoded_contract_id = get_unsigned_contract_create(alice_address, create_settings["create_contract"], external_api, internal_api)
    unsigned_tx = common.api_decode(encoded_tx)

    signed = keys.sign_verify_encode_tx(unsigned_tx, private_key, public_key)
    common.ensure_transaction_posted(external_api, signed)

    call_contract = test_settings["contract_call"]
    call_input = ContractCallInput("sophia-address", encoded_contract_id,\
                                   call_contract["data"]["function"],\
                                   call_contract["data"]["argument"])
    result = internal_api.call_contract(call_input)

    assert_equals(common.hexstring_to_contract_bytearray('0x000000000000000000000000000000000000000000000000000000000000002a'),
                   result.out)

    cleanup(node, root_dir)
Beispiel #3
0
def test_compile_and_call_id():
    # Alice should be able to compile a sophia contract with an identity
    # function into bytecode and call it.
    test_settings = settings["test_compile_and_call_id"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    # Read a contract
    currentFile = __file__
    dirPath = os.path.dirname(currentFile)
    contract_file = open(dirPath + "/identity.aes", "r")
    contract_string = contract_file.read()

    # Compile contract to bytecode
    contract = Contract(contract_string, "")
    compilation_result = api.compile_contract(contract)
    assert_regexp_matches(compilation_result.bytecode, '0x.*')

    # Call contract bytecode
    call_input = ContractCallInput("sophia", compilation_result.bytecode,
                                   "main", "42")
    call_result = api.call_contract(call_input)
    assert_regexp_matches(call_result.out, '0x.*')

    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #4
0
def test_compile_and_call_id():
    # Alice should be able to compile a sophia contract with an identity
    # function into bytecode and call it.
    test_settings = settings["test_compile_and_call_id"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = read_id_contract(api)

    # Call contract bytecode
    call_input = ContractCallInput("sophia", bytecode, "main", "42")
    call_result = api.call_contract(call_input)
    assert_regexp_matches(call_result.out, '0x.*')

    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #5
0
def test_encode_id_call():
    # Alice should be able to encode a call to a function in
    # a sophia contract.

    test_settings = settings["test_encode_id_call"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = '0x36600080376200002160005180805180516004146200002d57505b5060011951005b80590390f35b80905090565b602001517f6d61696e00000000000000000000000000000000000000000000000000000000146200005e576200001a565b60200151806200006e9062000027565b5960008152818162000081918091505090565b8152915050905090565b825180599081525060208401602084038393509350935050600082136200008b5780925050509056'
    call_input = ContractCallInput("sophia", bytecode, "main", "42")
    result = api.encode_calldata(call_input)

    calldata = '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000000000046d61696e00000000000000000000000000000000000000000000000000000000'
    assert_equals(result.calldata, calldata)

    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #6
0
def test_contract_on_chain_call_off_chain():
    test_settings = settings["test_contract_call"]
    create_settings = settings["test_contract_create"]
    beneficiary = common.setup_beneficiary()
    (node, (root_dir, external_api,
            top)) = setup_node_with_tokens(test_settings, beneficiary, "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(beneficiary, "alice", test_settings, external_api,
                        internal_api)

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

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

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

    top = external_api.get_top_block()
    common.wait_until_height(external_api, top.height + 3)

    call_contract = test_settings["contract_call"]
    call_input = ContractCallInput("sophia-address", encoded_contract_id,\
                                   call_contract["data"]["function"],\
                                   call_contract["data"]["argument"])
    result = internal_api.call_contract(call_input)

    assert_equals(
        '0x000000000000000000000000000000000000000000000000000000000000002a',
        result.out)

    cleanup(node, root_dir)
Beispiel #7
0
def test_id_call():
    # Alice should be able to call the id function in
    # an Id contract.

    test_settings = settings["test_id_call"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = '0x36600080376200002160008080805180516004146200003057505b5060011951005b60005260206000f35b80905090565b602001517f6d61696e000000000000000000000000000000000000000000000000000000001462000061576200001a565b602001519050809150506200002a56'

    call_input = ContractCallInput(bytecode, "main", "42")
    result = api.call_contract(call_input)
    print(result)

    retval = '0x000000000000000000000000000000000000000000000000000000000000002a'
    assert_equals(result.out, retval)
    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #8
0
def test_encode_id_call():
    # Alice should be able to encode a call to a function in
    # a ring contract.

    test_settings = settings["test_encode_id_call"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = '0x36600080376200002160008080805180516004146200003057505b5060011951005b60005260206000f35b80905090565b602001517f6d61696e000000000000000000000000000000000000000000000000000000001462000061576200001a565b602001519050809150506200002a56'

    call_input = ContractCallInput(bytecode, "main", "42")
    result = api.encode_calldata(call_input)

    calldata = '0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000000000046d61696e00000000000000000000000000000000000000000000000000000000'
    assert_equals(result.calldata, calldata)

    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #9
0
def test_id_call():
    # Alice should be able to call the id function in
    # an Id contract.

    test_settings = settings["test_id_call"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = '0x366000602037620000606200003460205180805180516004146200007b57505b80518051600414620000d857505b5060011951005b805903906000518059600081529081818162000056915b805081590391505090565b8352505060005250f35b8059039060008052f35b5990565b5080620001289080905090565b602001517f696e69740000000000000000000000000000000000000000000000000000000014620000ac576200001f565b50829150620000ba6200006a565b596000815290818181620000ce916200004b565b835250505b905090565b602001517f6d61696e000000000000000000000000000000000000000000000000000000001462000109576200002d565b6020015159506000516200006e90805180826200017291600091505090565b5960008152908181816200013d918091505090565b835250509050620000d3565b825180599081525060208401602084038393509350935050600082136200014957809250505090565b915050806000525959905090509056'

    call_input = ContractCallInput("sophia", bytecode, "main", "42")
    result = api.call_contract(call_input)
    print(result)

    retval = '0x000000000000000000000000000000000000000000000000000000000000002a'
    assert_equals(result.out, retval)
    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #10
0
def test_encode_id_call():
    # Alice should be able to encode a call to a function in
    # a sophia contract.

    test_settings = settings["test_encode_id_call"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = read_id_contract(api)

    call_input = ContractCallInput("sophia", bytecode, "main", "42")
    result = api.encode_calldata(call_input)

    calldata = '0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000046d61696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a'
    assert_equals(result.calldata, calldata)

    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #11
0
def test_encode_id_call():
    # Alice should be able to encode a call to a function in
    # a sophia contract.

    test_settings = settings["test_encode_id_call"]
    (root_dir, node, external_api,
     internal_api) = setup_node(test_settings, "alice")

    bytecode = read_id_contract(internal_api)

    call_input = ContractCallInput("sophia", bytecode, "main", "42")
    result = internal_api.encode_calldata(call_input)

    assert_regexp_matches(result.calldata, 'cb_.*')

    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #12
0
def test_id_call():
    # Alice should be able to call the id function in
    # an Id contract.

    test_settings = settings["test_id_call"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = read_id_contract(api)

    call_input = ContractCallInput("sophia", bytecode, "main", "42")
    result = api.call_contract(call_input)
    print(result)

    retval = '0x000000000000000000000000000000000000000000000000000000000000002a'
    assert_equals(result.out, retval)
    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #13
0
def test_solidity_greeter():
    # Alice should be able to initialize
    # the gretee Solidity contract.

    test_settings = settings["test_id_call"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = greeter_code()

    call_input = ContractCallInput("evm", bytecode, "", "0x00")
    result = api.call_contract(call_input)
    print(result)

    generatedcode = greeter_generated_code()

    assert_equals(result.out, generatedcode)
    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #14
0
def test_call_solidity_greeter():
    # Alice should be able to call the greet function in
    # an greete Solidity contract.

    test_settings = settings["test_id_call"]
    (root_dir, node, api) = setup_node(test_settings, "alice")

    bytecode = greeter_generated_code()

    setGreeting = "0xa4136862"
    HelloWorld = "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d48656c6c6c6f20576f726c642100000000000000000000000000000000000000"
    call_input = ContractCallInput("evm", bytecode, "",
                                   setGreeting + HelloWorld)
    result = api.call_contract(call_input)
    print(result)

    generatedcode = greeter_generated_code()

    # assert_equals(result.out, generatedcode)
    # stop node
    common.stop_node(node)
    shutil.rmtree(root_dir)
Beispiel #15
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, external_api, internal_api)

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

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

    alice_balance0 = common.get_account_balance(external_api, 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(external_api, 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"]
                  + create_settings["create_contract"]["gas_used"]
                  + create_settings["create_contract"]["deposit"]
                  + create_settings["create_contract"]["amount"])

    call_input = ContractCallInput("sophia", 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=encoded_contract_address,
        vm_version=call_contract["vm_version"],
        fee=call_contract["fee"],
        ttl=100,
        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)

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

    print("Signed transaction: " + signed_call)
    alice_balance0 = common.get_account_balance(external_api, 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(external_api, internal_api, pub_key=alice_address).balance

    # The call runs out of gas and all gas is consumed
    # assert contract called:
    assert_equals(alice_balance0, alice_balance
                  + test_settings["contract_call"]["fee"]
                  + test_settings["contract_call"]["gas"]
                  + test_settings["contract_call"]["amount"])
    print("Fee and gas was consumed, transaction is part of the chain")

    cleanup(node, root_dir)
Beispiel #16
0
def test_contract_call():
    test_settings = settings["test_contract_call"]
    create_settings = settings["test_contract_create"]
    beneficiary = common.setup_beneficiary()
    (node, (root_dir, external_api, internal_api,
            top)) = setup_node_with_tokens(test_settings, beneficiary, "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(beneficiary, "alice", test_settings, external_api,
                        internal_api)

    ## create contract
    encoded_tx, encoded_contract_id = get_unsigned_contract_create(
        alice_address, create_settings["create_contract"], external_api,
        internal_api)
    unsigned_tx = common.api_decode(encoded_tx)
    signed = keys.sign_verify_encode_tx(unsigned_tx, private_key, public_key)

    alice_balance0 = common.get_account_balance(external_api, alice_address)
    common.ensure_transaction_posted(external_api, signed)
    alice_balance = common.get_account_balance(external_api,
                                               pub_key=alice_address)

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

    bytecode = read_id_contract(internal_api)
    call_input = ContractCallInput("sophia-address", encoded_contract_id,
                                   call_contract["data"]["function"],
                                   call_contract["data"]["argument"])
    result = internal_api.call_contract(call_input)
    contract_call_obj = ContractCallTx(
        caller_id=test_settings["alice"]["pubkey"],
        contract_id=encoded_contract_id,
        vm_version=call_contract["vm_version"],
        fee=call_contract["fee"],
        ttl=100,
        amount=call_contract["amount"],
        gas=call_contract["gas"],
        gas_price=call_contract["gas_price"],
        call_data=result.out)

    call_tx_obj = internal_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.api_decode(encoded_call_tx)

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

    print("Signed transaction: " + signed_call)
    alice_balance0 = common.get_account_balance(external_api, alice_address)
    common.ensure_transaction_posted(external_api, signed_call)
    alice_balance = common.get_account_balance(external_api, alice_address)

    # The call runs out of gas and all gas is consumed
    # assert contract called:
    assert_equals(
        alice_balance0, alice_balance + test_settings["contract_call"]["fee"] +
        test_settings["contract_call"]["gas"])
    print("Fee and gas was consumed, transaction is part of the chain")

    cleanup(node, root_dir)
Beispiel #17
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)