Skip to content

Instantly share code, notes, and snippets.

@mfornet
Last active April 24, 2020 20:45
Show Gist options
  • Save mfornet/173bca53b5ea907d3988336065204816 to your computer and use it in GitHub Desktop.
Save mfornet/173bca53b5ea907d3988336065204816 to your computer and use it in GitHub Desktop.
Seats required for n validators
def tPoS(tokens, seats):
lo, hi = 0, 1
def get_seats(value):
total = 0
for tok in tokens:
total += tok // value
return total
while get_seats(hi) >= seats:
hi *= 2
while lo + 1 < hi:
mid = (lo + hi) // 2
if get_seats(mid) >= seats:
lo = mid
else:
hi = mid
return lo
def run(max_token, expected_validator, s=1):
"""
s: Parameter from power lay
max_token: Number of token that has the validator with more tokens
"""
tokens = [int(max_token // k**s) for k in range(1, expected_validator + 1)]
lo, hi = 0, 1
while tPoS(tokens, hi) > tokens[-1]:
hi *= 2
while lo + 1 < hi:
mid = (lo + hi) // 2
th = tPoS(tokens, mid)
if tPoS(tokens, mid) < tokens[-1]:
hi = mid
else:
lo = mid
seats = hi
threshold = tPoS(tokens, seats)
print("seats:", seats)
print("min tokens:", tokens[-1])
print("threshold:", threshold)
print("Tokens per validator")
for ix, token in enumerate(tokens):
print(f"{ix:>3} {token:>30}")
run(int(10**24), 100, s=1)
seats: 483
min tokens: 10000000000000000000000
threshold: 9900990099009900990099
Tokens per validator
0 1000000000000000000000000
1 500000000000000000000000
2 333333333333333333333333
3 250000000000000000000000
4 200000000000000000000000
5 166666666666666666666666
6 142857142857142857142857
7 125000000000000000000000
8 111111111111111111111111
9 100000000000000000000000
10 90909090909090909090909
11 83333333333333333333333
12 76923076923076923076923
13 71428571428571428571428
14 66666666666666666666666
15 62500000000000000000000
16 58823529411764705882352
17 55555555555555555555555
18 52631578947368421052631
19 50000000000000000000000
20 47619047619047619047619
21 45454545454545454545454
22 43478260869565217391304
23 41666666666666666666666
24 40000000000000000000000
25 38461538461538461538461
26 37037037037037037037037
27 35714285714285714285714
28 34482758620689655172413
29 33333333333333333333333
30 32258064516129032258064
31 31250000000000000000000
32 30303030303030303030303
33 29411764705882352941176
34 28571428571428571428571
35 27777777777777777777777
36 27027027027027027027027
37 26315789473684210526315
38 25641025641025641025641
39 25000000000000000000000
40 24390243902439024390243
41 23809523809523809523809
42 23255813953488372093023
43 22727272727272727272727
44 22222222222222222222222
45 21739130434782608695652
46 21276595744680851063829
47 20833333333333333333333
48 20408163265306122448979
49 20000000000000000000000
50 19607843137254901960784
51 19230769230769230769230
52 18867924528301886792452
53 18518518518518518518518
54 18181818181818181818181
55 17857142857142857142857
56 17543859649122807017543
57 17241379310344827586206
58 16949152542372881355932
59 16666666666666666666666
60 16393442622950819672131
61 16129032258064516129032
62 15873015873015873015873
63 15625000000000000000000
64 15384615384615384615384
65 15151515151515151515151
66 14925373134328358208955
67 14705882352941176470588
68 14492753623188405797101
69 14285714285714285714285
70 14084507042253521126760
71 13888888888888888888888
72 13698630136986301369863
73 13513513513513513513513
74 13333333333333333333333
75 13157894736842105263157
76 12987012987012987012987
77 12820512820512820512820
78 12658227848101265822784
79 12500000000000000000000
80 12345679012345679012345
81 12195121951219512195121
82 12048192771084337349397
83 11904761904761904761904
84 11764705882352941176470
85 11627906976744186046511
86 11494252873563218390804
87 11363636363636363636363
88 11235955056179775280898
89 11111111111111111111111
90 10989010989010989010989
91 10869565217391304347826
92 10752688172043010752688
93 10638297872340425531914
94 10526315789473684210526
95 10416666666666666666666
96 10309278350515463917525
97 10204081632653061224489
98 10101010101010101010101
99 10000000000000000000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment