Skip to content

Commit

Permalink
Merge pull request #465 from onflow/optimize-traces-engine
Browse files Browse the repository at this point in the history
Optimize traces engine to handle empty blocks
  • Loading branch information
sideninja authored Aug 26, 2024
2 parents 78fe801 + b00580f commit 3ec8286
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
5 changes: 0 additions & 5 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,7 @@ func startIngestion(
return err
}

initHeight, err := blocks.LatestEVMHeight()
if err != nil {
return err
}
tracesEngine := traces.NewTracesIngestionEngine(
initHeight,
blocksPublisher,
blocks,
trace,
Expand Down
11 changes: 6 additions & 5 deletions services/traces/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package traces
import (
"context"
"sync"
"sync/atomic"
"time"

"github.com/google/uuid"
Expand All @@ -30,17 +29,13 @@ type Engine struct {
}

func NewTracesIngestionEngine(
initEVMHeight uint64,
blocksPublisher *models.Publisher,
blocks storage.BlockIndexer,
traces storage.TraceIndexer,
downloader Downloader,
logger zerolog.Logger,
collector metrics.Collector,
) *Engine {
height := &atomic.Uint64{}
height.Store(initEVMHeight)

return &Engine{
status: models.NewEngineStatus(),
logger: logger.With().Str("component", "trace-ingestion").Logger(),
Expand Down Expand Up @@ -69,6 +64,12 @@ func (e *Engine) Notify(data any) {
return
}

// If the block has no transactions, we simply return early
// as there are no transaction traces to index.
if len(block.TransactionHashes) == 0 {
return
}

l := e.logger.With().Uint64("evm-height", block.Height).Logger()

cadenceID, err := e.blocks.GetCadenceID(block.Height)
Expand Down
27 changes: 24 additions & 3 deletions services/traces/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ func TestTraceIngestion(t *testing.T) {
return nil
})

engine := NewTracesIngestionEngine(latestHeight, blockPublisher, blocks, trace, downloader, zerolog.Nop(), metrics.NopCollector)
engine := NewTracesIngestionEngine(
blockPublisher,
blocks,
trace,
downloader,
zerolog.Nop(),
metrics.NopCollector,
)

err := engine.Run(context.Background())
require.NoError(t, err)
Expand Down Expand Up @@ -172,7 +179,14 @@ func TestTraceIngestion(t *testing.T) {
return nil
})

engine := NewTracesIngestionEngine(latestHeight, blocksPublisher, blocks, trace, downloader, zerolog.Nop(), metrics.NopCollector)
engine := NewTracesIngestionEngine(
blocksPublisher,
blocks,
trace,
downloader,
zerolog.Nop(),
metrics.NopCollector,
)

err := engine.Run(context.Background())
require.NoError(t, err)
Expand Down Expand Up @@ -252,7 +266,14 @@ func TestTraceIngestion(t *testing.T) {
return nil, fmt.Errorf("failed download")
})

engine := NewTracesIngestionEngine(latestHeight, blockBroadcaster, blocks, trace, downloader, logger, collector)
engine := NewTracesIngestionEngine(
blockBroadcaster,
blocks,
trace,
downloader,
logger,
collector,
)

err := engine.Run(context.Background())
require.NoError(t, err)
Expand Down

0 comments on commit 3ec8286

Please sign in to comment.