Skip to content

Commit d8fa7a2

Browse files
committed
Fix race for startingBlock field of eth_syncing endpoint
1 parent eb05840 commit d8fa7a2

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

api/api.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,8 @@ func NewBlockChainAPI(
170170
receipts storage.ReceiptIndexer,
171171
ratelimiter limiter.Store,
172172
collector metrics.Collector,
173-
) (*BlockChainAPI, error) {
174-
// get the height from which the indexing resumed since the last restart, this is needed for syncing status.
175-
indexingResumedHeight, err := blocks.LatestEVMHeight()
176-
if err != nil {
177-
return nil, fmt.Errorf("failed to retrieve the indexing resumed height: %w", err)
178-
}
179-
173+
indexingResumedHeight uint64,
174+
) *BlockChainAPI {
180175
return &BlockChainAPI{
181176
logger: logger,
182177
config: config,
@@ -187,7 +182,7 @@ func NewBlockChainAPI(
187182
indexingResumedHeight: indexingResumedHeight,
188183
limiter: ratelimiter,
189184
collector: collector,
190-
}, nil
185+
}
191186
}
192187

193188
// BlockNumber returns the block number of the chain head.

bootstrap/bootstrap.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,14 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error {
244244
return fmt.Errorf("failed to create rate limiter: %w", err)
245245
}
246246

247-
blockchainAPI, err := api.NewBlockChainAPI(
247+
// get the height from which the indexing resumed since the last restart,
248+
// this is needed for the `eth_syncing` endpoint.
249+
indexingResumedHeight, err := b.storages.Blocks.LatestEVMHeight()
250+
if err != nil {
251+
return fmt.Errorf("failed to retrieve the indexing resumed height: %w", err)
252+
}
253+
254+
blockchainAPI := api.NewBlockChainAPI(
248255
b.logger,
249256
b.config,
250257
evm,
@@ -253,10 +260,8 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error {
253260
b.storages.Receipts,
254261
ratelimiter,
255262
b.collector,
263+
indexingResumedHeight,
256264
)
257-
if err != nil {
258-
return err
259-
}
260265

261266
streamAPI := api.NewStreamAPI(
262267
b.logger,
@@ -565,14 +570,16 @@ func Run(ctx context.Context, cfg *config.Config, ready component.ReadyFunc) err
565570
return err
566571
}
567572

568-
if err := boot.StartEventIngestion(ctx); err != nil {
569-
return fmt.Errorf("failed to start event ingestion engine: %w", err)
570-
}
571-
573+
// Start the API Server first, to avoid any races with incoming
574+
// EVM events, that might affect the starting state.
572575
if err := boot.StartAPIServer(ctx); err != nil {
573576
return fmt.Errorf("failed to start API server: %w", err)
574577
}
575578

579+
if err := boot.StartEventIngestion(ctx); err != nil {
580+
return fmt.Errorf("failed to start event ingestion engine: %w", err)
581+
}
582+
576583
if err := boot.StartMetricsServer(ctx); err != nil {
577584
return fmt.Errorf("failed to start metrics server: %w", err)
578585
}

0 commit comments

Comments
 (0)