Skip to content

Commit

Permalink
eth: Fix for eth_getLogs failing w/ finalized and safe tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanschneider committed Oct 4, 2022
1 parent 1913b50 commit 32c2d48
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
38 changes: 31 additions & 7 deletions eth/filters/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,44 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
return nil, nil
}
var (
head = header.Number.Uint64()
end = uint64(f.end)
err error
head = header.Number.Int64()
pending = f.end == rpc.PendingBlockNumber.Int64()
)
if f.begin == rpc.LatestBlockNumber.Int64() {
f.begin = int64(head)
resolveSpecial := func(number int64) (int64, error) {
var hdr *types.Header
switch number {
case rpc.LatestBlockNumber.Int64():
return head, nil
case rpc.PendingBlockNumber.Int64():
// we should return head here since we've already captured
// that we need to get the pending logs in the pending boolean above
return head, nil
case rpc.FinalizedBlockNumber.Int64():
hdr, _ = f.sys.backend.HeaderByNumber(ctx, rpc.FinalizedBlockNumber)
if hdr == nil {
return 0, errors.New("finalized header not found")
}
case rpc.SafeBlockNumber.Int64():
hdr, _ = f.sys.backend.HeaderByNumber(ctx, rpc.SafeBlockNumber)
if hdr == nil {
return 0, errors.New("safe header not found")
}
default:
return number, nil
}
return hdr.Number.Int64(), nil
}
if f.end == rpc.LatestBlockNumber.Int64() || f.end == rpc.PendingBlockNumber.Int64() {
end = head
if f.begin, err = resolveSpecial(f.begin); err != nil {
return nil, err
}
if f.end, err = resolveSpecial(f.end); err != nil {
return nil, err
}
// Gather all indexed logs, and finish with non indexed ones
var (
logs []*types.Log
err error
end = uint64(f.end)
size, sections = f.sys.backend.BloomStatus()
)
if indexed := sections * size; indexed > uint64(f.begin) {
Expand Down
7 changes: 7 additions & 0 deletions eth/filters/filter_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe
return nil, nil
}
num = *number
} else if blockNr == rpc.FinalizedBlockNumber {
hash = rawdb.ReadFinalizedBlockHash(b.db)
number := rawdb.ReadHeaderNumber(b.db, hash)
if number == nil {
return nil, nil
}
num = *number
} else {
num = uint64(blockNr)
hash = rawdb.ReadCanonicalHash(b.db, num)
Expand Down
31 changes: 31 additions & 0 deletions eth/filters/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func TestFilters(t *testing.T) {
rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i])
}

// Set block 998 as Finalized (-3)
rawdb.WriteFinalizedBlockHash(db, chain[998].Hash())

filter := sys.NewRangeFilter(0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})

logs, _ := filter.Logs(context.Background())
Expand Down Expand Up @@ -224,4 +227,32 @@ func TestFilters(t *testing.T) {
if len(logs) != 0 {
t.Error("expected 0 log, got", len(logs))
}

filter = sys.NewRangeFilter(-1, -1, nil, nil)

logs, _ = filter.Logs(context.Background())
if len(logs) != 1 {
t.Error("expected 1 log, got", len(logs))
}

filter = sys.NewRangeFilter(-3, -1, nil, nil)

logs, _ = filter.Logs(context.Background())
if len(logs) != 2 {
t.Error("expected 2 log, got", len(logs))
}

filter = sys.NewRangeFilter(-3, -3, nil, nil)

logs, _ = filter.Logs(context.Background())
if len(logs) != 1 {
t.Error("expected 1 log, got", len(logs))
}

filter = sys.NewRangeFilter(-1, -3, nil, nil)

logs, _ = filter.Logs(context.Background())
if len(logs) != 0 {
t.Error("expected 0 log, got", len(logs))
}
}

0 comments on commit 32c2d48

Please sign in to comment.