Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit 81b6562

Browse files
Add new event tracker flags to server command
1 parent d75ea4d commit 81b6562

File tree

12 files changed

+176
-164
lines changed

12 files changed

+176
-164
lines changed

command/server/config/config.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ type Config struct {
3232
JSONLogFormat bool `json:"json_log_format" yaml:"json_log_format"`
3333
CorsAllowedOrigins []string `json:"cors_allowed_origins" yaml:"cors_allowed_origins"`
3434

35-
Relayer bool `json:"relayer" yaml:"relayer"`
36-
NumBlockConfirmations uint64 `json:"num_block_confirmations" yaml:"num_block_confirmations"`
35+
Relayer bool `json:"relayer" yaml:"relayer"`
3736

3837
ConcurrentRequestsDebug uint64 `json:"concurrent_requests_debug" yaml:"concurrent_requests_debug"`
3938
WebSocketReadLimit uint64 `json:"web_socket_read_limit" yaml:"web_socket_read_limit"`
4039

4140
MetricsInterval time.Duration `json:"metrics_interval" yaml:"metrics_interval"`
41+
42+
// event tracker
43+
NumBlockConfirmations uint64 `json:"num_block_confirmations" yaml:"num_block_confirmations"`
44+
TrackerSyncBatchSize uint64 `json:"tracker_sync_batch_size" yaml:"tracker_sync_batch_size"`
45+
TrackerBlocksToReconcile uint64 `json:"tracker_blocks_to_reconcile" yaml:"tracker_blocks_to_reconcile"`
4246
}
4347

4448
// Telemetry holds the config details for metric services.
@@ -81,10 +85,6 @@ const (
8185
// requests with fromBlock/toBlock values (e.g. eth_getLogs)
8286
DefaultJSONRPCBlockRangeLimit uint64 = 1000
8387

84-
// DefaultNumBlockConfirmations minimal number of child blocks required for the parent block to be considered final
85-
// on ethereum epoch lasts for 32 blocks. more details: https://www.alchemy.com/overviews/ethereum-commitment-levels
86-
DefaultNumBlockConfirmations uint64 = 64
87-
8888
// DefaultConcurrentRequestsDebug specifies max number of allowed concurrent requests for debug endpoints
8989
DefaultConcurrentRequestsDebug uint64 = 32
9090

@@ -96,6 +96,25 @@ const (
9696
// DefaultMetricsInterval specifies the time interval after which Prometheus metrics will be generated.
9797
// A value of 0 means the metrics are disabled.
9898
DefaultMetricsInterval time.Duration = time.Second * 8
99+
100+
// event tracker
101+
102+
// DefaultNumBlockConfirmations minimal number of child blocks required for the parent block to be considered final
103+
// on ethereum epoch lasts for 32 blocks. more details: https://www.alchemy.com/overviews/ethereum-commitment-levels
104+
DefaultNumBlockConfirmations uint64 = 64
105+
106+
// DefaultTrackerSyncBatchSize defines a default batch size of blocks that will be gotten from tracked chain,
107+
// when tracker is out of sync and needs to sync a number of blocks.
108+
DefaultTrackerSyncBatchSize uint64 = 10
109+
110+
// DefaultTrackerBlocksToReconcile defines how default number blocks that tracker
111+
// will sync up from the latest block on tracked chain.
112+
// If a node that has tracker, was offline for days, months, a year, it will miss a lot of blocks.
113+
// In the meantime, we expect the rest of nodes to have collected the desired events and did their
114+
// logic with them, continuing consensus and relayer stuff.
115+
// In order to not waste too much unnecessary time in syncing all those blocks, with NumOfBlocksToReconcile,
116+
// we tell the tracker to sync only latestBlock.Number - NumOfBlocksToReconcile number of blocks.
117+
DefaultTrackerBlocksToReconcile uint64 = 10000
99118
)
100119

101120
// DefaultConfig returns the default server configuration
@@ -131,10 +150,13 @@ func DefaultConfig() *Config {
131150
JSONRPCBatchRequestLimit: DefaultJSONRPCBatchRequestLimit,
132151
JSONRPCBlockRangeLimit: DefaultJSONRPCBlockRangeLimit,
133152
Relayer: false,
134-
NumBlockConfirmations: DefaultNumBlockConfirmations,
135153
ConcurrentRequestsDebug: DefaultConcurrentRequestsDebug,
136154
WebSocketReadLimit: DefaultWebSocketReadLimit,
137155
MetricsInterval: DefaultMetricsInterval,
156+
// event tracker
157+
NumBlockConfirmations: DefaultNumBlockConfirmations,
158+
TrackerSyncBatchSize: DefaultTrackerSyncBatchSize,
159+
TrackerBlocksToReconcile: DefaultTrackerBlocksToReconcile,
138160
}
139161
}
140162

command/server/params.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ const (
3737
corsOriginFlag = "access-control-allow-origins"
3838
logFileLocationFlag = "log-to"
3939

40-
relayerFlag = "relayer"
41-
numBlockConfirmationsFlag = "num-block-confirmations"
40+
relayerFlag = "relayer"
4241

4342
concurrentRequestsDebugFlag = "concurrent-requests-debug"
4443
webSocketReadLimitFlag = "websocket-read-limit"
4544

4645
metricsIntervalFlag = "metrics-interval"
46+
47+
// event tracker
48+
numBlockConfirmationsFlag = "num-block-confirmations"
49+
trackerSyncBatchSizeFlag = "tracker-sync-batch-size"
50+
trackerBlocksToReconcileFlag = "tracker-blocks-to-reconcile"
4751
)
4852

4953
// Flags that are deprecated, but need to be preserved for
@@ -185,8 +189,11 @@ func (p *serverParams) generateConfig() *server.Config {
185189
JSONLogFormat: p.rawConfig.JSONLogFormat,
186190
LogFilePath: p.logFileLocation,
187191

188-
Relayer: p.relayer,
189-
NumBlockConfirmations: p.rawConfig.NumBlockConfirmations,
190-
MetricsInterval: p.rawConfig.MetricsInterval,
192+
Relayer: p.relayer,
193+
MetricsInterval: p.rawConfig.MetricsInterval,
194+
// event tracker
195+
NumBlockConfirmations: p.rawConfig.NumBlockConfirmations,
196+
TrackerSyncBatchSize: p.rawConfig.TrackerSyncBatchSize,
197+
TrackerBlocksToReconcile: p.rawConfig.TrackerBlocksToReconcile,
191198
}
192199
}

command/server/server.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,6 @@ func setFlags(cmd *cobra.Command) {
214214
"start the state sync relayer service (PolyBFT only)",
215215
)
216216

217-
cmd.Flags().Uint64Var(
218-
&params.rawConfig.NumBlockConfirmations,
219-
numBlockConfirmationsFlag,
220-
defaultConfig.NumBlockConfirmations,
221-
"minimal number of child blocks required for the parent block to be considered final",
222-
)
223-
224217
cmd.Flags().Uint64Var(
225218
&params.rawConfig.ConcurrentRequestsDebug,
226219
concurrentRequestsDebugFlag,
@@ -242,6 +235,38 @@ func setFlags(cmd *cobra.Command) {
242235
"the interval (in seconds) at which special metrics are generated. a value of zero means the metrics are disabled",
243236
)
244237

238+
// event tracker config
239+
cmd.Flags().Uint64Var(
240+
&params.rawConfig.NumBlockConfirmations,
241+
numBlockConfirmationsFlag,
242+
defaultConfig.NumBlockConfirmations,
243+
"minimal number of child blocks required for the parent block to be considered final",
244+
)
245+
246+
cmd.Flags().Uint64Var(
247+
&params.rawConfig.TrackerSyncBatchSize,
248+
trackerSyncBatchSizeFlag,
249+
defaultConfig.TrackerSyncBatchSize,
250+
`defines a batch size of blocks that will be gotten from tracked chain,
251+
when tracker is out of sync and needs to sync a number of blocks.
252+
(e.g., SyncBatchSize = 10, trackers last processed block is 10, latest block on tracked chain is 100,
253+
it will get blocks 11-20, get logs from confirmed blocks of given batch, remove processed confirm logs
254+
from memory, and continue to the next batch)`,
255+
)
256+
257+
cmd.Flags().Uint64Var(
258+
&params.rawConfig.TrackerBlocksToReconcile,
259+
trackerBlocksToReconcileFlag,
260+
defaultConfig.TrackerBlocksToReconcile,
261+
`defines how many blocks we will sync up from the latest block on tracked chain.
262+
If a node that has tracker, was offline for days, months, a year, it will miss a lot of blocks.
263+
In the meantime, we expect the rest of nodes to have collected the desired events and did their
264+
logic with them, continuing consensus and relayer stuff.
265+
In order to not waste too much unnecessary time in syncing all those blocks, with NumOfBlocksToReconcile,
266+
we tell the tracker to sync only latestBlock.Number - NumOfBlocksToReconcile number of blocks.
267+
If 0 is set to this flag, event tracker will sync all the blocks from tracked chain`,
268+
)
269+
245270
setLegacyFlags(cmd)
246271

247272
setDevFlags(cmd)

consensus/consensus.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ type Params struct {
8484
SecretsManager secrets.SecretsManager
8585
BlockTime uint64
8686

87-
NumBlockConfirmations uint64
88-
MetricsInterval time.Duration
87+
MetricsInterval time.Duration
88+
// event tracker
89+
NumBlockConfirmations uint64
90+
TrackerSyncBatchSize uint64
91+
TrackerBlocksToReconcile uint64
8992
}
9093

9194
// Factory is the factory function to create a discovery consensus

consensus/polybft/consensus_runtime.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,19 @@ type guardedDataDTO struct {
7474

7575
// runtimeConfig is a struct that holds configuration data for given consensus runtime
7676
type runtimeConfig struct {
77-
PolyBFTConfig *PolyBFTConfig
78-
DataDir string
79-
Key *wallet.Key
80-
State *State
81-
blockchain blockchainBackend
82-
polybftBackend polybftBackend
83-
txPool txPoolInterface
84-
bridgeTopic topic
85-
numBlockConfirmations uint64
86-
consensusConfig *consensus.Config
77+
PolyBFTConfig *PolyBFTConfig
78+
DataDir string
79+
Key *wallet.Key
80+
State *State
81+
blockchain blockchainBackend
82+
polybftBackend polybftBackend
83+
txPool txPoolInterface
84+
bridgeTopic topic
85+
consensusConfig *consensus.Config
86+
// event tracker
87+
numBlockConfirmations uint64
88+
trackerSyncBatchSize uint64
89+
trackerBlocksToReconcile uint64
8790
}
8891

8992
// consensusRuntime is a struct that provides consensus runtime features like epoch, state and event management
@@ -197,15 +200,18 @@ func (c *consensusRuntime) initStateSyncManager(logger hcf.Logger) error {
197200
logger.Named("state-sync-manager"),
198201
c.config.State,
199202
&stateSyncConfig{
200-
key: c.config.Key,
201-
stateSenderAddr: stateSenderAddr,
202-
stateSenderStartBlock: c.config.PolyBFTConfig.Bridge.EventTrackerStartBlocks[stateSenderAddr],
203-
jsonrpcAddr: c.config.PolyBFTConfig.Bridge.JSONRPCEndpoint,
204-
dataDir: c.config.DataDir,
205-
topic: c.config.bridgeTopic,
206-
maxCommitmentSize: maxCommitmentSize,
203+
key: c.config.Key,
204+
stateSenderAddr: stateSenderAddr,
205+
stateSenderStartBlock: c.config.PolyBFTConfig.Bridge.EventTrackerStartBlocks[stateSenderAddr],
206+
jsonrpcAddr: c.config.PolyBFTConfig.Bridge.JSONRPCEndpoint,
207+
dataDir: c.config.DataDir,
208+
topic: c.config.bridgeTopic,
209+
maxCommitmentSize: maxCommitmentSize,
210+
// event tracker
207211
numBlockConfirmations: c.config.numBlockConfirmations,
208212
blockTrackerPollInterval: c.config.PolyBFTConfig.BlockTrackerPollInterval.Duration,
213+
trackerSyncBatchSize: c.config.trackerSyncBatchSize,
214+
trackerBlocksToReconcile: c.config.trackerBlocksToReconcile,
209215
},
210216
c,
211217
)

consensus/polybft/eventtracker/event_tracker.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type EventTracker struct {
8787
//
8888
// Example Usage:
8989
//
90-
// config := &EventTracker{
90+
// config := &EventTrackerConfig{
9191
// RpcEndpoint: "http://some-json-rpc-url.com",
9292
// StartBlockFromConfig: 100_000,
9393
// NumBlockConfirmations: 10,
@@ -110,32 +110,36 @@ type EventTracker struct {
110110
//
111111
// Outputs:
112112
// - A new instance of the EventTracker struct.
113-
func NewEventTracker(config *EventTrackerConfig) (*EventTracker, error) {
113+
func NewEventTracker(config *EventTrackerConfig, startBlockFromGenesis uint64) (*EventTracker, error) {
114114
lastProcessedBlock, err := config.Store.GetLastProcessedBlock()
115115
if err != nil {
116116
return nil, err
117117
}
118118

119-
definiteLastProcessedBlock := lastProcessedBlock
119+
if lastProcessedBlock == 0 {
120+
lastProcessedBlock = startBlockFromGenesis
120121

121-
if lastProcessedBlock == 0 && config.NumOfBlocksToReconcile > 0 {
122-
latestBlock, err := config.BlockProvider.GetBlockByNumber(ethgo.Latest, false)
123-
if err != nil {
124-
return nil, err
125-
}
122+
if config.NumOfBlocksToReconcile > 0 {
123+
latestBlock, err := config.BlockProvider.GetBlockByNumber(ethgo.Latest, false)
124+
if err != nil {
125+
return nil, err
126+
}
126127

127-
if latestBlock.Number > config.NumOfBlocksToReconcile {
128-
// if this is a fresh start, then we should start syncing from
129-
// latestBlock.Number - NumOfBlocksToReconcile
130-
definiteLastProcessedBlock = latestBlock.Number - config.NumOfBlocksToReconcile
128+
if latestBlock.Number > config.NumOfBlocksToReconcile &&
129+
startBlockFromGenesis < latestBlock.Number-config.NumOfBlocksToReconcile {
130+
// if this is a fresh start, and we missed too much blocks,
131+
// then we should start syncing from
132+
// latestBlock.Number - NumOfBlocksToReconcile
133+
lastProcessedBlock = latestBlock.Number - config.NumOfBlocksToReconcile
134+
}
131135
}
132136
}
133137

134138
return &EventTracker{
135139
config: config,
136140
closeCh: make(chan struct{}),
137141
blockTracker: blocktracker.NewJSONBlockTracker(config.BlockProvider),
138-
blockContainer: NewTrackerBlockContainer(definiteLastProcessedBlock),
142+
blockContainer: NewTrackerBlockContainer(lastProcessedBlock),
139143
}, nil
140144
}
141145

@@ -175,6 +179,7 @@ func (e *EventTracker) Start() error {
175179
"syncBatchSize", e.config.SyncBatchSize,
176180
"numOfBlocksToReconcile", e.config.NumOfBlocksToReconcile,
177181
"logFilter", e.config.LogFilter,
182+
"lastBlockProcessed", e.blockContainer.LastProcessedBlock(),
178183
)
179184

180185
ctx, cancelFn := context.WithCancel(context.Background())

consensus/polybft/eventtracker/event_tracker_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestEventTracker_TrackBlock(t *testing.T) {
8989
t.Run("Add block by block - no confirmed blocks", func(t *testing.T) {
9090
t.Parallel()
9191

92-
tracker, err := NewEventTracker(createTestTrackerConfig(t, 10, 10, 0))
92+
tracker, err := NewEventTracker(createTestTrackerConfig(t, 10, 10, 0), 0)
9393

9494
require.NoError(t, err)
9595

@@ -131,7 +131,7 @@ func TestEventTracker_TrackBlock(t *testing.T) {
131131
blockProviderMock := new(mockProvider)
132132
blockProviderMock.On("GetLogs", mock.Anything).Return([]*ethgo.Log{}, nil).Once()
133133

134-
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, 10, 0))
134+
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, 10, 0), 0)
135135
require.NoError(t, err)
136136

137137
tracker.config.BlockProvider = blockProviderMock
@@ -197,7 +197,7 @@ func TestEventTracker_TrackBlock(t *testing.T) {
197197
blockProviderMock := new(mockProvider)
198198
blockProviderMock.On("GetLogs", mock.Anything).Return(logs, nil).Once()
199199

200-
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, 10, 0))
200+
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, 10, 0), 0)
201201
require.NoError(t, err)
202202

203203
tracker.config.BlockProvider = blockProviderMock
@@ -265,7 +265,7 @@ func TestEventTracker_TrackBlock(t *testing.T) {
265265
blockProviderMock := new(mockProvider)
266266
blockProviderMock.On("GetLogs", mock.Anything).Return(nil, errors.New("some error occurred")).Once()
267267

268-
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, 10, 0))
268+
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, 10, 0), 0)
269269
require.NoError(t, err)
270270

271271
tracker.config.BlockProvider = blockProviderMock
@@ -335,7 +335,7 @@ func TestEventTracker_TrackBlock(t *testing.T) {
335335
// just mock the call, it will use the provider.blocks map to handle proper returns
336336
blockProviderMock.On("GetBlockByNumber", mock.Anything, mock.Anything).Return(nil, nil).Times(int(numOfMissedBlocks))
337337

338-
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, batchSize, 0))
338+
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, batchSize, 0), 0)
339339
require.NoError(t, err)
340340

341341
tracker.config.BlockProvider = blockProviderMock
@@ -420,7 +420,7 @@ func TestEventTracker_TrackBlock(t *testing.T) {
420420
// just mock the call, it will use the provider.blocks map to handle proper returns
421421
blockProviderMock.On("GetBlockByNumber", mock.Anything, mock.Anything).Return(nil, nil).Times(int(numOfMissedBlocks + numOfCachedBlocks))
422422

423-
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, batchSize, 0))
423+
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, batchSize, 0), 0)
424424
require.NoError(t, err)
425425

426426
tracker.config.BlockProvider = blockProviderMock
@@ -515,7 +515,7 @@ func TestEventTracker_TrackBlock(t *testing.T) {
515515
// just mock the call, it will use the provider.blocks map to handle proper returns
516516
blockProviderMock.On("GetBlockByNumber", mock.Anything, mock.Anything).Return(nil, nil).Times(int(numOfCachedBlocks))
517517

518-
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, batchSize, 0))
518+
tracker, err := NewEventTracker(createTestTrackerConfig(t, numBlockConfirmations, batchSize, 0), 0)
519519
require.NoError(t, err)
520520

521521
tracker.config.BlockProvider = blockProviderMock

consensus/polybft/polybft.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -543,16 +543,19 @@ func (p *Polybft) Start() error {
543543
// initRuntime creates consensus runtime
544544
func (p *Polybft) initRuntime() error {
545545
runtimeConfig := &runtimeConfig{
546-
PolyBFTConfig: p.consensusConfig,
547-
Key: p.key,
548-
DataDir: p.dataDir,
549-
State: p.state,
550-
blockchain: p.blockchain,
551-
polybftBackend: p,
552-
txPool: p.txPool,
553-
bridgeTopic: p.bridgeTopic,
554-
numBlockConfirmations: p.config.NumBlockConfirmations,
555-
consensusConfig: p.config.Config,
546+
PolyBFTConfig: p.consensusConfig,
547+
Key: p.key,
548+
DataDir: p.dataDir,
549+
State: p.state,
550+
blockchain: p.blockchain,
551+
polybftBackend: p,
552+
txPool: p.txPool,
553+
bridgeTopic: p.bridgeTopic,
554+
consensusConfig: p.config.Config,
555+
// event tracker
556+
numBlockConfirmations: p.config.NumBlockConfirmations,
557+
trackerSyncBatchSize: p.config.TrackerSyncBatchSize,
558+
trackerBlocksToReconcile: p.config.TrackerBlocksToReconcile,
556559
}
557560

558561
runtime, err := newConsensusRuntime(p.logger, runtimeConfig)

0 commit comments

Comments
 (0)