コード例 #1
0
ファイル: test_contract.py プロジェクト: heshu-by/likelib
def main(env, logger):
    contract_text = '''
pragma solidity >=0.4.0 <0.7.0;

contract SimpleStorage {
    uint storedData;

    constructor(uint x) public{
        storedData = x;
    }

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint data) {
        return storedData;
    }
}

'''
    contract_file_path = os.path.abspath("contract.sol")
    with open(contract_file_path, "wt", encoding='utf8') as f:
        f.write(contract_text)

    node_settings = Node.Settings(Node.Id(20205, 50055), start_up_time=2)
    with Node(env, node_settings, logger) as node:
        node.run_check_test()

        contracts = node.compile_contract(code=contract_file_path)
        test_contract = contracts[0]
        target_value = 8888
        test_contract_init_message = node.encode_message(
            code=test_contract, message=f"constructor({target_value})")

        distributor_address = node.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)
        deployed_contract = node.push_contract(
            from_address=distributor_address,
            code=test_contract,
            amount=0,
            gas=10000000,
            init_message=test_contract_init_message,
            timeout=5)

        call_message = node.encode_message(code=test_contract, message="get()")
        message_result = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract,
            gas=10000000,
            amount=0,
            message=call_message,
            timeout=5)
        res = node.decode_message(code=test_contract,
                                  method="get",
                                  message=message_result)
        TEST_CHECK(res['data'] == target_value)

    return 0
コード例 #2
0
def main(env, logger):
    contract_a_file_path = os.path.join(CONTRACTS_FOLDER,
                                        "call_other_contract", "a.sol")
    if not os.path.exists(contract_a_file_path):
        TEST_CHECK(False, message="Contracts folder was not found")

    contract_b_file_path = os.path.join(CONTRACTS_FOLDER,
                                        "call_other_contract", "b.sol")
    if not os.path.exists(contract_b_file_path):
        TEST_CHECK(False, message="Contracts folder was not found")

    node_settings = Node.Settings(Node.Id(20216, 50066), start_up_time=2)
    with Node(env, node_settings, logger) as node:
        node.run_check_test()

        contracts_a = node.compile_contract(code=contract_a_file_path)
        test_contract_a = "A"

        contracts_b = node.compile_contract(code=contract_b_file_path)
        test_contract_b = "B"

        distributor_address = node.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)
        deployed_contract_a = node.push_contract(
            from_address=distributor_address,
            code=test_contract_a,
            amount=0,
            gas=10000000,
            init_message="",
            timeout=5)

        deployed_contract_b = node.push_contract(
            from_address=distributor_address,
            code=test_contract_b,
            amount=0,
            gas=10000000,
            init_message="",
            timeout=5)

        arg1 = 5
        arg2 = 6
        call_message = node.encode_message(
            code=test_contract_b,
            message=
            f"doYourThing(Address({deployed_contract_a.address}), {arg1}, {arg2})"
        )
        message_result = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract_b,
            gas=10000000,
            amount=0,
            message=call_message,
            timeout=5)
        res = node.decode_message(code=test_contract_b,
                                  method="doYourThing",
                                  message=message_result)
        TEST_CHECK(res['result'] == (arg1 + arg2))

    return 0
コード例 #3
0
def main(env, logger):
    contract_file_path = os.path.join(CONTRACTS_FOLDER,
                                      "address_send_contract", "contract.sol")
    if not os.path.exists(contract_file_path):
        TEST_CHECK(False, message="Contracts folder was not found")

    node_settings = Node.Settings(Node.Id(20217, 50067), start_up_time=2)
    with Node(env, node_settings, logger) as node:
        node.run_check_test()

        distributor_address = node.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)
        new_test_account = node.create_new_address(keys_path="account_1")

        initializing_amount = 100000000
        TEST_CHECK(
            node.transfer(to_address=new_test_account,
                          from_address=distributor_address,
                          amount=initializing_amount,
                          fee=0))

        node.run_check_balance(new_test_account, initializing_amount)

        contracts = node.compile_contract(code=contract_file_path)
        test_contract = contracts[0]

        deployed_contract = node.push_contract(from_address=new_test_account,
                                               code=test_contract,
                                               amount=0,
                                               gas=100000,
                                               init_message="",
                                               timeout=5)

        current_balance = node.get_balance(address=new_test_account)

        gas_for_call = 10000000
        amount_for_call = 5000000
        call_message = node.encode_message(
            code=test_contract, message=f"testAddressSend({amount_for_call})")
        message_result = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract,
            gas=gas_for_call,
            amount=amount_for_call,
            message=call_message,
            timeout=5)
        res = node.decode_message(code=test_contract,
                                  method="testAddressSend",
                                  message=message_result)
        TEST_CHECK(res["is_success"])

        new_balance = node.get_balance(address=new_test_account)
        TEST_CHECK(new_balance == (current_balance + amount_for_call))

    return 0
コード例 #4
0
def main(env, logger):
    node_settings = Node.Settings(Node.Id(20206, 50056))
    with Node(env, node_settings, logger) as node:
        node.run_check_test()

        target_address = node.create_new_address(keys_path="keys1")
        node.run_check_balance(target_address, 0)

        distributor_address = node.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)
        amount = 333
        node.run_check_transfer(to_address=target_address,
                                amount=amount,
                                from_address=distributor_address,
                                fee=0,
                                timeout=2,
                                wait=1)
        node.run_check_balance(target_address, amount)
    return 0
コード例 #5
0
def main(env, logger):
    contract_file_path = os.path.join(CONTRACTS_FOLDER, "get_balance_contract",
                                      "contract.sol")
    if not os.path.exists(contract_file_path):
        TEST_CHECK(False, message="Contracts folder was not found")

    node_settings = Node.Settings(Node.Id(20216, 50066), start_up_time=2)
    with Node(env, node_settings, logger) as node:
        node.run_check_test()

        contracts = node.compile_contract(code=contract_file_path)
        test_contract = contracts[0]
        distributor_address = node.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)
        deployed_contract = node.push_contract(
            from_address=distributor_address,
            code=test_contract,
            amount=0,
            gas=10000000,
            init_message="",
            timeout=5)

        current_balance = node.get_balance(address=distributor_address)
        gas_for_call = 10000000

        call_message = node.encode_message(code=test_contract,
                                           message="getBalance()")
        message_result = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract,
            gas=gas_for_call,
            amount=0,
            message=call_message,
            timeout=5)
        res = node.decode_message(code=test_contract,
                                  method="getBalance",
                                  message=message_result)
        TEST_CHECK((res["balance"] + gas_for_call) == current_balance)

    return 0
コード例 #6
0
def main(env, logger):
    settings_node_1 = Node.Settings(Node.Id(20300, 50150))
    settings_node_2 = Node.Settings(Node.Id(20301, 50151),
                                    nodes=[
                                        settings_node_1.id,
                                    ])

    with Node(env, settings_node_1, logger) as node_1:
        node_1.run_check_test()

        with Node(env, settings_node_2, logger) as node_2:
            node_2.run_check_test()
            target_address = node_1.create_new_address(keys_path="keys1")

            node_1.run_check_balance(address=target_address, balance=0)
            node_2.run_check_balance(address=target_address, balance=0)

            distributor_address = node_1.load_address(
                keys_path=node_1.DISTRIBUTOR_ADDRESS_PATH)
            amount = 333
            transaction_wait = 5
            transaction_timeout = 3
            node_2.run_check_transfer(to_address=target_address,
                                      amount=amount,
                                      from_address=distributor_address,
                                      fee=0,
                                      timeout=transaction_timeout,
                                      wait=transaction_wait)

            node_2.run_check_balance(address=target_address, balance=amount)
            node_1.run_check_balance(address=target_address, balance=amount)

    return 0
コード例 #7
0
def main(env, logger):
    contract_file_path = os.path.join(CONTRACTS_FOLDER, "auction", "contract.sol")
    if not os.path.exists(contract_file_path):
        TEST_CHECK(False, message="Contracts folder was not found")

    node_settings = Node.Settings(Node.Id(20225, 50075), start_up_time=2)
    with Node(env, node_settings, logger) as node:
        node.run_check_test()

        contracts = node.compile_contract(code=contract_file_path)
        auction_contract_data = contracts[0]

        beneficiary_address = node.create_new_address(keys_path='beneficiary')
        beneficiary_init_balance = node.get_balance(address=beneficiary_address)
        account_seconds_duration = 40
        auction_init_message = node.encode_message(code=auction_contract_data,
                                                   message=f"constructor({account_seconds_duration}, Address({beneficiary_address.address}))")
        start_time = datetime.datetime.now()

        distributor_address = node.load_address(keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)
        deployed_contract = node.push_contract(from_address=distributor_address, code=auction_contract_data, amount=0,
                                               gas=10000000, init_message=auction_init_message, timeout=10)

        client_1 = node.create_new_address(keys_path='client_1')
        client_2 = node.create_new_address(keys_path='client_2')
        client_1_init_amount = 5000000
        client_2_init_amount = 5000000

        node.transfer(from_address=distributor_address, to_address=client_1, amount=client_1_init_amount, fee=5,
                      timeout=10)
        node.transfer(from_address=distributor_address, to_address=client_2, amount=client_2_init_amount, fee=5,
                      timeout=10)

        bid_message = node.encode_message(code=auction_contract_data, message="bid()")
        max_bid_message = node.encode_message(code=auction_contract_data, message="highestBid()")

        client_1_bid_1 = 10000
        client_1_bid_1_gas = 100000
        node.message_to_contract(from_address=client_1, to_address=deployed_contract,
                                 gas=client_1_bid_1_gas, amount=client_1_bid_1, message=bid_message,
                                 timeout=10)

        client_1_bid_2 = 15000
        client_1_bid_2_gas = 100000
        node.message_to_contract(from_address=client_1, to_address=deployed_contract,
                                 gas=client_1_bid_2_gas, amount=client_1_bid_2, message=bid_message,
                                 timeout=10)

        result = node.message_to_contract(from_address=distributor_address,
                                          to_address=deployed_contract,
                                          gas=100000, amount=0,
                                          message=max_bid_message, timeout=10)
        current_max_bid = node.decode_message(code=auction_contract_data, method="highestBid", message=result)
        TEST_CHECK(current_max_bid[''] == client_1_bid_2)

        client_2_bid_1 = 10000
        client_2_bid_1_gas = 100000
        node.message_to_contract(from_address=client_2, to_address=deployed_contract,
                                 gas=client_2_bid_1_gas, amount=client_2_bid_1,
                                 message=bid_message, timeout=10)

        result = node.message_to_contract(from_address=distributor_address,
                                          to_address=deployed_contract,
                                          gas=100000, amount=0,
                                          message=max_bid_message, timeout=10)
        current_max_bid = node.decode_message(code=auction_contract_data, method="highestBid", message=result)
        TEST_CHECK(current_max_bid[''] == client_1_bid_2)

        client_2_bid_2 = 20000
        client_2_bid_2_gas = 100000
        node.message_to_contract(from_address=client_2, to_address=deployed_contract,
                                 gas=client_2_bid_2_gas, amount=client_2_bid_2,
                                 message=bid_message, timeout=10)

        result = node.message_to_contract(from_address=distributor_address,
                                          to_address=deployed_contract,
                                          gas=100000, amount=0,
                                          message=max_bid_message, timeout=10)
        current_max_bid = node.decode_message(code=auction_contract_data, method="highestBid", message=result)
        TEST_CHECK(current_max_bid[''] == client_2_bid_2)

        time.sleep(account_seconds_duration - (datetime.datetime.now() - start_time).total_seconds())

        max_bid_message = node.encode_message(code=auction_contract_data, message="highestBid()")
        result = node.message_to_contract(from_address=distributor_address,
                                          to_address=deployed_contract,
                                          gas=100000, amount=0,
                                          message=max_bid_message, timeout=10)
        current_max_bid = node.decode_message(code=auction_contract_data, method="highestBid", message=result)

        auction_end_message = node.encode_message(code=auction_contract_data, message="auctionEnd()")
        result = node.message_to_contract(from_address=distributor_address,
                                          to_address=deployed_contract,
                                          gas=100000, amount=0,
                                          message=auction_end_message, timeout=10)
        if result.message:
            TEST_CHECK(
                node.decode_message(code=auction_contract_data, method="auctionEnd", message=result)['is_end'])

        beneficiary_current_balance = node.get_balance(address=beneficiary_address)
        TEST_CHECK(beneficiary_init_balance + current_max_bid[''] == beneficiary_current_balance)

    return 0
コード例 #8
0
def main(env, logger):
    contract_file_path = os.path.join(CONTRACTS_FOLDER,
                                      "selfdestruct_contract", "contract.sol")
    if not os.path.exists(contract_file_path):
        TEST_CHECK(False, message="Contracts folder was not found")

    node_settings = Node.Settings(Node.Id(20219, 50069), start_up_time=2)
    with Node(env, node_settings, logger) as node:
        node.run_check_test()

        new_account = node.create_new_address(keys_path="new_account")
        contracts = node.compile_contract(code=contract_file_path)
        test_contract = contracts[0]
        distributor_address = node.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)
        init_message = node.encode_message(
            code=test_contract,
            message=f"constructor(Address({new_account.address}))")
        deployed_contract = node.push_contract(
            from_address=distributor_address,
            code=test_contract,
            amount=50,
            gas=10000000,
            init_message=init_message,
            timeout=7)
        amount_for_call = 100000

        pay_message = node.encode_message(code=test_contract,
                                          message="payMe()")
        message_result = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract,
            gas=10000000,
            amount=amount_for_call,
            message=pay_message,
            timeout=7)
        res = node.decode_message(code=test_contract,
                                  method="payMe",
                                  message=message_result)
        TEST_CHECK(res["current_balance"] == node.get_balance(
            address=deployed_contract))

        message_result = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract,
            gas=10000000,
            amount=amount_for_call,
            message=pay_message,
            timeout=7)
        res = node.decode_message(code=test_contract,
                                  method="payMe",
                                  message=message_result)
        TEST_CHECK(res["current_balance"] == node.get_balance(
            address=deployed_contract))

        TEST_CHECK(0 == node.get_balance(address=new_account))
        last_balance = node.get_balance(address=deployed_contract)
        delete_message = node.encode_message(code=test_contract,
                                             message="initDelete()")
        node.message_to_contract(from_address=distributor_address,
                                 to_address=deployed_contract,
                                 gas=10000000,
                                 amount=0,
                                 message=delete_message,
                                 timeout=7)
        TEST_CHECK(last_balance == node.get_balance(address=new_account))
        TEST_CHECK(0 == node.get_balance(address=deployed_contract))

        message_result = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract,
            gas=10000000,
            amount=1,
            message=pay_message,
            timeout=7)
        TEST_CHECK(not message_result.message)

    return 0
コード例 #9
0
def main(env, logger):
    contract_file_path = os.path.join(CONTRACTS_FOLDER,
                                      "simple_storage_contract",
                                      "contract.sol")
    if not os.path.exists(contract_file_path):
        TEST_CHECK(False, message="Contracts folder was not found")

    node_settings = Node.Settings(Node.Id(20215, 50065), start_up_time=2)
    with Node(env, node_settings, logger) as node:
        node.run_check_test()

        contracts = node.compile_contract(code=contract_file_path)
        test_contract = contracts[0]
        target_value = 8888
        test_contract_init_message = node.encode_message(
            code=test_contract, message=f"constructor({target_value})")

        distributor_address = node.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)
        deployed_contract = node.push_contract(
            from_address=distributor_address,
            code=test_contract,
            amount=0,
            gas=10000000,
            init_message=test_contract_init_message,
            timeout=5)

        call_message = node.encode_message(code=test_contract, message="get()")
        message_result = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract,
            gas=10000000,
            amount=0,
            message=call_message,
            timeout=5)
        res = node.decode_message(code=test_contract,
                                  method="get",
                                  message=message_result)
        TEST_CHECK(res['stored_data'] == target_value)

        target_value_2 = 5555
        call_message_2 = node.encode_message(code=test_contract,
                                             message=f"set({target_value_2})")
        node.message_to_contract(from_address=distributor_address,
                                 to_address=deployed_contract,
                                 gas=10000000,
                                 amount=0,
                                 message=call_message_2,
                                 timeout=5)

        message_result_3 = node.message_to_contract(
            from_address=distributor_address,
            to_address=deployed_contract,
            gas=10000000,
            amount=0,
            message=call_message,
            timeout=5)

        res = node.decode_message(code=test_contract,
                                  method="get",
                                  message=message_result_3)
        TEST_CHECK(res['stored_data'] == target_value_2)

    return 0
コード例 #10
0
ファイル: test_connection.py プロジェクト: heshu-by/likelib
def main(env, logger):
    node_settings = Node.Settings(Node.Id(20205, 50055))
    with Node(env, node_settings, logger) as node:
        node.run_check_test()
    return 0
コード例 #11
0
def main(env, logger):
    count_nodes = 10
    start_sync_port = 20302
    start_rpc_port = 50152
    waiting_time = 5
    transaction_timeout = 5
    transaction_wait = 5

    with NodePoll() as pool:
        pool.append(
            Node(env, Node.Settings(Node.Id(start_sync_port, start_rpc_port)),
                 logger))
        pool.last.start_node(waiting_time)
        pool.last.run_check_test()

        # initializing connections with nodes
        for i in range(1, count_nodes):
            curent_sync_port = start_sync_port + i
            curent_rpc_port = start_rpc_port + i

            pool.append(
                Node(
                    env,
                    Node.Settings(Node.Id(curent_sync_port, curent_rpc_port),
                                  nodes=pool.ids), logger))

            pool.last.start_node(waiting_time)
            for node in pool:
                node.run_check_test()

        addresses = [
            pool.last.create_new_address(keys_path=f"keys{i}")
            for i in range(1, len(pool))
        ]
        init_amount = 1000

        distributor_address = pool.last.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)

        # init addresses with amount
        for to_address in addresses:
            pool.last.run_check_balance(address=to_address, balance=0)
            pool.last.run_check_transfer(to_address=to_address,
                                         amount=init_amount,
                                         from_address=distributor_address,
                                         fee=0,
                                         timeout=transaction_timeout,
                                         wait=transaction_wait)
            for node in pool:
                node.run_check_balance(address=to_address, balance=init_amount)

        for i in range(1, len(addresses) - 1):
            from_address = addresses[i]
            to_address = addresses[i + 1]
            amount = i * 100
            pool.last.run_check_transfer(to_address=to_address,
                                         amount=amount,
                                         from_address=from_address,
                                         fee=0,
                                         timeout=transaction_timeout,
                                         wait=transaction_wait)
            for node in pool:
                node.run_check_balance(address=to_address,
                                       balance=amount + init_amount)

        first_address = addresses[0]
        first_address_balance = init_amount
        for node in pool:
            node.run_check_balance(address=first_address,
                                   balance=first_address_balance)

    return 0
コード例 #12
0
def main(env, logger):
    count_nodes = 7
    start_sync_port = 20330
    start_rpc_port = 50180
    node_startup_time = 5
    transaction_wait = 10
    transaction_timeout = 42

    init_amount = 1000
    address_per_nodes = 3

    with NodePoll() as pool:
        pool.append(
            Node(env, Node.Settings(Node.Id(start_sync_port, start_rpc_port)),
                 logger))
        pool.last.start_node(node_startup_time)
        pool.last.run_check_test()

        # initializing connections with nodes
        for i in range(1, count_nodes):
            curent_sync_port = start_sync_port + i
            curent_rpc_port = start_rpc_port + i

            pool.append(
                Node(
                    env,
                    Node.Settings(Node.Id(curent_sync_port, curent_rpc_port),
                                  nodes=pool.ids), logger))

            pool.last.start_node(node_startup_time)
            for node in pool:
                node.run_check_test()

        addresses = [
            pool.last.create_new_address(keys_path=f"keys{i}")
            for i in range(1, count_nodes * address_per_nodes + 1)
        ]
        distributor_address = pool.last.load_address(
            keys_path=Node.DISTRIBUTOR_ADDRESS_PATH)

        # init addresses with amount
        for to_address in addresses:
            pool.last.run_check_balance(address=to_address, balance=0)
            pool.last.run_check_transfer(to_address=to_address,
                                         amount=init_amount,
                                         from_address=distributor_address,
                                         fee=0,
                                         timeout=transaction_timeout,
                                         wait=transaction_wait)
            for node in pool:
                node.run_check_balance(address=to_address, balance=init_amount)

        with concurrent.futures.ThreadPoolExecutor(len(pool)) as executor:
            threads = []
            for i in range(len(pool)):
                first_address_number = i * address_per_nodes
                last_address_number = (i *
                                       address_per_nodes) + address_per_nodes
                threads.append(
                    executor.submit(
                        node_transfers, pool[i],
                        addresses[first_address_number:last_address_number],
                        transaction_wait))
            for i in threads:
                i.result()

        for address in addresses:
            for node in pool:
                node.run_check_balance(address=address, balance=init_amount)

    return 0