Skip to content

eth: fix the block range assignment for log filter #17284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,13 @@ func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([
filter = NewBlockFilter(api.backend, *crit.BlockHash, crit.Addresses, crit.Topics)
} else {
// Convert the RPC block numbers into internal representations
var (
begin int64
end int64
)
if crit.FromBlock == nil {
begin = int64(rpc.LatestBlockNumber)
begin := rpc.LatestBlockNumber.Int64()
Copy link
Member

@ligi ligi Jul 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: seems a bit wasteful to first get the value and then don't use it in some cases. How about if crit.FromBlock != nil begin = crit.FromBlock.Int64() else begin = rpc.LatestBlockNumber.Int64()

SCNR: would be even nicer in a language that supports this: begin = (if (crit.FromBlock != nil) crit.FromBlock else rpc.LatestBlockNumber).Int64() or even begin = (crit.FromBlock?:rpc.LatestBlockNumber).Int64

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LatestBlockNumber is just a big.Int constant, so the cost of this op is a cast from big.Int to int64. The more optimal version isn't really worth the more complex code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think the code is more complex this way. I think this way it is more expressive and tells the developer what it is doing instantly - saving a few cycles in the process. But might also just be my language bias as I would have this in mind but it is not possible in go AFAIK: begin = (crit.FromBlock?:rpc.LatestBlockNumber).Int64
As I prefixed - just a nit & just something that went through my mind when reading the code - no real need to change something.

if crit.FromBlock != nil {
begin = crit.FromBlock.Int64()
}
if crit.ToBlock == nil {
end = int64(rpc.LatestBlockNumber)
end := rpc.LatestBlockNumber.Int64()
if crit.ToBlock != nil {
end = crit.ToBlock.Int64()
}
// Construct the range filter
filter = NewRangeFilter(api.backend, begin, end, crit.Addresses, crit.Topics)
Expand Down