Fix parsing of integer block numbers

This commit is contained in:
Nick Johnson 2018-09-25 12:58:31 +01:00
parent b68c3ddfef
commit 18090ea4ed
3 changed files with 41 additions and 18 deletions

View file

@ -22,7 +22,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"strings"
"sync" "sync"
"time" "time"
@ -477,8 +476,8 @@ func returnLogs(logs []*types.Log) []*types.Log {
func (args *FilterCriteria) UnmarshalJSON(data []byte) error { func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
type input struct { type input struct {
BlockHash *common.Hash `json:"blockHash"` BlockHash *common.Hash `json:"blockHash"`
FromBlock *string `json:"fromBlock"` FromBlock *rpc.BlockNumberOrHash `json:"fromBlock"`
ToBlock *string `json:"toBlock"` ToBlock *rpc.BlockNumberOrHash `json:"toBlock"`
Addresses interface{} `json:"address"` Addresses interface{} `json:"address"`
Topics []interface{} `json:"topics"` Topics []interface{} `json:"topics"`
} }
@ -496,19 +495,17 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
args.BlockHash = raw.BlockHash args.BlockHash = raw.BlockHash
} else { } else {
if raw.FromBlock != nil { if raw.FromBlock != nil {
if strings.HasPrefix(*raw.FromBlock, "0x") { if raw.FromBlock.IsHash() {
hash := common.HexToHash(*raw.FromBlock) args.FromBlockHash = raw.FromBlock.Hash()
args.FromBlockHash = &hash
} else { } else {
args.FromBlock.UnmarshalJSON([]byte(*raw.FromBlock)) args.FromBlock = big.NewInt(int64(raw.FromBlock.Number()))
} }
} }
if raw.ToBlock != nil { if raw.ToBlock != nil {
if strings.HasPrefix(*raw.ToBlock, "0x") { if raw.ToBlock.IsHash() {
hash := common.HexToHash(*raw.ToBlock) args.ToBlockHash = raw.ToBlock.Hash()
args.ToBlockHash = &hash
} else { } else {
args.ToBlock.UnmarshalJSON([]byte(*raw.ToBlock)) args.ToBlock = big.NewInt(int64(raw.ToBlock.Number()))
} }
} }
} }

View file

@ -223,18 +223,18 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
// Gather all indexed logs, and finish with non indexed ones // Gather all indexed logs, and finish with non indexed ones
if mainChain { if mainChain {
size, sections := f.backend.BloomStatus() 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() { 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 { } else {
logs, err = f.indexedLogs(ctx, begin.Number.Uint64(), indexed-1) logs, err = f.indexedLogs(ctx, ancestor.Number.Uint64(), indexed-1)
} }
if err != nil { if err != nil {
return logs, err 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...) logs = append(logs, rest...)
sort.Sort(logList(logs)) sort.Sort(logList(logs))

View file

@ -24,6 +24,7 @@ import (
"sync" "sync"
mapset "github.com/deckarep/golang-set" mapset "github.com/deckarep/golang-set"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
) )
@ -163,3 +164,28 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
func (bn BlockNumber) Int64() int64 { func (bn BlockNumber) Int64() int64 {
return (int64)(bn) 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()
}