You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.9 KiB
117 lines
3.9 KiB
3 years ago
|
# SPDX-License-Identifier: BSD-3-Clause
|
||
|
# Copyright (c) 2020, The Monero Project.
|
||
|
# Copyright (c) 2020, dsc@xmr.pm
|
||
|
|
||
|
import json
|
||
|
from typing import List
|
||
|
|
||
|
import settings
|
||
3 years ago
|
from wowlet_backend.utils import httpget, popularity_contest
|
||
|
from wowlet_backend.tasks import FeatherTask
|
||
3 years ago
|
|
||
|
|
||
|
class RPCNodeCheckTask(FeatherTask):
|
||
|
def __init__(self, interval: int = 60):
|
||
|
super(RPCNodeCheckTask, self).__init__(interval)
|
||
|
|
||
|
self._cache_key = "rpc_nodes"
|
||
|
self._cache_expiry = None
|
||
|
|
||
|
self._websocket_cmd = "nodes"
|
||
|
|
||
|
self._http_timeout = 5
|
||
|
self._http_timeout_onion = 10
|
||
|
|
||
|
async def task(self) -> List[dict]:
|
||
|
"""Check RPC nodes status"""
|
||
3 years ago
|
from wowlet_backend.factory import app, cache
|
||
3 years ago
|
|
||
|
try:
|
||
|
heights = json.loads(await cache.get("blockheights"))
|
||
|
except:
|
||
|
heights = {}
|
||
|
|
||
3 years ago
|
rpc_nodes = await self.cache_json_get("nodes")
|
||
|
|
||
3 years ago
|
nodes = []
|
||
|
for network_type_coin, _ in rpc_nodes.items():
|
||
|
data = []
|
||
|
|
||
|
for network_type, _nodes in _.items():
|
||
|
for node in _nodes:
|
||
|
try:
|
||
|
blob = await self.node_check(node, network_type=network_type)
|
||
|
data.append(blob)
|
||
|
except Exception as ex:
|
||
|
app.logger.warning(f"node {node} not reachable; {ex}")
|
||
3 years ago
|
data.append(self._bad_node({
|
||
3 years ago
|
"address": node,
|
||
|
"nettype": network_type_coin,
|
||
|
"type": network_type,
|
||
|
"height": 0
|
||
3 years ago
|
}, reason="unreachable"))
|
||
3 years ago
|
|
||
|
# not neccesary for stagenet/testnet nodes to be validated
|
||
|
if network_type_coin != "mainnet":
|
||
|
nodes += data
|
||
|
continue
|
||
|
|
||
|
if not data:
|
||
|
continue
|
||
|
|
||
|
# Filter out nodes affected by < v0.17.1.3 sybil attack
|
||
3 years ago
|
data = list(map(lambda _node: _node if _node['target_height'] <= _node['height']
|
||
3 years ago
|
else self._bad_node(_node, reason="+2_attack"), data))
|
||
3 years ago
|
|
||
|
allowed_offset = 3
|
||
|
valid_heights = []
|
||
3 years ago
|
# current_blockheight = heights.get(network_type_coin, 0)
|
||
|
|
||
|
# popularity contest
|
||
3 years ago
|
common_height = popularity_contest([z['height'] for z in data if z['height'] != 0])
|
||
3 years ago
|
valid_heights = range(common_height + allowed_offset, common_height - allowed_offset, -1)
|
||
3 years ago
|
|
||
|
data = list(map(lambda _node: _node if _node['height'] in valid_heights
|
||
3 years ago
|
else self._bad_node(_node, reason="out_of_sync"), data))
|
||
3 years ago
|
nodes += data
|
||
|
return nodes
|
||
|
|
||
|
async def node_check(self, node, network_type: str) -> dict:
|
||
|
"""Call /get_info on the RPC, return JSON"""
|
||
|
opts = {
|
||
|
"timeout": self._http_timeout,
|
||
|
"json": True
|
||
|
}
|
||
|
|
||
|
if network_type == "tor":
|
||
|
opts["socks5"] = settings.TOR_SOCKS_PROXY
|
||
|
opts["timeout"] = self._http_timeout_onion
|
||
|
|
||
|
blob = await httpget(f"http://{node}/get_info", **opts)
|
||
|
for expect in ["nettype", "height", "target_height"]:
|
||
|
if expect not in blob:
|
||
|
raise Exception(f"Invalid JSON response from RPC; expected key '{expect}'")
|
||
|
|
||
|
height = int(blob.get("height", 0))
|
||
|
target_height = int(blob.get("target_height", 0))
|
||
|
|
||
|
return {
|
||
|
"address": node,
|
||
|
"height": height,
|
||
|
"target_height": target_height,
|
||
|
"online": True,
|
||
|
"nettype": blob["nettype"],
|
||
|
"type": network_type
|
||
|
}
|
||
|
|
||
3 years ago
|
def _bad_node(self, node: dict, reason=""):
|
||
3 years ago
|
return {
|
||
3 years ago
|
"address": node['address'],
|
||
|
"height": node['height'],
|
||
3 years ago
|
"target_height": 0,
|
||
|
"online": False,
|
||
3 years ago
|
"nettype": node['nettype'],
|
||
|
"type": node['type'],
|
||
|
"reason": reason
|
||
3 years ago
|
}
|