Skip to content

Instantly share code, notes, and snippets.

@moocowmoo
Last active October 9, 2018 23:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save moocowmoo/5338fb594c88f57d374eb8b9603260ae to your computer and use it in GitHub Desktop.
Save moocowmoo/5338fb594c88f57d374eb8b9603260ae to your computer and use it in GitHub Desktop.
calculate your masternode queue position
import io
import os
from bitcoinrpc.authproxy import AuthServiceProxy
def ParseConfig(fileBuffer):
assert type(fileBuffer) is type(b'')
f = io.StringIO(fileBuffer.decode('ascii', errors='ignore'), newline=None)
result = {}
for line in f:
assert type(line) is type(b''.decode())
stripped = line.strip()
if stripped.startswith('#'):
continue
if stripped == '':
continue
parts = stripped.split('=')
assert len(parts) == 2
parts[0] = parts[0].strip()
parts[1] = parts[1].strip()
result[parts[0]] = parts[1]
return result
# fix lib pathing - cwd only for now
def Connect():
home = os.path.expanduser("~")
with open(os.path.join(home,'.dashcore','dash.conf'), mode='rb') as f:
configFileBuffer = f.read()
config = ParseConfig(configFileBuffer)
protocol = 'http'
if 'rpcssl' in config and bool(config['rpcssl']) and int(config['rpcssl']) > 0:
protocol = 'https'
serverURL = protocol + '://' + config['rpcuser'] + ':' + config['rpcpassword'] + \
'@127.0.0.1:' + str(config['rpcport'])
return AuthServiceProxy(serverURL)
def getrpc():
return Connect()
#!/usr/bin/env python
import json
import subprocess
import sys
import time
import datetime
sys.path.insert(0, '.')
import dashrpc
rpc = dashrpc.getrpc()
mns = rpc.masternodelist('full')
now = int(datetime.datetime.utcnow().strftime("%s"))
mn_queue=[]
for line in mns:
mnstat = mns[line].split()
if mnstat[0] == 'ENABLED':
# if last paid time == 0
if int(mnstat[5]) == 0:
# use active seconds
mnstat.append(int(mnstat[4]))
else:
# now minus last paid
delta = now - int(mnstat[5])
# if > active seconds, use active seconds
if delta >= int(mnstat[4]):
mnstat.append(int(mnstat[4]))
# use active seconds
else:
mnstat.append(delta)
mn_queue.append(mnstat)
mn_queue = sorted(mn_queue, key=lambda x: x[8])
for line in mn_queue:
print line
#!/bin/bash
MNLISTCMD="dash-cli masternodelist full 2>/dev/null"
MNADDR=$1
if [ -z $MNADDR ]; then
echo "usage: $0 <masternode address>"
exit -1
fi
function _cache_command(){
# cache life in minutes
AGE=2
FILE=$1
AGE=$2
CMD=$3
OLD=0
CONTENTS=""
if [ -e $FILE ]; then
OLD=$(find $FILE -mmin +$AGE -ls | wc -l)
CONTENTS=$(cat $FILE);
fi
if [ -z "$CONTENTS" ] || [ "$OLD" -gt 0 ]; then
echo "REBUILD"
CONTENTS=$(eval $CMD)
echo "$CONTENTS" > $FILE
fi
echo "$CONTENTS"
}
MN_LIST=$(_cache_command /tmp/cached_mnlistfull 2 "$MNLISTCMD")
SORTED_MN_LIST=$(echo "$MN_LIST" | grep -w ENABLED | sed -e 's/[}|{]//' -e 's/"//g' -e 's/,//g' | grep -v ^$ | \
awk ' \
{
if ($7 == 0) {
TIME = $6
print $_ " " TIME
}
else {
xxx = ("'$NOW'" - $7)
if ( xxx >= $6) {
TIME = $6
}
else {
TIME = xxx
}
print $_ " " TIME
}
}' | sort -k10 -n)
MN_VISIBLE=$(echo "$SORTED_MN_LIST" | grep $MNADDR | wc -l)
MN_QUEUE_LENGTH=$(echo "$SORTED_MN_LIST" | wc -l)
MN_QUEUE_POSITION=$(echo "$SORTED_MN_LIST" | grep -A9999999 $MNADDR | wc -l)
MN_QUEUE_IN_SELECTION=$(( $MN_QUEUE_POSITION <= $(( $MN_QUEUE_LENGTH / 10 )) ))
echo "masternode $MNADDR"
if [ $MN_VISIBLE -gt 0 ]; then
echo " -> queue position $MN_QUEUE_POSITION/$MN_QUEUE_LENGTH"
if [ $MN_QUEUE_IN_SELECTION -gt 0 ]; then
echo " -> SELECTION PENDING"
fi
else
echo "is not in masternode list"
fi
@moocowmoo
Copy link
Author

thanks go to @chaeplin for the awk preprocessing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment