commit 66dad7f2f6e9432464c45d1c5ea1abae0ee5877c
parent aff525d22b1b000f136062ee10e2709108cc66c8
Author: chris-belcher <chris-belcher@users.noreply.github.com>
Date: Mon, 16 Apr 2018 00:33:02 +0100
added information in banner about total downloads and uploads, plus other cleanups
Diffstat:
2 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/electrumpersonalserver/hashes.py b/electrumpersonalserver/hashes.py
@@ -1,5 +1,5 @@
-import hashlib, binascii
+import hashlib, binascii, math
## stuff copied from electrum's source
@@ -77,3 +77,20 @@ def address_to_script(addr, rpc):
def address_to_scripthash(addr, rpc):
return script_to_scripthash(address_to_script(addr, rpc))
+# doesnt really fit here but i dont want to clutter up server.py
+
+unit_list = list(zip(['B', 'kB', 'MB', 'GB', 'TB', 'PB'], [0, 0, 1, 2, 2, 2]))
+
+def bytes_fmt(num):
+ """Human friendly file size"""
+ if num > 1:
+ exponent = min(int(math.log(num, 1000)), len(unit_list) - 1)
+ quotient = float(num) / 1000**exponent
+ unit, num_decimals = unit_list[exponent]
+ format_string = '{:.%sf} {}' % (num_decimals)
+ return format_string.format(quotient, unit)
+ if num == 0:
+ return '0 bytes'
+ if num == 1:
+ return '1 byte'
+
diff --git a/server.py b/server.py
@@ -24,6 +24,8 @@ Peers: {peers}
Uptime: {uptime}
Blocksonly: {blocksonly}
Pruning: {pruning}
+Download: {recvbytes}
+Upload: {sentbytes}
https://github.com/chris-belcher/electrum-personal-server
@@ -180,9 +182,7 @@ def handle_query(sock, line, rpc, txmonitor):
if "previousblockhash" in header:
prevblockhash = header["previousblockhash"]
else:
- # this is the genesis block
- # it does not have a previous block hash
- prevblockhash = "00"*32
+ prevblockhash = "00"*32 #genesis block
h1 = struct.pack("<i32s32sIII", header["version"],
binascii.unhexlify(prevblockhash)[::-1],
binascii.unhexlify(header["merkleroot"])[::-1],
@@ -215,6 +215,7 @@ def handle_query(sock, line, rpc, txmonitor):
networkinfo = rpc.call("getnetworkinfo", [])
blockchaininfo = rpc.call("getblockchaininfo", [])
uptime = rpc.call("uptime", [])
+ nettotals = rpc.call("getnettotals", [])
send_response(sock, query, BANNER.format(
detwallets=len(txmonitor.deterministic_wallets),
addr=len(txmonitor.address_history),
@@ -223,6 +224,8 @@ def handle_query(sock, line, rpc, txmonitor):
uptime=str(datetime.timedelta(seconds=uptime)),
blocksonly=not networkinfo["localrelay"],
pruning=blockchaininfo["pruned"],
+ recvbytes=hashes.bytes_fmt(nettotals["totalbytesrecv"]),
+ sentbytes=hashes.bytes_fmt(nettotals["totalbytessent"]),
donationaddr=DONATION_ADDR))
elif method == "server.donation_address":
send_response(sock, query, DONATION_ADDR)
@@ -240,9 +243,7 @@ def get_block_header(rpc, blockhash):
if "previousblockhash" in rpc_head:
prevblockhash = rpc_head["previousblockhash"]
else:
- # this is the genesis block
- # it does not have a previous block hash
- prevblockhash = "00"*32
+ prevblockhash = "00"*32 #genesis block
header = {"block_height": rpc_head["height"],
"prev_block_hash": prevblockhash,
"timestamp": rpc_head["time"],
@@ -258,8 +259,6 @@ def get_current_header(rpc):
return new_bestblockhash, header
def check_for_new_blockchain_tip(rpc):
- #TODO might not handle more than one block appearing, might need to
- # use a "last known block" similar to the transaction code
new_bestblockhash, header = get_current_header(rpc)
is_tip_new = bestblockhash[0] != new_bestblockhash
bestblockhash[0] = new_bestblockhash