Files
chia-blockchain/tests/core/ssl/test_ssl.py
T
ab53d48995 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 01:35:41 -05:00

226 lines
9.8 KiB
Python

import asyncio
import aiohttp
import pytest
from chia.protocols.shared_protocol import protocol_version, capabilities
from chia.server.outbound_message import NodeType
from chia.server.server import ChiaServer, ssl_context_for_client
from chia.server.ws_connection import WSChiaConnection
from chia.ssl.create_ssl import generate_ca_signed_cert
from chia.types.peer_info import PeerInfo
from chia.util.ints import uint16
async def establish_connection(server: ChiaServer, self_hostname: str, ssl_context) -> None:
timeout = aiohttp.ClientTimeout(total=10)
dummy_port = 5 # this does not matter
async with aiohttp.ClientSession(timeout=timeout) as session:
incoming_queue: asyncio.Queue = asyncio.Queue()
url = f"wss://{self_hostname}:{server._port}/ws"
ws = await session.ws_connect(url, autoclose=False, autoping=True, ssl=ssl_context)
wsc = WSChiaConnection(
NodeType.FULL_NODE,
ws,
server._port,
server.log,
True,
False,
self_hostname,
incoming_queue,
lambda x, y: x,
None,
100,
30,
local_capabilities_for_handshake=capabilities,
)
await wsc.perform_handshake(server._network_id, protocol_version, dummy_port, NodeType.FULL_NODE)
class TestSSL:
@pytest.mark.asyncio
async def test_public_connections(self, wallet_node_sim_and_wallet, self_hostname):
full_nodes, wallets = wallet_node_sim_and_wallet
full_node_api = full_nodes[0]
server_1: ChiaServer = full_node_api.full_node.server
wallet_node, server_2 = wallets[0]
success = await server_2.start_client(PeerInfo(self_hostname, uint16(server_1._port)), None)
assert success is True
@pytest.mark.asyncio
async def test_farmer(self, farmer_one_harvester, self_hostname):
_, farmer_service = farmer_one_harvester
farmer_api = farmer_service._api
farmer_server = farmer_api.farmer.server
# Create valid cert (valid meaning signed with private CA)
priv_crt = farmer_server._private_key_path.parent / "valid.crt"
priv_key = farmer_server._private_key_path.parent / "valid.key"
generate_ca_signed_cert(
farmer_server.ca_private_crt_path.read_bytes(),
farmer_server.ca_private_key_path.read_bytes(),
priv_crt,
priv_key,
)
ssl_context = ssl_context_for_client(
farmer_server.ca_private_crt_path, farmer_server.ca_private_key_path, priv_crt, priv_key
)
await establish_connection(farmer_server, self_hostname, ssl_context)
# Create not authenticated cert
pub_crt = farmer_server._private_key_path.parent / "non_valid.crt"
pub_key = farmer_server._private_key_path.parent / "non_valid.key"
generate_ca_signed_cert(
farmer_server.chia_ca_crt_path.read_bytes(), farmer_server.chia_ca_key_path.read_bytes(), pub_crt, pub_key
)
ssl_context = ssl_context_for_client(
farmer_server.chia_ca_crt_path, farmer_server.chia_ca_key_path, pub_crt, pub_key
)
with pytest.raises(aiohttp.ClientConnectorCertificateError):
await establish_connection(farmer_server, self_hostname, ssl_context)
ssl_context = ssl_context_for_client(
farmer_server.ca_private_crt_path, farmer_server.ca_private_key_path, pub_crt, pub_key
)
with pytest.raises(aiohttp.ServerDisconnectedError):
await establish_connection(farmer_server, self_hostname, ssl_context)
@pytest.mark.asyncio
async def test_full_node(self, wallet_node_sim_and_wallet, self_hostname):
full_nodes, wallets = wallet_node_sim_and_wallet
full_node_api = full_nodes[0]
full_node_server = full_node_api.full_node.server
# Create not authenticated cert
pub_crt = full_node_server._private_key_path.parent / "p2p.crt"
pub_key = full_node_server._private_key_path.parent / "p2p.key"
generate_ca_signed_cert(
full_node_server.chia_ca_crt_path.read_bytes(),
full_node_server.chia_ca_key_path.read_bytes(),
pub_crt,
pub_key,
)
ssl_context = ssl_context_for_client(
full_node_server.chia_ca_crt_path, full_node_server.chia_ca_key_path, pub_crt, pub_key
)
await establish_connection(full_node_server, self_hostname, ssl_context)
@pytest.mark.asyncio
async def test_wallet(self, wallet_node_sim_and_wallet, self_hostname):
full_nodes, wallets = wallet_node_sim_and_wallet
wallet_node, wallet_server = wallets[0]
# Wallet should not accept incoming connections
pub_crt = wallet_server._private_key_path.parent / "p2p.crt"
pub_key = wallet_server._private_key_path.parent / "p2p.key"
generate_ca_signed_cert(
wallet_server.chia_ca_crt_path.read_bytes(), wallet_server.chia_ca_key_path.read_bytes(), pub_crt, pub_key
)
ssl_context = ssl_context_for_client(
wallet_server.chia_ca_crt_path, wallet_server.chia_ca_key_path, pub_crt, pub_key
)
with pytest.raises(aiohttp.ClientConnectorError):
await establish_connection(wallet_server, self_hostname, ssl_context)
# Not even signed by private cert
priv_crt = wallet_server._private_key_path.parent / "valid.crt"
priv_key = wallet_server._private_key_path.parent / "valid.key"
generate_ca_signed_cert(
wallet_server.ca_private_crt_path.read_bytes(),
wallet_server.ca_private_key_path.read_bytes(),
priv_crt,
priv_key,
)
ssl_context = ssl_context_for_client(
wallet_server.ca_private_crt_path, wallet_server.ca_private_key_path, priv_crt, priv_key
)
with pytest.raises(aiohttp.ClientConnectorError):
await establish_connection(wallet_server, self_hostname, ssl_context)
@pytest.mark.asyncio
async def test_harvester(self, farmer_one_harvester, self_hostname):
harvesters, _ = farmer_one_harvester
harvester_server = harvesters[0]._server
# harvester should not accept incoming connections
pub_crt = harvester_server._private_key_path.parent / "p2p.crt"
pub_key = harvester_server._private_key_path.parent / "p2p.key"
generate_ca_signed_cert(
harvester_server.chia_ca_crt_path.read_bytes(),
harvester_server.chia_ca_key_path.read_bytes(),
pub_crt,
pub_key,
)
ssl_context = ssl_context_for_client(
harvester_server.chia_ca_crt_path, harvester_server.chia_ca_key_path, pub_crt, pub_key
)
with pytest.raises(aiohttp.ClientConnectorError):
await establish_connection(harvester_server, self_hostname, ssl_context)
# Not even signed by private cert
priv_crt = harvester_server._private_key_path.parent / "valid.crt"
priv_key = harvester_server._private_key_path.parent / "valid.key"
generate_ca_signed_cert(
harvester_server.ca_private_crt_path.read_bytes(),
harvester_server.ca_private_key_path.read_bytes(),
priv_crt,
priv_key,
)
ssl_context = ssl_context_for_client(
harvester_server.ca_private_crt_path, harvester_server.ca_private_key_path, priv_crt, priv_key
)
with pytest.raises(aiohttp.ClientConnectorError):
await establish_connection(harvester_server, self_hostname, ssl_context)
@pytest.mark.asyncio
async def test_introducer(self, introducer, self_hostname):
introducer_api, introducer_server = introducer
# Create not authenticated cert
pub_crt = introducer_server.chia_ca_key_path.parent / "p2p.crt"
pub_key = introducer_server.chia_ca_key_path.parent / "p2p.key"
generate_ca_signed_cert(
introducer_server.chia_ca_crt_path.read_bytes(),
introducer_server.chia_ca_key_path.read_bytes(),
pub_crt,
pub_key,
)
ssl_context = ssl_context_for_client(
introducer_server.chia_ca_crt_path, introducer_server.chia_ca_key_path, pub_crt, pub_key
)
await establish_connection(introducer_server, self_hostname, ssl_context)
@pytest.mark.asyncio
async def test_timelord(self, timelord, self_hostname):
timelord_api, timelord_server = timelord
# timelord should not accept incoming connections
pub_crt = timelord_server._private_key_path.parent / "p2p.crt"
pub_key = timelord_server._private_key_path.parent / "p2p.key"
generate_ca_signed_cert(
timelord_server.chia_ca_crt_path.read_bytes(),
timelord_server.chia_ca_key_path.read_bytes(),
pub_crt,
pub_key,
)
ssl_context = ssl_context_for_client(
timelord_server.chia_ca_crt_path, timelord_server.chia_ca_key_path, pub_crt, pub_key
)
with pytest.raises(aiohttp.ClientConnectorError):
await establish_connection(timelord_server, self_hostname, ssl_context)
# Not even signed by private cert
priv_crt = timelord_server._private_key_path.parent / "valid.crt"
priv_key = timelord_server._private_key_path.parent / "valid.key"
generate_ca_signed_cert(
timelord_server.ca_private_crt_path.read_bytes(),
timelord_server.ca_private_key_path.read_bytes(),
priv_crt,
priv_key,
)
ssl_context = ssl_context_for_client(
timelord_server.ca_private_crt_path, timelord_server.ca_private_key_path, priv_crt, priv_key
)
with pytest.raises(aiohttp.ClientConnectorError):
await establish_connection(timelord_server, self_hostname, ssl_context)