Fix some more tests

This commit is contained in:
Mariano Sorgente 2020-06-18 15:35:13 +09:00 committed by Gene Hoffman
parent 116ee138b8
commit fa39350758
8 changed files with 81 additions and 62 deletions

View File

@ -214,7 +214,9 @@ class RpcServer:
await asyncio.sleep(1)
async def start_rpc_server(rpc_api: Any, rpc_port: uint16, stop_cb: Callable):
async def start_rpc_server(
rpc_api: Any, rpc_port: uint16, stop_cb: Callable, connect_to_daemon=True
):
"""
Starts an HTTP server with the following RPC methods, to be used by local clients to
query the node.
@ -247,7 +249,8 @@ async def start_rpc_server(rpc_api: Any, rpc_port: uint16, stop_cb: Callable):
]
app.add_routes(routes)
daemon_connection = asyncio.create_task(rpc_server.connect_to_daemon())
if connect_to_daemon:
daemon_connection = asyncio.create_task(rpc_server.connect_to_daemon())
runner = aiohttp.web.AppRunner(app, access_log=None)
await runner.setup()
site = aiohttp.web.TCPSite(runner, "localhost", int(rpc_port))
@ -256,6 +259,7 @@ async def start_rpc_server(rpc_api: Any, rpc_port: uint16, stop_cb: Callable):
async def cleanup():
await rpc_server.stop()
await runner.cleanup()
await daemon_connection
if connect_to_daemon:
await daemon_connection
return cleanup

View File

@ -124,6 +124,7 @@ class Service:
self._stop_callback = stop_callback
self._await_closed_callback = await_closed_callback
self._advertised_port = advertised_port
self._server_sockets: List = []
def start(self):
if self._task is not None:

View File

@ -73,7 +73,7 @@ class TestWalletSimulator:
]
)
await self.time_out_assert(15, wallet.get_confirmed_balance, funds)
await time_out_assert(15, wallet.get_confirmed_balance, funds)
cc_wallet: CCWallet = await CCWallet.create_new_cc(
wallet_node.wallet_state_manager, wallet, uint64(100)
@ -82,8 +82,8 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
@pytest.mark.asyncio
async def test_cc_spend(self, two_wallet_nodes):
@ -110,7 +110,7 @@ class TestWalletSimulator:
]
)
await self.time_out_assert(15, wallet.get_confirmed_balance, funds)
await time_out_assert(15, wallet.get_confirmed_balance, funds)
cc_wallet: CCWallet = await CCWallet.create_new_cc(
wallet_node.wallet_state_manager, wallet, uint64(100)
@ -119,8 +119,8 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
assert cc_wallet.cc_info.my_core is not None
colour = cc_wallet_puzzles.get_genesis_from_core(cc_wallet.cc_info.my_core)
@ -137,11 +137,11 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 40)
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 40)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 40)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 40)
await self.time_out_assert(30, cc_wallet_2.get_confirmed_balance, 60)
await self.time_out_assert(30, cc_wallet_2.get_unconfirmed_balance, 60)
await time_out_assert(30, cc_wallet_2.get_confirmed_balance, 60)
await time_out_assert(30, cc_wallet_2.get_unconfirmed_balance, 60)
cc_hash = await cc_wallet.get_new_inner_hash()
await cc_wallet_2.cc_spend(uint64(15), cc_hash)
@ -149,8 +149,8 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 55)
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 55)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 55)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 55)
@pytest.mark.asyncio
async def test_get_wallet_for_colour(self, two_wallet_nodes):
@ -174,7 +174,7 @@ class TestWalletSimulator:
]
)
await self.time_out_assert(15, wallet.get_confirmed_balance, funds)
await time_out_assert(15, wallet.get_confirmed_balance, funds)
cc_wallet: CCWallet = await CCWallet.create_new_cc(
wallet_node.wallet_state_manager, wallet, uint64(100)
@ -212,7 +212,7 @@ class TestWalletSimulator:
for i in range(1, num_blocks - 2)
]
)
await self.time_out_assert(15, wallet.get_confirmed_balance, funds)
await time_out_assert(15, wallet.get_confirmed_balance, funds)
cc_wallet: CCWallet = await CCWallet.create_new_cc(
wallet_node.wallet_state_manager, wallet, uint64(100)
@ -222,8 +222,8 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
assert cc_wallet.cc_info.my_core is not None
colour = cc_wallet_puzzles.get_genesis_from_core(cc_wallet.cc_info.my_core)
@ -273,7 +273,7 @@ class TestWalletSimulator:
]
)
await self.time_out_assert(15, wallet.get_confirmed_balance, funds)
await time_out_assert(15, wallet.get_confirmed_balance, funds)
cc_wallet: CCWallet = await CCWallet.create_new_cc(
wallet_node.wallet_state_manager, wallet, uint64(100)
@ -282,8 +282,8 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
assert cc_wallet.cc_info.my_core is not None
colour = cc_wallet_puzzles.get_genesis_from_core(cc_wallet.cc_info.my_core)
@ -335,8 +335,8 @@ class TestWalletSimulator:
for i in range(0, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(token_bytes()))
await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30)
await self.time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 30)
await time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30)
await time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 30)
@pytest.mark.asyncio
async def test_cc_spend_uncoloured(self, two_wallet_nodes):
@ -363,7 +363,7 @@ class TestWalletSimulator:
]
)
await self.time_out_assert(15, wallet.get_confirmed_balance, funds)
await time_out_assert(15, wallet.get_confirmed_balance, funds)
cc_wallet: CCWallet = await CCWallet.create_new_cc(
wallet_node.wallet_state_manager, wallet, uint64(100)
@ -372,8 +372,8 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
assert cc_wallet.cc_info.my_core is not None
colour = cc_wallet_puzzles.get_genesis_from_core(cc_wallet.cc_info.my_core)
@ -390,11 +390,11 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 40)
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 40)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 40)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 40)
await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 60)
await self.time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 60)
await time_out_assert(15, cc_wallet_2.get_confirmed_balance, 60)
await time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 60)
cc2_ph = await cc_wallet_2.get_new_cc_puzzle_hash()
spend_bundle = await wallet.wallet_state_manager.main_wallet.generate_signed_transaction(
@ -407,9 +407,9 @@ class TestWalletSimulator:
id = cc_wallet_2.wallet_info.id
wsm = cc_wallet_2.wallet_state_manager
await self.time_out_assert(15, wsm.get_confirmed_balance_for_wallet, 70, id)
await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 60)
await self.time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 60)
await time_out_assert(15, wsm.get_confirmed_balance_for_wallet, 70, id)
await time_out_assert(15, cc_wallet_2.get_confirmed_balance, 60)
await time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 60)
@pytest.mark.asyncio
async def test_cc_trade_with_multiple_colours(self, two_wallet_nodes):
@ -437,7 +437,7 @@ class TestWalletSimulator:
]
)
await self.time_out_assert(15, wallet.get_confirmed_balance, funds)
await time_out_assert(15, wallet.get_confirmed_balance, funds)
red_wallet: CCWallet = await CCWallet.create_new_cc(
wallet_node.wallet_state_manager, wallet, uint64(100)
@ -446,8 +446,8 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, red_wallet.get_confirmed_balance, 100)
await self.time_out_assert(15, red_wallet.get_unconfirmed_balance, 100)
await time_out_assert(15, red_wallet.get_confirmed_balance, 100)
await time_out_assert(15, red_wallet.get_unconfirmed_balance, 100)
assert red_wallet.cc_info.my_core is not None
red = cc_wallet_puzzles.get_genesis_from_core(red_wallet.cc_info.my_core)
@ -519,17 +519,17 @@ class TestWalletSimulator:
for i in range(0, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(token_bytes()))
await self.time_out_assert(15, red_wallet_2.get_confirmed_balance, 30)
await self.time_out_assert(15, red_wallet_2.get_unconfirmed_balance, 30)
await time_out_assert(15, red_wallet_2.get_confirmed_balance, 30)
await time_out_assert(15, red_wallet_2.get_unconfirmed_balance, 30)
await self.time_out_assert(15, blue_wallet_2.get_confirmed_balance, 100)
await self.time_out_assert(15, blue_wallet_2.get_unconfirmed_balance, 100)
await time_out_assert(15, blue_wallet_2.get_confirmed_balance, 100)
await time_out_assert(15, blue_wallet_2.get_unconfirmed_balance, 100)
await self.time_out_assert(15, blue_wallet.get_confirmed_balance, 50)
await self.time_out_assert(15, blue_wallet.get_unconfirmed_balance, 50)
await time_out_assert(15, blue_wallet.get_confirmed_balance, 50)
await time_out_assert(15, blue_wallet.get_unconfirmed_balance, 50)
await self.time_out_assert(15, red_wallet.get_confirmed_balance, 70)
await self.time_out_assert(15, red_wallet.get_unconfirmed_balance, 70)
await time_out_assert(15, red_wallet.get_confirmed_balance, 70)
await time_out_assert(15, red_wallet.get_unconfirmed_balance, 70)
@pytest.mark.asyncio
async def test_create_offer_with_zero_val(self, two_wallet_nodes):
@ -557,7 +557,7 @@ class TestWalletSimulator:
]
)
await self.time_out_assert(15, wallet.get_confirmed_balance, funds)
await time_out_assert(15, wallet.get_confirmed_balance, funds)
cc_wallet: CCWallet = await CCWallet.create_new_cc(
wallet_node.wallet_state_manager, wallet, uint64(100)
@ -566,8 +566,8 @@ class TestWalletSimulator:
for i in range(1, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100)
await time_out_assert(15, cc_wallet.get_confirmed_balance, 100)
assert cc_wallet.cc_info.my_core is not None
colour = cc_wallet_puzzles.get_genesis_from_core(cc_wallet.cc_info.my_core)
@ -619,5 +619,5 @@ class TestWalletSimulator:
for i in range(0, num_blocks):
await full_node_1.farm_new_block(FarmNewBlockProtocol(token_bytes()))
await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30)
await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30)
await time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30)
await time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30)

View File

@ -44,10 +44,10 @@ class TestRpc:
harvester_rpc_api = HarvesterRpcApi(harvester)
rpc_cleanup = await start_rpc_server(
farmer_rpc_api, test_rpc_port, stop_node_cb
farmer_rpc_api, test_rpc_port, stop_node_cb, connect_to_daemon=False
)
rpc_cleanup_2 = await start_rpc_server(
harvester_rpc_api, test_rpc_port_2, stop_node_cb_2
harvester_rpc_api, test_rpc_port_2, stop_node_cb_2, connect_to_daemon=False
)
try:

View File

@ -47,7 +47,7 @@ class TestRpc:
full_node_rpc_api = FullNodeRpcApi(full_node_1)
rpc_cleanup = await start_rpc_server(
full_node_rpc_api, test_rpc_port, stop_node_cb
full_node_rpc_api, test_rpc_port, stop_node_cb, connect_to_daemon=False
)
try:

View File

@ -1,7 +1,7 @@
import asyncio
import signal
from typing import Any, Dict, Tuple, List
from typing import Any, Dict, Tuple, List, Optional
from src.full_node.full_node import FullNode
from src.server.connection import NodeType
from src.server.server import ChiaServer, start_server
@ -253,7 +253,7 @@ async def setup_harvester(port, farmer_port, dic={}):
await run_task
async def setup_farmer(port, full_node_port, dic={}):
async def setup_farmer(port, full_node_port: Optional[uint16] = None, dic={}):
config = load_config(bt.root_path, "config.yaml", "farmer")
config_pool = load_config(root_path, "config.yaml", "pool")
test_constants_copy = test_constants.copy()
@ -264,6 +264,10 @@ async def setup_farmer(port, full_node_port, dic={}):
bytes(epk.get_public_key()).hex() for epk in bt.keychain.get_all_public_keys()
]
config_pool["xch_target_puzzle_hash"] = bt.fee_target.hex()
if full_node_port:
connect_peers = [PeerInfo("127.0.0.1", full_node_port)]
else:
connect_peers = []
api = Farmer(config, config_pool, bt.keychain, test_constants_copy)
@ -281,7 +285,7 @@ async def setup_farmer(port, full_node_port, dic={}):
service_name="farmer",
server_listen_ports=[port],
on_connect_callback=api._on_connect,
connect_peers=[PeerInfo("127.0.0.1", full_node_port)],
connect_peers=connect_peers,
auth_connect_peers=False,
start_callback=start_callback,
)
@ -456,7 +460,7 @@ async def setup_simulators_and_wallets(
async def setup_farmer_harvester(dic={}):
node_iters = [
setup_harvester(21234, 21235, dic),
setup_farmer(21235, 21237, dic),
setup_farmer(21235, None, dic),
]
harvester, harvester_server = await node_iters[0].__anext__()
@ -464,12 +468,14 @@ async def setup_farmer_harvester(dic={}):
yield (harvester, farmer)
await _teardown_nodes(node_iters)
async def setup_full_system(dic={}):
node_iters = [
setup_introducer(21233),
setup_harvester(21234, 21235, dic),
setup_farmer(21235, 21237, dic),
setup_farmer(21235, uint16(21237), dic),
setup_timelord(21236, 21237, False, dic),
setup_vdf_clients(8000),
setup_full_node("blockchain_test.db", 21237, 21233, False, 10, dic),

View File

@ -70,4 +70,6 @@ class TestSimulation:
return False
return True
await time_out_assert_custom_interval(120, 2, all_compact, True, node1, node2, max_height)
await time_out_assert_custom_interval(
120, 2, all_compact, True, node1, node2, max_height
)

View File

@ -1,7 +1,10 @@
import asyncio
import time
async def time_out_assert_custom_interval(timeout: int, interval, function, value, *args, **kwargs):
async def time_out_assert_custom_interval(
timeout: int, interval, function, value, *args, **kwargs
):
start = time.time()
while time.time() - start < timeout:
if asyncio.iscoroutinefunction(function):
@ -13,8 +16,12 @@ async def time_out_assert_custom_interval(timeout: int, interval, function, valu
await asyncio.sleep(interval)
assert False
async def time_out_assert(timeout: int, function, value, *args, **kwargs):
await time_out_assert_custom_interval(timeout, 0.05, function, value, *args, *kwargs)
await time_out_assert_custom_interval(
timeout, 0.05, function, value, *args, *kwargs
)
async def time_out_assert_not_None(timeout: int, function, *args, **kwargs):
start = time.time()
@ -27,4 +34,3 @@ async def time_out_assert_not_None(timeout: int, function, *args, **kwargs):
return
await asyncio.sleep(0.05)
assert False