forked from Layr-Labs/nitro-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_monitor.go
207 lines (182 loc) · 6.43 KB
/
sync_monitor.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
package arbnode
import (
"context"
"errors"
"sync/atomic"
"github.com/offchainlabs/nitro/arbutil"
"github.com/offchainlabs/nitro/execution"
flag "github.com/spf13/pflag"
)
type SyncMonitor struct {
config *SyncMonitorConfig
inboxReader *InboxReader
txStreamer *TransactionStreamer
coordinator *SeqCoordinator
exec execution.FullExecutionClient
initialized bool
}
func NewSyncMonitor(config *SyncMonitorConfig) *SyncMonitor {
return &SyncMonitor{
config: config,
}
}
type SyncMonitorConfig struct {
BlockBuildLag uint64 `koanf:"block-build-lag"`
BlockBuildSequencerInboxLag uint64 `koanf:"block-build-sequencer-inbox-lag"`
CoordinatorMsgLag uint64 `koanf:"coordinator-msg-lag"`
SafeBlockWaitForBlockValidator bool `koanf:"safe-block-wait-for-block-validator"`
FinalizedBlockWaitForBlockValidator bool `koanf:"finalized-block-wait-for-block-validator"`
}
var DefaultSyncMonitorConfig = SyncMonitorConfig{
BlockBuildLag: 20,
BlockBuildSequencerInboxLag: 0,
CoordinatorMsgLag: 15,
SafeBlockWaitForBlockValidator: false,
FinalizedBlockWaitForBlockValidator: false,
}
func SyncMonitorConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Uint64(prefix+".block-build-lag", DefaultSyncMonitorConfig.BlockBuildLag, "allowed lag between messages read and blocks built")
f.Uint64(prefix+".block-build-sequencer-inbox-lag", DefaultSyncMonitorConfig.BlockBuildSequencerInboxLag, "allowed lag between messages read from sequencer inbox and blocks built")
f.Uint64(prefix+".coordinator-msg-lag", DefaultSyncMonitorConfig.CoordinatorMsgLag, "allowed lag between local and remote messages")
f.Bool(prefix+".safe-block-wait-for-block-validator", DefaultSyncMonitorConfig.SafeBlockWaitForBlockValidator, "wait for block validator to complete before returning safe block number")
f.Bool(prefix+".finalized-block-wait-for-block-validator", DefaultSyncMonitorConfig.FinalizedBlockWaitForBlockValidator, "wait for block validator to complete before returning finalized block number")
}
func (s *SyncMonitor) Initialize(inboxReader *InboxReader, txStreamer *TransactionStreamer, coordinator *SeqCoordinator, exec execution.FullExecutionClient) {
s.inboxReader = inboxReader
s.txStreamer = txStreamer
s.coordinator = coordinator
s.exec = exec
s.initialized = true
}
func (s *SyncMonitor) SyncProgressMap() map[string]interface{} {
syncing := false
res := make(map[string]interface{})
if !s.initialized {
res["err"] = "uninitialized"
return res
}
broadcasterQueuedMessagesPos := atomic.LoadUint64(&(s.txStreamer.broadcasterQueuedMessagesPos))
if broadcasterQueuedMessagesPos != 0 { // unprocessed feed
syncing = true
}
res["broadcasterQueuedMessagesPos"] = broadcasterQueuedMessagesPos
builtMessageCount, err := s.exec.HeadMessageNumber()
if err != nil {
res["builtMessageCountError"] = err.Error()
syncing = true
builtMessageCount = 0
} else {
blockNum := s.exec.MessageIndexToBlockNumber(builtMessageCount)
res["blockNum"] = blockNum
builtMessageCount++
res["messageOfLastBlock"] = builtMessageCount
}
msgCount, err := s.txStreamer.GetMessageCount()
if err != nil {
res["msgCountError"] = err.Error()
syncing = true
} else {
res["msgCount"] = msgCount
if builtMessageCount+arbutil.MessageIndex(s.config.BlockBuildLag) < msgCount {
syncing = true
}
}
if s.inboxReader != nil {
batchSeen := s.inboxReader.GetLastSeenBatchCount()
_, batchProcessed := s.inboxReader.GetLastReadBlockAndBatchCount()
if (batchSeen == 0) || // error or not yet read inbox
(batchProcessed < batchSeen) { // unprocessed inbox messages
syncing = true
}
res["batchSeen"] = batchSeen
res["batchProcessed"] = batchProcessed
processedMetadata, err := s.inboxReader.Tracker().GetBatchMetadata(batchProcessed - 1)
if err != nil {
res["batchMetadataError"] = err.Error()
syncing = true
} else {
res["messageOfProcessedBatch"] = processedMetadata.MessageCount
if builtMessageCount+arbutil.MessageIndex(s.config.BlockBuildSequencerInboxLag) < processedMetadata.MessageCount {
syncing = true
}
}
l1reader := s.inboxReader.l1Reader
if l1reader != nil {
header, err := l1reader.LastHeaderWithError()
if err != nil {
res["lastL1HeaderErr"] = err
}
if header != nil {
res["lastL1BlockNum"] = header.Number
res["lastl1BlockHash"] = header.Hash()
}
}
}
if s.coordinator != nil {
coordinatorMessageCount, err := s.coordinator.GetRemoteMsgCount() //NOTE: this creates a remote call
if err != nil {
res["coordinatorMsgCountError"] = err.Error()
syncing = true
} else {
res["coordinatorMessageCount"] = coordinatorMessageCount
if msgCount+arbutil.MessageIndex(s.config.CoordinatorMsgLag) < coordinatorMessageCount {
syncing = true
}
}
}
if !syncing {
return make(map[string]interface{})
}
return res
}
func (s *SyncMonitor) SafeBlockNumber(ctx context.Context) (uint64, error) {
if s.inboxReader == nil || !s.initialized {
return 0, errors.New("not set up for safeblock")
}
msg, err := s.inboxReader.GetSafeMsgCount(ctx)
if err != nil {
return 0, err
}
// If SafeBlockWaitForBlockValidator is true, we want to wait for the block validator to finish
if s.config.SafeBlockWaitForBlockValidator {
latestValidatedCount, err := s.getLatestValidatedCount()
if err != nil {
return 0, err
}
if msg > latestValidatedCount {
msg = latestValidatedCount
}
}
block := s.exec.MessageIndexToBlockNumber(msg - 1)
return block, nil
}
func (s *SyncMonitor) getLatestValidatedCount() (arbutil.MessageIndex, error) {
if s.txStreamer.validator == nil {
return 0, errors.New("validator not set up")
}
return s.txStreamer.validator.GetValidated(), nil
}
func (s *SyncMonitor) FinalizedBlockNumber(ctx context.Context) (uint64, error) {
if s.inboxReader == nil || !s.initialized {
return 0, errors.New("not set up for safeblock")
}
msg, err := s.inboxReader.GetFinalizedMsgCount(ctx)
if err != nil {
return 0, err
}
// If FinalizedBlockWaitForBlockValidator is true, we want to wait for the block validator to finish
if s.config.FinalizedBlockWaitForBlockValidator {
latestValidatedCount, err := s.getLatestValidatedCount()
if err != nil {
return 0, err
}
if msg > latestValidatedCount {
msg = latestValidatedCount
}
}
block := s.exec.MessageIndexToBlockNumber(msg - 1)
return block, nil
}
func (s *SyncMonitor) Synced() bool {
return len(s.SyncProgressMap()) == 0
}