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"
"fmt"
"math/big"
"strings"
"sync"
"time"
@ -477,8 +476,8 @@ func returnLogs(logs []*types.Log) []*types.Log {
func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
type input struct {
BlockHash *common.Hash `json:"blockHash"`
FromBlock *string `json:"fromBlock"`
ToBlock *string `json:"toBlock"`
FromBlock *rpc.BlockNumberOrHash `json:"fromBlock"`
ToBlock *rpc.BlockNumberOrHash `json:"toBlock"`
Addresses interface{} `json:"address"`
Topics []interface{} `json:"topics"`
}
@ -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()))
}
}
}

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
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))

View file

@ -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()
}