-
Notifications
You must be signed in to change notification settings - Fork 211
/
syncer.go
460 lines (408 loc) · 11.5 KB
/
syncer.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package malsync
import (
"context"
"errors"
"fmt"
"time"
"github.com/jonboulle/clockwork"
"go.uber.org/zap"
"golang.org/x/exp/maps"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/fetch"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/identities"
"github.com/spacemeshos/go-spacemesh/sql/localsql"
"github.com/spacemeshos/go-spacemesh/sql/malsync"
"github.com/spacemeshos/go-spacemesh/system"
)
//go:generate mockgen -typed -package=mocks -destination=./mocks/mocks.go -source=./syncer.go
type fetcher interface {
SelectBestShuffled(int) []p2p.Peer
GetMaliciousIDs(context.Context, p2p.Peer) ([]types.NodeID, error)
system.MalfeasanceProofFetcher
}
type Opt func(*Syncer)
func WithLogger(logger *zap.Logger) Opt {
return func(s *Syncer) {
s.logger = logger
}
}
type counter interface {
Inc()
}
type noCounter struct{}
func (noCounter) Inc() {}
func WithPeerErrMetric(counter counter) Opt {
return func(s *Syncer) {
s.peerErrMetric = counter
}
}
func withClock(clock clockwork.Clock) Opt {
return func(s *Syncer) {
s.clock = clock
}
}
func DefaultConfig() Config {
return Config{
IDRequestInterval: 30 * time.Minute,
MalfeasanceIDPeers: 3,
MinSyncPeers: 3,
MaxEpochFraction: 0.25,
MaxBatchSize: 1000,
RequestsLimit: 20,
RetryInterval: time.Minute,
}
}
type Config struct {
// IDRequestInterval specifies the interval for malfeasance proof id requests to the network.
IDRequestInterval time.Duration `mapstructure:"id-request-interval"`
// MalfeasanceIDPeers is the number of peers to fetch node IDs for malfeasance proofs from.
MalfeasanceIDPeers int `mapstructure:"malfeasance-id-peers"`
// Minimum number of peers to sync against for initial sync to be considered complete.
MinSyncPeers int `mapstructure:"min-sync-peers"`
// MaxEpochFraction specifies maximum fraction of epoch to expire before
// synchronous malfeasance proof sync is needed upon startup.
MaxEpochFraction float64 `mapstructure:"max-epoch-fraction"`
// MaxBatchSize is the maximum number of node IDs to sync in a single request.
MaxBatchSize int `mapstructure:"max-batch-size"`
// RequestsLimit is the maximum number of requests for a single malfeasance proof.
//
// The purpose of it is to prevent peers from advertising invalid node ID and disappearing.
// Which will make node ask other peers for invalid malfeasance proofs.
// It will be reset to 0 once malfeasance proof is advertised again.
RequestsLimit int `mapstructure:"requests-limit"`
// RetryInterval specifies retry interval for the initial sync.
RetryInterval time.Duration `mapstructure:"retry-interval"`
}
func WithConfig(cfg Config) Opt {
return func(s *Syncer) {
s.cfg = cfg
}
}
type syncPeerSet map[p2p.Peer]struct{}
func (sps syncPeerSet) add(peer p2p.Peer) {
sps[peer] = struct{}{}
}
func (sps syncPeerSet) clear() {
maps.Clear(sps)
}
func (sps syncPeerSet) updateFrom(other syncPeerSet) {
maps.Copy(sps, other)
}
// syncState stores malfeasance sync state.
type syncState struct {
limit int
initial bool
state map[types.NodeID]int
syncingPeers syncPeerSet
syncedPeers syncPeerSet
}
func newSyncState(limit int, initial bool) *syncState {
return &syncState{
limit: limit,
initial: initial,
state: make(map[types.NodeID]int),
syncedPeers: make(syncPeerSet),
syncingPeers: make(syncPeerSet),
}
}
func (sst *syncState) done() {
if sst.initial {
sst.syncedPeers.updateFrom(sst.syncingPeers)
sst.syncingPeers.clear()
}
maps.Clear(sst.state)
}
func (sst *syncState) numSyncedPeers() int {
return len(sst.syncedPeers)
}
func (sst *syncState) update(update malUpdate) {
if sst.initial {
sst.syncingPeers.add(update.peer)
}
for _, id := range update.nodeIDs {
if _, found := sst.state[id]; !found {
sst.state[id] = 0
}
}
}
func (sst *syncState) has(nodeID types.NodeID) bool {
_, found := sst.state[nodeID]
return found
}
func (sst *syncState) failed(nodeID types.NodeID) {
// possibly temporary failure, count failed attempt
n := sst.state[nodeID]
if n >= 0 {
sst.state[nodeID] = n + 1
}
}
func (sst *syncState) rejected(nodeID types.NodeID) {
// malfeasance proof didn't pass validation, no sense in requesting it anymore
n := sst.state[nodeID]
if n >= 0 {
sst.state[nodeID] = sst.limit
}
}
func (sst *syncState) downloaded(nodeID types.NodeID) {
sst.state[nodeID] = -1
}
func (sst *syncState) missing(max int, has func(nodeID types.NodeID) (bool, error)) ([]types.NodeID, error) {
r := make([]types.NodeID, 0, max)
for nodeID, count := range sst.state {
if count < 0 {
continue // already downloaded
}
exists, err := has(nodeID)
if err != nil {
return nil, err
}
if exists {
sst.downloaded(nodeID)
continue
}
if count >= sst.limit {
// unsuccessfully requested too many times
delete(sst.state, nodeID)
continue
}
r = append(r, nodeID)
if len(r) == cap(r) {
break
}
}
return r, nil
}
type Syncer struct {
logger *zap.Logger
cfg Config
fetcher fetcher
db sql.Executor
localdb *localsql.Database
clock clockwork.Clock
peerErrMetric counter
}
func New(fetcher fetcher, db sql.Executor, localdb *localsql.Database, opts ...Opt) *Syncer {
s := &Syncer{
logger: zap.NewNop(),
cfg: DefaultConfig(),
fetcher: fetcher,
db: db,
localdb: localdb,
clock: clockwork.NewRealClock(),
peerErrMetric: noCounter{},
}
for _, opt := range opts {
opt(s)
}
return s
}
func (s *Syncer) shouldSync(epochStart, epochEnd time.Time) (bool, error) {
timestamp, err := malsync.GetSyncState(s.localdb)
if err != nil {
return false, fmt.Errorf("error getting malfeasance sync state: %w", err)
}
if timestamp.Before(epochStart) {
return true, nil
}
cutoff := epochEnd.Sub(epochStart).Seconds() * s.cfg.MaxEpochFraction
return s.clock.Now().Sub(timestamp).Seconds() > cutoff, nil
}
func (s *Syncer) download(parent context.Context, initial bool) error {
s.logger.Info("starting malfeasance proof sync", log.ZContext(parent))
defer s.logger.Debug("malfeasance proof sync terminated", log.ZContext(parent))
ctx, cancel := context.WithCancel(parent)
eg, ctx := errgroup.WithContext(ctx)
updates := make(chan malUpdate, s.cfg.MalfeasanceIDPeers)
eg.Go(func() error {
return s.downloadNodeIDs(ctx, initial, updates)
})
eg.Go(func() error {
defer cancel()
return s.downloadMalfeasanceProofs(ctx, initial, updates)
})
if err := eg.Wait(); err != nil {
return err
}
return parent.Err()
}
func (s *Syncer) downloadNodeIDs(ctx context.Context, initial bool, updates chan<- malUpdate) error {
interval := s.cfg.IDRequestInterval
if initial {
interval = 0
}
for {
if interval != 0 {
s.logger.Debug(
"pausing between malfeasant node ID requests",
zap.Duration("duration", interval))
select {
case <-ctx.Done():
return nil
// TODO(ivan4th) this has to be randomized in a followup
// when sync will be schedulled in advance, in order to smooth out request rate across the network
case <-s.clock.After(interval):
}
}
peers := s.fetcher.SelectBestShuffled(s.cfg.MalfeasanceIDPeers)
if len(peers) == 0 {
s.logger.Debug(
"don't have enough peers for malfeasance sync",
zap.Int("nPeers", s.cfg.MalfeasanceIDPeers),
)
if interval == 0 {
interval = s.cfg.RetryInterval
}
continue
}
var eg errgroup.Group
for _, peer := range peers {
eg.Go(func() error {
malIDs, err := s.fetcher.GetMaliciousIDs(ctx, peer)
if err != nil {
if errors.Is(err, context.Canceled) {
return ctx.Err()
}
s.peerErrMetric.Inc()
s.logger.Warn("failed to download malfeasant node IDs",
log.ZContext(ctx),
zap.String("peer", peer.String()),
zap.Error(err),
)
return nil
}
s.logger.Info("downloaded malfeasant node IDs",
log.ZContext(ctx),
zap.String("peer", peer.String()),
zap.Int("ids", len(malIDs)),
)
select {
case <-ctx.Done():
return ctx.Err()
case updates <- malUpdate{peer: peer, nodeIDs: malIDs}:
}
return nil
})
}
if err := eg.Wait(); err != nil {
return err
}
if interval == 0 {
interval = s.cfg.RetryInterval
}
}
}
func (s *Syncer) updateState() error {
if err := malsync.UpdateSyncState(s.localdb, s.clock.Now()); err != nil {
return fmt.Errorf("error updating malsync state: %w", err)
}
return nil
}
func (s *Syncer) downloadMalfeasanceProofs(ctx context.Context, initial bool, updates <-chan malUpdate) error {
var (
update malUpdate
sst = newSyncState(s.cfg.RequestsLimit, initial)
nothingToDownload = true
gotUpdate = false
)
for {
if nothingToDownload {
sst.done()
if initial && sst.numSyncedPeers() >= s.cfg.MinSyncPeers {
if err := s.updateState(); err != nil {
return nil
}
s.logger.Info("initial sync of malfeasance proofs completed", log.ZContext(ctx))
return nil
} else if !initial && gotUpdate {
if err := s.updateState(); err != nil {
return nil
}
}
select {
case <-ctx.Done():
return ctx.Err()
case update = <-updates:
s.logger.Debug("malfeasance sync update",
log.ZContext(ctx), zap.Int("count", len(update.nodeIDs)))
sst.update(update)
gotUpdate = true
}
} else {
select {
case <-ctx.Done():
return ctx.Err()
case update = <-updates:
s.logger.Debug("malfeasance sync update",
log.ZContext(ctx), zap.Int("count", len(update.nodeIDs)))
sst.update(update)
gotUpdate = true
default:
// If we have some hashes to fetch already, don't wait for
// another update
}
}
batch, err := sst.missing(s.cfg.MaxBatchSize, func(nodeID types.NodeID) (bool, error) {
// TODO(ivan4th): check multiple node IDs at once in a single SQL query
isMalicious, err := identities.IsMalicious(s.db, nodeID)
if err != nil && errors.Is(err, sql.ErrNotFound) {
return false, nil
}
return isMalicious, err
})
if err != nil {
return fmt.Errorf("error checking malfeasant node IDs: %w", err)
}
nothingToDownload = len(batch) == 0
if len(batch) != 0 {
s.logger.Debug("retrieving malfeasant identities",
log.ZContext(ctx),
zap.Int("count", len(batch)))
err := s.fetcher.GetMalfeasanceProofs(ctx, batch)
if err != nil {
if errors.Is(err, context.Canceled) {
return ctx.Err()
}
s.logger.Debug("failed to download malfeasance proofs",
log.ZContext(ctx),
log.NiceZapError(err),
)
}
batchError := &fetch.BatchError{}
if errors.As(err, &batchError) {
for hash, err := range batchError.Errors {
nodeID := types.NodeID(hash)
switch {
case !sst.has(nodeID):
continue
case errors.Is(err, fetch.ErrExceedMaxRetries):
sst.failed(nodeID)
case errors.Is(err, pubsub.ErrValidationReject):
sst.rejected(nodeID)
}
}
}
} else {
s.logger.Debug("no new malfeasant identities", log.ZContext(ctx))
}
}
}
func (s *Syncer) EnsureInSync(parent context.Context, epochStart, epochEnd time.Time) error {
if shouldSync, err := s.shouldSync(epochStart, epochEnd); err != nil {
return err
} else if !shouldSync {
return nil
}
return s.download(parent, true)
}
func (s *Syncer) DownloadLoop(parent context.Context) error {
return s.download(parent, false)
}
type malUpdate struct {
peer p2p.Peer
nodeIDs []types.NodeID
}