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

Commit 8ad99c7

Browse files
Add new event tracker flags to server command
1 parent bdc00bb commit 8ad99c7

File tree

11 files changed

+167
-86
lines changed

11 files changed

+167
-86
lines changed

command/server/config/config.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ type Config struct {
3434
CorsAllowedOrigins []string `json:"cors_allowed_origins" yaml:"cors_allowed_origins"`
3535

3636
Relayer bool `json:"relayer" yaml:"relayer"`
37-
NumBlockConfirmations uint64 `json:"num_block_confirmations" yaml:"num_block_confirmations"`
3837
RelayerTrackerPollInterval time.Duration `json:"relayer_tracker_poll_interval" yaml:"relayer_tracker_poll_interval"`
3938

4039
ConcurrentRequestsDebug uint64 `json:"concurrent_requests_debug" yaml:"concurrent_requests_debug"`
4140
WebSocketReadLimit uint64 `json:"web_socket_read_limit" yaml:"web_socket_read_limit"`
4241

4342
MetricsInterval time.Duration `json:"metrics_interval" yaml:"metrics_interval"`
43+
44+
// event tracker
45+
NumBlockConfirmations uint64 `json:"num_block_confirmations" yaml:"num_block_confirmations"`
46+
TrackerSyncBatchSize uint64 `json:"tracker_sync_batch_size" yaml:"tracker_sync_batch_size"`
47+
TrackerBlocksToReconcile uint64 `json:"tracker_blocks_to_reconcile" yaml:"tracker_blocks_to_reconcile"`
4448
}
4549

4650
// Telemetry holds the config details for metric services.
@@ -83,10 +87,6 @@ const (
8387
// requests with fromBlock/toBlock values (e.g. eth_getLogs)
8488
DefaultJSONRPCBlockRangeLimit uint64 = 1000
8589

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

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

107126
// DefaultConfig returns the default server configuration
@@ -138,11 +157,14 @@ func DefaultConfig() *Config {
138157
JSONRPCBatchRequestLimit: DefaultJSONRPCBatchRequestLimit,
139158
JSONRPCBlockRangeLimit: DefaultJSONRPCBlockRangeLimit,
140159
Relayer: false,
141-
NumBlockConfirmations: DefaultNumBlockConfirmations,
142160
ConcurrentRequestsDebug: DefaultConcurrentRequestsDebug,
143161
WebSocketReadLimit: DefaultWebSocketReadLimit,
144162
RelayerTrackerPollInterval: DefaultRelayerTrackerPollInterval,
145163
MetricsInterval: DefaultMetricsInterval,
164+
// event tracker
165+
NumBlockConfirmations: DefaultNumBlockConfirmations,
166+
TrackerSyncBatchSize: DefaultTrackerSyncBatchSize,
167+
TrackerBlocksToReconcile: DefaultTrackerBlocksToReconcile,
146168
}
147169
}
148170

command/server/params.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@ const (
3838
corsOriginFlag = "access-control-allow-origins"
3939
logFileLocationFlag = "log-to"
4040

41-
relayerFlag = "relayer"
42-
numBlockConfirmationsFlag = "num-block-confirmations"
41+
relayerFlag = "relayer"
4342

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

4746
relayerTrackerPollIntervalFlag = "relayer-poll-interval"
4847

4948
metricsIntervalFlag = "metrics-interval"
49+
50+
// event tracker
51+
numBlockConfirmationsFlag = "num-block-confirmations"
52+
trackerSyncBatchSizeFlag = "tracker-sync-batch-size"
53+
trackerBlocksToReconcileFlag = "tracker-blocks-to-reconcile"
5054
)
5155

5256
// Flags that are deprecated, but need to be preserved for
@@ -190,8 +194,11 @@ func (p *serverParams) generateConfig() *server.Config {
190194
LogFilePath: p.logFileLocation,
191195

192196
Relayer: p.relayer,
193-
NumBlockConfirmations: p.rawConfig.NumBlockConfirmations,
194197
RelayerTrackerPollInterval: p.rawConfig.RelayerTrackerPollInterval,
195198
MetricsInterval: p.rawConfig.MetricsInterval,
199+
// event tracker
200+
NumBlockConfirmations: p.rawConfig.NumBlockConfirmations,
201+
TrackerSyncBatchSize: p.rawConfig.TrackerSyncBatchSize,
202+
TrackerBlocksToReconcile: p.rawConfig.TrackerBlocksToReconcile,
196203
}
197204
}

command/server/server.go

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

224-
cmd.Flags().Uint64Var(
225-
&params.rawConfig.NumBlockConfirmations,
226-
numBlockConfirmationsFlag,
227-
defaultConfig.NumBlockConfirmations,
228-
"minimal number of child blocks required for the parent block to be considered final",
229-
)
230-
231224
cmd.Flags().Uint64Var(
232225
&params.rawConfig.ConcurrentRequestsDebug,
233226
concurrentRequestsDebugFlag,
@@ -256,6 +249,38 @@ func setFlags(cmd *cobra.Command) {
256249
"the interval (in seconds) at which special metrics are generated. a value of zero means the metrics are disabled",
257250
)
258251

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

261286
setDevFlags(cmd)

consensus/consensus.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ type Params struct {
7878
SecretsManager secrets.SecretsManager
7979
BlockTime uint64
8080

81-
NumBlockConfirmations uint64
82-
MetricsInterval time.Duration
81+
MetricsInterval time.Duration
82+
// event tracker
83+
NumBlockConfirmations uint64
84+
TrackerSyncBatchSize uint64
85+
TrackerBlocksToReconcile uint64
8386
}
8487

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

consensus/polybft/consensus_runtime.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,18 @@ type guardedDataDTO struct {
7171

7272
// runtimeConfig is a struct that holds configuration data for given consensus runtime
7373
type runtimeConfig struct {
74-
PolyBFTConfig *PolyBFTConfig
75-
DataDir string
76-
Key *wallet.Key
77-
State *State
78-
blockchain blockchainBackend
79-
polybftBackend polybftBackend
80-
txPool txPoolInterface
81-
bridgeTopic topic
82-
numBlockConfirmations uint64
74+
PolyBFTConfig *PolyBFTConfig
75+
DataDir string
76+
Key *wallet.Key
77+
State *State
78+
blockchain blockchainBackend
79+
polybftBackend polybftBackend
80+
txPool txPoolInterface
81+
bridgeTopic topic
82+
// event tracker
83+
numBlockConfirmations uint64
84+
trackerSyncBatchSize uint64
85+
trackerBlocksToReconcile uint64
8386
}
8487

8588
// consensusRuntime is a struct that provides consensus runtime features like epoch, state and event management
@@ -171,15 +174,18 @@ func (c *consensusRuntime) initStateSyncManager(logger hcf.Logger) error {
171174
logger.Named("state-sync-manager"),
172175
c.config.State,
173176
&stateSyncConfig{
174-
key: c.config.Key,
175-
stateSenderAddr: stateSenderAddr,
176-
stateSenderStartBlock: c.config.PolyBFTConfig.Bridge.EventTrackerStartBlocks[stateSenderAddr],
177-
jsonrpcAddr: c.config.PolyBFTConfig.Bridge.JSONRPCEndpoint,
178-
dataDir: c.config.DataDir,
179-
topic: c.config.bridgeTopic,
180-
maxCommitmentSize: maxCommitmentSize,
177+
key: c.config.Key,
178+
stateSenderAddr: stateSenderAddr,
179+
stateSenderStartBlock: c.config.PolyBFTConfig.Bridge.EventTrackerStartBlocks[stateSenderAddr],
180+
jsonrpcAddr: c.config.PolyBFTConfig.Bridge.JSONRPCEndpoint,
181+
dataDir: c.config.DataDir,
182+
topic: c.config.bridgeTopic,
183+
maxCommitmentSize: maxCommitmentSize,
184+
// event tracker
181185
numBlockConfirmations: c.config.numBlockConfirmations,
182186
blockTrackerPollInterval: c.config.PolyBFTConfig.BlockTrackerPollInterval.Duration,
187+
trackerSyncBatchSize: c.config.trackerSyncBatchSize,
188+
trackerBlocksToReconcile: c.config.trackerBlocksToReconcile,
183189
},
184190
c,
185191
)

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: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -537,15 +537,18 @@ func (p *Polybft) Start() error {
537537
// initRuntime creates consensus runtime
538538
func (p *Polybft) initRuntime() error {
539539
runtimeConfig := &runtimeConfig{
540-
PolyBFTConfig: p.consensusConfig,
541-
Key: p.key,
542-
DataDir: p.dataDir,
543-
State: p.state,
544-
blockchain: p.blockchain,
545-
polybftBackend: p,
546-
txPool: p.txPool,
547-
bridgeTopic: p.bridgeTopic,
548-
numBlockConfirmations: p.config.NumBlockConfirmations,
540+
PolyBFTConfig: p.consensusConfig,
541+
Key: p.key,
542+
DataDir: p.dataDir,
543+
State: p.state,
544+
blockchain: p.blockchain,
545+
polybftBackend: p,
546+
txPool: p.txPool,
547+
bridgeTopic: p.bridgeTopic,
548+
// event tracker
549+
numBlockConfirmations: p.config.NumBlockConfirmations,
550+
trackerSyncBatchSize: p.config.TrackerSyncBatchSize,
551+
trackerBlocksToReconcile: p.config.TrackerBlocksToReconcile,
549552
}
550553

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

0 commit comments

Comments
 (0)