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), ABI.u32(deploy_weight)]) return node.p_client.deploy_and_propose( from_address=IDENTITY_KEY.public_key_hex, session_contract=Contract.SET_KEY_THRESHOLDS, public_key=weight_key.public_key_path, private_key=weight_key.private_key_path, session_args=args, )
def transfer_to_account( node, from_address: str, to_address: str, amount: int, public_key: str, private_key: str, gas_price: int = 1, session_contract=Contract.TRANSFER_TO_ACCOUNT, ): session_args = ABI.args( [ABI.account(bytes.fromhex(to_address)), ABI.u32(amount)]) _, deploy_hash_bytes = node.p_client.deploy( from_address=from_address, session_contract=session_contract, public_key=public_key, private_key=private_key, gas_price=gas_price, session_args=session_args, ) deploy_hash_hex = deploy_hash_bytes.hex() deploy_hash_hex = deploy_hash_hex block_hash = node.p_client.propose().block_hash.hex() for deploy_info in node.p_client.show_deploys(block_hash): if deploy_info.is_error: raise Exception( f"transfer_to_account: {deploy_info.error_message}") return block_hash
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(bytes.fromhex(GENESIS_ACCOUNT.public_key_hex)), ABI.u32(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(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
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.p_client.deploy_and_propose( from_address=IDENTITY_KEY.public_key_hex, session_contract=contract, public_key=weight_key.public_key_path, private_key=weight_key.private_key_path, session_args=session_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(associated_acct.public_key_binary), ABI.u32(1)]) _, deploy_hash_bytes = 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, ) return deploy_hash_bytes.hex()
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)])
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(bytes.fromhex(to_account.public_key_hex)), ABI.u32(10**7) ]) payment_args = ABI.args([ABI.u512(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.ERR_STANDARD_PAYMENT, 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
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 transfer_to_account( self, to_account_id: int, amount: int, from_account_id: Union[str, int] = "genesis", session_contract: str = Contract.TRANSFER_TO_ACCOUNT_IT, 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.u32("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
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 test_args_u32.wasm and test_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.u32), (resources_path() / Contract.ARGS_U512, ABI.u512), ]: for number in [1, 12, 256, 1024]: response, deploy_hash = client.deploy( session=wasm, session_args=ABI.args([encode(number)]), payment=resources_path() / Contract.STANDARD_PAYMENT, payment_args=MAX_PAYMENT_ABI, public_key=GENESIS_ACCOUNT.public_key_path, private_key=GENESIS_ACCOUNT.private_key_path, ) logging.info( f"DEPLOY RESPONSE: {response} deploy_hash: {deploy_hash.hex()}" ) response = client.propose() # Need to convert to hex string from bytes block_hash = response.block_hash.hex() for deploy_info in client.showDeploys(block_hash): assert deploy_info.is_error is True assert deploy_info.error_message == f"Exit code: {number}" wasm = resources_path() / Contract.ARGS_MULTI account_hex = "0101010102020202030303030404040405050505060606060707070708080808" number = 1000 total_sum = sum([1, 2, 3, 4, 5, 6, 7, 8]) * 4 + number response, deploy_hash = client.deploy( session=wasm, session_args=ABI.args( [ABI.account(bytes.fromhex(account_hex)), ABI.u32(number)] ), payment=resources_path() / Contract.STANDARD_PAYMENT, payment_args=MAX_PAYMENT_ABI, public_key=GENESIS_ACCOUNT.public_key_path, private_key=GENESIS_ACCOUNT.private_key_path, ) logging.info(f"DEPLOY RESPONSE: {response} deploy_hash: {deploy_hash.hex()}") response = client.propose() block_hash = response.block_hash.hex() for deploy_info in client.showDeploys(block_hash): assert deploy_info.is_error is True assert deploy_info.error_message == f"Exit code: {total_sum}" for blockInfo in client.showBlocks(10): assert blockInfo.status.stats.block_size_bytes > 0