diff --git a/eth/filters/api.go b/eth/filters/api.go index 945b99f484..61eddcc662 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "math/big" - "strings" "sync" "time" @@ -476,11 +475,11 @@ func returnLogs(logs []*types.Log) []*types.Log { // UnmarshalJSON sets *args fields with given data. func (args *FilterCriteria) UnmarshalJSON(data []byte) error { type input struct { - BlockHash *common.Hash `json:"blockHash"` - FromBlock *string `json:"fromBlock"` - ToBlock *string `json:"toBlock"` - Addresses interface{} `json:"address"` - Topics []interface{} `json:"topics"` + BlockHash *common.Hash `json:"blockHash"` + FromBlock *rpc.BlockNumberOrHash `json:"fromBlock"` + ToBlock *rpc.BlockNumberOrHash `json:"toBlock"` + Addresses interface{} `json:"address"` + Topics []interface{} `json:"topics"` } var raw input @@ -496,19 +495,17 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error { args.BlockHash = raw.BlockHash } else { if raw.FromBlock != nil { - if strings.HasPrefix(*raw.FromBlock, "0x") { - hash := common.HexToHash(*raw.FromBlock) - args.FromBlockHash = &hash + if raw.FromBlock.IsHash() { + args.FromBlockHash = raw.FromBlock.Hash() } else { - args.FromBlock.UnmarshalJSON([]byte(*raw.FromBlock)) + args.FromBlock = big.NewInt(int64(raw.FromBlock.Number())) } } if raw.ToBlock != nil { - if strings.HasPrefix(*raw.ToBlock, "0x") { - hash := common.HexToHash(*raw.ToBlock) - args.ToBlockHash = &hash + if raw.ToBlock.IsHash() { + args.ToBlockHash = raw.ToBlock.Hash() } else { - args.ToBlock.UnmarshalJSON([]byte(*raw.ToBlock)) + args.ToBlock = big.NewInt(int64(raw.ToBlock.Number())) } } } diff --git a/eth/filters/filter.go b/eth/filters/filter.go index b114ba9ad0..b2aad2e41e 100644 --- a/eth/filters/filter.go +++ b/eth/filters/filter.go @@ -223,18 +223,18 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) { // Gather all indexed logs, and finish with non indexed ones if mainChain { size, sections := f.backend.BloomStatus() - if indexed := sections * size; indexed > begin.Number.Uint64() { + if indexed := sections * size; indexed > ancestor.Number.Uint64() { if indexed > end.Number.Uint64() { - logs, err = f.indexedLogs(ctx, begin.Number.Uint64(), end.Number.Uint64()) + logs, err = f.indexedLogs(ctx, ancestor.Number.Uint64(), end.Number.Uint64()) } else { - logs, err = f.indexedLogs(ctx, begin.Number.Uint64(), indexed-1) + logs, err = f.indexedLogs(ctx, ancestor.Number.Uint64(), indexed-1) } if err != nil { return logs, err } } } - rest, err := f.unindexedLogs(ctx, begin.Hash(), end.Hash()) + rest, err := f.unindexedLogs(ctx, ancestor.Hash(), end.Hash()) logs = append(logs, rest...) sort.Sort(logList(logs)) diff --git a/rpc/types.go b/rpc/types.go index 4252c36027..0b09763f1d 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -24,6 +24,7 @@ import ( "sync" mapset "github.com/deckarep/golang-set" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ) @@ -163,3 +164,28 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error { func (bn BlockNumber) Int64() int64 { return (int64)(bn) } + +type BlockNumberOrHash string + +func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error { + *bnh = BlockNumberOrHash(data) + return nil +} + +func (bnh BlockNumberOrHash) IsHash() bool { + return bnh[0] == '"' && bnh[len(bnh)-1] == '"' && len(bnh) == 44 +} + +func (bnh BlockNumberOrHash) Hash() *common.Hash { + if !bnh.IsHash() { + return nil + } + hash := common.HexToHash(string(bnh[1 : len(bnh)-1])) + return &hash +} + +func (bnh BlockNumberOrHash) Number() int64 { + var bn BlockNumber + (&bn).UnmarshalJSON([]byte(bnh)) + return bn.Int64() +}