-
Notifications
You must be signed in to change notification settings - Fork 12
/
syncmanager.go
423 lines (327 loc) · 11.9 KB
/
syncmanager.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
package trivialsyncmanager
import (
"context"
"time"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/module"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/hive.go/runtime/timeutil"
"github.com/iotaledger/hive.go/runtime/workerpool"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/protocol/engine"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
"github.com/iotaledger/iota-core/pkg/protocol/engine/syncmanager"
iotago "github.com/iotaledger/iota.go/v4"
)
type (
isBootstrappedFunc func(e *engine.Engine) bool
)
func WithSyncThreshold(threshold time.Duration) options.Option[SyncManager] {
return func(s *SyncManager) {
s.optsSyncThreshold = threshold
}
}
func WithIsBootstrappedFunc(isBootstrapped isBootstrappedFunc) options.Option[SyncManager] {
return func(s *SyncManager) {
s.optsIsBootstrappedFunc = isBootstrapped
}
}
func WithBootstrappedThreshold(threshold time.Duration) options.Option[SyncManager] {
return func(s *SyncManager) {
s.optsBootstrappedThreshold = threshold
}
}
type SyncManager struct {
events *syncmanager.Events
engine *engine.Engine
// options
optsSyncThreshold time.Duration
optsIsBootstrappedFunc isBootstrappedFunc
optsBootstrappedThreshold time.Duration
// state
isBootstrappedLock syncutils.RWMutex
isBootstrapped bool
isSyncedLock syncutils.RWMutex
isSynced bool
isSyncedTicker *timeutil.Ticker
isFinalizationDelayedLock syncutils.RWMutex
isFinalizationDelayed bool
lastAcceptedBlockSlotLock syncutils.RWMutex
lastAcceptedBlockSlot iotago.SlotIndex
lastConfirmedBlockSlotLock syncutils.RWMutex
lastConfirmedBlockSlot iotago.SlotIndex
latestCommitmentLock syncutils.RWMutex
latestCommitment *model.Commitment
latestFinalizedSlotLock syncutils.RWMutex
latestFinalizedSlot iotago.SlotIndex
lastPrunedEpochLock syncutils.RWMutex
lastPrunedEpoch iotago.EpochIndex
hasPruned bool
module.Module
}
// NewProvider creates a new SyncManager provider.
func NewProvider(opts ...options.Option[SyncManager]) module.Provider[*engine.Engine, syncmanager.SyncManager] {
return module.Provide(func(e *engine.Engine) syncmanager.SyncManager {
s := New(e.NewSubModule("SyncManager"), e, e.Storage.Settings().LatestCommitment(), e.Storage.Settings().LatestFinalizedSlot(), opts...)
asyncOpt := event.WithWorkerPool(e.Workers.CreatePool("SyncManager", workerpool.WithWorkerCount(1)))
e.ConstructedEvent().OnTrigger(func() {
e.Events.BlockGadget.BlockAccepted.Hook(func(b *blocks.Block) {
if s.updateLastAcceptedBlock(b.ID()) {
s.triggerUpdate()
}
}, asyncOpt)
e.Events.BlockGadget.BlockConfirmed.Hook(func(b *blocks.Block) {
if s.updateLastConfirmedBlock(b.ID()) {
s.triggerUpdate()
}
}, asyncOpt)
e.Events.Notarization.LatestCommitmentUpdated.Hook(func(commitment *model.Commitment) {
var bootstrapChanged bool
if !s.IsBootstrapped() {
bootstrapChanged = s.updateBootstrappedStatus()
}
syncChanged := s.updateSyncStatus()
commitmentChanged := s.updateLatestCommitment(commitment)
if bootstrapChanged || syncChanged || commitmentChanged {
s.triggerUpdate()
}
}, asyncOpt)
e.Events.SlotGadget.SlotFinalized.Hook(func(slot iotago.SlotIndex) {
if s.updateFinalizedSlot(slot) {
s.triggerUpdate()
}
}, asyncOpt)
e.Storage.Pruned.Hook(func(epoch iotago.EpochIndex) {
if s.updatePrunedEpoch(epoch, true) {
s.triggerUpdate()
}
}, asyncOpt)
e.Events.SyncManager.LinkTo(s.events)
s.InitializedEvent().Trigger()
})
return s
})
}
func New(subModule module.Module, e *engine.Engine, latestCommitment *model.Commitment, finalizedSlot iotago.SlotIndex, opts ...options.Option[SyncManager]) *SyncManager {
ctxUpdateSyncStatusTicker, ctxCancelUpdateSyncStatusTicker := context.WithCancel(context.Background())
return options.Apply(&SyncManager{
Module: subModule,
events: syncmanager.NewEvents(),
engine: e,
optsSyncThreshold: 10 * time.Second,
optsIsBootstrappedFunc: nil,
optsBootstrappedThreshold: 10 * time.Second,
isBootstrapped: false,
isSynced: false,
isSyncedTicker: nil,
isFinalizationDelayed: true,
lastAcceptedBlockSlot: latestCommitment.Slot(),
lastConfirmedBlockSlot: latestCommitment.Slot(),
latestCommitment: latestCommitment,
latestFinalizedSlot: finalizedSlot,
lastPrunedEpoch: 0,
hasPruned: false,
}, opts, func(s *SyncManager) {
// start the sync status update ticker, tick every half slot duration
s.isSyncedTicker = timeutil.NewTicker(func() {
s.updateSyncStatus()
}, time.Duration(e.CommittedAPI().ProtocolParameters().SlotDurationInSeconds())*time.Second/2, ctxUpdateSyncStatusTicker)
e.InitializedEvent().OnTrigger(func() {
s.updatePrunedEpoch(s.engine.Storage.LastPrunedEpoch())
})
// set the default bootstrapped function
if s.optsIsBootstrappedFunc == nil {
s.optsIsBootstrappedFunc = func(e *engine.Engine) bool {
return time.Since(e.Clock.Accepted().RelativeTime()) < s.optsBootstrappedThreshold && e.Notarization.IsBootstrapped()
}
}
s.ShutdownEvent().OnTrigger(func() {
// stop the ticker when the engine is shutting down
ctxCancelUpdateSyncStatusTicker()
// wait for the ticker to gracefully shut down
s.isSyncedTicker.WaitForGracefulShutdown()
s.StoppedEvent().Trigger()
})
s.ConstructedEvent().Trigger()
})
}
func (s *SyncManager) SyncStatus() *syncmanager.SyncStatus {
// get all the locks so we have an atomic view of the state
s.isBootstrappedLock.RLock()
s.isSyncedLock.RLock()
s.isFinalizationDelayedLock.RLock()
s.lastAcceptedBlockSlotLock.RLock()
s.lastConfirmedBlockSlotLock.RLock()
s.latestCommitmentLock.RLock()
s.latestFinalizedSlotLock.RLock()
s.lastPrunedEpochLock.RLock()
defer func() {
s.isBootstrappedLock.RUnlock()
s.isSyncedLock.RUnlock()
s.isFinalizationDelayedLock.RUnlock()
s.lastAcceptedBlockSlotLock.RUnlock()
s.lastConfirmedBlockSlotLock.RUnlock()
s.latestCommitmentLock.RUnlock()
s.latestFinalizedSlotLock.RUnlock()
s.lastPrunedEpochLock.RUnlock()
}()
return &syncmanager.SyncStatus{
NodeBootstrapped: s.isBootstrapped,
NodeSynced: s.isSynced,
FinalizationDelayed: s.isFinalizationDelayed,
LastAcceptedBlockSlot: s.lastAcceptedBlockSlot,
LastConfirmedBlockSlot: s.lastConfirmedBlockSlot,
LatestCommitment: s.latestCommitment,
LatestFinalizedSlot: s.latestFinalizedSlot,
LastPrunedEpoch: s.lastPrunedEpoch,
HasPruned: s.hasPruned,
}
}
// Reset resets the component to a clean state as if it was created at the last commitment.
func (s *SyncManager) Reset() {
s.isSyncedLock.Lock()
s.isFinalizationDelayedLock.Lock()
s.lastAcceptedBlockSlotLock.Lock()
s.lastConfirmedBlockSlotLock.Lock()
s.latestCommitmentLock.RLock()
defer func() {
s.isSyncedLock.Unlock()
s.isFinalizationDelayedLock.Unlock()
s.lastAcceptedBlockSlotLock.Unlock()
s.lastConfirmedBlockSlotLock.Unlock()
s.latestCommitmentLock.RUnlock()
}()
// Mark the synced flag as false,
// because we clear the latest accepted blocks and return the whole state to the last committed slot.
s.isSynced = false
s.isFinalizationDelayed = true
s.lastAcceptedBlockSlot = s.latestCommitment.Slot()
s.lastConfirmedBlockSlot = s.latestCommitment.Slot()
}
func (s *SyncManager) triggerUpdate() {
s.events.UpdatedStatus.Trigger(s.SyncStatus())
}
func (s *SyncManager) updateBootstrappedStatus() (changed bool) {
s.isBootstrappedLock.Lock()
defer s.isBootstrappedLock.Unlock()
if !s.isBootstrapped && s.optsIsBootstrappedFunc(s.engine) {
s.isBootstrapped = true
return true
}
return false
}
func (s *SyncManager) IsBootstrapped() bool {
s.isBootstrappedLock.RLock()
defer s.isBootstrappedLock.RUnlock()
return s.isBootstrapped
}
func (s *SyncManager) updateSyncStatus() (changed bool) {
s.isSyncedLock.Lock()
defer s.isSyncedLock.Unlock()
snapshot := s.engine.Clock.Snapshot()
isSynced := s.IsBootstrapped() && ((snapshot.RelativeAcceptedTime.Sub(snapshot.AcceptedTime)) < s.optsSyncThreshold)
if s.isSynced != isSynced {
s.isSynced = isSynced
return true
}
return false
}
func (s *SyncManager) IsNodeSynced() bool {
s.isSyncedLock.RLock()
defer s.isSyncedLock.RUnlock()
return s.isSynced
}
func (s *SyncManager) updateIsFinalizationDelayed(latestFinalizedSlot iotago.SlotIndex, latestCommitmentSlot iotago.SlotIndex) {
s.isFinalizationDelayedLock.Lock()
defer s.isFinalizationDelayedLock.Unlock()
if latestCommitmentSlot < latestFinalizedSlot {
// This should never happen, but if it does, we don't want to panic.
return
}
s.isFinalizationDelayed = latestCommitmentSlot-latestFinalizedSlot > s.engine.CommittedAPI().ProtocolParameters().MaxCommittableAge()
}
func (s *SyncManager) IsFinalizationDelayed() bool {
s.isFinalizationDelayedLock.RLock()
defer s.isFinalizationDelayedLock.RUnlock()
return s.isFinalizationDelayed
}
func (s *SyncManager) updateLastAcceptedBlock(lastAcceptedBlockID iotago.BlockID) (changed bool) {
s.lastAcceptedBlockSlotLock.Lock()
defer s.lastAcceptedBlockSlotLock.Unlock()
if lastAcceptedBlockID.Slot() > s.lastAcceptedBlockSlot {
s.lastAcceptedBlockSlot = lastAcceptedBlockID.Slot()
return true
}
return false
}
func (s *SyncManager) LastAcceptedBlockSlot() iotago.SlotIndex {
s.lastAcceptedBlockSlotLock.RLock()
defer s.lastAcceptedBlockSlotLock.RUnlock()
return s.lastAcceptedBlockSlot
}
func (s *SyncManager) updateLastConfirmedBlock(lastConfirmedBlockID iotago.BlockID) (changed bool) {
s.lastConfirmedBlockSlotLock.Lock()
defer s.lastConfirmedBlockSlotLock.Unlock()
if lastConfirmedBlockID.Slot() > s.lastConfirmedBlockSlot {
s.lastConfirmedBlockSlot = lastConfirmedBlockID.Slot()
return true
}
return false
}
func (s *SyncManager) LastConfirmedBlockSlot() iotago.SlotIndex {
s.lastConfirmedBlockSlotLock.RLock()
defer s.lastConfirmedBlockSlotLock.RUnlock()
return s.lastConfirmedBlockSlot
}
func (s *SyncManager) updateLatestCommitment(commitment *model.Commitment) (changed bool) {
s.latestCommitmentLock.Lock()
if s.latestCommitment != commitment {
s.latestCommitment = commitment
// we need to unlock the lock before calling updateIsFinalizationDelayed,
// otherwise it might deadlock if isFinalizationDelayedLock is Rlocked in SyncStatus().
s.latestCommitmentLock.Unlock()
s.updateIsFinalizationDelayed(s.LatestFinalizedSlot(), commitment.Slot())
return true
}
s.latestCommitmentLock.Unlock()
return false
}
func (s *SyncManager) LatestCommitment() *model.Commitment {
s.latestCommitmentLock.RLock()
defer s.latestCommitmentLock.RUnlock()
return s.latestCommitment
}
func (s *SyncManager) updateFinalizedSlot(slot iotago.SlotIndex) (changed bool) {
s.latestFinalizedSlotLock.Lock()
if s.latestFinalizedSlot != slot {
s.latestFinalizedSlot = slot
// we need to unlock the lock before calling updateIsFinalizationDelayed,
// otherwise it might deadlock if isFinalizationDelayedLock is Rlocked in SyncStatus().
s.latestFinalizedSlotLock.Unlock()
s.updateIsFinalizationDelayed(slot, s.LatestCommitment().Slot())
return true
}
s.latestFinalizedSlotLock.Unlock()
return false
}
func (s *SyncManager) LatestFinalizedSlot() iotago.SlotIndex {
s.latestFinalizedSlotLock.RLock()
defer s.latestFinalizedSlotLock.RUnlock()
return s.latestFinalizedSlot
}
func (s *SyncManager) updatePrunedEpoch(epoch iotago.EpochIndex, hasPruned bool) (changed bool) {
s.lastPrunedEpochLock.Lock()
defer s.lastPrunedEpochLock.Unlock()
if s.lastPrunedEpoch != epoch || s.hasPruned != hasPruned {
s.lastPrunedEpoch = epoch
s.hasPruned = hasPruned
return true
}
return false
}
func (s *SyncManager) LastPrunedEpoch() (iotago.EpochIndex, bool) {
s.lastPrunedEpochLock.RLock()
defer s.lastPrunedEpochLock.RUnlock()
return s.lastPrunedEpoch, s.hasPruned
}