-
Notifications
You must be signed in to change notification settings - Fork 719
Expand file tree
/
Copy pathconsensus_execution_syncer.go
More file actions
209 lines (179 loc) · 6.54 KB
/
consensus_execution_syncer.go
File metadata and controls
209 lines (179 loc) · 6.54 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2021-2026, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md
package arbnode
import (
"context"
"errors"
"time"
"github.com/spf13/pflag"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/arbutil"
"github.com/offchainlabs/nitro/execution"
"github.com/offchainlabs/nitro/staker"
"github.com/offchainlabs/nitro/util"
"github.com/offchainlabs/nitro/util/headerreader"
"github.com/offchainlabs/nitro/util/stopwaiter"
)
type ConsensusExecutionSyncerConfig struct {
SyncInterval time.Duration `koanf:"sync-interval"`
}
var DefaultConsensusExecutionSyncerConfig = ConsensusExecutionSyncerConfig{
SyncInterval: 300 * time.Millisecond,
}
var TestConsensusExecutionSyncerConfig = ConsensusExecutionSyncerConfig{
SyncInterval: TestSyncMonitorConfig.MsgLag / 2,
}
// We don't define a Test config. For most tests we want the Syncer to behave
// the same as in production.
func ConsensusExecutionSyncerConfigAddOptions(prefix string, f *pflag.FlagSet) {
f.Duration(prefix+".sync-interval", DefaultConsensusExecutionSyncerConfig.SyncInterval, "Interval in which finality and sync data is pushed from consensus to execution")
}
type MessageCountFetcher interface {
GetSafeMsgCount(ctx context.Context) (arbutil.MessageIndex, error)
GetFinalizedMsgCount(ctx context.Context) (arbutil.MessageIndex, error)
SupportsPushingFinalityData() bool
}
// lint:require-exhaustive-initialization
type ConsensusExecutionSyncer struct {
stopwaiter.StopWaiter
config func() *ConsensusExecutionSyncerConfig
msgCountFetcher MessageCountFetcher
execClient execution.ExecutionClient
blockValidator *staker.BlockValidator
txStreamer *TransactionStreamer
syncMonitor *SyncMonitor
msgCountErrorHandler *util.EphemeralErrorHandler
}
func NewConsensusExecutionSyncer(
config func() *ConsensusExecutionSyncerConfig,
msgCountFetcher MessageCountFetcher,
execClient execution.ExecutionClient,
blockValidator *staker.BlockValidator,
txStreamer *TransactionStreamer,
syncMonitor *SyncMonitor,
) *ConsensusExecutionSyncer {
return &ConsensusExecutionSyncer{
StopWaiter: stopwaiter.StopWaiter{},
config: config,
msgCountFetcher: msgCountFetcher,
execClient: execClient,
blockValidator: blockValidator,
txStreamer: txStreamer,
syncMonitor: syncMonitor,
// For the first 2 minutes, log msg count error as WARN, then as ERROR.
msgCountErrorHandler: util.NewEphemeralErrorHandler(2*time.Minute, "", 0),
}
}
func (c *ConsensusExecutionSyncer) Start(ctx_in context.Context) {
c.StopWaiter.Start(ctx_in, c)
if c.msgCountFetcher != nil && c.msgCountFetcher.SupportsPushingFinalityData() {
c.CallIteratively(c.pushFinalityDataFromConsensusToExecution)
}
c.CallIteratively(c.pushConsensusSyncDataToExecution)
}
func (c *ConsensusExecutionSyncer) getFinalityData(
msgCount arbutil.MessageIndex,
errMsgCount error,
scenario string,
) (*arbutil.FinalityData, error) {
if errors.Is(errMsgCount, headerreader.ErrBlockNumberNotSupported) {
log.Debug("Finality not supported, not pushing finality data to execution")
return nil, errMsgCount
} else if errMsgCount != nil {
c.msgCountErrorHandler.LogLevel(errMsgCount, log.Error)("Error getting finality msg count", "scenario", scenario, "err", errMsgCount)
return nil, errMsgCount
}
c.msgCountErrorHandler.Reset()
if msgCount == 0 {
return nil, nil
}
msgIdx := msgCount - 1
msgResult, err := c.txStreamer.ResultAtMessageIndex(msgIdx)
if errors.Is(err, execution.ErrResultNotFound) {
log.Debug("Message result not found, node out of sync", "msgIdx", msgIdx, "err", err)
return nil, nil
} else if err != nil {
log.Error("Error getting message result", "msgIdx", msgIdx, "err", err)
return nil, err
}
finalityData := &arbutil.FinalityData{
MsgIdx: msgIdx,
BlockHash: msgResult.BlockHash,
}
return finalityData, nil
}
func (c *ConsensusExecutionSyncer) pushFinalityDataFromConsensusToExecution(ctx context.Context) time.Duration {
safeMsgCount, err := c.msgCountFetcher.GetSafeMsgCount(ctx)
if err != nil {
return c.config().SyncInterval
}
safeFinalityData, err := c.getFinalityData(safeMsgCount, err, "safe")
if err != nil {
return c.config().SyncInterval
}
finalizedMsgCount, err := c.msgCountFetcher.GetFinalizedMsgCount(ctx)
if err != nil {
return c.config().SyncInterval
}
finalizedFinalityData, err := c.getFinalityData(finalizedMsgCount, err, "finalized")
if err != nil {
return c.config().SyncInterval
}
var validatedFinalityData *arbutil.FinalityData
var validatedMsgCount arbutil.MessageIndex
if c.blockValidator != nil {
validatedMsgCount = c.blockValidator.GetValidated()
validatedFinalityData, err = c.getFinalityData(validatedMsgCount, nil, "validated")
if err != nil {
return c.config().SyncInterval
}
}
_, err = c.execClient.SetFinalityData(safeFinalityData, finalizedFinalityData, validatedFinalityData).Await(ctx)
if err != nil {
log.Error("Error pushing finality data from consensus to execution", "err", err)
} else {
finalityMsgCount := func(fd *arbutil.FinalityData) arbutil.MessageIndex {
if fd != nil {
return fd.MsgIdx + 1
}
return 0
}
log.Debug("Pushed finality data from consensus to execution",
"safeMsgCount", finalityMsgCount(safeFinalityData),
"finalizedMsgCount", finalityMsgCount(finalizedFinalityData),
"validatedMsgCount", finalityMsgCount(validatedFinalityData),
)
}
return c.config().SyncInterval
}
func (c *ConsensusExecutionSyncer) pushConsensusSyncDataToExecution(ctx context.Context) time.Duration {
synced := c.syncMonitor.Synced()
maxMessageCount, err := c.syncMonitor.GetMaxMessageCount()
if err != nil {
log.Error("Error getting max message count", "err", err)
return c.config().SyncInterval
}
var syncProgressMap map[string]interface{}
if !synced {
// Only populate the full progress map when not synced (for debugging)
syncProgressMap = c.syncMonitor.FullSyncProgressMap()
}
syncData := &execution.ConsensusSyncData{
Synced: synced,
MaxMessageCount: maxMessageCount,
SyncProgressMap: syncProgressMap,
UpdatedAt: time.Now(),
}
_, err = c.execClient.SetConsensusSyncData(syncData).Await(ctx)
if err != nil {
log.Error("Error pushing sync data from consensus to execution", "err", err)
} else {
log.Debug("Pushed sync data from consensus to execution",
"synced", syncData.Synced,
"maxMessageCount", syncData.MaxMessageCount,
"updatedAt", syncData.UpdatedAt,
"hasProgressMap", syncData.SyncProgressMap != nil,
)
}
return c.config().SyncInterval
}