-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisSlotLeader.py
executable file
·188 lines (154 loc) · 6.18 KB
/
isSlotLeader.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Thanks to Papacarp [LOVE] and others who contributed.
import math
import binascii
import hashlib
import pytz
import argparse
from decimal import *
getcontext().prec = 9
getcontext().rounding = ROUND_HALF_UP
from datetime import datetime, timezone
from ctypes import *
parser = argparse.ArgumentParser(description="Calculate the leadership log.")
parser.add_argument('--epoch-length',
type=int,
dest='epochLength',
help='the epoch length [e.g. 432000]',
required=True)
parser.add_argument('--genesis-start',
dest='genesisStart',
help='network start time',
required=True)
parser.add_argument('--first-slot-of-epoch',
type=int,
dest='fslot',
help='the first slot of the epoch',
required=True)
parser.add_argument('--epoch-nonce',
dest='eta0',
help='the epoch nonce to check',
required=True)
parser.add_argument('--vrf-skey',
dest='skey',
help='provide the path to the pool.vrf.skey file',
required=True)
parser.add_argument(
'--sigma',
type=float,
dest='sigma',
help=
'the controlled stake sigma value of the pool (e.g. 0.0034052348379780869',
required=True)
parser.add_argument('--d',
type=float,
dest='d',
help='the decentralizationParam param d',
required=True)
parser.add_argument('--active-slots-coeff',
type=float,
dest='activeSlotsCoeff',
help='the activeSlotsCoeff [e.g. 0.05]',
required=True)
parser.add_argument('--libsodium-binary',
dest='libsodiumBin',
help='the path to libsodium',
required=True)
parser.add_argument('--time-zone',
type=str,
dest='timezone',
help='the timezone of the user',
required=True)
args = parser.parse_args()
epochLength = args.epochLength
genesisStart = args.genesisStart
activeSlotsCoeff = args.activeSlotsCoeff
firstSlotOfEpoch = args.fslot
sigma = args.sigma
eta0 = args.eta0
poolVrfSkey = args.skey
decentralizationParam = args.d
timezone = 'Europe/Berlin' if args.timezone not in pytz.all_timezones else args.timezone
def getGenesisStartEpoch(genesisStart):
genesis_start_date_obj = datetime.strptime(genesisStart,
'%Y-%m-%dT%H:%M:%SZ')
genesis_start_date_obj = genesis_start_date_obj.replace(tzinfo=pytz.UTC)
genesis_start_epoch = genesis_start_date_obj.timestamp()
return int(genesis_start_epoch)
slotcount = 0
try:
local_tz = pytz.timezone(timezone)
except pytz.exceptions.UnknownTimeZoneError as error:
print(
"Wrong or unknown timeZone, reverting back to default timeZone ==> 'Europe/Berlin'"
)
local_tz = pytz.timezone('Europe/Berlin')
# Bindings are not avaliable so using ctypes to just force it in for now.
libsodium = cdll.LoadLibrary(args.libsodiumBin)
libsodium.sodium_init()
def mkSeed(slot, eta0):
h = hashlib.blake2b(digest_size=32)
h.update(slot.to_bytes(8, byteorder='big') + binascii.unhexlify(eta0))
slotToSeedBytes = h.digest()
return slotToSeedBytes
def vrfEvalCertified(seed, praosCanBeLeaderSignKeyVRF):
if isinstance(seed, bytes) and isinstance(praosCanBeLeaderSignKeyVRF,
bytes):
proof = create_string_buffer(
libsodium.crypto_vrf_ietfdraft03_proofbytes())
libsodium.crypto_vrf_prove(proof, praosCanBeLeaderSignKeyVRF, seed,
len(seed))
proofHash = create_string_buffer(libsodium.crypto_vrf_outputbytes())
libsodium.crypto_vrf_proof_to_hash(proofHash, proof)
return proofHash.raw
else:
print("error. Feed me bytes")
exit()
def vrfLeaderValue(vrfCert):
h = hashlib.blake2b(digest_size=32)
h.update(str.encode("L"))
h.update(vrfCert)
vrfLeaderValueBytes = h.digest()
return int.from_bytes(vrfLeaderValueBytes, byteorder="big", signed=False)
def isOverlaySlot(firstSlotOfEpoch, currentSlot, decentralizationParam):
diff_slot = float(currentSlot - firstSlotOfEpoch)
left = Decimal(diff_slot) * Decimal(decentralizationParam)
right = Decimal(diff_slot + 1) * Decimal(decentralizationParam)
if math.ceil(left) < math.ceil(right):
return True
return False
# Determine if our pool is a slot leader for this given slot
# @param slot The slot to check
# @param activeSlotsCoeff The activeSlotsCoeff value from protocol params
# @param sigma The controlled stake proportion for the pool
# @param eta0 The epoch nonce value
# @param poolVrfSkey The vrf signing key for the pool
def isSlotLeader(slot, activeSlotsCoeff, sigma, eta0, poolVrfSkey):
seed = mkSeed(slot, eta0)
praosCanBeLeaderSignKeyVRFb = binascii.unhexlify(poolVrfSkey)
cert = vrfEvalCertified(seed, praosCanBeLeaderSignKeyVRFb)
certLeaderVrf = vrfLeaderValue(cert)
certNatMax = math.pow(2, 256)
denominator = certNatMax - certLeaderVrf
q = certNatMax / denominator
c = math.log(1.0 - activeSlotsCoeff)
sigmaOfF = math.exp(-sigma * c)
return q <= sigmaOfF
print("[")
for slot in range(firstSlotOfEpoch, epochLength + firstSlotOfEpoch):
if isOverlaySlot(firstSlotOfEpoch, slot, decentralizationParam):
continue
slotLeader = isSlotLeader(slot, activeSlotsCoeff, sigma, eta0, poolVrfSkey)
if slotLeader:
if slotcount > 0:
print(" ,")
slotcount += 1
timestamp = datetime.fromtimestamp(slot +
1591566291,
tz=local_tz)
print(" {")
print(" \"index\": " + str(slotcount) + ",")
print(" \"slot\": " + str(slot - firstSlotOfEpoch) + ",")
print(" \"date\": \"" +
timestamp.strftime('%Y-%m-%d %H:%M:%S') + "\"")
print(" }")
print("]")