chia-blockchain/src/server/start_full_node.py

78 lines
3.1 KiB
Python
Raw Normal View History

import asyncio
import logging
from src import full_node
2019-09-12 07:31:49 +03:00
import sys
2019-08-22 07:19:24 +03:00
from src.server.server import start_chia_server, start_chia_client
2019-09-12 07:31:49 +03:00
from src.util.network import parse_host_port
2019-09-17 08:44:16 +03:00
from src.server.outbound_message import NodeType
2019-09-17 12:17:11 +03:00
from src.types.peer_info import PeerInfo
logging.basicConfig(format='FullNode %(name)-23s: %(levelname)-8s %(message)s', level=logging.INFO)
2019-09-12 07:31:49 +03:00
log = logging.getLogger(__name__)
2019-07-31 19:48:30 +03:00
"""
Full node startup algorithm:
- Update peer list (?)
- Start server
2019-09-17 08:44:16 +03:00
- Sync
- If connected to farmer, send challenges
- If connected to timelord, send challenges
"""
2019-07-31 19:48:30 +03:00
async def main():
2019-09-12 07:31:49 +03:00
# Starts the full node server (which full nodes can connect to)
host, port = parse_host_port(full_node)
2019-09-17 08:44:16 +03:00
server, client = await start_chia_server(host, port, full_node, NodeType.FULL_NODE, full_node.on_connect)
2019-09-12 07:31:49 +03:00
connect_to_farmer = ("-f" in sys.argv)
connect_to_timelord = ("-t" in sys.argv)
2019-09-12 07:31:49 +03:00
waitable_tasks = [server]
2019-09-12 07:31:49 +03:00
peer_tasks = []
2019-09-17 12:17:11 +03:00
for peer in full_node.config['initial_peers']:
if not (host == peer['host'] and port == peer['port']):
2019-09-12 07:31:49 +03:00
# TODO: check if not in blacklist
2019-09-17 12:17:11 +03:00
peer_task = start_chia_client(PeerInfo(peer['host'], peer['port'], bytes.fromhex(peer['node_id'])),
full_node, NodeType.FULL_NODE)
2019-09-12 07:31:49 +03:00
peer_tasks.append(peer_task)
awaited = await asyncio.gather(*peer_tasks, return_exceptions=True)
connected_tasks = [response[0] for response in awaited if not isinstance(response, asyncio.CancelledError)]
waitable_tasks = waitable_tasks + connected_tasks
log.info(f"Connected to {len(connected_tasks)} peers.")
2019-09-17 08:44:16 +03:00
async for msg in full_node.sync():
client.push(msg)
if connect_to_farmer:
try:
2019-09-17 12:17:11 +03:00
peer_info = PeerInfo(full_node.config['farmer_peer']['host'],
full_node.config['farmer_peer']['port'],
bytes.fromhex(full_node.config['farmer_peer']['node_id']))
farmer_con_task, farmer_client = await start_chia_client(peer_info, full_node, NodeType.FARMER)
2019-09-17 08:44:16 +03:00
async for msg in full_node.send_heads_to_farmers():
farmer_client.push(msg)
waitable_tasks.append(farmer_con_task)
except asyncio.CancelledError:
log.warning("Connection to farmer failed.")
if connect_to_timelord:
try:
2019-09-17 12:17:11 +03:00
peer_info = PeerInfo(full_node.config['timelord_peer']['host'],
full_node.config['timelord_peer']['port'],
bytes.fromhex(full_node.config['timelord_peer']['node_id']))
timelord_con_task, timelord_client = await start_chia_client(peer_info, full_node, NodeType.TIMELORD)
2019-09-17 08:44:16 +03:00
async for msg in full_node.send_challenges_to_timelords():
timelord_client.push(msg)
waitable_tasks.append(timelord_con_task)
except asyncio.CancelledError:
log.warning("Connection to timelord failed.")
# Periodically update our estimate of proof of time speeds
asyncio.create_task(full_node.proof_of_time_estimate_interval())
2019-09-12 07:31:49 +03:00
await asyncio.gather(*waitable_tasks)
asyncio.run(main())