Skip to content

Commit

Permalink
added information in banner about total downloads and uploads, plus o…
Browse files Browse the repository at this point in the history
…ther cleanups
  • Loading branch information
chris-belcher committed Apr 15, 2018
1 parent aff525d commit 66dad7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
19 changes: 18 additions & 1 deletion electrumpersonalserver/hashes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import hashlib, binascii
import hashlib, binascii, math

## stuff copied from electrum's source

Expand Down Expand Up @@ -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'

15 changes: 7 additions & 8 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
Uptime: {uptime}
Blocksonly: {blocksonly}
Pruning: {pruning}
Download: {recvbytes}
Upload: {sentbytes}
https://github.com/chris-belcher/electrum-personal-server
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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"],
Expand All @@ -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
Expand Down

0 comments on commit 66dad7f

Please sign in to comment.