def test_no_min_balance_in_account(payment_node_network_no_min_balance):
    network = payment_node_network_no_min_balance
    node0: DockerNode = network.docker_nodes[0]
    node0.use_docker_client()
    blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    genesis_hash = blocks[0].summary.block_hash
    assert len(
        blocks) == 1  # There should be only one block - the genesis block
    genesis_balance = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=genesis_hash)
    assert genesis_balance == 10**3
    block_hash = node0.transfer_to_account(1,
                                           amount=10**7,
                                           is_deploy_error_check=False)
    deploy = node0.client.show_deploys(block_hash)[0]
    assert deploy.is_error is True
    assert deploy.error_message == "Insufficient payment"
    cost_of_execution = deploy.cost
    assert cost_of_execution == 0
    genesis_balance_after_transfer = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=parse_show_blocks(
            node0.d_client.show_blocks(1000))[0].summary.block_hash,
    )
    assert genesis_balance == genesis_balance_after_transfer
def test_deduct_execution_cost_from_account(payment_node_network):
    network = payment_node_network
    node0: DockerNode = network.docker_nodes[0]
    node0.use_docker_client()
    blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    genesis_hash = blocks[0].summary.block_hash
    assert len(
        blocks) == 1  # There should be only one block - the genesis block
    genesis_balance = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=genesis_hash)
    assert genesis_balance == INITIAL_MOTES_AMOUNT

    account1 = network.test_account(node0, 10**8)
    account1_block_hash = parse_show_blocks(
        node0.d_client.show_blocks(1000))[0].summary.block_hash
    deploys = node0.client.show_deploys(account1_block_hash)
    deploy_cost = deploys[0].cost
    assert deploy_cost != 0
    account1_balance = node0.client.get_balance(
        account_address=account1.public_key_hex,
        block_hash=account1_block_hash)
    assert account1_balance == 10**8
    genesis_balance_after_transfer = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=parse_show_blocks(
            node0.d_client.show_blocks(1000))[0].summary.block_hash,
    )
    assert (genesis_balance == genesis_balance_after_transfer +
            account1_balance + deploy_cost * CONV_RATE)
def test_error_in_session_contract(payment_node_network):
    network = payment_node_network
    node0: DockerNode = network.docker_nodes[0]

    blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    genesis_hash = blocks[0].summary.block_hash
    assert len(
        blocks) == 1  # There should be only one block - the genesis block
    genesis_balance = node0.d_client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=genesis_hash)
    assert genesis_balance == INITIAL_MOTES_AMOUNT
    block_hash = node0.transfer_to_account(
        1,
        session_contract=Contract.DIRECT_REVERT,
        amount=10**7,
        is_deploy_error_check=False,
    )
    deploy = node0.d_client.show_deploys(block_hash)[0]
    assert deploy.is_error is True
    assert deploy.error_message == "User error: 1"
    cost_of_execution = deploy.cost
    assert cost_of_execution > 0
    genesis_balance_after_transfer = node0.d_client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=parse_show_blocks(
            node0.d_client.show_blocks(1000))[0].summary.block_hash,
    )
    expected_sum = genesis_balance_after_transfer + cost_of_execution * CONV_RATE
    assert genesis_balance == expected_sum
def test_not_enough_funds_to_run_payment_code(payment_node_network):
    network = payment_node_network
    node0: DockerNode = network.docker_nodes[0]
    blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    genesis_hash = blocks[0].summary.block_hash
    assert len(
        blocks) == 1  # There should be only one block - the genesis block

    genesis_balance = node0.d_client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=genesis_hash)
    assert genesis_balance == INITIAL_MOTES_AMOUNT
    session_args = ABI.args([
        ABI.account("account", GENESIS_ACCOUNT.public_key_hex),
        ABI.u64("amount", 10**7),
    ])
    node0.p_client.deploy(
        from_address=GENESIS_ACCOUNT.public_key_hex,
        session_contract=Contract.TRANSFER_TO_ACCOUNT,
        payment_contract=Contract.STANDARD_PAYMENT,
        public_key=GENESIS_ACCOUNT.public_key_path,
        private_key=GENESIS_ACCOUNT.private_key_path,
        gas_price=1,
        session_args=session_args,
        payment_args=ABI.args([ABI.u512("amount", 450)]),
    )

    latest_block_hash = parse_show_blocks(
        node0.d_client.show_blocks(1000))[0].summary.block_hash
    genesis_balance_after_transfer = node0.d_client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=latest_block_hash)
    assert genesis_balance == genesis_balance_after_transfer
示例#5
0
def test_not_enough_to_run_session(trillion_payment_node_network):
    """
    The caller has not transferred enough funds to the payment purse
    to completely run the session code.
    The deploy will fail and the caller will not receive a refund.
    """
    network = trillion_payment_node_network
    node0: DockerNode = network.docker_nodes[0]

    blocks = parse_show_blocks(node0.d_client.show_blocks(10))
    genesis_hash = blocks[0].summary.block_hash
    assert len(
        blocks) == 1  # There should be only one block - the genesis block
    genesis_balance = node0.d_client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=genesis_hash)
    assert genesis_balance == 1000000000000
    transfer_amount = 10**8
    account1 = network.test_account(node0, transfer_amount)
    account1_starting_balance = node0.d_client.get_balance(
        account_address=account1.public_key_hex,
        block_hash=get_latest_hash(node0))
    assert account1_starting_balance == 10**8

    _, _ = node0.p_client.deploy(
        from_address=account1.public_key_hex,
        payment_contract=Contract.STANDARD_PAYMENT,
        session_contract=Contract.ENDLESS_LOOP,
        public_key=account1.public_key_path,
        private_key=account1.private_key_path,
        gas_price=1,
        session_args=None,
        payment_args=MAX_PAYMENT_ABI,
    )
    try:
        node0.p_client.propose()
    except Exception as ex:
        print(ex)

    latest_blocks = parse_show_blocks(node0.d_client.show_blocks(100))
    block_hash = latest_blocks[0].summary.block_hash
    deploy = node0.d_client.show_deploys(block_hash)[0]
    assert deploy.cost > 0
    motes = deploy.cost * CONV_RATE
    account1_balance_after_computation = node0.d_client.get_balance(
        account_address=account1.public_key_hex, block_hash=block_hash)

    assert account1_balance_after_computation == account1_starting_balance - motes
示例#6
0
def test_multiple_deploys_at_once(
    three_node_network,
    contract_paths: List[List[str]],
    expected_deploy_counts_in_blocks,
):
    """
    Feature file : multiple_simultaneous_deploy.feature
    Scenario: Multiple simultaneous deploy after single deploy
    """
    nodes = three_node_network.docker_nodes

    deploy_threads = [
        DeployThread("node" + str(i + 1), node, contract_paths)
        for i, node in enumerate(nodes)
    ]

    for t in deploy_threads:
        t.start()

    for t in deploy_threads:
        t.join()

    # See COMMENT_EXPECTED_BLOCKS
    block_hashes = reduce(add, [t.block_hashes for t in deploy_threads])
    wait_for_block_hashes_propagated_to_all_nodes(nodes, block_hashes)

    for node in nodes:
        blocks = parse_show_blocks(
            node.client.show_blocks(len(expected_deploy_counts_in_blocks) * 100)
        )
        n_blocks = len(expected_deploy_counts_in_blocks)
        tmp = [b.summary.header.deploy_count for b in blocks][:n_blocks]
        assert (
            tmp.sort() == expected_deploy_counts_in_blocks.sort()
        ), "Unexpected deploy counts in blocks"
示例#7
0
文件: cli.py 项目: zie1ony/CasperLabs
    def parse_output(command, binary_output):

        if command in ("make-deploy", "sign-deploy"):
            return binary_output

        output = binary_output.decode("utf-8")

        if command in ("deploy", "send-deploy", "bond", "unbond", "transfer"):
            return output.split()[2]
            # "Success! Deploy 0d4036bebb95de793b28de452d594531a29f8dc3c5394526094d30723fa5ff65 deployed."

        if command == "show-blocks":
            return parse_show_blocks(output)

        if command == "show-deploys":
            return parse_show_deploys(output)

        if command in ("show-deploy", "show-block", "query-state"):
            return parse(output)

        if command in ("balance", ):
            # 'Balance:\n9d39b7fba47d07c1af6f711efe604a112ab371e2deefb99a613d2b3dcdfba414 : 1000000000'
            return int(output.split(":")[-1])

        return output
示例#8
0
文件: cli.py 项目: fizyk20/CasperLabs
    def parse_output(command, binary_output):

        if command in ("make-deploy", "sign-deploy"):
            return binary_output

        output = binary_output.decode("utf-8")

        if command in ("deploy", "send-deploy", "bond", "unbond"):
            return output.split()[2]
            # "Success! Deploy 0d4036bebb95de793b28de452d594531a29f8dc3c5394526094d30723fa5ff65 deployed."

        if command in ("propose",):
            # "Response: Success! Block 47338c65992e7d5062aec2200ad8d7284ae49f6c3e7c37fa7eb46fb6fc8ae3d8 created and added."
            return output.split()[3]

        if command == "show-blocks":
            return parse_show_blocks(output)

        if command == "show-deploys":
            return parse_show_deploys(output)

        if command in ("show-deploy", "show-block", "query-state"):
            return parse(output)

        return output
def test_refund_after_session_code_error(payment_node_network):
    network = payment_node_network
    node0: DockerNode = network.docker_nodes[0]
    blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    genesis_init_balance = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=blocks[0].summary.block_hash,
    )

    deploy_hash = node0.p_client.deploy(
        from_address=GENESIS_ACCOUNT.public_key_hex,
        session_contract=Contract.ARGS_U512,
        payment_contract=Contract.STANDARD_PAYMENT,
        public_key=GENESIS_ACCOUNT.public_key_path,
        private_key=GENESIS_ACCOUNT.private_key_path,
        gas_price=1,
        session_args=ABI.args([ABI.u512("number", 100)]),
        payment_args=ABI.args([ABI.u32("amount", 10**6)])
        # 100 is a revert code.
    )
    result = node0.p_client.client.wait_for_deploy_processed(
        deploy_hash, on_error_raise=False)
    last_processing_result = result.processing_results[0]
    block_hash = last_processing_result.block_info.summary.block_hash.hex()
    cost = last_processing_result.cost

    assert cost == MAX_PAYMENT_COST / CONV_RATE
    motes = cost * CONV_RATE

    later_balance = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex, block_hash=block_hash)

    expected_sum = later_balance + motes
    assert genesis_init_balance == expected_sum
示例#10
0
def assert_pre_state_of_network(network: OneNodeNetwork, stakes: List[int]):
    node0 = network.docker_nodes[0]
    blocks = parse_show_blocks(node0.client.show_blocks(1000))
    assert len(blocks) == 1
    genesis_block = blocks[0]
    item = list(
        filter(
            lambda x: x.stake in stakes and x.validator_public_key == node0.
            from_address,
            genesis_block.summary.header.state.bonds,
        ))
    assert len(item) == 0
def test_error_in_payment_contract(payment_node_network):
    network = payment_node_network
    node0: DockerNode = network.docker_nodes[0]
    node0.use_docker_client()
    blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    genesis_hash = blocks[0].summary.block_hash
    assert len(
        blocks) == 1  # There should be only one block - the genesis block
    genesis_balance = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=genesis_hash)
    assert genesis_balance == INITIAL_MOTES_AMOUNT

    from_account = Account("genesis")
    to_account = Account(1)

    session_args = ABI.args([
        ABI.account("account", to_account.public_key_hex),
        ABI.u64("amount", 10**7)
    ])
    payment_args = ABI.args([ABI.u512("amount", 10**6)])

    node0.p_client.deploy(
        from_address=from_account.public_key_hex,
        session_contract=Contract.TRANSFER_TO_ACCOUNT,
        payment_contract=Contract.DIRECT_REVERT,
        public_key=from_account.public_key_path,
        private_key=from_account.private_key_path,
        gas_price=1,
        session_args=session_args,
        payment_args=payment_args,
    )
    genesis_balance_after_transfer = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=parse_show_blocks(
            node0.d_client.show_blocks(1000))[0].summary.block_hash,
    )

    assert genesis_balance == genesis_balance_after_transfer
示例#12
0
def test_refund_after_session_code_error(payment_node_network):
    network = payment_node_network
    node0: DockerNode = network.docker_nodes[0]
    blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    genesis_init_balance = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=blocks[0].summary.block_hash,
    )

    _, deploy_hash = node0.p_client.deploy(
        from_address=GENESIS_ACCOUNT.public_key_hex,
        session_contract=Contract.ARGS_U512,
        payment_contract=Contract.STANDARD_PAYMENT,
        public_key=GENESIS_ACCOUNT.public_key_path,
        private_key=GENESIS_ACCOUNT.private_key_path,
        gas_price=1,
        session_args=ABI.args([ABI.u512("number", 100)]),
        payment_args=ABI.args([ABI.u32("amount", 10 ** 6)])
        # 100 is a revert code.
    )
    try:
        node0.p_client.propose()
    except Exception as ex:
        print(ex)

    latest_blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    deploy_hash = latest_blocks[0].summary.block_hash
    deploy = node0.client.show_deploys(deploy_hash)[0]
    assert deploy.cost == MAX_PAYMENT_COST / CONV_RATE
    motes = deploy.cost * CONV_RATE

    later_balance = node0.client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex, block_hash=deploy_hash
    )

    expected_sum = later_balance + motes
    assert genesis_init_balance == expected_sum
def test_not_enough_to_run_session(trillion_payment_node_network):
    """
    The caller has not transferred enough funds to the payment purse
    to completely run the session code.
    The deploy will fail and the caller will not receive a refund.
    """
    network = trillion_payment_node_network
    node0: DockerNode = network.docker_nodes[0]

    blocks = parse_show_blocks(node0.d_client.show_blocks(10))
    genesis_hash = blocks[0].summary.block_hash
    assert len(
        blocks) == 1  # There should be only one block - the genesis block
    genesis_balance = node0.d_client.get_balance(
        account_address=GENESIS_ACCOUNT.public_key_hex,
        block_hash=genesis_hash)
    assert genesis_balance == 1000000000000
    transfer_amount = 10**8
    account1 = network.test_account(node0, transfer_amount)
    account1_starting_balance = node0.d_client.get_balance(
        account_address=account1.public_key_hex,
        block_hash=get_latest_hash(node0))
    assert account1_starting_balance == 10**8

    deploy_hash = node0.p_client.deploy(
        from_address=account1.public_key_hex,
        payment_contract=Contract.STANDARD_PAYMENT,
        session_contract=Contract.ENDLESS_LOOP,
        public_key=account1.public_key_path,
        private_key=account1.private_key_path,
        gas_price=1,
        session_args=None,
        payment_args=MAX_PAYMENT_ABI,
    )
    result = node0.p_client.client.wait_for_deploy_processed(
        deploy_hash, on_error_raise=False)
    last_processing_result = result.processing_results[0]

    cost = last_processing_result.cost
    assert cost > 0

    motes = cost * CONV_RATE
    block_hash = last_processing_result.block_info.summary.block_hash.hex()
    account1_balance_after_computation = node0.d_client.get_balance(
        account_address=account1.public_key_hex, block_hash=block_hash)

    assert account1_balance_after_computation == account1_starting_balance - motes
示例#14
0
def test_client_parser_show_blocks():
    r = parse_show_blocks(SHOW_BLOCKS)[0]
    bonds = r.summary.header.state.bonds
    assert len(bonds) == 10
    assert sum(bond.stake for bond in bonds) == 190
示例#15
0
def assert_pre_state_of_network(network: OneNodeNetwork):
    node0 = network.docker_nodes[0]
    blocks = parse_show_blocks(node0.client.show_blocks(1000))
    assert len(blocks) == 1
示例#16
0
 def is_satisfied(self) -> bool:
     n = self.prefix_length
     return all((self.block_hashes.issubset(
         set(b.summary.block_hash[:n]
             for b in parse_show_blocks(node.d_client.show_blocks(1000))))
                 for node in self.nodes))
def get_latest_hash(node0):
    blocks = parse_show_blocks(node0.d_client.show_blocks(1000))
    return blocks[0].summary.block_hash