Skip to content

Commit

Permalink
optimize getLogs
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Dec 4, 2024
1 parent d125382 commit 21797ce
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions evmrpc/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ type LogFetcher struct {
includeSyntheticReceipts bool
}

func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCriteria, lastToHeight int64) ([]*ethtypes.Log, int64, error) {
func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCriteria, lastToHeight int64) (res []*ethtypes.Log, end int64, err error) {
bloomIndexes := EncodeFilters(crit.Addresses, crit.Topics)
if crit.BlockHash != nil {
block, err := blockByHashWithRetry(ctx, f.tmClient, crit.BlockHash[:], 1)
Expand Down Expand Up @@ -316,25 +316,40 @@ func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCr
return nil, 0, fmt.Errorf("fromBlock %d is after toBlock %d", begin, end)
}
blockHeights := f.FindBlockesByBloom(begin, end, bloomIndexes)
res := []*ethtypes.Log{}
for _, height := range blockHeights {
h := height
block, err := blockByNumberWithRetry(ctx, f.tmClient, &h, 1)
if err != nil {
return nil, 0, err
}
res = append(res, f.GetLogsForBlock(ctx, block, crit, bloomIndexes)...)
if applyOpenEndedLogLimit && int64(len(res)) >= f.filterConfig.maxLog {
res = res[:int(f.filterConfig.maxLog)]
break
wg := sync.WaitGroup{}
res = []*ethtypes.Log{}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%s", e)

Check warning on line 323 in evmrpc/filter.go

View check run for this annotation

Codecov / codecov/patch

evmrpc/filter.go#L323

Added line #L323 was not covered by tests
}
}()
slots := make([][]*ethtypes.Log, len(blockHeights))
for i, height := range blockHeights {
wg.Add(1)
h := height
i := i
go func() {
defer wg.Done()
block, err := blockByNumberWithRetry(ctx, f.tmClient, &h, 1)
if err != nil {
panic(err)

Check warning on line 335 in evmrpc/filter.go

View check run for this annotation

Codecov / codecov/patch

evmrpc/filter.go#L335

Added line #L335 was not covered by tests
}
slots[i] = f.GetLogsForBlock(ctx, block, crit, bloomIndexes)
}()

Check notice

Code scanning / CodeQL

Spawning a Go routine Note

Spawning a Go routine may be a possible source of non-determinism
}
wg.Wait()
for _, logs := range slots {
res = append(res, logs...)
}
if applyOpenEndedLogLimit && int64(len(res)) >= f.filterConfig.maxLog {
res = res[:int(f.filterConfig.maxLog)]
}

return res, end, nil
}

func (f *LogFetcher) GetLogsForBlock(ctx context.Context, block *coretypes.ResultBlock, crit filters.FilterCriteria, filters [][]bloomIndexes) []*ethtypes.Log {
possibleLogs := f.FindLogsByBloom(block.Block.Height, filters)
possibleLogs := f.FindLogsByBloom(block, filters)
matchedLogs := utils.Filter(possibleLogs, func(l *ethtypes.Log) bool { return f.IsLogExactMatch(l, crit) })
for _, l := range matchedLogs {
l.BlockHash = common.Hash(block.BlockID.Hash)
Expand All @@ -358,13 +373,8 @@ func (f *LogFetcher) FindBlockesByBloom(begin, end int64, filters [][]bloomIndex
return
}

func (f *LogFetcher) FindLogsByBloom(height int64, filters [][]bloomIndexes) (res []*ethtypes.Log) {
func (f *LogFetcher) FindLogsByBloom(block *coretypes.ResultBlock, filters [][]bloomIndexes) (res []*ethtypes.Log) {
ctx := f.ctxProvider(LatestCtxHeight)
block, err := blockByNumberWithRetry(context.Background(), f.tmClient, &height, 1)
if err != nil {
fmt.Printf("error getting block when querying logs: %s\n", err)
return
}

for _, hash := range getEvmTxHashesFromBlock(block, f.txConfig) {
receipt, err := f.k.GetReceipt(ctx, hash)
Expand Down

0 comments on commit 21797ce

Please sign in to comment.