Пример #1
0
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),
    ])
    _, deploy_hash = 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
Пример #2
0
def test_args_parser():
    account_hex = "0001000200030004000500060007000800000001000200030004000500060007"
    account_bytes = (
        b"\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07\x00\x08"
        b"\x00\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07")
    u32 = 1024
    u64 = 1234567890
    json_str = json.dumps([{
        "u32": u32
    }, {
        "account": account_hex
    }, {
        "u64": u64
    }])

    assert ABI.args_from_json(json_str) == ABI.args(
        [ABI.u32(1024),
         ABI.account(account_bytes),
         ABI.u64(1234567890)])
Пример #3
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)])

    response, deploy_hash_bytes = 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
Пример #4
0
    def transfer_to_account(
        self,
        to_account_id: int,
        amount: int,
        from_account_id: Union[str, int] = "genesis",
        session_contract: str = Contract.TRANSFER_TO_ACCOUNT,
        payment_contract: str = Contract.STANDARD_PAYMENT,
        payment_args: bytes = MAX_PAYMENT_ABI,
        gas_price: int = 1,
        is_deploy_error_check: bool = True,
    ) -> str:
        """
        Performs a transfer using the from account if given (or genesis if not)

        :param to_account_id: 1-20 index of test account for transfer into
        :param amount: amount of motes to transfer (mote = smallest unit of token)
        :param from_account_id: default 'genesis' account, but previously funded account_id is also valid.
        :param session_contract: session contract to execute.
        :param payment_contract: Payment contract to execute.
        :param payment_args: Payment Amount ABI
        :param gas_price: Gas price
        :param is_deploy_error_check: Check that amount transfer is success.

        :returns block_hash in hex str
        """
        logging.info(f"=== Transferring {amount} to {to_account_id}")

        assert (
            is_valid_account(to_account_id) and to_account_id != "genesis"
        ), "Can transfer only to non-genesis accounts in test framework (1-20)."
        assert is_valid_account(
            from_account_id
        ), "Must transfer from a valid account_id: 1-20 or 'genesis'"

        from_account = Account(from_account_id)
        to_account = Account(to_account_id)

        session_args = ABI.args([
            ABI.account("account", to_account.public_key_binary),
            ABI.u64("amount", amount),
        ])

        response, deploy_hash_bytes = self.p_client.deploy(
            from_address=from_account.public_key_hex,
            session_contract=session_contract,
            payment_contract=payment_contract,
            public_key=from_account.public_key_path,
            private_key=from_account.private_key_path,
            gas_price=gas_price,
            session_args=session_args,
            payment_args=payment_args,
        )

        deploy_hash_hex = deploy_hash_bytes.hex()
        assert len(deploy_hash_hex) == 64

        response = self.p_client.propose()

        block_hash = response.block_hash.hex()
        assert len(deploy_hash_hex) == 64

        if is_deploy_error_check:
            for deploy_info in self.p_client.show_deploys(block_hash):
                if deploy_info.is_error:
                    raise Exception(
                        f"transfer_to_account: {deploy_info.error_message}")

        return block_hash