-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvoting.go
More file actions
191 lines (156 loc) · 4.19 KB
/
voting.go
File metadata and controls
191 lines (156 loc) · 4.19 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
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
package cronjob
import (
"flare-indexer/database"
indexerctx "flare-indexer/indexer/context"
"flare-indexer/indexer/pchain"
"flare-indexer/logger"
"flare-indexer/utils"
"flare-indexer/utils/staking"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
)
const (
votingStateName string = "voting_cronjob"
)
var (
zeroBytes [32]byte = [32]byte{}
zeroBytesHash common.Hash = crypto.Keccak256Hash(zeroBytes[:])
ErrEpochConfig = errors.New("epoch config mismatch")
)
type votingCronjob struct {
epochCronjob
db votingDB
contract votingContract
// For testing to set "now" to some past date
time utils.ShiftedTime
}
type votingDB interface {
FetchState(name string) (database.State, error)
FetchPChainVotingData(start, end time.Time) ([]database.PChainTxData, error)
UpdateState(state *database.State) error
}
type votingContract interface {
ShouldVote(epoch *big.Int) (bool, error)
SubmitVote(epoch *big.Int, merkleRoot [32]byte) error
EpochConfig() (time.Time, time.Duration, error)
}
func NewVotingCronjob(ctx indexerctx.IndexerContext) (*votingCronjob, error) {
cfg := ctx.Config()
if !cfg.VotingCronjob.Enabled {
return &votingCronjob{}, nil
}
db := &votingDBGorm{g: ctx.DB()}
contract, err := newVotingContractCChain(cfg)
if err != nil {
return nil, err
}
start, period, err := contract.EpochConfig()
if err != nil {
return nil, err
}
epochs := staking.NewEpochInfo(&cfg.VotingCronjob.EpochConfig, start, period)
vc := &votingCronjob{
epochCronjob: newEpochCronjob(&cfg.VotingCronjob.CronjobConfig, epochs),
db: db,
contract: contract,
}
err = vc.reset(ctx.Flags().ResetVotingCronjob)
if err != nil {
return nil, err
}
vc.metrics = newEpochCronjobMetrics(votingStateName)
return vc, nil
}
func (c *votingCronjob) Name() string {
return votingStateName
}
func (c *votingCronjob) OnStart() error {
return nil
}
func (c *votingCronjob) RandomTimeoutDelta() time.Duration {
return 10 * time.Second
}
func (c *votingCronjob) Call() error {
idxState, err := c.db.FetchState(pchain.StateName)
if err != nil {
return err
}
state, err := c.db.FetchState(votingStateName)
if err != nil {
return err
}
now := c.time.Now()
// Last epoch that was submitted to the contract
epochRange := c.getEpochRange(int64(state.NextDBIndex), now)
logger.Debug("Voting needed for epochs [%d, %d]", epochRange.start, epochRange.end)
c.updateLastEpochMetrics(epochRange.end)
for e := epochRange.start; e <= epochRange.end; e++ {
start, end := c.epochs.GetTimeRange(e)
if c.indexerBehind(&idxState, e) {
logger.Debug("indexer is behind, skipping voting for epoch %d", e)
return nil
}
votingData, err := c.db.FetchPChainVotingData(start, end)
if err != nil {
return err
}
err = c.submitVotes(e, votingData)
if err != nil {
return err
}
state.NextDBIndex = uint64(e + 1)
if err := c.db.UpdateState(&state); err != nil {
return err
}
c.updateLastProcessedEpochMetrics(e)
}
return nil
}
// Return true if the vote was submitted, and false if shouldVote returned false
func (c *votingCronjob) submitVotes(e int64, votingData []database.PChainTxData) error {
votingData = staking.DedupeTxs(votingData)
shouldVote, err := c.contract.ShouldVote(big.NewInt(e))
if err != nil {
return err
}
if !shouldVote {
logger.Debug("Voting not needed for epoch %d", e)
return nil
}
var merkleRoot common.Hash
if len(votingData) == 0 {
merkleRoot = zeroBytesHash
} else {
merkleRoot, err = staking.GetMerkleRoot(votingData)
if err != nil {
return err
}
}
// Submit vote and wait for the transaction to be mined
err = c.contract.SubmitVote(big.NewInt(e), [32]byte(merkleRoot))
if err != nil {
return err
}
logger.Info("Submitted vote for epoch %d", e)
return nil
}
func (c *votingCronjob) reset(firstEpoch int64) error {
if firstEpoch <= 0 {
return nil
}
logger.Info("Resetting voting cronjob state to epoch %d", firstEpoch)
state, err := c.db.FetchState(votingStateName)
if err != nil {
return err
}
state.NextDBIndex = uint64(firstEpoch)
err = c.db.UpdateState(&state)
if err != nil {
return err
}
c.epochs.First = firstEpoch
return nil
}