From fe8c6ba241035c8546fbad7e411dfcc9c1d27bc7 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Sat, 21 Nov 2020 09:52:31 +0100 Subject: [PATCH] Load nodes from cache --- fapi/factory.py | 14 ++++++-------- fapi/fapi.py | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/fapi/factory.py b/fapi/factory.py index 0cef375..269122d 100644 --- a/fapi/factory.py +++ b/fapi/factory.py @@ -15,7 +15,6 @@ app = None cache = None connected_websockets = set() api_data = {} -nodes = {} user_agents = None txfiatdb = None @@ -53,17 +52,16 @@ def create_app(): @app.before_serving async def startup(): - global nodes, txfiatdb, user_agents + global txfiatdb, user_agents await _setup_cache(app) loop = asyncio.get_event_loop() - f = open("data/nodes.json", "r") - nodes = json.loads(f.read()) - f.close() + with open('data/nodes.json', 'r') as f: + nodes = json.loads(f.read()) + cache.execute('JSON.SET', 'nodes', '.', json.dumps(nodes)) - f = open("data/user_agents.txt", "r") - user_agents = [l.strip() for l in f.readlines() if l.strip()] - f.close() + with open('data/user_agents.txt', 'r') as f: + user_agents = [l.strip() for l in f.readlines() if l.strip()] from fapi.fapi import FeatherApi from fapi.utils import loopyloop, TxFiatDb, XmrRig diff --git a/fapi/fapi.py b/fapi/fapi.py index 5723d55..deb8180 100644 --- a/fapi/fapi.py +++ b/fapi/fapi.py @@ -23,6 +23,16 @@ class FeatherApi: except Exception as ex: app.logger.error(f"Redis error: {ex}") + @staticmethod + async def redis_json_get(key, path="."): + from fapi.factory import app, cache + try: + data = await cache.execute('JSON.GET', key, path) + if data: + return json.loads(data) + except Exception as ex: + app.logger.error(f"Redis error: {ex}") + @staticmethod async def xmrto_rates(): from fapi.factory import app, cache @@ -338,7 +348,10 @@ class FeatherApi: @staticmethod async def check_nodes(): - from fapi.factory import nodes, app + from fapi.factory import app + + nodes = await FeatherApi.redis_json_get("nodes") + data = [] for network_type, network_name in nodes.items(): for k, _nodes in nodes[network_type].items(): @@ -362,6 +375,11 @@ class FeatherApi: "nettype": blob["nettype"], "type": k } + + # Filter out nodes affected by < v0.17.1.3 sybil attack + if _node['target_height'] > _node["height"]: + continue + except Exception as ex: app.logger.warning(f"node {node} not reachable") _node = {