From 2350f4319bee7b48748113cfaffc41ab616cb3e7 Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Fri, 22 Sep 2023 11:51:23 +1000 Subject: [PATCH] refactor: Remove unnecessary code duplication in GetBeaconBlock function --- pkg/cannon/ethereum/beacon.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/cannon/ethereum/beacon.go b/pkg/cannon/ethereum/beacon.go index 281fff29..49342aff 100644 --- a/pkg/cannon/ethereum/beacon.go +++ b/pkg/cannon/ethereum/beacon.go @@ -209,25 +209,25 @@ func (b *BeaconNode) GetBeaconBlock(ctx context.Context, identifier string, igno // Create a buffered channel (semaphore) to limit the number of concurrent goroutines. sem := make(chan struct{}, b.config.BlockPreloadWorkers) + // Check the cache first. + if item := b.blockCache.Get(identifier); item != nil { + if len(ignoreMetrics) != 0 && ignoreMetrics[0] { + b.metrics.IncBlockCacheHit(string(b.Metadata().Network.Name)) + } + + return item.Value(), nil + } + + if len(ignoreMetrics) != 0 && ignoreMetrics[0] { + b.metrics.IncBlockCacheMiss(string(b.Metadata().Network.Name)) + } + // Use singleflight to ensure we only make one request for a block at a time. x, err, _ := b.sfGroup.Do(identifier, func() (interface{}, error) { // Acquire a semaphore before proceeding. sem <- struct{}{} defer func() { <-sem }() - // Check the cache first. - if item := b.blockCache.Get(identifier); item != nil { - if len(ignoreMetrics) != 0 && ignoreMetrics[0] { - b.metrics.IncBlockCacheHit(string(b.Metadata().Network.Name)) - } - - return item.Value(), nil - } - - if len(ignoreMetrics) != 0 && ignoreMetrics[0] { - b.metrics.IncBlockCacheMiss(string(b.Metadata().Network.Name)) - } - // Not in the cache, so fetch it. block, err := b.beacon.FetchBlock(ctx, identifier) if err != nil {