Skip to content

Commit 5cf1d35

Browse files
committed
eth, les, light: filter on logs only, derive receipts on demand
1 parent 4535247 commit 5cf1d35

8 files changed

Lines changed: 123 additions & 17 deletions

File tree

‎accounts/abi/bind/backends/simulated.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,23 @@ func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumb
428428
}
429429
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
430430
}
431+
431432
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
432433
return core.GetBlockReceipts(fb.db, hash, core.GetBlockNumber(fb.db, hash)), nil
433434
}
434435

436+
func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
437+
receipts := core.GetBlockReceipts(fb.db, hash, core.GetBlockNumber(fb.db, hash))
438+
if receipts == nil {
439+
return nil, nil
440+
}
441+
logs := make([][]*types.Log, len(receipts))
442+
for i, receipt := range receipts {
443+
logs[i] = receipt.Logs
444+
}
445+
return logs, nil
446+
}
447+
435448
func (fb *filterBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
436449
return event.NewSubscription(func(quit <-chan struct{}) error {
437450
<-quit

‎eth/api_backend.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash)
104104
return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil
105105
}
106106

107+
func (b *EthApiBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
108+
receipts := core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
109+
if receipts == nil {
110+
return nil, nil
111+
}
112+
logs := make([][]*types.Log, len(receipts))
113+
for i, receipt := range receipts {
114+
logs[i] = receipt.Logs
115+
}
116+
return logs, nil
117+
}
118+
107119
func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
108120
return b.eth.blockchain.GetTdByHash(blockHash)
109121
}

‎eth/filters/filter.go‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Backend interface {
3434
EventMux() *event.TypeMux
3535
HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
3636
GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
37+
GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
3738

3839
SubscribeTxPreEvent(chan<- core.TxPreEvent) event.Subscription
3940
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
@@ -201,16 +202,28 @@ func (f *Filter) unindexedLogs(ctx context.Context, end uint64) ([]*types.Log, e
201202
// match the filter criteria. This function is called when the bloom filter signals a potential match.
202203
func (f *Filter) checkMatches(ctx context.Context, header *types.Header) (logs []*types.Log, err error) {
203204
// Get the logs of the block
204-
receipts, err := f.backend.GetReceipts(ctx, header.Hash())
205+
logsList, err := f.backend.GetLogs(ctx, header.Hash())
205206
if err != nil {
206207
return nil, err
207208
}
208209
var unfiltered []*types.Log
209-
for _, receipt := range receipts {
210-
unfiltered = append(unfiltered, receipt.Logs...)
210+
for _, logs := range logsList {
211+
unfiltered = append(unfiltered, logs...)
211212
}
212213
logs = filterLogs(unfiltered, nil, nil, f.addresses, f.topics)
213214
if len(logs) > 0 {
215+
// We have matching logs, check if we need to resolve full logs via the light client
216+
if logs[0].TxHash == (common.Hash{}) {
217+
receipts, err := f.backend.GetReceipts(ctx, header.Hash())
218+
if err != nil {
219+
return nil, err
220+
}
221+
unfiltered = unfiltered[:0]
222+
for _, receipt := range receipts {
223+
unfiltered = append(unfiltered, receipt.Logs...)
224+
}
225+
logs = filterLogs(unfiltered, nil, nil, f.addresses, f.topics)
226+
}
214227
return logs, nil
215228
}
216229
return nil, nil

‎eth/filters/filter_system.go‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,19 +375,35 @@ func (es *EventSystem) lightFilterLogs(header *types.Header, addresses []common.
375375
// Get the logs of the block
376376
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
377377
defer cancel()
378-
receipts, err := es.backend.GetReceipts(ctx, header.Hash())
378+
logsList, err := es.backend.GetLogs(ctx, header.Hash())
379379
if err != nil {
380380
return nil
381381
}
382382
var unfiltered []*types.Log
383-
for _, receipt := range receipts {
384-
for _, log := range receipt.Logs {
383+
for _, logs := range logsList {
384+
for _, log := range logs {
385385
logcopy := *log
386386
logcopy.Removed = remove
387387
unfiltered = append(unfiltered, &logcopy)
388388
}
389389
}
390390
logs := filterLogs(unfiltered, nil, nil, addresses, topics)
391+
if len(logs) > 0 && logs[0].TxHash == (common.Hash{}) {
392+
// We have matching but non-derived logs
393+
receipts, err := es.backend.GetReceipts(ctx, header.Hash())
394+
if err != nil {
395+
return nil
396+
}
397+
unfiltered = unfiltered[:0]
398+
for _, receipt := range receipts {
399+
for _, log := range receipt.Logs {
400+
logcopy := *log
401+
logcopy.Removed = remove
402+
unfiltered = append(unfiltered, &logcopy)
403+
}
404+
}
405+
logs = filterLogs(unfiltered, nil, nil, addresses, topics)
406+
}
391407
return logs
392408
}
393409
return nil

‎eth/filters/filter_system_test.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,19 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe
6969
}
7070

7171
func (b *testBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
72-
num := core.GetBlockNumber(b.db, blockHash)
73-
return core.GetBlockReceipts(b.db, blockHash, num), nil
72+
number := core.GetBlockNumber(b.db, blockHash)
73+
return core.GetBlockReceipts(b.db, blockHash, number), nil
74+
}
75+
76+
func (b *testBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
77+
number := core.GetBlockNumber(b.db, blockHash)
78+
receipts := core.GetBlockReceipts(b.db, blockHash, number)
79+
80+
logs := make([][]*types.Log, len(receipts))
81+
for i, receipt := range receipts {
82+
logs[i] = receipt.Logs
83+
}
84+
return logs, nil
7485
}
7586

7687
func (b *testBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {

‎internal/ethapi/api.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,15 +1032,19 @@ func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context,
10321032
}
10331033

10341034
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
1035-
func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) {
1035+
func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
10361036
tx, blockHash, blockNumber, index := core.GetTransaction(s.b.ChainDb(), hash)
10371037
if tx == nil {
10381038
return nil, errors.New("unknown transaction")
10391039
}
1040-
receipt, _, _, _ := core.GetReceipt(s.b.ChainDb(), hash) // Old receipts don't have the lookup data available
1041-
if receipt == nil {
1040+
receipts, err := s.b.GetReceipts(ctx, blockHash)
1041+
if err != nil {
1042+
return nil, err
1043+
}
1044+
if len(receipts) <= int(index) {
10421045
return nil, errors.New("unknown receipt")
10431046
}
1047+
receipt := receipts[index]
10441048

10451049
var signer types.Signer = types.FrontierSigner{}
10461050
if tx.Protected() {

‎les/api_backend.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func (b *LesApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash)
8787
return light.GetBlockReceipts(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
8888
}
8989

90+
func (b *LesApiBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
91+
return light.GetBlockLogs(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
92+
}
93+
9094
func (b *LesApiBackend) GetTd(blockHash common.Hash) *big.Int {
9195
return b.eth.blockchain.GetTdByHash(blockHash)
9296
}

‎light/odr_util.go‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,48 @@ func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash, number uint
126126
// GetBlockReceipts retrieves the receipts generated by the transactions included
127127
// in a block given by its hash.
128128
func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (types.Receipts, error) {
129+
// Retrieve the potentially incomplete receipts from disk or network
129130
receipts := core.GetBlockReceipts(odr.Database(), hash, number)
130-
if receipts != nil {
131-
return receipts, nil
131+
if receipts == nil {
132+
r := &ReceiptsRequest{Hash: hash, Number: number}
133+
if err := odr.Retrieve(ctx, r); err != nil {
134+
return nil, err
135+
}
136+
receipts = r.Receipts
132137
}
133-
r := &ReceiptsRequest{Hash: hash, Number: number}
134-
if err := odr.Retrieve(ctx, r); err != nil {
135-
return nil, err
138+
// If the receipts are incomplete, fill the derived fields
139+
if len(receipts) > 0 && receipts[0].TxHash == (common.Hash{}) {
140+
block, err := GetBlock(ctx, odr, hash, number)
141+
if err != nil {
142+
return nil, err
143+
}
144+
genesis := core.GetCanonicalHash(odr.Database(), 0)
145+
config, _ := core.GetChainConfig(odr.Database(), genesis)
146+
147+
core.SetReceiptsData(config, block, receipts)
148+
core.WriteBlockReceipts(odr.Database(), hash, number, receipts)
149+
}
150+
return receipts, nil
151+
}
152+
153+
// GetBlockLogs retrieves the logs generated by the transactions included in a
154+
// block given by its hash.
155+
func GetBlockLogs(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) ([][]*types.Log, error) {
156+
// Retrieve the potentially incomplete receipts from disk or network
157+
receipts := core.GetBlockReceipts(odr.Database(), hash, number)
158+
if receipts == nil {
159+
r := &ReceiptsRequest{Hash: hash, Number: number}
160+
if err := odr.Retrieve(ctx, r); err != nil {
161+
return nil, err
162+
}
163+
receipts = r.Receipts
164+
}
165+
// Return the logs without deriving any computed fields on the receipts
166+
logs := make([][]*types.Log, len(receipts))
167+
for i, receipt := range receipts {
168+
logs[i] = receipt.Logs
136169
}
137-
return r.Receipts, nil
170+
return logs, nil
138171
}
139172

140173
// GetBloomBits retrieves a batch of compressed bloomBits vectors belonging to the given bit index and section indexes

0 commit comments

Comments
 (0)