This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
period.go
220 lines (175 loc) · 5.29 KB
/
period.go
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package services
import (
"fmt"
"github.com/everstake/teztracker/models"
"github.com/guregu/null"
"math"
)
const (
cyclesOnVotingPeriod = 8
firstPeriodStartBlock = 1
periodKindProposal = "proposals"
periodKindBallot = "ballot"
quorumFormulaChangePeriod = 19
supermajority = 80
genesisQuorum = 0.8
minQuorum = 0.2
maxQuorum = 0.7
)
func (t *TezTracker) VotingPeriodsList() ([]models.PeriodInfo, error) {
periods, err := t.repoProvider.GetVotingPeriod().List()
if err != nil {
return periods, err
}
return periods, nil
}
func (t *TezTracker) VotingPeriodStats(id *int64) (info models.PeriodStats, err error) {
repo := t.repoProvider.GetVotingPeriod()
if id == nil {
info.Period, err = repo.GetCurrentPeriodId()
if err != nil {
return
}
} else {
info.Period = *id
}
info, err = repo.Info(info.Period)
if err != nil {
return info, err
}
var proposalHash string
if info.Kind == periodKindBallot {
ballots, err := repo.BallotsList(info.Period)
if err != nil {
return info, err
}
var b models.BallotsStat
for _, value := range ballots {
switch value.Ballot {
case "yay":
b.Yay = value.Rolls
case "nay":
b.Nay = value.Rolls
case "pass":
b.Pass = value.Rolls
}
proposalHash = value.Proposal
}
b.Supermajority = supermajority
b.Quorum, err = t.calcQuorumForPeriod(info.Period)
info.BallotsStat = &b
}
_, block, err := t.repoProvider.GetBlock().Find(models.Block{
Level: null.IntFrom(info.StartBlock),
})
if err != nil {
return info, err
}
//If block have active proposal replace
//for old proposals
if block.ActiveProposal != "" {
proposalHash = block.ActiveProposal
}
//Get additional information if proposalHash presented
if proposalHash != "" {
proposalInfo, err := repo.ProposalInfo(proposalHash)
if err != nil {
return info, err
}
info.Proposal = &proposalInfo
}
return info, nil
}
func (t *TezTracker) calcQuorumForPeriod(id int64) (quorum float64, err error) {
stats, err := t.repoProvider.GetVotingPeriod().StatsByKind(periodKindBallot)
if err != nil {
return quorum, err
}
calcFormula := QuorumOldFormula
quorum = genesisQuorum
//Used on new formula
avP := genesisQuorum
if len(stats) == 0 {
return quorum, nil
}
if stats[0].Period == id {
return quorum, nil
}
for i := 1; i < len(stats); i++ {
if stats[i].Period > quorumFormulaChangePeriod {
calcFormula = QuorumNewFormula
}
quorum, avP = calcFormula(avP, float64(stats[i-1].Rolls)/float64(stats[i-1].TotalRolls))
//Truncate
avP = math.Trunc(quorum*10000) / 10000
quorum = math.Trunc(quorum*10000) / 10000
if stats[i].Period == id {
return quorum, nil
}
}
return 0, fmt.Errorf("Level not found")
}
type QuorumFormula func(prevAverPart, actualPart float64) (quorum float64, averPart float64)
func QuorumOldFormula(prevAverPart, actualPart float64) (q float64, avP float64) {
avP = 0.8*prevAverPart + 0.2*actualPart
return avP, avP
}
func QuorumNewFormula(prevAverPart, actualPart float64) (q float64, avP float64) {
_, avP = QuorumOldFormula(prevAverPart, actualPart)
return minQuorum + avP*(maxQuorum-minQuorum), avP
}
func (t *TezTracker) ProposalsByPeriodID(id *int64, limits Limiter) (proposals []models.VotingProposal, count int64, err error) {
proposals, err = t.repoProvider.GetVotingPeriod().ProposalsList(id, limits.Limit())
if err != nil {
return proposals, 0, err
}
return proposals, 0, nil
}
func (t *TezTracker) GetProposalVoters(id int64, limits Limiter) (votes []models.ProposalVoter, count int64, err error) {
periodRepo := t.repoProvider.GetVotingPeriod()
count, err = periodRepo.VotersCount(id, periodKindProposal)
if err != nil {
return votes, 0, err
}
votes, err = periodRepo.VotersList(id, periodKindProposal, limits.Limit(), limits.Offset())
if err != nil {
return votes, 0, err
}
return votes, count, nil
}
func (t *TezTracker) GetBallotVoters(id int64, limits Limiter) (votes []models.ProposalVoter, count int64, err error) {
periodRepo := t.repoProvider.GetVotingPeriod()
count, err = periodRepo.VotersCount(id, periodKindBallot)
if err != nil {
return votes, 0, err
}
votes, err = t.repoProvider.GetVotingPeriod().VotersList(id, periodKindBallot, limits.Limit(), limits.Offset())
if err != nil {
return votes, 0, err
}
return votes, count, nil
}
func (t *TezTracker) GetProtocolsList(limits Limiter) (protocols []models.Protocol, count int64, err error) {
protocols, err = t.repoProvider.GetVotingPeriod().ProtocolsList(limits.Limit(), limits.Offset())
if err != nil {
return protocols, 0, err
}
return protocols, count, nil
}
func (t *TezTracker) GetPeriodNonVoters(id int64, limits Limiter) (proposals []models.Voter, count int64, err error) {
periodRepo := t.repoProvider.GetVotingPeriod()
count, err = periodRepo.PeriodNonVotersCount(id)
if err != nil {
return proposals, count, err
}
proposals, err = periodRepo.PeriodNonVotersList(id, limits.Limit(), limits.Offset())
if err != nil {
return proposals, 0, err
}
return proposals, count, err
}
func (t *TezTracker) calcVotingPeriod(id int64) (startBlock, endBlock int64) {
blocksInPeriod := t.BlocksInCycle() * cyclesOnVotingPeriod
startBlock = blocksInPeriod*id + firstPeriodStartBlock
return startBlock, startBlock + blocksInPeriod - 1
}