mark nodes unhealthy if over 100 blocks away from highest (#13)

pull/15/head
lalanza808 2 years ago committed by GitHub
parent 310905237f
commit bac563254c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,7 +15,7 @@ from flask import render_template, flash, url_for
from urllib.parse import urlparse, urlencode from urllib.parse import urlparse, urlencode
from xmrnodes.helpers import determine_crypto, is_onion, make_request from xmrnodes.helpers import determine_crypto, is_onion, make_request
from xmrnodes.helpers import retrieve_peers, rw_cache from xmrnodes.helpers import retrieve_peers, rw_cache, get_highest_block
from xmrnodes.forms import SubmitNode from xmrnodes.forms import SubmitNode
from xmrnodes.models import Node, HealthCheck, Peer from xmrnodes.models import Node, HealthCheck, Peer
from xmrnodes import config from xmrnodes import config
@ -29,6 +29,7 @@ logging.basicConfig(
app = Flask(__name__) app = Flask(__name__)
app.config.from_envvar("FLASK_SECRETS") app.config.from_envvar("FLASK_SECRETS")
app.secret_key = app.config["SECRET_KEY"] app.secret_key = app.config["SECRET_KEY"]
HEALTHY_BLOCK_DIFF = 100 # idc to config this. hardcode is fine.
@app.route("/", methods=["GET", "POST"]) @app.route("/", methods=["GET", "POST"])
def index(): def index():
@ -38,6 +39,8 @@ def index():
onion = request.args.get("onion", False) onion = request.args.get("onion", False)
show_all = "true" == request.args.get("all", "false") show_all = "true" == request.args.get("all", "false")
web_compatible = request.args.get("web_compatible", False) web_compatible = request.args.get("web_compatible", False)
highest_block = get_highest_block(nettype, crypto)
healthy_block = highest_block - HEALTHY_BLOCK_DIFF
nodes = Node.select().where( nodes = Node.select().where(
Node.validated == True, Node.validated == True,
@ -49,10 +52,15 @@ def index():
nodes = nodes.where(Node.web_compatible == True) nodes = nodes.where(Node.web_compatible == True)
nodes_all = nodes.count() nodes_all = nodes.count()
nodes_unhealthy = nodes.where(Node.available == False).count() nodes_unhealthy = nodes.where(
(Node.available == False) | (Node.last_height < healthy_block)
).count()
if not show_all: if not show_all:
nodes = nodes.where(Node.available == True) nodes = nodes.where(
Node.available == True,
Node.last_height > healthy_block
)
nodes = nodes.order_by( nodes = nodes.order_by(
Node.datetime_entered.desc() Node.datetime_entered.desc()
@ -171,11 +179,17 @@ def check():
has_cors = 'Access-Control-Allow-Origin' in r.headers has_cors = 'Access-Control-Allow-Origin' in r.headers
is_ssl = node.url.startswith('https://') is_ssl = node.url.startswith('https://')
if r.json()["status"] == "OK": if r.json()["status"] == "OK":
logging.info("success")
node.available = True
node.web_compatible = has_cors and is_ssl node.web_compatible = has_cors and is_ssl
node.last_height = r.json()["height"] node.last_height = r.json()["height"]
hc.health = True hc.health = True
highest_block = get_highest_block(node.nettype, node.crypto)
healthy_block = highest_block - HEALTHY_BLOCK_DIFF
if r.json()["height"] < healthy_block:
node.available = False
logging.info("unhealthy")
else:
node.available = True
logging.info("success")
else: else:
raise raise
except: except:

@ -9,6 +9,7 @@ from levin.bucket import Bucket
from levin.ctypes import * from levin.ctypes import *
from levin.constants import LEVIN_SIGNATURE from levin.constants import LEVIN_SIGNATURE
from xmrnodes.models import Node
from xmrnodes import config from xmrnodes import config
@ -119,3 +120,11 @@ def retrieve_peers(host, port):
return peers return peers
else: else:
return None return None
def get_highest_block(nettype, crypto):
highest = Node.select().where(
Node.validated == True,
Node.nettype == nettype,
Node.crypto == crypto
).order_by(Node.last_height.desc()).limit(1).first()
return highest.last_height

@ -42,7 +42,9 @@
{% if nodes %} {% if nodes %}
<div class="xmrnodes"> <div class="xmrnodes">
<p class="center"> <p class="center">
Tracking {{ nodes_all }} {{ nettype }} {{ crypto | capitalize }} nodes in the database. Of those, {{ nodes_unhealthy }} nodes failed their last check-in. Tracking {{ nodes_all }} {{ nettype }} {{ crypto | capitalize }} nodes in the database.
<br>
Of those, {{ nodes_unhealthy }} nodes failed their last check-in (unresponsive to ping or over 100 blocks away from highest reported block).
</p> </p>
<p class="center">Showing {{ nodes | length }} nodes. <p class="center">Showing {{ nodes | length }} nodes.
{% if 'all' not in request.args %} {% if 'all' not in request.args %}

Loading…
Cancel
Save