Skip to content

Commit 3886744

Browse files
committed
eth/filters: fix for eth_getLogs failing with tag (ethereum#25922)
1 parent 024a7ec commit 3886744

File tree

4 files changed

+76
-18
lines changed

4 files changed

+76
-18
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,32 @@ type filterBackend struct {
895895
func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db }
896896
func (fb *filterBackend) EventMux() *event.TypeMux { panic("not supported") }
897897

898-
func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumber) (*types.Header, error) {
899-
if block == rpc.LatestBlockNumber {
898+
func (fb *filterBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
899+
switch number {
900+
case rpc.PendingBlockNumber:
901+
if block := fb.backend.pendingBlock; block != nil {
902+
return block.Header(), nil
903+
}
904+
return nil, nil
905+
case rpc.LatestBlockNumber:
900906
return fb.bc.CurrentHeader(), nil
907+
case rpc.CommittedBlockNumber:
908+
if fb.bc.Config().XDPoS == nil {
909+
return nil, errors.New("only XDPoS v2 supports committed block lookup")
910+
}
911+
current := fb.bc.CurrentBlock().Header()
912+
if fb.bc.Config().XDPoS.BlockConsensusVersion(
913+
current.Number,
914+
current.Extra,
915+
XDPoS.ExtraFieldCheck,
916+
) == params.ConsensusEngineVersion2 {
917+
confirmedHash := fb.bc.Engine().(*XDPoS.XDPoS).EngineV2.GetLatestCommittedBlockInfo().Hash
918+
return fb.bc.GetHeaderByHash(confirmedHash), nil
919+
}
920+
return nil, errors.New("only XDPoS v2 can lookup committed block")
921+
default:
922+
return fb.bc.GetHeaderByNumber(uint64(number.Int64())), nil
901923
}
902-
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
903924
}
904925

905926
func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {

core/rawdb/accessors_chain.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ func WriteHeadHeaderHash(db ethdb.KeyValueWriter, hash common.Hash) {
9797
}
9898
}
9999

100+
// ReadHeadBlockHash retrieves the hash of the current canonical head block.
101+
func ReadHeadBlockHash(db ethdb.KeyValueReader) common.Hash {
102+
data, _ := db.Get(headBlockKey)
103+
if len(data) == 0 {
104+
return common.Hash{}
105+
}
106+
return common.BytesToHash(data)
107+
}
108+
100109
// WriteHeadBlockHash stores the head block's hash.
101110
func WriteHeadBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
102111
if err := db.Put(headBlockKey, hash.Bytes()); err != nil {

eth/filters/filter.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,39 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
119119
return nil, nil
120120
}
121121
var (
122-
head = header.Number.Uint64()
123-
end = uint64(f.end)
122+
err error
123+
head = header.Number.Int64()
124124
pending = f.end == rpc.PendingBlockNumber.Int64()
125125
)
126-
if f.begin == rpc.LatestBlockNumber.Int64() {
127-
f.begin = int64(head)
126+
resolveSpecial := func(number int64) (int64, error) {
127+
var hdr *types.Header
128+
switch number {
129+
case rpc.LatestBlockNumber.Int64():
130+
return head, nil
131+
case rpc.PendingBlockNumber.Int64():
132+
// we should return head here since we've already captured
133+
// that we need to get the pending logs in the pending boolean above
134+
return head, nil
135+
case rpc.CommittedBlockNumber.Int64():
136+
hdr, _ = f.sys.backend.HeaderByNumber(ctx, rpc.CommittedBlockNumber)
137+
if hdr == nil {
138+
return 0, errors.New("committed header not found")
139+
}
140+
default:
141+
return number, nil
142+
}
143+
return hdr.Number.Int64(), nil
128144
}
129-
if f.end == rpc.LatestBlockNumber.Int64() || f.end == rpc.PendingBlockNumber.Int64() {
130-
end = head
145+
if f.begin, err = resolveSpecial(f.begin); err != nil {
146+
return nil, err
147+
}
148+
if f.end, err = resolveSpecial(f.end); err != nil {
149+
return nil, err
131150
}
132151
// Gather all indexed logs, and finish with non indexed ones
133152
var (
134153
logs []*types.Log
135-
err error
154+
end = uint64(f.end)
136155
size, sections = f.sys.backend.BloomStatus()
137156
)
138157
if indexed := sections * size; indexed > uint64(f.begin) {

eth/filters/filter_system_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,25 @@ func (b *testBackend) ChainDb() ethdb.Database {
5757
}
5858

5959
func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
60-
var hash common.Hash
61-
var num uint64
62-
if blockNr == rpc.LatestBlockNumber {
63-
hash = core.GetHeadBlockHash(b.db)
64-
num = core.GetBlockNumber(b.db, hash)
65-
} else {
60+
var (
61+
hash common.Hash
62+
num uint64
63+
)
64+
switch blockNr {
65+
case rpc.LatestBlockNumber:
66+
hash = rawdb.ReadHeadBlockHash(b.db)
67+
number := rawdb.ReadHeaderNumber(b.db, hash)
68+
if number == nil {
69+
return nil, nil
70+
}
71+
num = *number
72+
case rpc.CommittedBlockNumber:
73+
return nil, nil
74+
default:
6675
num = uint64(blockNr)
67-
hash = core.GetCanonicalHash(b.db, num)
76+
hash = rawdb.ReadCanonicalHash(b.db, num)
6877
}
69-
return core.GetHeader(b.db, hash, num), nil
78+
return rawdb.ReadHeader(b.db, hash, num), nil
7079
}
7180

7281
func (b *testBackend) HeaderByHash(ctx context.Context, blockHash common.Hash) (*types.Header, error) {

0 commit comments

Comments
 (0)