-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSigma.py
98 lines (84 loc) · 3.14 KB
/
getSigma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import json
import argparse
import os.path
from os import path
parser = argparse.ArgumentParser(description="Calculate the sigma value of the specified pool. If you do not provide the path to a ledger file via the --ledger option then the script will look for a ledger.json file in the current directory")
parser.add_argument('--pool-id', dest='id', help='the pool ID', required=True)
parser.add_argument('--ledger', dest='ledger', default='ledger.json', help='the path to a current ledger-state JSON file')
parser.add_argument('--next', action='store_true', help='if specified will provide sigma for the next epoch instead of the current epoch')
parser.add_argument('--porcelain', action='store_true', help='if specified will print JSON')
args = parser.parse_args()
poolId = args.id
ledger_file = args.ledger
if not path.exists(ledger_file):
txt1 = "We tried but could not locate your ledger-state JSON file!"
txt1 += "Use: \033[1;34mcardano-cli shelley query ledger-state --mainnet --out-file ledger.json\033[0m to export one!"
if not args.porcelain:
print(txt1)
else:
print('{ "error": "' + txt1 + '"}')
exit(1)
with open(ledger_file) as f:
ledger = json.load(f)
stakequery="_pstakeSet"
if args.next:
stakequery="_pstakeMark"
blockstakedelegators={}
blockstake={}
bs={}
delegations_attr = "_delegations"
stake_attr = "_stake"
if 'stateBefore' in ledger:
stake_set_query="pstakeSet"
delegations_attr = "delegations"
stake_attr = "stake"
if args.next:
stake_set_query="pstakeMark"
# jq '.stateBefore .esSnapshots .pstakeSet' /tmp/ledger.json
ledger_set=ledger['stateBefore']['esSnapshots'][stake_set_query]
elif 'nesEs' in ledger:
ledger_set=ledger['nesEs']['esSnapshots'][stakequery]
else:
ledger_set=ledger['esSnapshots'][stakequery]
for item2 in ledger_set[delegations_attr]:
keyhashobj = []
for itemsmall in item2:
if 'key hash' in itemsmall:
keyhashobj.append(itemsmall['key hash'])
else:
poolid = itemsmall
if poolid not in blockstakedelegators:
blockstakedelegators[poolid]=keyhashobj
else:
blockstakedelegators[poolid]=blockstakedelegators[poolid]+keyhashobj
for item2 in ledger_set[stake_attr]:
delegatorid = None
for itemsmall in item2:
if isinstance(itemsmall,int):
snapstake = itemsmall
else:
delegatorid=itemsmall['key hash']
if delegatorid != None:
if delegatorid not in blockstake:
blockstake[delegatorid]=snapstake
else:
blockstake[delegatorid]=blockstake[delegatorid]+snapstake
total_bs=0
for poolid in blockstakedelegators:
bs[poolid]=0
for d in blockstakedelegators[poolid]:
if d in blockstake:
bs[poolid]=bs[poolid]+blockstake[d]
total_bs=total_bs + blockstake[d]
sigma = float(bs[poolId]/total_bs)
if not args.porcelain:
print("Sigma:",sigma)
else:
result = dict()
result["sigma"] = "%.10f" % float(sigma)
if args.next:
result["stakeinfo"] = "next"
else:
result["stakeinfo"] = "active"
result["stakequery"] = stakequery
print(json.dumps(result))