Skip to content

Commit

Permalink
more use of RpcClient.create_as_context() (#18903)
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky authored Nov 21, 2024
1 parent f2ef329 commit ad8bac3
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 303 deletions.
40 changes: 10 additions & 30 deletions chia/_tests/core/data_layer/test_data_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2125,17 +2125,13 @@ async def test_clear_pending_roots(
# https://github.com/python/cpython/issues/92841
assert stderr == b"" or b"_ProactorBasePipeTransport.__del__" in stderr
elif layer == InterfaceLayer.client:
client = await DataLayerRpcClient.create(
async with DataLayerRpcClient.create_as_context(
self_hostname=self_hostname,
port=rpc_port,
root_path=bt.root_path,
net_config=bt.config,
)
try:
) as client:
cleared_root = await client.clear_pending_roots(store_id=store_id)
finally:
client.close()
await client.await_closed()
else: # pragma: no cover
assert False, "unhandled parametrization"

Expand Down Expand Up @@ -2392,17 +2388,13 @@ async def test_wallet_log_in_changes_active_fingerprint(
if layer == InterfaceLayer.direct:
await data_rpc_api.wallet_log_in({"fingerprint": secondary_fingerprint})
elif layer == InterfaceLayer.client:
client = await DataLayerRpcClient.create(
async with DataLayerRpcClient.create_as_context(
self_hostname=self_hostname,
port=rpc_port,
root_path=bt.root_path,
net_config=bt.config,
)
try:
) as client:
await client.wallet_log_in(fingerprint=secondary_fingerprint)
finally:
client.close()
await client.await_closed()
elif layer == InterfaceLayer.funcs:
await wallet_log_in_cmd(rpc_port=rpc_port, fingerprint=secondary_fingerprint, root_path=bt.root_path)
elif layer == InterfaceLayer.cli:
Expand Down Expand Up @@ -3146,13 +3138,12 @@ async def test_pagination_cmds(
# https://github.com/python/cpython/issues/92841
assert stderr == b"" or b"_ProactorBasePipeTransport.__del__" in stderr
elif layer == InterfaceLayer.client:
client = await DataLayerRpcClient.create(
async with DataLayerRpcClient.create_as_context(
self_hostname=self_hostname,
port=rpc_port,
root_path=bt.root_path,
net_config=bt.config,
)
try:
) as client:
keys = await client.get_keys(
store_id=store_id,
root_hash=None,
Expand All @@ -3172,9 +3163,6 @@ async def test_pagination_cmds(
page=0,
max_page_size=max_page_size,
)
finally:
client.close()
await client.await_closed()
else: # pragma: no cover
assert False, "unhandled parametrization"
if max_page_size is None or max_page_size == 100:
Expand Down Expand Up @@ -3330,23 +3318,19 @@ async def test_unsubmitted_batch_update(
assert stderr == b"" or b"_ProactorBasePipeTransport.__del__" in stderr
assert res == {"success": True}
elif layer == InterfaceLayer.client:
client = await DataLayerRpcClient.create(
async with DataLayerRpcClient.create_as_context(
self_hostname=self_hostname,
port=rpc_port,
root_path=bt.root_path,
net_config=bt.config,
)
try:
) as client:
res = await client.update_data_store(
store_id=store_id,
changelist=changelist,
fee=None,
submit_on_chain=False,
)
assert res == {"success": True}
finally:
client.close()
await client.await_closed()
else: # pragma: no cover
assert False, "unhandled parametrization"

Expand Down Expand Up @@ -3473,18 +3457,14 @@ async def test_unsubmitted_batch_update(
assert stderr == b"" or b"_ProactorBasePipeTransport.__del__" in stderr
update_tx_rec1 = bytes32.from_hexstr(res["tx_id"])
elif layer == InterfaceLayer.client:
client = await DataLayerRpcClient.create(
async with DataLayerRpcClient.create_as_context(
self_hostname=self_hostname,
port=rpc_port,
root_path=bt.root_path,
net_config=bt.config,
)
try:
) as client:
res = await client.submit_pending_root(store_id=store_id, fee=None)
update_tx_rec1 = bytes32.from_hexstr(res["tx_id"])
finally:
client.close()
await client.await_closed()
else: # pragma: no cover
assert False, "unhandled parametrization"

Expand Down
89 changes: 30 additions & 59 deletions chia/_tests/core/test_full_node_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ async def test1(two_nodes_sim_and_wallets_services, self_hostname, consensus_mod
full_node_api_2 = full_node_service_2._api
server_2 = full_node_api_2.full_node.server

try:
client = await FullNodeRpcClient.create(
self_hostname,
full_node_service_1.rpc_server.listen_port,
full_node_service_1.root_path,
full_node_service_1.config,
)
async with FullNodeRpcClient.create_as_context(
self_hostname,
full_node_service_1.rpc_server.listen_port,
full_node_service_1.root_path,
full_node_service_1.config,
) as client:
await validate_get_routes(client, full_node_service_1.rpc_server.rpc_api)
state = await client.get_blockchain_state()
assert state["peak"] is None
Expand Down Expand Up @@ -426,11 +425,6 @@ async def num_connections():
assert blocks[2].header_hash == new_blocks[2].header_hash
assert blocks[3].header_hash != new_blocks[3].header_hash

finally:
# Checks that the RPC manages to stop the node
client.close()
await client.await_closed()


@pytest.mark.anyio
async def test_signage_points(two_nodes_sim_and_wallets_services, empty_blockchain):
Expand All @@ -446,14 +440,12 @@ async def test_signage_points(two_nodes_sim_and_wallets_services, empty_blockcha

peer = await connect_and_get_peer(server_1, server_2, self_hostname)

try:
client = await FullNodeRpcClient.create(
self_hostname,
full_node_service_1.rpc_server.listen_port,
full_node_service_1.root_path,
full_node_service_1.config,
)

async with FullNodeRpcClient.create_as_context(
self_hostname,
full_node_service_1.rpc_server.listen_port,
full_node_service_1.root_path,
full_node_service_1.config,
) as client:
# Only provide one
res = await client.get_recent_signage_point_or_eos(None, None)
assert res is None
Expand Down Expand Up @@ -556,11 +548,6 @@ async def test_signage_points(two_nodes_sim_and_wallets_services, empty_blockcha
assert res["eos"] == selected_eos
assert res["reverted"]

finally:
# Checks that the RPC manages to stop the node
client.close()
await client.await_closed()


@pytest.mark.anyio
async def test_get_network_info(one_wallet_and_one_simulator_services, self_hostname):
Expand Down Expand Up @@ -689,56 +676,44 @@ async def test_get_blockchain_state(one_wallet_and_one_simulator_services, self_
async def test_coin_name_not_in_request(one_node, self_hostname):
[full_node_service], _, _ = one_node

try:
client = await FullNodeRpcClient.create(
self_hostname,
full_node_service.rpc_server.listen_port,
full_node_service.root_path,
full_node_service.config,
)
async with FullNodeRpcClient.create_as_context(
self_hostname,
full_node_service.rpc_server.listen_port,
full_node_service.root_path,
full_node_service.config,
) as client:
with pytest.raises(ValueError, match="No coin_name in request"):
await client.fetch("get_mempool_items_by_coin_name", {})
finally:
# Checks that the RPC manages to stop the node
client.close()
await client.await_closed()


@pytest.mark.anyio
async def test_coin_name_not_found_in_mempool(one_node, self_hostname):
[full_node_service], _, _ = one_node

try:
client = await FullNodeRpcClient.create(
self_hostname,
full_node_service.rpc_server.listen_port,
full_node_service.root_path,
full_node_service.config,
)

async with FullNodeRpcClient.create_as_context(
self_hostname,
full_node_service.rpc_server.listen_port,
full_node_service.root_path,
full_node_service.config,
) as client:
empty_coin_name = bytes32.zeros
mempool_item = await client.get_mempool_items_by_coin_name(empty_coin_name)
assert mempool_item["success"]
assert "mempool_items" in mempool_item
assert len(mempool_item["mempool_items"]) == 0
finally:
client.close()
await client.await_closed()


@pytest.mark.anyio
async def test_coin_name_found_in_mempool(one_node, self_hostname):
[full_node_service], _, bt = one_node
full_node_api = full_node_service._api

try:
client = await FullNodeRpcClient.create(
self_hostname,
full_node_service.rpc_server.listen_port,
full_node_service.root_path,
full_node_service.config,
)

async with FullNodeRpcClient.create_as_context(
self_hostname,
full_node_service.rpc_server.listen_port,
full_node_service.root_path,
full_node_service.config,
) as client:
blocks = bt.get_consecutive_blocks(2)
blocks = bt.get_consecutive_blocks(2, block_list_input=blocks, guarantee_transaction_block=True)

Expand Down Expand Up @@ -791,7 +766,3 @@ async def test_coin_name_found_in_mempool(one_node, self_hostname):
for item in mempool_item["mempool_items"]:
removals = [Coin.from_json_dict(coin) for coin in item["removals"]]
assert coin_to_spend.name() in [coin.name() for coin in removals]

finally:
client.close()
await client.await_closed()
Loading

0 comments on commit ad8bac3

Please sign in to comment.