Skip to content

Instantly share code, notes, and snippets.

@stefanopepe
Created November 1, 2020 20:10
Show Gist options
  • Save stefanopepe/e60b974701ac64cec51632fc57d3e5e8 to your computer and use it in GitHub Desktop.
Save stefanopepe/e60b974701ac64cec51632fc57d3e5e8 to your computer and use it in GitHub Desktop.
A simple epoch counter for NEAR validators. It takes as input the folder containing json files of every new epoch, and creates an output with a list of all validators and the times they were in the active set.

NEAR Simple Epoch Counter

Use this python script together with Frol's near-validators-scoreboard to count how many epochs each of the validators was in the active set.

  1. Clone the repo above from the tree scoreboard for betanet (use other branches for different networks)
  2. Put the three python files above in your working folder
  3. Launch with the command python3 -i folder_processor.py <scoreboard_folder>*
  4. Call function processFiles(filesList) from the REPL to begin the parsing

*Where <scoreboard_folder> is the relative path to the git repo above, such as ../near-validators-scoreboard/

The end result will be a json file similar to the one below:

{
  "processed": [
    "9990957",
    "9980955",
    "9970951"
  ],
  "validators": {
    "31337x.31337.betanet": 3,
    "a_validator": 3,
    "ag_staking": 3,
    "alive29": 3,
    "aptemuyc4": 3,
    "averonix": 3,
    "betapool": 3,
    "bitcat.stakehouse.betanet": 3,
    "blink": 3,
    "c1.hashquark": 3,
    "cloudpost_pool": 2,
    "contract.bartalamew.betanet": 3,
    "contract.nats.betanet": 3,
    "felixschulz": 3,
    "fredrik_pool": 3,
    "gems": 3,
    "huglester": 3,
    "isillien": 3,
    "janliamnilsson": 3,
    "jaroslavrud": 3,
    "jazza": 2,
    "launooskuarttu": 3,
    "masternode24": 3,
    "mathwallet": 3,
    "maxhealth": 3,
    "maximus-power-pool.stakehouse.betanet": 3,
    "megatron": 3,
    "meleapool": 3,
    "mfsc": 3,
    "mort1.mort.betanet": 3,
    "nearcola": 2,
    "neozaru.stakehouse.betanet": 2,
    "olaiolsen": 3,
    "olegjan": 3,
    "oligarr": 3,
    "optimisticvalidator": 3,
    "paulainsley1betanet.stakehouse.betanet": 1,
    "phenom": 3,
    "plex2": 3,
    "pool.bibiwang.betanet": 3,
    "pool.everstake.betanet": 3,
    "pool.zeropool.betanet": 1,
    "poolz": 3,
    "pro.stakehouse.betanet": 3,
    "rockpathpool": 3,
    "ryabina": 3,
    "sfpool": 3,
    "shudo": 3,
    "simonhugo": 3,
    "sl-he": 3,
    "sl1sub": 2,
    "sllpool": 3,
    "smart.askold.betanet": 3,
    "somebody": 3,
    "sparkpool.test": 3,
    "stake-machine": 3,
    "stake_pool.sharchov.betanet": 2,
    "stakedpool.staked.test": 3,
    "stakefish.betanet": 2,
    "stakery": 3,
    "stakewolf_pool": 3,
    "stakin": 1,
    "stakingpool.youlaiwuqu.betanet": 2,
    "sub.rosalessierra91.betanet": 3,
    "sub1.crazylandd.betanet": 3,
    "swissqstakepoolsw3": 3,
    "tatianka": 3,
    "thepassivetrust.stakehouse.betanet": 2,
    "tommywesley": 3,
    "valeraverim": 2,
    "validator.arno_nym.betanet": 3,
    "validator_italia_contract": 3,
    "villiamsivertsen": 3,
    "vipstakes": 3,
    "volvos60909": 2,
    "www.nodeasy.test": 3,
    "yoda": 1,
    "yyyyyyyyyyyyy1": 3,
    "zenqqqq": 2,
    "777stakes": 2,
    "arm.armada.betanet": 2,
    "bucket": 2,
    "c2.inotel.betanet": 2,
    "delight_pool": 2,
    "dochpryof": 2,
    "driftforall": 2,
    "get_rich": 2,
    "joe1.joejoen.betanet": 2,
    "nearcool2": 2,
    "pool": 2,
    "sc.galbraith.betanet": 2,
    "stake2earnfunds2": 2,
    "staking_pool.lizhongbo3.betanet": 2,
    "techno_validator": 2,
    "c.eosis.betanet": 1,
    "interference": 1,
    "ou812": 1,
    "sc.foxnorth1210.betanet": 1
  }
}
#!/usr/bin/env python
import sys
import json
# use this class to open and write the epoch counter json file
class fileHandler:
def __init__(self, filePath):
self.filePath = filePath
# process the json file
def parseJson():
with open(filePath, "r") as fp:
# parse the file into json
return json.load(fp)
self.jsonFile = parseJson()
# update the json file
def updateJson(self, newContent):
with open(self.filePath, "w") as fp:
json.dump(newContent, fp)
fp.close()
# use this class to update the json file containing the epoch counter
class outputJson:
def __init__(self, output, epochId, validatorsArray):
self.output = output
self.epochId = epochId
self.validatorsArray = validatorsArray
self.isProcessed = False
# check if the epoch was already processed
def checkId():
try:
output.get("processed").index(epochId)
return True
except ValueError:
# add the epochId to the json
output["processed"].append(epochId)
return False
self.isProcessed = checkId()
# add one more epoch to the validator scoreboard
def addValidator():
for i in validatorsArray:
try:
output.get("validators").keys() == i
output["validators"][i] += 1
except KeyError:
# if not present, add the validator to the scoreboard
output["validators"][i] = 1
addValidator()
# use this class to parse the existing epoch json, and append the validators to an array
class inputJson:
def __init__(self, jsonString, filePath):
self.jsonString = jsonString
self.validators = []
# epochId
self.epochId = filePath.split('/')[-1].split('.')[0]
# append validators in this epochId to the array
def appendValidators():
for i in jsonString:
self.validators.append(i.get("account_id"))
appendValidators()
#!/usr/bin/env python
import sys
import re
from os import walk
import epoch_counter as myparser
# mypath is the directory containing the json files
mypath = sys.argv[1]
# filesList is the array with all epoch files to be processed
filesList = []
# this function appends only filenames made of numbers (the epoch files)
for (dirpath, dirnames, filenames) in walk(mypath):
for f in filenames:
if bool(re.search("^[0-9]", f)):
#print(f)
filesList.append(f)
# instantiating the object with the results. Change "validators_counted.json" with the desired output file
scoreFile = myparser.fileHandler("validators_counted.json")
# use the epoch_counter to process each of the files in the mypath folder
def processFiles(epochs):
for n in epochs:
epochFile = myparser.fileHandler(mypath + n)
validators = myparser.inputJson(epochFile.jsonFile, epochFile.filePath)
scoreboard = myparser.outputJson(scoreFile.jsonFile, validators.epochId, validators.validators)
if (scoreboard.isProcessed):
print("Epoch " + validators.epochId + " already processed")
else:
scoreFile.updateJson(scoreboard.output)
print("Epoch " + validators.epochId + " added to the epoch counter")
{"processed": [], "validators": {}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment