-
Notifications
You must be signed in to change notification settings - Fork 0
/
committee.py
47 lines (42 loc) · 1.64 KB
/
committee.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
from validator import Validator
class Committee:
def __init__(self, size, setup):
self.size = size
self.validators = []
self.votes = {}
self.proposer = None
self.selectedVoters = []
self.setup = setup
def chooseProposer(self):
self.proposer = self.setup.chooseProposer(self.validators)
def totalVotersVotingPower(self):
total = sum(v.votingPower for v in self.selectedVoters)
return total
def totalCommitteeVotingPower(self):
total = sum(v.votingPower for v in self.validators)
return total
def calculateRewards(self, reward):
bonus = self.setup.bonus * reward
reward = reward - bonus
total = self.totalVotersVotingPower()
totalCommittee = self.totalCommitteeVotingPower()
for validator in self.selectedVoters:
share = (validator.votingPower / total) * reward
if validator == self.proposer:
if self.setup.variational:
bonus = ((total - ((2/3)*totalCommittee)) / ((1/3)*totalCommittee))*bonus
share += bonus
else:
share += bonus
validator.updateReward(self.validators, share, reward)
def round(self):
self.chooseProposer()
newBlock = self.proposer.propose(self)
for v in self.validators:
self.votes[v] = v.sign(newBlock)
self.selectedVoters = self.proposer.selectVoters(self.votes)
if newBlock.isConfirmed(self.validators, self.selectedVoters):
return newBlock
else:
print ("Invalid")
return None