seeder: Drop `CrawlerAPI.__getattr__` (#15489)
* Test sending unknown messages to the crawler * Drop `CrawlerAPI.__getattr__` * Use a real crawler service for the test
This commit is contained in:
parent
2d46ecfd3e
commit
d6a2a9aa5d
|
@ -32,7 +32,7 @@ log = logging.getLogger(__name__)
|
|||
class Crawler:
|
||||
sync_store: Any
|
||||
coin_store: CoinStore
|
||||
connection: aiosqlite.Connection
|
||||
connection: Optional[aiosqlite.Connection]
|
||||
config: Dict
|
||||
_server: Optional[ChiaServer]
|
||||
crawl_store: Optional[CrawlStore]
|
||||
|
@ -63,6 +63,7 @@ class Crawler:
|
|||
self.initialized = False
|
||||
self.root_path = root_path
|
||||
self.config = config
|
||||
self.connection = None
|
||||
self._server = None
|
||||
self._shut_down = False # Set to true to close all infinite loops
|
||||
self.constants = consensus_constants
|
||||
|
@ -372,4 +373,5 @@ class Crawler:
|
|||
self._shut_down = True
|
||||
|
||||
async def _await_closed(self):
|
||||
await self.connection.close()
|
||||
if self.connection is not None:
|
||||
await self.connection.close()
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any, Optional
|
||||
from typing import Optional
|
||||
|
||||
from chia.protocols import full_node_protocol, wallet_protocol
|
||||
from chia.seeder.crawler import Crawler
|
||||
|
@ -17,12 +17,6 @@ class CrawlerAPI:
|
|||
def __init__(self, crawler: Crawler) -> None:
|
||||
self.crawler = crawler
|
||||
|
||||
def __getattr__(self, attr_name: str) -> Any:
|
||||
async def invoke(*args: Any, **kwargs: Any) -> Any:
|
||||
pass
|
||||
|
||||
return invoke
|
||||
|
||||
@property
|
||||
def server(self) -> ChiaServer:
|
||||
assert self.crawler.server is not None
|
||||
|
|
|
@ -18,6 +18,8 @@ from chia.full_node.full_node import FullNode
|
|||
from chia.harvester.harvester import Harvester
|
||||
from chia.introducer.introducer import Introducer
|
||||
from chia.protocols.shared_protocol import Capability, capabilities
|
||||
from chia.seeder.crawler import Crawler
|
||||
from chia.seeder.start_crawler import create_full_node_crawler_service
|
||||
from chia.server.start_farmer import create_farmer_service
|
||||
from chia.server.start_full_node import create_full_node_service
|
||||
from chia.server.start_harvester import create_harvester_service
|
||||
|
@ -165,6 +167,32 @@ async def setup_full_node(
|
|||
db_path.unlink()
|
||||
|
||||
|
||||
async def setup_crawler(
|
||||
bt: BlockTools,
|
||||
) -> AsyncGenerator[Service[Crawler], None]:
|
||||
config = bt.config
|
||||
service_config = config["seeder"]
|
||||
service_config["selected_network"] = "testnet0"
|
||||
service_config["port"] = 0
|
||||
service_config["start_rpc_server"] = False
|
||||
overrides = service_config["network_overrides"]["constants"][service_config["selected_network"]]
|
||||
updated_constants = bt.constants.replace_str_to_bytes(**overrides)
|
||||
bt.change_config(config)
|
||||
service = create_full_node_crawler_service(
|
||||
bt.root_path,
|
||||
config,
|
||||
updated_constants,
|
||||
connect_to_daemon=False,
|
||||
)
|
||||
await service.start()
|
||||
|
||||
try:
|
||||
yield service
|
||||
finally:
|
||||
service.stop()
|
||||
await service.wait_closed()
|
||||
|
||||
|
||||
# Note: convert these setup functions to fixtures, or push it one layer up,
|
||||
# keeping these usable independently?
|
||||
async def setup_wallet_node(
|
||||
|
|
|
@ -24,6 +24,7 @@ from chia.full_node.full_node import FullNode
|
|||
from chia.full_node.full_node_api import FullNodeAPI
|
||||
from chia.protocols import full_node_protocol
|
||||
from chia.rpc.wallet_rpc_client import WalletRpcClient
|
||||
from chia.seeder.crawler import Crawler
|
||||
from chia.server.server import ChiaServer
|
||||
from chia.server.start_service import Service
|
||||
from chia.simulator.full_node_simulator import FullNodeSimulator
|
||||
|
@ -36,7 +37,7 @@ from chia.simulator.setup_nodes import (
|
|||
setup_simulators_and_wallets_service,
|
||||
setup_two_nodes,
|
||||
)
|
||||
from chia.simulator.setup_services import setup_daemon, setup_introducer, setup_timelord
|
||||
from chia.simulator.setup_services import setup_crawler, setup_daemon, setup_introducer, setup_timelord
|
||||
from chia.simulator.time_out_assert import time_out_assert
|
||||
from chia.simulator.wallet_tools import WalletTool
|
||||
from chia.types.peer_info import PeerInfo
|
||||
|
@ -819,6 +820,12 @@ async def timelord_service(bt):
|
|||
yield _
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
async def crawler_service(bt: BlockTools) -> AsyncIterator[Service[Crawler]]:
|
||||
async for service in setup_crawler(bt):
|
||||
yield service
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def tmp_chia_root(tmp_path):
|
||||
"""
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
import pytest
|
||||
|
||||
from chia.full_node.full_node_api import FullNodeAPI
|
||||
from chia.protocols.protocol_message_types import ProtocolMessageTypes
|
||||
from chia.protocols.wallet_protocol import RequestChildren
|
||||
from chia.seeder.crawler import Crawler
|
||||
from chia.server.outbound_message import make_msg
|
||||
from chia.server.start_service import Service
|
||||
from chia.simulator.setup_nodes import SimulatorsAndWalletsServices
|
||||
from chia.simulator.time_out_assert import time_out_assert
|
||||
from chia.types.blockchain_format.sized_bytes import bytes32
|
||||
from chia.types.peer_info import PeerInfo
|
||||
from chia.util.ints import uint16
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unknown_messages(
|
||||
self_hostname: str,
|
||||
one_node: SimulatorsAndWalletsServices,
|
||||
crawler_service: Service[Crawler],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
[full_node_service], _, _ = one_node
|
||||
crawler = crawler_service._node
|
||||
full_node = full_node_service._node
|
||||
assert await crawler.server.start_client(
|
||||
PeerInfo(self_hostname, uint16(cast(FullNodeAPI, full_node_service._api).server._port)), None
|
||||
)
|
||||
connection = full_node.server.all_connections[crawler.server.node_id]
|
||||
|
||||
def receiving_failed() -> bool:
|
||||
return "Non existing function: request_children" in caplog.text
|
||||
|
||||
with caplog.at_level(logging.ERROR):
|
||||
msg = make_msg(ProtocolMessageTypes.request_children, RequestChildren(bytes32(b"\0" * 32)))
|
||||
assert await connection.send_message(msg)
|
||||
await time_out_assert(10, receiving_failed)
|
Loading…
Reference in New Issue