-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProofOfStake.py
50 lines (40 loc) · 1.55 KB
/
ProofOfStake.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
from BlockchainUtils import BlockchainUtils
from Lot import Lot
class ProofOfStake():
def __init__(self):
self.stakers = {}
self.setGenesisNodeStake()
def setGenesisNodeStake(self):
genesisPublicKey = open('keys/genesisPublicKey.pem', 'r').read()
self.stakers[genesisPublicKey] = 1
def update(self, publicKeyString, stake):
if publicKeyString in self.stakers.keys():
self.stakers[publicKeyString] += stake
else:
self.stakers[publicKeyString] = stake
def get(self, publicKeyString):
if publicKeyString in self.stakers.keys():
return self.stakers.get(publicKeyString)
else:
None
def validatorLots(self, seed):
lots = []
for validator in self.stakers.keys():
for stake in range(self.get(validator)):
lots.append(Lot(validator, stake+1, seed))
return lots
def winnerLot(self, lots, seed):
winnerLot = None
leastOffset = None
referenceHashIntValue = int(BlockchainUtils.hash(seed).hexdigest(), 16)
for lot in lots:
lotIntValue = int(lot.lotHash(), 16)
offset = abs(lotIntValue - referenceHashIntValue)
if leastOffset is None or offset < leastOffset:
leastOffset = offset
winnerLot = lot
return winnerLot
def forger(self, lastBlockHash):
lots = self.validatorLots(lastBlockHash)
winnerLot = self.winnerLot(lots, lastBlockHash)
return winnerLot.publicKey