From d8fa7a2f352e867c33a15aa70bca863f825adf86 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 27 Nov 2024 12:35:44 +0200 Subject: [PATCH] Fix race for startingBlock field of eth_syncing endpoint --- api/api.go | 11 +++-------- bootstrap/bootstrap.go | 23 +++++++++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/api/api.go b/api/api.go index a8c96cc91..43972c445 100644 --- a/api/api.go +++ b/api/api.go @@ -170,13 +170,8 @@ func NewBlockChainAPI( receipts storage.ReceiptIndexer, ratelimiter limiter.Store, collector metrics.Collector, -) (*BlockChainAPI, error) { - // get the height from which the indexing resumed since the last restart, this is needed for syncing status. - indexingResumedHeight, err := blocks.LatestEVMHeight() - if err != nil { - return nil, fmt.Errorf("failed to retrieve the indexing resumed height: %w", err) - } - + indexingResumedHeight uint64, +) *BlockChainAPI { return &BlockChainAPI{ logger: logger, config: config, @@ -187,7 +182,7 @@ func NewBlockChainAPI( indexingResumedHeight: indexingResumedHeight, limiter: ratelimiter, collector: collector, - }, nil + } } // BlockNumber returns the block number of the chain head. diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 7f0c6c0bb..7ea78f8c7 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -244,7 +244,14 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error { return fmt.Errorf("failed to create rate limiter: %w", err) } - blockchainAPI, err := api.NewBlockChainAPI( + // get the height from which the indexing resumed since the last restart, + // this is needed for the `eth_syncing` endpoint. + indexingResumedHeight, err := b.storages.Blocks.LatestEVMHeight() + if err != nil { + return fmt.Errorf("failed to retrieve the indexing resumed height: %w", err) + } + + blockchainAPI := api.NewBlockChainAPI( b.logger, b.config, evm, @@ -253,10 +260,8 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error { b.storages.Receipts, ratelimiter, b.collector, + indexingResumedHeight, ) - if err != nil { - return err - } streamAPI := api.NewStreamAPI( b.logger, @@ -565,14 +570,16 @@ func Run(ctx context.Context, cfg *config.Config, ready component.ReadyFunc) err return err } - if err := boot.StartEventIngestion(ctx); err != nil { - return fmt.Errorf("failed to start event ingestion engine: %w", err) - } - + // Start the API Server first, to avoid any races with incoming + // EVM events, that might affect the starting state. if err := boot.StartAPIServer(ctx); err != nil { return fmt.Errorf("failed to start API server: %w", err) } + if err := boot.StartEventIngestion(ctx); err != nil { + return fmt.Errorf("failed to start event ingestion engine: %w", err) + } + if err := boot.StartMetricsServer(ctx); err != nil { return fmt.Errorf("failed to start metrics server: %w", err) }