forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitelist.go
231 lines (183 loc) · 6.38 KB
/
whitelist.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
221
222
223
224
225
226
227
228
229
230
231
package finality
import (
"context"
"errors"
"fmt"
"time"
"github.com/nebojsa94/erigon/erigon-lib/kv"
"github.com/nebojsa94/erigon/consensus/bor/finality/flags"
"github.com/nebojsa94/erigon/consensus/bor/finality/whitelist"
"github.com/nebojsa94/erigon/consensus/bor/heimdall"
"github.com/nebojsa94/erigon/turbo/services"
"github.com/ledgerwatch/log/v3"
)
type config struct {
heimdall heimdall.IHeimdallClient
borDB kv.RwDB
chainDB kv.RwDB
blockReader services.BlockReader
logger log.Logger
borAPI BorAPI
closeCh chan struct{}
}
type BorAPI interface {
GetRootHash(start uint64, end uint64) (string, error)
}
func Whitelist(heimdall heimdall.IHeimdallClient, borDB kv.RwDB, chainDB kv.RwDB, blockReader services.BlockReader, logger log.Logger, borAPI BorAPI, closeCh chan struct{}) {
if !flags.Milestone {
return
}
config := &config{
heimdall: heimdall,
borDB: borDB,
chainDB: chainDB,
blockReader: blockReader,
logger: logger,
borAPI: borAPI,
closeCh: closeCh,
}
go startCheckpointWhitelistService(config)
go startMilestoneWhitelistService(config)
go startNoAckMilestoneService(config)
go startNoAckMilestoneByIDService(config)
}
const (
whitelistTimeout = 30 * time.Second
noAckMilestoneTimeout = 4 * time.Second
)
// StartCheckpointWhitelistService starts the goroutine to fetch checkpoints and update the
// checkpoint whitelist map.
func startCheckpointWhitelistService(config *config) {
const (
tickerDuration = 100 * time.Second
fnName = "whitelist checkpoint"
)
RetryHeimdallHandler(handleWhitelistCheckpoint, config, tickerDuration, whitelistTimeout, fnName)
}
// startMilestoneWhitelistService starts the goroutine to fetch milestiones and update the
// milestone whitelist map.
func startMilestoneWhitelistService(config *config) {
const (
tickerDuration = 12 * time.Second
fnName = "whitelist milestone"
)
RetryHeimdallHandler(handleMilestone, config, tickerDuration, whitelistTimeout, fnName)
}
func startNoAckMilestoneService(config *config) {
const (
tickerDuration = 6 * time.Second
fnName = "no-ack-milestone service"
)
RetryHeimdallHandler(handleNoAckMilestone, config, tickerDuration, noAckMilestoneTimeout, fnName)
}
func startNoAckMilestoneByIDService(config *config) {
const (
tickerDuration = 1 * time.Minute
fnName = "no-ack-milestone-by-id service"
)
RetryHeimdallHandler(handleNoAckMilestoneByID, config, tickerDuration, noAckMilestoneTimeout, fnName)
}
type heimdallHandler func(ctx context.Context, heimdallClient heimdall.IHeimdallClient, config *config) error
func RetryHeimdallHandler(fn heimdallHandler, config *config, tickerDuration time.Duration, timeout time.Duration, fnName string) {
retryHeimdallHandler(fn, config, tickerDuration, timeout, fnName)
}
func retryHeimdallHandler(fn heimdallHandler, config *config, tickerDuration time.Duration, timeout time.Duration, fnName string) {
// a shortcut helps with tests and early exit
select {
case <-config.closeCh:
return
default:
}
if config.heimdall == nil {
config.logger.Error("[bor] engine not available")
return
}
// first run for fetching milestones
firstCtx, cancel := context.WithTimeout(context.Background(), timeout)
err := fn(firstCtx, config.heimdall, config)
cancel()
if err != nil {
config.logger.Warn(fmt.Sprintf("[bor] unable to start the %s service - first run", fnName), "err", err)
}
ticker := time.NewTicker(tickerDuration)
defer ticker.Stop()
for {
defer func() {
r := recover()
if r != nil {
log.Warn(fmt.Sprintf("[bor] service %s- run failed with panic", fnName), "err", r)
}
}()
select {
case <-ticker.C:
ctx, cancel := context.WithTimeout(context.Background(), timeout)
err := fn(ctx, config.heimdall, config)
cancel()
if err != nil {
config.logger.Warn(fmt.Sprintf("[bor] unable to handle %s", fnName), "err", err)
}
case <-config.closeCh:
return
}
}
}
// handleWhitelistCheckpoint handles the checkpoint whitelist mechanism.
func handleWhitelistCheckpoint(ctx context.Context, heimdallClient heimdall.IHeimdallClient, config *config) error {
service := whitelist.GetWhitelistingService()
// Create a new bor verifier, which will be used to verify checkpoints and milestones
verifier := newBorVerifier()
blockNum, blockHash, err := fetchWhitelistCheckpoint(ctx, heimdallClient, verifier, config)
// If the array is empty, we're bound to receive an error. Non-nill error and non-empty array
// means that array has partial elements and it failed for some block. We'll add those partial
// elements anyway.
if err != nil {
return err
}
service.ProcessCheckpoint(blockNum, blockHash)
return nil
}
// handleMilestone handles the milestone mechanism.
func handleMilestone(ctx context.Context, heimdallClient heimdall.IHeimdallClient, config *config) error {
service := whitelist.GetWhitelistingService()
// Create a new bor verifier, which will be used to verify checkpoints and milestones
verifier := newBorVerifier()
num, hash, err := fetchWhitelistMilestone(ctx, heimdallClient, verifier, config)
// If the current chain head is behind the received milestone, add it to the future milestone
// list. Also, the hash mismatch (end block hash) error will lead to rewind so also
// add that milestone to the future milestone list.
if errors.Is(err, errMissingBlocks) || errors.Is(err, errHashMismatch) {
service.ProcessFutureMilestone(num, hash)
return nil
}
if errors.Is(err, heimdall.ErrServiceUnavailable) {
return nil
}
if err != nil {
return err
}
service.ProcessMilestone(num, hash)
return nil
}
func handleNoAckMilestone(ctx context.Context, heimdallClient heimdall.IHeimdallClient, config *config) error {
service := whitelist.GetWhitelistingService()
milestoneID, err := fetchNoAckMilestone(ctx, heimdallClient, config.logger)
if errors.Is(err, heimdall.ErrServiceUnavailable) {
return nil
}
if err != nil {
return err
}
service.RemoveMilestoneID(milestoneID)
return nil
}
func handleNoAckMilestoneByID(ctx context.Context, heimdallClient heimdall.IHeimdallClient, config *config) error {
service := whitelist.GetWhitelistingService()
milestoneIDs := service.GetMilestoneIDsList()
for _, milestoneID := range milestoneIDs {
err := fetchNoAckMilestoneByID(ctx, heimdallClient, milestoneID, config.logger)
if err == nil {
service.RemoveMilestoneID(milestoneID)
}
}
return nil
}