Async update on Bitcoin sync page

This commit is contained in:
Taylor Helsper 2021-04-22 21:43:16 -05:00
parent 3b0f5eecca
commit 2dcbf819bf
5 changed files with 130 additions and 24 deletions

View File

@ -13,6 +13,8 @@ sleep 60s
# Give admin the ability to access the BTC cookie
chmod 640 /mnt/hdd/mynode/bitcoin/.cookie
cp -f /mnt/hdd/mynode/bitcoin/.cookie /home/admin/.bitcoin/.cookie
chown admin:admin /home/admin/.bitcoin/.cookie
if [ -f /mnt/hdd/mynode/bitcoin/testnet3/.cookie ]; then
mkdir -p /mnt/hdd/mynode/bitcoin/testnet3

View File

@ -11,6 +11,7 @@ from whirlpool import get_whirlpool_status
from thread_functions import *
from systemctl_info import *
from application_info import *
from messages import *
import qrcode
import cStringIO
import json
@ -28,6 +29,7 @@ def api_get_bitcoin_info():
data = {}
data["current_block"] = get_mynode_block_height()
data["block_height"] = get_bitcoin_block_height()
data["peer_count"] = get_bitcoin_peer_count()
#data["difficulty"] = get_bitcoin_difficulty() # Dont send difficulty, it causes errors in jsonify
data["mempool_size"] = get_bitcoin_mempool_size()
@ -181,4 +183,16 @@ def api_get_qr_code_image():
img = generate_qr_code(url)
img.save(img_buf)
img_buf.seek(0)
return send_file(img_buf, mimetype='image/png')
return send_file(img_buf, mimetype='image/png')
@mynode_api.route("/api/get_message")
def api_get_message():
check_logged_in()
funny = False
if request.args.get("funny"):
funny = True
data = {}
data["message"] = get_message(funny)
return jsonify(data)

View File

@ -442,19 +442,19 @@ def index():
# Display sync info if not synced
if not is_bitcoin_synced():
subheader = Markup("Syncing...")
if bitcoin_block_height != None:
message = "<div class='small_message'>{}</<div>".format( get_message(include_funny=True) )
remaining = bitcoin_block_height - mynode_block_height
subheader = Markup("Syncing...<br/>Block {} of {}{}".format(mynode_block_height, bitcoin_block_height, message))
if bitcoin_block_height == None:
bitcoin_block_height = 0
if mynode_block_height == None:
mynode_block_height = 0
templateData = {
"title": "myNode Sync",
"header_text": "Bitcoin Blockchain",
"subheader_text": subheader,
"refresh_rate": 10,
"bitcoin_block_height": bitcoin_block_height,
"mynode_block_height": mynode_block_height,
"message": get_message(include_funny=True),
"ui_settings": read_ui_settings()
}
return render_template('state.html', **templateData)
return render_template('syncing.html', **templateData)
# Find tor status
tor_status_color = get_service_status_color("tor@default")
@ -851,7 +851,7 @@ def start_threads():
app.logger.info("STARTING THREADS")
# Start threads
btc_thread1 = BackgroundThread(update_bitcoin_main_info_thread, 15)
btc_thread1 = BackgroundThread(update_bitcoin_main_info_thread, 60) # Restart after 60, thread manages timing
btc_thread1.start()
threads.append(btc_thread1)
btc_thread2 = BackgroundThread(update_bitcoin_other_info_thread, 60)

View File

@ -0,0 +1,76 @@
<!DOCTYPE html lang="en">
<head>
<title>{{ title }}</title>
{% include 'includes/head.html' %}
<script>
$(document).ready(function() {
current_block = {{mynode_block_height}};
block_height = {{bitcoin_block_height}};
function update_counts() {
// Update Bitcoin Info
$.getJSON("/api/get_bitcoin_info", function( data ) {
//console.log("Bitcoin Data: " + JSON.stringify(data))
if ("current_block" in data && data["current_block"] != null &&
"block_height" in data && data["block_height"] != null) {
if (current_block != parseInt(data["current_block"])) {
$("#current_block").fadeOut(250, function() {
$(this).text(data["current_block"]);
}).fadeIn(250);
current_block = parseInt(data["current_block"]);
}
if (block_height != parseInt(data["block_height"])) {
$("#block_height").fadeOut(250, function() {
$(this).text(data["block_height"]);
}).fadeIn(250);
block_height = parseInt(data["block_height"]);
}
// We're close, so do a full refresh to get to home page faster
if (current_block > (block_height - 5)) {
location.reload();
}
}
});
}
function update_message() {
// Update message
if (update_count % 12 == 0) {
$.getJSON("/api/get_message", function( data ) {
if ("message" in data && data["message"] != null) {
$("#message").fadeOut(250, function() {
$(this).text(data["message"]);
}).fadeIn(250);
}
});
}
}
// Update info
const update_block_interval = setInterval(update_counts, 5000);
const update_message_interval = setInterval(update_message, 60000);
});
</script>
<!-- Refresh in 15 min, other updates async -->
<meta http-equiv="refresh" content="900">
</head>
<body>
{% include 'includes/logo_header.html' %}
<div class="state_header">{{ header_text }}</div>
<div class="state_subheader">
Syncing... <br/>
<span id="current_block">{{mynode_block_height}}</span> of <span id="block_height">{{bitcoin_block_height}}</span>
<div id="message" class='small_message'>{{ message }}</div>
</div>
<div style="height: 40px;">&nbsp;</div>
{% include 'includes/footer.html' %}
</body>
</html>

View File

@ -92,21 +92,35 @@ def update_bitcoin_main_info_thread():
global has_updated_btc_info
try:
# Get bitcoin info
if update_bitcoin_main_info():
# Mark on update complete
has_updated_btc_info = True
synced = False
while True:
# Get bitcoin info
if update_bitcoin_main_info():
# Mark on update complete
has_updated_btc_info = True
# Calculate sync status
bitcoin_block_height = get_bitcoin_block_height()
mynode_block_height = get_mynode_block_height()
remaining = bitcoin_block_height - mynode_block_height
if remaining == 0 and bitcoin_block_height > 670000:
if not os.path.isfile(BITCOIN_SYNCED_FILE):
open(BITCOIN_SYNCED_FILE, 'a').close() # touch file
elif remaining > 18:
if os.path.isfile(BITCOIN_SYNCED_FILE):
os.remove(BITCOIN_SYNCED_FILE)
# Calculate sync status
bitcoin_block_height = get_bitcoin_block_height()
mynode_block_height = get_mynode_block_height()
remaining = bitcoin_block_height - mynode_block_height
if remaining == 0 and bitcoin_block_height > 680000:
synced = True
if not os.path.isfile(BITCOIN_SYNCED_FILE):
open(BITCOIN_SYNCED_FILE, 'a').close() # touch file
elif remaining > 18:
synced = False
if os.path.isfile(BITCOIN_SYNCED_FILE):
os.remove(BITCOIN_SYNCED_FILE)
# Poll slower if synced
if synced:
time.sleep(15)
else:
time.sleep(3)
else:
# Failed - try again in 10s
time.sleep(10)
except Exception as e:
print("CAUGHT update_bitcoin_main_info_thread EXCEPTION: " + str(e))