Skip to content

Commit

Permalink
eth/filters: reduce database read when handling eth_getLogs
Browse files Browse the repository at this point in the history
While profiling, we see that when bloom filter hits, 1/3 CPU time in check log
match routine is used to get the corresponding block number from block hash.
This is redundant as this information is available already and can be passed via
function argument. This commit adds block number parameter to GetLogs interface
to pass the available block number information.
  • Loading branch information
minh-bq committed Sep 25, 2024
1 parent ce6c0e8 commit 15ef4fa
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 28 deletions.
8 changes: 2 additions & 6 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,8 @@ func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (typ
return rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config()), nil
}

func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
number := rawdb.ReadHeaderNumber(fb.db, hash)
if number == nil {
return nil, nil
}
receipts := rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config())
func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash, blockNumber uint64) ([][]*types.Log, error) {
receipts := rawdb.ReadReceipts(fb.db, hash, blockNumber, fb.bc.Config())
if receipts == nil {
return nil, nil
}
Expand Down
8 changes: 2 additions & 6 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,9 @@ func (b *EthAPIBackend) BlobSidecarsByNumberOrHash(ctx context.Context, blockNrO
return nil, errors.New("invalid arguments: neither block number nor block hash hash specified")
}

func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash, blockNumber uint64) ([][]*types.Log, error) {
db := b.eth.ChainDb()
number := rawdb.ReadHeaderNumber(db, hash)
if number == nil {
return nil, errors.New("failed to get block number from hash")
}
logs := rawdb.ReadLogs(db, hash, *number, b.eth.blockchain.Config())
logs := rawdb.ReadLogs(db, hash, blockNumber, b.eth.blockchain.Config())
if logs == nil {
return nil, errors.New("failed to get logs for block")
}
Expand Down
4 changes: 2 additions & 2 deletions eth/filters/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Backend interface {
HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
HeaderByHash(ctx context.Context, blockHash common.Hash) (*types.Header, error)
GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
GetLogs(ctx context.Context, blockHash common.Hash, blockNumber uint64) ([][]*types.Log, error)

SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
Expand Down Expand Up @@ -271,7 +271,7 @@ func (f *Filter) blockLogs(ctx context.Context, header *types.Header) (logs []*t
// match the filter criteria. This function is called when the bloom filter signals a potential match.
func (f *Filter) checkMatches(ctx context.Context, header *types.Header) (logs []*types.Log, err error) {
// Get the logs of the block
logsList, err := f.backend.GetLogs(ctx, header.Hash())
logsList, err := f.backend.GetLogs(ctx, header.Hash(), header.Number.Uint64())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func (es *EventSystem) lightFilterLogs(header *types.Header, addresses []common.
// Get the logs of the block
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
logsList, err := es.backend.GetLogs(ctx, header.Hash())
logsList, err := es.backend.GetLogs(ctx, header.Hash(), header.Number.Uint64())
if err != nil {
return nil
}
Expand Down
8 changes: 2 additions & 6 deletions eth/filters/filter_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,8 @@ func (b *testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.
return nil, nil
}

func (b *testBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
number := rawdb.ReadHeaderNumber(b.db, hash)
if number == nil {
return nil, nil
}
receipts := rawdb.ReadReceipts(b.db, hash, *number, params.TestChainConfig)
func (b *testBackend) GetLogs(ctx context.Context, hash common.Hash, blockNumber uint64) ([][]*types.Log, error) {
receipts := rawdb.ReadReceipts(b.db, hash, blockNumber, params.TestChainConfig)

logs := make([][]*types.Log, len(receipts))
for i, receipt := range receipts {
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func (b testBackend) SubscribeNewTxsEvent(events chan<- core.NewTxsEvent) event.
}
func (b testBackend) ChainConfig() *params.ChainConfig { return b.chain.Config() }
func (b testBackend) Engine() consensus.Engine { return b.chain.Engine() }
func (b testBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
func (b testBackend) GetLogs(ctx context.Context, blockHash common.Hash, blockNumber uint64) ([][]*types.Log, error) {
panic("implement me")
}
func (b testBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type Backend interface {

// Filter API
BloomStatus() (uint64, uint64)
GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
GetLogs(ctx context.Context, blockHash common.Hash, blockNumber uint64) ([][]*types.Log, error)
ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
Expand Down
7 changes: 2 additions & 5 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,8 @@ func (b *LesApiBackend) BlobSidecarsByNumberOrHash(ctx context.Context, blockNrO
return nil, nil
}

func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
return light.GetBlockLogs(ctx, b.eth.odr, hash, *number)
}
return nil, nil
func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash, blockNumber uint64) ([][]*types.Log, error) {
return light.GetBlockLogs(ctx, b.eth.odr, hash, blockNumber)
}

func (b *LesApiBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
Expand Down

0 comments on commit 15ef4fa

Please sign in to comment.