Skip to content

eth/filters: Avoid copy the whole logs when TxHash is empty #31672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions eth/filters/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,21 +471,7 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*typ
if len(logs) == 0 {
return nil, nil
}
// Most backends will deliver un-derived logs, but check nevertheless.
if len(logs) > 0 && logs[0].TxHash != (common.Hash{}) {
return logs, nil
}

body, err := f.sys.cachedGetBody(ctx, cached, hash, header.Number.Uint64())
if err != nil {
return nil, err
}
for i, log := range logs {
// Copy log not to modify cache elements
logcopy := *log
logcopy.TxHash = body.Transactions[logcopy.TxIndex].Hash()
logs[i] = &logcopy
}
return logs, nil
}

Expand Down
26 changes: 12 additions & 14 deletions eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -94,7 +93,6 @@ func NewFilterSystem(backend Backend, config Config) *FilterSystem {

type logCacheElem struct {
logs []*types.Log
body atomic.Pointer[types.Body]
}

// cachedLogElem loads block logs from the backend and caches the result.
Expand Down Expand Up @@ -125,23 +123,23 @@ func (sys *FilterSystem) cachedLogElem(ctx context.Context, blockHash common.Has
flattened = append(flattened, log)
}
}

// Most backends will deliver un-derived logs, but check nevertheless.
if len(flattened) > 0 && flattened[0].TxHash == (common.Hash{}) {
body, err := sys.backend.GetBody(ctx, blockHash, rpc.BlockNumber(number))
if err != nil {
return nil, err
}
for _, log := range flattened {
log.TxHash = body.Transactions[log.TxIndex].Hash()
}
}

elem := &logCacheElem{logs: flattened}
sys.logsCache.Add(blockHash, elem)
return elem, nil
}

func (sys *FilterSystem) cachedGetBody(ctx context.Context, elem *logCacheElem, hash common.Hash, number uint64) (*types.Body, error) {
if body := elem.body.Load(); body != nil {
return body, nil
}
body, err := sys.backend.GetBody(ctx, hash, rpc.BlockNumber(number))
if err != nil {
return nil, err
}
elem.body.Store(body)
return body, nil
}

// Type determines the kind of filter and is used to put the filter in to
// the correct bucket when added.
type Type byte
Expand Down