-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProofOfStake.py
More file actions
55 lines (48 loc) · 1.84 KB
/
Copy pathProofOfStake.py
File metadata and controls
55 lines (48 loc) · 1.84 KB
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
from Lot import Lot
from Utils import Utils
class ProofOfStake():
def __init__(self):
self.stakers = {}
self.setGenesisNodeStaker()
# init genesis staker
def setGenesisNodeStaker(self):
genesisPubKey = open('keys/genesisPublicKey.pem', 'r').read()
self.stakers[genesisPubKey] = 1
# update stake of a staker
def update(self, pubKeyString, stake):
if pubKeyString in self.stakers:
self.stakers[pubKeyString] += stake
else:
self.stakers[pubKeyString] = stake
# get stake of a staker
def get(self, pubKeyString):
if pubKeyString in self.stakers:
return self.stakers[pubKeyString]
else:
return None
# get all lots
def getValidatorLots(self, seed):
lots = []
for validator in self.stakers.keys():
# validator has more stake will have more lots
for stake in range(self.get(validator)):
lots.append(Lot(validator, stake+1, seed))
return lots
# determine whose lot is the winner
# use probability: the one who has more lots will get higher chance to win
def getWinnerLot(self, lots, seed):
winnerLot = None
leastOffset = None
referenceHashIntValue = int(Utils.hash(seed).hexdigest(), 16) #hash to make random
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
# find the forger of the next block
def getForger(self, lastBlockHash):
lots = self.getValidatorLots(lastBlockHash)
winnerLot: Lot = self.getWinnerLot(lots, lastBlockHash)
return winnerLot.pubKey