add block height assertions after block farming (#7526)

add tx confirmed assertions
use time_out_assert only when needed
This commit is contained in:
Almog De Paz 2021-07-28 00:21:19 +03:00 committed by GitHub
parent 8e60a9b357
commit 85fd9f7d89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,34 @@ def event_loop():
yield loop
async def is_transaction_in_mempool(user_wallet_id, api, tx_id: bytes32) -> bool:
try:
val = await api.get_transaction({"wallet_id": user_wallet_id, "transaction_id": tx_id.hex()})
except ValueError:
return False
for _, mis, _ in val["transaction"].sent_to:
if (
MempoolInclusionStatus(mis) == MempoolInclusionStatus.SUCCESS
or MempoolInclusionStatus(mis) == MempoolInclusionStatus.PENDING
):
return True
return False
async def is_transaction_confirmed(user_wallet_id, api, tx_id: bytes32) -> bool:
try:
val = await api.get_transaction({"wallet_id": user_wallet_id, "transaction_id": tx_id.hex()})
except ValueError:
return False
return val["transaction"].confirmed
async def check_balance(api, wallet_id):
balance_response = await api.get_wallet_balance({"wallet_id": wallet_id})
balance = balance_response["wallet_balance"]["confirmed_wallet_balance"]
return balance
class TestRLWallet:
@pytest.fixture(scope="function")
async def three_wallet_nodes(self):
@ -46,8 +74,8 @@ class TestRLWallet:
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(ph))
for i in range(0, num_blocks + 1):
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, wallet_height_at_least, True, wallet_node, 6)
fund_owners_initial_balance = await wallet.get_confirmed_balance()
api_user = WalletRpcApi(wallet_node_1)
val = await api_user.create_new_wallet(
{"wallet_type": "rl_wallet", "rl_type": "user", "host": f"{self_hostname}:5000"}
@ -103,67 +131,47 @@ class TestRLWallet:
] == 0
for i in range(0, 2 * num_blocks):
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, wallet_height_at_least, True, wallet_node, 14)
assert await wallet.get_confirmed_balance() == fund_owners_initial_balance - 101
async def check_balance(api, wallet_id):
balance_response = await api.get_wallet_balance({"wallet_id": wallet_id})
balance = balance_response["wallet_balance"]["confirmed_wallet_balance"]
return balance
await time_out_assert(15, check_balance, 100, api_user, user_wallet_id)
assert await check_balance(api_user, user_wallet_id) == 100
receiving_wallet = wallet_node_2.wallet_state_manager.main_wallet
address = encode_puzzle_hash(await receiving_wallet.get_new_puzzlehash(), "xch")
assert await receiving_wallet.get_spendable_balance() == 0
val = await api_user.send_transaction({"wallet_id": user_wallet_id, "amount": 3, "fee": 2, "address": address})
assert "transaction_id" in val
async def is_transaction_in_mempool(api, tx_id: bytes32) -> bool:
try:
val = await api.get_transaction({"wallet_id": user_wallet_id, "transaction_id": tx_id.hex()})
except ValueError:
return False
for _, mis, _ in val["transaction"].sent_to:
if (
MempoolInclusionStatus(mis) == MempoolInclusionStatus.SUCCESS
or MempoolInclusionStatus(mis) == MempoolInclusionStatus.PENDING
):
return True
return False
await time_out_assert(15, is_transaction_in_mempool, True, api_user, val["transaction_id"])
await time_out_assert(15, is_transaction_in_mempool, True, user_wallet_id, api_user, val["transaction_id"])
for i in range(0, num_blocks):
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, check_balance, 95, api_user, user_wallet_id)
await time_out_assert(15, receiving_wallet.get_spendable_balance, 3)
await time_out_assert(15, wallet_height_at_least, True, wallet_node, 18)
assert await is_transaction_confirmed(user_wallet_id, api_user, val["transaction_id"])
assert await check_balance(api_user, user_wallet_id) == 95
assert await receiving_wallet.get_spendable_balance() == 3
val = await api_admin.add_rate_limited_funds({"wallet_id": admin_wallet_id, "amount": 100, "fee": 7})
assert val["status"] == "SUCCESS"
for i in range(0, 50):
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, check_balance, 195, api_user, user_wallet_id)
await time_out_assert(15, wallet_height_at_least, True, wallet_node, 68)
assert await check_balance(api_user, user_wallet_id) == 195
# test spending
puzzle_hash = encode_puzzle_hash(await receiving_wallet.get_new_puzzlehash(), "xch")
val = await api_user.send_transaction(
{
"wallet_id": user_wallet_id,
"amount": 105,
"fee": 0,
"address": puzzle_hash,
}
{"wallet_id": user_wallet_id, "amount": 105, "fee": 0, "address": puzzle_hash}
)
await time_out_assert(15, is_transaction_in_mempool, True, api_user, val["transaction_id"])
await time_out_assert(15, is_transaction_in_mempool, True, user_wallet_id, api_user, val["transaction_id"])
for i in range(0, num_blocks):
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, check_balance, 90, api_user, user_wallet_id)
await time_out_assert(15, receiving_wallet.get_spendable_balance, 108)
await time_out_assert(15, wallet_height_at_least, True, wallet_node, 72)
assert await is_transaction_confirmed(user_wallet_id, api_user, val["transaction_id"])
assert await check_balance(api_user, user_wallet_id) == 90
assert await receiving_wallet.get_spendable_balance() == 108
val = await api_admin.send_clawback_transaction({"wallet_id": admin_wallet_id, "fee": 11})
await time_out_assert(15, is_transaction_in_mempool, True, api_admin, val["transaction_id"])
await time_out_assert(15, is_transaction_in_mempool, True, user_wallet_id, api_admin, val["transaction_id"])
for i in range(0, num_blocks):
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, check_balance, 0, api_user, user_wallet_id)
await time_out_assert(15, check_balance, 0, api_admin, user_wallet_id)
await time_out_assert(15, wallet_height_at_least, True, wallet_node, 76)
assert await is_transaction_confirmed(user_wallet_id, api_admin, val["transaction_id"])
assert await check_balance(api_user, user_wallet_id) == 0
final_balance = await wallet.get_confirmed_balance()
assert final_balance == fund_owners_initial_balance - 129