def test_invalid_unbonding(one_node_network_fn): """ Feature file: consensus.feature Scenario: unbonding a bonded validator node from an existing network. """ def get_bonded_list(node, block): return list( filter( lambda x: x.stake == bonding_amount and x.validator_public_key == node.genesis_account.public_key_hex, block.summary.header.state.bonds, )) bonding_amount = 2000 assert_pre_state_of_network(one_node_network_fn, [bonding_amount]) block_hash = bond_to_the_network(one_node_network_fn, Contract.BONDING_CALL, bonding_amount) assert block_hash is not None node0 = one_node_network_fn.docker_nodes[0] r = node0.d_client.show_deploys(block_hash)[0] assert r.is_error is False block = node0.d_client.show_block(block_hash) block_ds = parse_show_block(block) bonded_list = get_bonded_list(node0, block_ds) assert len(bonded_list) == 1 node1 = one_node_network_fn.docker_nodes[1] block_hash2 = node1.unbond( session_contract=Contract.UNBONDING_CALL, maybe_amount=1985, # 1985 > (2000+190) * 0.9 ) assert block_hash2 is not None r = node1.d_client.show_deploys(block_hash2)[0] assert r.is_error is True # PoS Error::UnbondTooLarge is 65287 assert r.error_message == "Exit code: 65287" block2 = node1.d_client.show_block(block_hash2) block_ds = parse_show_block(block2) bonded_list = get_bonded_list(node1, block_ds) assert len(bonded_list) == 1 block_hash2 = node1.unbond(session_contract=Contract.UNBONDING_CALL, maybe_amount=None) assert block_hash2 is not None r = node1.d_client.show_deploys(block_hash2)[0] assert r.is_error is True assert r.error_message == "Exit code: 65287" block2 = node1.d_client.show_block(block_hash2) block_ds = parse_show_block(block2) bonded_list = get_bonded_list(node1, block_ds) assert len(bonded_list) == 1
def test_invalid_bonding(one_node_network_fn): """ Feature file: consensus.feature Scenario: Bonding a validator node to an existing network. """ # 190 is current total staked amount. bonding_amount = (190 * 1000) + 1 block_hash = bond_to_the_network(one_node_network_fn, Contract.BONDING_CALL, bonding_amount) assert block_hash is not None node1 = one_node_network_fn.docker_nodes[1] block1 = node1.d_client.show_block(block_hash) r = node1.d_client.show_deploys(block_hash)[0] assert r.is_error is True # PoS Error::BondTooLarge is 65286 assert r.error_message == "Exit code: 65286" block_ds = parse_show_block(block1) public_key = node1.genesis_account.public_key_hex item = list( filter( lambda x: x.stake == bonding_amount and x.validator_public_key == public_key, block_ds.summary.header.state.bonds, )) assert len(item) == 0
def test_double_bonding(one_node_network_fn): """ Feature file: consensus.feature Scenario: Bonding a validator node twice to an existing network. """ bonding_amount = 1 stakes = [1, 2] assert_pre_state_of_network(one_node_network_fn, stakes) block_hash = bond_to_the_network(one_node_network_fn, Contract.BONDING_CALL, bonding_amount) assert block_hash is not None node1 = one_node_network_fn.docker_nodes[1] block_hash = node1.bond(session_contract=Contract.BONDING_CALL, amount=bonding_amount) assert block_hash is not None r = node1.client.show_deploys(block_hash)[0] assert r.is_error is False assert r.error_message == "" node1 = one_node_network_fn.docker_nodes[1] block1 = node1.client.show_block(block_hash) block_ds = parse_show_block(block1) public_key = node1.genesis_account.public_key_hex item = list( filter( lambda x: x.stake == bonding_amount + bonding_amount and x. validator_public_key == public_key, block_ds.summary.header.state.bonds, )) assert len(item) == 1
def test_unbonding_without_bonding(one_node_network_fn): """ Feature file: consensus.feature Scenario: unbonding a validator node which was not bonded to an existing network. """ bonding_amount = 1 assert_pre_state_of_network(one_node_network_fn, [bonding_amount]) one_node_network_fn.add_new_node_to_network() assert (len(one_node_network_fn.docker_nodes) == 2 ), "Total number of nodes should be 2." node0, node1 = one_node_network_fn.docker_nodes[:2] public_key = node1.genesis_account.public_key_hex block_hash = node1.unbond(session_contract=Contract.UNBONDING_CALL, maybe_amount=None) assert block_hash is not None r = node1.client.show_deploys(block_hash)[0] assert r.is_error is True # PoS Error::NotBonded is 65280 assert r.error_message == "Exit code: 65280" block2 = node1.client.show_block(block_hash) block_ds = parse_show_block(block2) item = list( filter( lambda x: x.validator_public_key == public_key, block_ds.summary.header.state.bonds, )) assert len(item) == 0
def test_partial_amount_unbonding(one_node_network_fn): """ Feature file: consensus.feature Scenario: unbonding a bonded validator node with partial bonding amount from an existing network. """ bonding_amount = 11 unbond_amount = 4 assert_pre_state_of_network( one_node_network_fn, [bonding_amount, unbond_amount, bonding_amount - unbond_amount], ) block_hash = bond_to_the_network(one_node_network_fn, Contract.BONDING_CALL, bonding_amount) assert block_hash is not None node1 = one_node_network_fn.docker_nodes[1] public_key = node1.genesis_account.public_key_hex block_hash2 = node1.unbond(session_contract=Contract.UNBONDING_CALL, maybe_amount=unbond_amount) r = node1.client.show_deploys(block_hash2)[0] assert r.is_error is False assert r.error_message == "" assert block_hash2 is not None block2 = node1.client.show_block(block_hash2) block_ds = parse_show_block(block2) item = list( filter( lambda x: x.stake == bonding_amount - unbond_amount and x. validator_public_key == public_key, block_ds.summary.header.state.bonds, )) assert len(item) == 1
def test_unbonding_without_bonding(one_node_network_fn): """ Feature file: consensus.feature Scenario: unbonding a validator node which was not bonded to an existing network. """ bonding_amount = 1 account = Account(BONDING_ACCT) assert_pre_state_of_network(one_node_network_fn) add_funded_account_to_network(one_node_network_fn, BONDING_ACCT) assert ( len(one_node_network_fn.docker_nodes) == 2 ), "Total number of nodes should be 2." node0, node1 = one_node_network_fn.docker_nodes r = node0.d_client.unbond(bonding_amount, account.private_key_docker_path) assert "Success!" in r r = node0.d_client.propose() block_hash = extract_block_hash_from_propose_output(r) assert block_hash is not None # block_hash, account = unbond_from_network(one_node_network_fn, bonding_amount, BONDING_ACCT) r = node0.client.show_deploys(block_hash)[0] assert r.is_error is True assert r.error_message == "Exit code: 65280" block = node1.client.show_block(block_hash) block_ds = parse_show_block(block) bonds = list( filter( lambda x: x.validator_public_key == account.public_key_hex, block_ds.summary.header.state.bonds, ) ) assert len(bonds) == 0
def bonds_by_account_and_stake(node, block_hash: str, bonding_amount: int, public_key: str): block = node.d_client.show_block(block_hash) block_ds = parse_show_block(block) bonds = list( filter( lambda x: int(x.stake.value) == bonding_amount and x. validator_public_key == public_key, block_ds.summary.header.state.bonds, )) return bonds
def test_unbonding_without_bonding(one_node_network_fn, acct_num, cli_method): """ Feature file: consensus.feature Scenario: unbonding a validator node which was not bonded to an existing network. """ onn = one_node_network_fn bonding_amount = 1 account = Account(acct_num) assert_pre_state_of_network(onn) add_funded_account_to_network(onn, acct_num) assert len(onn.docker_nodes) == 2, "Total number of nodes should be 2." node0, node1 = onn.docker_nodes cli = cli_method(node0) deploy_hash = cli( "unbond", "--amount", bonding_amount, "--private-key", cli.private_key_path(account), "--payment-amount", EXECUTION_PAYMENT_AMOUNT, ) block_hash = cli.node.wait_for_deploy_processed_and_get_block_hash( deploy_hash, on_error_raise=False) r = node0.client.show_deploys(block_hash)[0] assert r.is_error is True assert r.error_message == "PoS error: 0" block = node1.client.show_block(block_hash) block_ds = parse_show_block(block) bonds = list( filter( lambda x: x.validator_public_key == account.public_key_hex, block_ds.summary.header.state.bonds, )) assert len(bonds) == 0