mynode/rootfs/standard/var/pynode/electrum_info.py

120 lines
4.3 KiB
Python
Raw Normal View History

2019-11-09 19:01:21 +00:00
from bitcoin_info import get_bitcoin_block_height
2019-11-10 02:21:25 +00:00
from prometheus_client.parser import text_string_to_metric_families
from systemctl_info import *
2021-11-07 04:40:57 +00:00
from utilities import *
2019-11-10 02:21:25 +00:00
import subprocess
import requests
2019-11-09 19:01:21 +00:00
import socket
import json
2019-12-11 02:31:32 +00:00
import os
2019-11-09 19:01:21 +00:00
electrum_server_current_block = None
electrs_active = False
def get_electrum_server_current_block():
global electrum_server_current_block
return electrum_server_current_block
def update_electrs_info():
global electrum_server_current_block
2019-12-11 02:31:32 +00:00
global electrs_active
2019-11-09 19:01:21 +00:00
try:
raw_data = requests.get("http://localhost:4224")
prom_data = text_string_to_metric_families(raw_data.text)
for family in prom_data:
for sample in family.samples:
if sample.name == "electrs_index_height":
electrum_server_current_block = int(sample.value)
2021-10-01 02:03:16 +00:00
elif sample.name == "index_height":
electrum_server_current_block = int(sample.value)
2019-12-11 02:31:32 +00:00
bitcoin_block_height = get_bitcoin_block_height()
if electrum_server_current_block != None and bitcoin_block_height != None:
if electrum_server_current_block > bitcoin_block_height - 2:
touch("/tmp/electrs_up_to_date")
2019-12-11 02:31:32 +00:00
electrs_active = True
2019-11-09 19:01:21 +00:00
except:
pass
def is_electrs_active():
global electrs_active
if not is_service_enabled("electrs"):
2020-04-20 03:34:35 +00:00
return False
2019-11-09 19:01:21 +00:00
return electrs_active
def get_electrs_status():
global electrum_server_current_block
global electrs_active
2020-04-18 04:00:48 +00:00
if not is_service_enabled("electrs"):
2020-04-18 04:00:48 +00:00
return "Disabled"
2019-11-09 19:01:21 +00:00
bitcoin_block_height = get_bitcoin_block_height()
log = ""
try:
2022-01-30 19:58:00 +00:00
log += to_string(subprocess.check_output("journalctl --unit=electrs --no-pager | tail -n 100", shell=True))
2019-11-09 19:01:21 +00:00
except:
log += ""
lines = log.splitlines()
lines.reverse()
for line in lines:
2021-10-01 02:03:16 +00:00
# Electrs pre v9
2019-11-09 19:01:21 +00:00
if "left to index)" in line:
break
elif "Checking if Bitcoin is synced..." in line or "NetworkInfo {" in line or "BlockchainInfo {" in line:
return "Starting..."
2020-04-18 04:00:48 +00:00
elif "opening DB at" in line or "enabling auto-compactions" in line:
return "Starting..."
2021-03-09 03:55:39 +00:00
elif "downloading 100000 block headers" in line or "downloading new block headers" in line:
2020-10-03 04:43:30 +00:00
return "Getting headers..."
2019-11-09 19:01:21 +00:00
elif "starting full compaction" in line:
2020-10-03 04:43:30 +00:00
return "Compressing..."
2019-11-09 19:01:21 +00:00
elif "enabling auto-compactions" in line:
break
elif "RPC server running on" in line:
break
2021-10-01 02:03:16 +00:00
# Electrs v9+
elif "stopping Electrum RPC server" in line or "notified via SIG15" in line:
return "Stopping..."
elif "serving Electrum RPC on 0.0.0.0:50001" in line:
break
elif "indexing 2000 blocks" in line:
break
elif "indexing 1 blocks" in line:
break
elif "starting config compaction" in line or "starting headers compaction" in line or "starting txid compaction" in line:
return "Compressing..."
elif "starting funding compaction" in line or "starting spending compaction" in line:
return "Compressing..."
elif "loading 12 blocks" in line:
break
2019-11-09 19:01:21 +00:00
if electrum_server_current_block != None and bitcoin_block_height != None:
if electrum_server_current_block < bitcoin_block_height - 10:
percent = 100.0 * (float(electrum_server_current_block) / bitcoin_block_height)
return "Syncing... {:.2f}%".format(abs(percent))
else:
electrs_active = True
return "Running"
return ""
def get_electrs_db_size(is_testnet=False):
size = "Unknown"
try:
2021-10-01 02:03:16 +00:00
folder = "/mnt/hdd/mynode/electrs/bitcoin"
if is_testnet:
folder = "/mnt/hdd/mynode/electrs/testnet"
2022-01-30 19:58:00 +00:00
size = to_string(subprocess.check_output("du -h "+folder+" | head -n1 | awk '{print $1;}'", shell=True))
except Exception as e:
size = "Error"
2022-01-30 19:58:00 +00:00
return size
2019-11-09 19:01:21 +00:00
def get_from_electrum(method, params=[]):
params = [params] if type(params) is not list else params
s = socket.create_connection(('127.0.0.1', 50001))
s.send(json.dumps({"id": 0, "method": method, "params": params}).encode() + b'\n')
return json.loads(s.recv(99999)[:-1].decode())