chia-blockchain/tests/connection_utils.py

113 lines
4.4 KiB
Python
Raw Normal View History

from __future__ import annotations
2021-03-03 21:27:00 +03:00
import asyncio
import logging
from pathlib import Path
from typing import Set, Tuple
2021-03-03 21:27:00 +03:00
import aiohttp
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
Request header blocks, and new rate limits (#11636) * new blob block api method integrated into wallet * direct msg streaming of headers, rename, tests * perform_handshake call fix * updated trusted sync with new block header calls * add max blocks limit to fetch * added tests for rejected block header msgs * avoid parsing transactions info if not required * avoid looking up capabilities setting * move block tests out of a class * test fix * Merge changes * added docs and increased rate limits * increased block header request interval from 32 to 128 * remove fetching hashes and use height range * fetching by height in db v2 * update capabilities, other fixes * fixed range block header call * Add type hints * Start work on optimizing fetch_last_tx_from_peer * Huge speedup in trusted wallet sync * Revert unintentional changes * Fix trade issue * Improve the code * Str format * Optimize handling of farming rewards * Fix bug * Performance fixes * Optimizations to wallet syncing * Don't return all coins in respond_additions * Revert concurrency numbers * More optimization of the caches * Small optimization in coin_added * Optimize request_additions significantly by using a cache * fixes from feedback * capabilities check fixes * Increase rate limits to allow 250tps in verification requests * Start work on rate limits * New rate limit versioning support * Revert unrelated changes * revert return False * Lint * Revert cbi * try tests with trusted peer * Revert unrelated wallet changes * Revert more debug changes * Add test and throw on an error if not found * Reject invalid requests * Revert bad change with uint32, and change warning to info * Parametrize wallet sync test * Merge and LGTM * More clean way to choose peers * Fix lint * add the new RejectBlockHeaders, RequestBlockHeaders and RespondBlockHeaders to the network protocol regression test and regenerate test files * Rate limit diffs only * Improve performance * Simpler * Lint Co-authored-by: Sebastjan <trepca@gmail.com> Co-authored-by: arvidn <arvid@libtorrent.org>
2022-06-11 09:35:41 +03:00
from chia.protocols.shared_protocol import capabilities, protocol_version
from chia.server.outbound_message import NodeType
from chia.server.server import ChiaServer, ssl_context_for_client
from chia.server.ssl_context import chia_ssl_ca_paths, private_ssl_ca_paths
from chia.server.ws_connection import WSChiaConnection
from chia.simulator.time_out_assert import adjusted_timeout, time_out_assert
from chia.ssl.create_ssl import generate_ca_signed_cert
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.peer_info import PeerInfo
from chia.util.config import load_config
from chia.util.ints import uint16
2021-03-03 21:27:00 +03:00
log = logging.getLogger(__name__)
2022-06-26 09:11:19 +03:00
async def disconnect_all(server: ChiaServer) -> None:
connections = list(server.all_connections.values())
await asyncio.gather(*(connection.close() for connection in connections))
await asyncio.sleep(adjusted_timeout(5)) # 5 seconds to allow connections and tasks to all drain
2022-06-26 09:11:19 +03:00
async def disconnect_all_and_reconnect(server: ChiaServer, reconnect_to: ChiaServer, self_hostname: str) -> bool:
await disconnect_all(server)
return await server.start_client(PeerInfo(self_hostname, uint16(reconnect_to._port)), None)
async def add_dummy_connection(
server: ChiaServer, self_hostname: str, dummy_port: int, type: NodeType = NodeType.FULL_NODE
) -> Tuple[asyncio.Queue, bytes32]:
wsc, peer_id = await add_dummy_connection_wsc(server, self_hostname, dummy_port, type)
return wsc.incoming_queue, peer_id
async def add_dummy_connection_wsc(
server: ChiaServer, self_hostname: str, dummy_port: int, type: NodeType = NodeType.FULL_NODE
) -> Tuple[WSChiaConnection, bytes32]:
2021-03-03 21:27:00 +03:00
timeout = aiohttp.ClientTimeout(total=10)
session = aiohttp.ClientSession(timeout=timeout)
config = load_config(server.root_path, "config.yaml")
ca_crt_path: Path
ca_key_path: Path
authenticated_client_types: Set[NodeType] = {NodeType.HARVESTER}
if type in authenticated_client_types:
private_ca_crt_path, private_ca_key_path = private_ssl_ca_paths(server.root_path, config)
ca_crt_path = private_ca_crt_path
ca_key_path = private_ca_key_path
else:
chia_ca_crt_path, chia_ca_key_path = chia_ssl_ca_paths(server.root_path, config)
ca_crt_path = chia_ca_crt_path
ca_key_path = chia_ca_key_path
dummy_crt_path = server.root_path / "dummy.crt"
dummy_key_path = server.root_path / "dummy.key"
generate_ca_signed_cert(ca_crt_path.read_bytes(), ca_key_path.read_bytes(), dummy_crt_path, dummy_key_path)
ssl_context = ssl_context_for_client(ca_crt_path, ca_key_path, dummy_crt_path, dummy_key_path)
2021-03-03 21:27:00 +03:00
pem_cert = x509.load_pem_x509_certificate(dummy_crt_path.read_bytes(), default_backend())
der_cert = x509.load_der_x509_certificate(pem_cert.public_bytes(serialization.Encoding.DER), default_backend())
peer_id = bytes32(der_cert.fingerprint(hashes.SHA256()))
url = f"wss://{self_hostname}:{server._port}/ws"
ws = await session.ws_connect(url, autoclose=True, autoping=True, ssl=ssl_context)
wsc = WSChiaConnection.create(
type,
2021-03-03 21:27:00 +03:00
ws,
server.api,
2021-03-03 21:27:00 +03:00
server._port,
log,
True,
server.received_message_callback,
None,
2021-03-03 21:27:00 +03:00
peer_id,
100,
30,
Request header blocks, and new rate limits (#11636) * new blob block api method integrated into wallet * direct msg streaming of headers, rename, tests * perform_handshake call fix * updated trusted sync with new block header calls * add max blocks limit to fetch * added tests for rejected block header msgs * avoid parsing transactions info if not required * avoid looking up capabilities setting * move block tests out of a class * test fix * Merge changes * added docs and increased rate limits * increased block header request interval from 32 to 128 * remove fetching hashes and use height range * fetching by height in db v2 * update capabilities, other fixes * fixed range block header call * Add type hints * Start work on optimizing fetch_last_tx_from_peer * Huge speedup in trusted wallet sync * Revert unintentional changes * Fix trade issue * Improve the code * Str format * Optimize handling of farming rewards * Fix bug * Performance fixes * Optimizations to wallet syncing * Don't return all coins in respond_additions * Revert concurrency numbers * More optimization of the caches * Small optimization in coin_added * Optimize request_additions significantly by using a cache * fixes from feedback * capabilities check fixes * Increase rate limits to allow 250tps in verification requests * Start work on rate limits * New rate limit versioning support * Revert unrelated changes * revert return False * Lint * Revert cbi * try tests with trusted peer * Revert unrelated wallet changes * Revert more debug changes * Add test and throw on an error if not found * Reject invalid requests * Revert bad change with uint32, and change warning to info * Parametrize wallet sync test * Merge and LGTM * More clean way to choose peers * Fix lint * add the new RejectBlockHeaders, RequestBlockHeaders and RespondBlockHeaders to the network protocol regression test and regenerate test files * Rate limit diffs only * Improve performance * Simpler * Lint Co-authored-by: Sebastjan <trepca@gmail.com> Co-authored-by: arvidn <arvid@libtorrent.org>
2022-06-11 09:35:41 +03:00
local_capabilities_for_handshake=capabilities,
2021-03-03 21:27:00 +03:00
)
await wsc.perform_handshake(server._network_id, protocol_version, dummy_port, type)
if wsc.incoming_message_task is not None:
wsc.incoming_message_task.cancel()
return wsc, peer_id
2021-03-03 21:27:00 +03:00
async def connect_and_get_peer(server_1: ChiaServer, server_2: ChiaServer, self_hostname: str) -> WSChiaConnection:
2021-03-03 21:27:00 +03:00
"""
Connect server_2 to server_1, and get return the connection in server_1.
"""
await server_2.start_client(PeerInfo(self_hostname, uint16(server_1._port)))
async def connected():
for node_id_c, _ in server_1.all_connections.items():
if node_id_c == server_2.node_id:
return True
return False
await time_out_assert(10, connected, True)
for node_id, wsc in server_1.all_connections.items():
if node_id == server_2.node_id:
return wsc
assert False