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
def test_deploy_with_args(one_node_network, genesis_public_signing_key):
    """
    Deploys test contracts that do:

        revert(get_arg(0)); // for u32 and u512

    and

        revert(sum(address_bytes[u8; 32]) + u32); for multiple argument test.

    Tests args get correctly encoded and decoded in the contract.

    Test expects the test contracts args_u32.wasm and args_u512.wasm
    to deserialize correctly their arguments and then call revert with value
    of the argument (converted to a Rust native int, as expected by revert).
    If the test contracts don't fail or if their exit code is different
    than expected, the test will fail.
    """
    node = one_node_network.docker_nodes[0]
    client = node.p_client.client

    for wasm, encode in [
        (resources_path() / Contract.ARGS_U32, ABI.int_value),
        (resources_path() / Contract.ARGS_U512, ABI.big_int),
    ]:
        for number in [1, 12, 256, 1024]:
            block_hash = node.deploy_and_get_block_hash(
                GENESIS_ACCOUNT,
                wasm,
                on_error_raise=False,
                session_args=ABI.args([encode("number", number)]),
            )

            for deploy_info in client.showDeploys(block_hash):
                exit_code = number + USER_ERROR_MIN
                assert deploy_info.is_error is True
                assert deploy_info.error_message == f"Exit code: {exit_code}"

    wasm = resources_path() / Contract.ARGS_MULTI
    account_hex = "0101010102020202030303030404040405050505060606060707070708080808"
    number = 1000
    total_sum = sum([1, 2, 3, 4, 5, 6, 7, 8]) * 4 + number

    block_hash = node.deploy_and_get_block_hash(
        GENESIS_ACCOUNT,
        wasm,
        on_error_raise=False,
        session_args=ABI.args(
            [ABI.account("account", account_hex),
             ABI.u32("number", number)]),
    )

    for deploy_info in client.showDeploys(block_hash):
        exit_code = total_sum + USER_ERROR_MIN
        assert deploy_info.is_error is True
        assert deploy_info.error_message == f"Exit code: {exit_code}"

    for blockInfo in client.showBlocks(10):
        assert blockInfo.status.stats.block_size_bytes > 0
def set_key_thresholds(node, weight_key, key_mgmt_weight: int, deploy_weight: int):
    """ Sets key management and deploy thresholds for IDENTITY_KEY account """
    args = ABI.args(
        [
            ABI.u32("key_mgmt_weight", key_mgmt_weight),
            ABI.u32("deploy_weight", deploy_weight),
        ]
    )
    return node.deploy_and_get_block_hash(
        weight_key,
        Contract.SET_KEY_THRESHOLDS,
        on_error_raise=False,
        from_addr=IDENTITY_KEY.public_key_hex,
        public_key=weight_key.public_key_path,
        private_key=weight_key.private_key_path,
        session_args=args,
    )
 def create_associate_deploy(acct_num: int):
     """ Add associated key of acct_num + 1 to acct_num account """
     acct = Account(acct_num)
     associated_acct = Account(acct_num + 1)
     args = ABI.args([
         ABI.account("account", associated_acct.public_key_binary),
         ABI.u32("amount", 1),
     ])
     return node.p_client.deploy(
         from_address=acct.public_key_hex,
         session_contract=Contract.ADD_ASSOCIATED_KEY,
         public_key=acct.public_key_path,
         private_key=acct.private_key_path,
         session_args=args,
     )
def _add_update_associate_key(
    node, weight_key: Account, key: Account, weight: int, contract: str
):
    """ Handles both add and update calls due to commonality """
    session_args = ABI.args(
        [ABI.account("account", key.public_key_hex), ABI.u32("amount", weight)]
    )
    return node.deploy_and_get_block_hash(
        weight_key,
        contract,
        on_error_raise=False,
        from_addr=IDENTITY_KEY.public_key_hex,
        public_key=weight_key.public_key_path,
        private_key=weight_key.private_key_path,
        session_args=session_args,
    )