Skip to content

Commit 8d2c317

Browse files
s1nablakehhuynh
authored andcommitted
eth/filters: fix getLogs for pending block (ethereum#24949)
* eth/filters: fix pending for getLogs * add pending method to test backend * fix block range validation
1 parent 80db4fb commit 8d2c317

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type SimulatedBackend struct {
6767
stuckTransactions types.Transactions // holds onto all txes that don't go into the pending block due to low gas
6868
pendingBlock *types.Block // Currently pending block that will be imported on request
6969
pendingState *state.StateDB // Currently pending state that will be the active on request
70+
pendingReceipts types.Receipts // Currently receipts for the pending block
7071

7172
events *filters.EventSystem // Event system for filtering log events live
7273

@@ -87,8 +88,8 @@ func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.Genesis
8788
database: database,
8889
blockchain: blockchain,
8990
config: genesis.Config,
90-
events: filters.NewEventSystem(&filterBackend{database, blockchain}, false),
9191
}
92+
backend.events = filters.NewEventSystem(&filterBackend{database, blockchain, backend}, false)
9293
backend.rollback(blockchain.CurrentBlock())
9394
return backend
9495
}
@@ -690,7 +691,7 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
690691
}
691692
var remainingTxes types.Transactions
692693
// Include tx in chain
693-
blocks, _ := core.GenerateChain(b.config, block, ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
694+
blocks, receipts := core.GenerateChain(b.config, block, ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
694695
for _, tx := range b.pendingBlock.Transactions() {
695696
// if market gas price is not set or tx gas price is lower than the set market gas price
696697
if b.marketGasPrice == nil || b.marketGasPrice.Cmp(tx.GasPrice()) <= 0 {
@@ -711,6 +712,7 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
711712
b.pendingBlock = blocks[0]
712713
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil)
713714
b.stuckTransactions = remainingTxes
715+
b.pendingReceipts = receipts[0]
714716

715717
return nil
716718
}
@@ -723,7 +725,7 @@ func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.Filter
723725
var filter *filters.Filter
724726
if query.BlockHash != nil {
725727
// Block filter requested, construct a single-shot filter
726-
filter = filters.NewBlockFilter(&filterBackend{b.database, b.blockchain}, *query.BlockHash, query.Addresses, query.Topics)
728+
filter = filters.NewBlockFilter(&filterBackend{b.database, b.blockchain, b}, *query.BlockHash, query.Addresses, query.Topics)
727729
} else {
728730
// Initialize unset filter boundaries to run from genesis to chain head
729731
from := int64(0)
@@ -735,7 +737,7 @@ func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.Filter
735737
to = query.ToBlock.Int64()
736738
}
737739
// Construct the range filter
738-
filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics)
740+
filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain, b}, from, to, query.Addresses, query.Topics)
739741
}
740742
// Run the filter and return all the logs
741743
logs, err := filter.Logs(ctx)
@@ -856,8 +858,9 @@ func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList }
856858
// filterBackend implements filters.Backend to support filtering for logs without
857859
// taking bloom-bits acceleration structures into account.
858860
type filterBackend struct {
859-
db ethdb.Database
860-
bc *core.BlockChain
861+
db ethdb.Database
862+
bc *core.BlockChain
863+
backend *SimulatedBackend
861864
}
862865

863866
func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db }
@@ -874,6 +877,10 @@ func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*t
874877
return fb.bc.GetHeaderByHash(hash), nil
875878
}
876879

880+
func (fb *filterBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
881+
return fb.backend.pendingBlock, fb.backend.pendingReceipts
882+
}
883+
877884
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
878885
number := rawdb.ReadHeaderNumber(fb.db, hash)
879886
if number == nil {

eth/filters/filter.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Backend interface {
3636
HeaderByHash(ctx context.Context, blockHash common.Hash) (*types.Header, error)
3737
GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
3838
GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
39+
PendingBlockAndReceipts() (*types.Block, types.Receipts)
3940

4041
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
4142
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
@@ -128,26 +129,35 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
128129
}
129130
return f.blockLogs(ctx, header)
130131
}
132+
// Short-cut if all we care about is pending logs
133+
if f.begin == rpc.PendingBlockNumber.Int64() {
134+
if f.end != rpc.PendingBlockNumber.Int64() {
135+
return nil, errors.New("invalid block range")
136+
}
137+
return f.pendingLogs()
138+
}
131139
// Figure out the limits of the filter range
132140
header, _ := f.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
133141
if header == nil {
134142
return nil, nil
135143
}
136-
head := header.Number.Uint64()
137-
138-
if f.begin == -1 {
144+
var (
145+
head = header.Number.Uint64()
146+
end = uint64(f.end)
147+
pending = f.end == rpc.PendingBlockNumber.Int64()
148+
)
149+
if f.begin == rpc.LatestBlockNumber.Int64() {
139150
f.begin = int64(head)
140151
}
141-
end := uint64(f.end)
142-
if f.end == -1 {
152+
if f.end == rpc.LatestBlockNumber.Int64() || f.end == rpc.PendingBlockNumber.Int64() {
143153
end = head
144154
}
145155
// Gather all indexed logs, and finish with non indexed ones
146156
var (
147-
logs []*types.Log
148-
err error
157+
logs []*types.Log
158+
err error
159+
size, sections = f.backend.BloomStatus()
149160
)
150-
size, sections := f.backend.BloomStatus()
151161
if indexed := sections * size; indexed > uint64(f.begin) {
152162
if indexed > end {
153163
logs, err = f.indexedLogs(ctx, end)
@@ -160,6 +170,13 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
160170
}
161171
rest, err := f.unindexedLogs(ctx, end)
162172
logs = append(logs, rest...)
173+
if pending {
174+
pendingLogs, err := f.pendingLogs()
175+
if err != nil {
176+
return nil, err
177+
}
178+
logs = append(logs, pendingLogs...)
179+
}
163180
return logs, err
164181
}
165182

@@ -272,6 +289,19 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) (logs [
272289
return nil, nil
273290
}
274291

292+
// pendingLogs returns the logs matching the filter criteria within the pending block.
293+
func (f *Filter) pendingLogs() ([]*types.Log, error) {
294+
block, receipts := f.backend.PendingBlockAndReceipts()
295+
if bloomFilter(block.Bloom(), f.addresses, f.topics) {
296+
var unfiltered []*types.Log
297+
for _, r := range receipts {
298+
unfiltered = append(unfiltered, r.Logs...)
299+
}
300+
return filterLogs(unfiltered, nil, nil, f.addresses, f.topics), nil
301+
}
302+
return nil, nil
303+
}
304+
275305
func includes(addresses []common.Address, a common.Address) bool {
276306
for _, addr := range addresses {
277307
if addr == a {

eth/filters/filter_system_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ func (b *testBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types
106106
return logs, nil
107107
}
108108

109+
func (b *testBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
110+
return nil, nil
111+
}
112+
109113
func (b *testBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
110114
return b.txFeed.Subscribe(ch)
111115
}

internal/ethapi/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Backend interface {
6565
BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
6666
StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
6767
StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
68+
PendingBlockAndReceipts() (*types.Block, types.Receipts)
6869
GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
6970
GetTd(ctx context.Context, hash common.Hash) *big.Int
7071
GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error)

0 commit comments

Comments
 (0)