Skip to content

Commit d9566e3

Browse files
authored
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 7e9514b commit d9566e3

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ type SimulatedBackend struct {
6363
database ethdb.Database // In memory database to store our testing data
6464
blockchain *core.BlockChain // Ethereum blockchain to handle the consensus
6565

66-
mu sync.Mutex
67-
pendingBlock *types.Block // Currently pending block that will be imported on request
68-
pendingState *state.StateDB // Currently pending state that will be the active on request
66+
mu sync.Mutex
67+
pendingBlock *types.Block // Currently pending block that will be imported on request
68+
pendingState *state.StateDB // Currently pending state that will be the active on request
69+
pendingReceipts types.Receipts // Currently receipts for the pending block
6970

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

@@ -84,8 +85,8 @@ func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.Genesis
8485
database: database,
8586
blockchain: blockchain,
8687
config: genesis.Config,
87-
events: filters.NewEventSystem(&filterBackend{database, blockchain}, false),
8888
}
89+
backend.events = filters.NewEventSystem(&filterBackend{database, blockchain, backend}, false)
8990
backend.rollback(blockchain.CurrentBlock())
9091
return backend
9192
}
@@ -662,7 +663,7 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
662663
return fmt.Errorf("invalid transaction nonce: got %d, want %d", tx.Nonce(), nonce)
663664
}
664665
// Include tx in chain
665-
blocks, _ := core.GenerateChain(b.config, block, ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
666+
blocks, receipts := core.GenerateChain(b.config, block, ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
666667
for _, tx := range b.pendingBlock.Transactions() {
667668
block.AddTxWithChain(b.blockchain, tx)
668669
}
@@ -672,6 +673,7 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
672673

673674
b.pendingBlock = blocks[0]
674675
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil)
676+
b.pendingReceipts = receipts[0]
675677
return nil
676678
}
677679

@@ -683,7 +685,7 @@ func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.Filter
683685
var filter *filters.Filter
684686
if query.BlockHash != nil {
685687
// Block filter requested, construct a single-shot filter
686-
filter = filters.NewBlockFilter(&filterBackend{b.database, b.blockchain}, *query.BlockHash, query.Addresses, query.Topics)
688+
filter = filters.NewBlockFilter(&filterBackend{b.database, b.blockchain, b}, *query.BlockHash, query.Addresses, query.Topics)
687689
} else {
688690
// Initialize unset filter boundaries to run from genesis to chain head
689691
from := int64(0)
@@ -695,7 +697,7 @@ func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.Filter
695697
to = query.ToBlock.Int64()
696698
}
697699
// Construct the range filter
698-
filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics)
700+
filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain, b}, from, to, query.Addresses, query.Topics)
699701
}
700702
// Run the filter and return all the logs
701703
logs, err := filter.Logs(ctx)
@@ -816,8 +818,9 @@ func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList }
816818
// filterBackend implements filters.Backend to support filtering for logs without
817819
// taking bloom-bits acceleration structures into account.
818820
type filterBackend struct {
819-
db ethdb.Database
820-
bc *core.BlockChain
821+
db ethdb.Database
822+
bc *core.BlockChain
823+
backend *SimulatedBackend
821824
}
822825

823826
func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db }
@@ -834,6 +837,10 @@ func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*t
834837
return fb.bc.GetHeaderByHash(hash), nil
835838
}
836839

840+
func (fb *filterBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
841+
return fb.backend.pendingBlock, fb.backend.pendingReceipts
842+
}
843+
837844
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
838845
number := rawdb.ReadHeaderNumber(fb.db, hash)
839846
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)