Repurpose fromBlock/toBlock RPC parameters, update web3.js

This commit is contained in:
Nick Johnson 2018-09-24 17:45:25 +01:00
parent 6d019710ef
commit b68c3ddfef
4 changed files with 90 additions and 69 deletions

View file

@ -22,6 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"strings"
"sync" "sync"
"time" "time"
@ -475,13 +476,11 @@ func returnLogs(logs []*types.Log) []*types.Log {
// UnmarshalJSON sets *args fields with given data. // UnmarshalJSON sets *args fields with given data.
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 *rpc.BlockNumber `json:"fromBlock"` FromBlock *string `json:"fromBlock"`
FromBlockHash *common.Hash `json:"fromBlockHash"` ToBlock *string `json:"toBlock"`
ToBlock *rpc.BlockNumber `json:"toBlock"` Addresses interface{} `json:"address"`
ToBlockHash *common.Hash `json:"toBlockHash"` Topics []interface{} `json:"topics"`
Addresses interface{} `json:"address"`
Topics []interface{} `json:"topics"`
} }
var raw input var raw input
@ -497,16 +496,20 @@ 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 {
args.FromBlock = big.NewInt(raw.FromBlock.Int64()) if strings.HasPrefix(*raw.FromBlock, "0x") {
} hash := common.HexToHash(*raw.FromBlock)
if raw.FromBlockHash != nil { args.FromBlockHash = &hash
args.FromBlockHash = raw.FromBlockHash } else {
args.FromBlock.UnmarshalJSON([]byte(*raw.FromBlock))
}
} }
if raw.ToBlock != nil { if raw.ToBlock != nil {
args.ToBlock = big.NewInt(raw.ToBlock.Int64()) if strings.HasPrefix(*raw.ToBlock, "0x") {
} hash := common.HexToHash(*raw.ToBlock)
if raw.ToBlockHash != nil { args.ToBlockHash = &hash
args.ToBlockHash = raw.ToBlockHash } else {
args.ToBlock.UnmarshalJSON([]byte(*raw.ToBlock))
}
} }
} }

View file

@ -200,6 +200,10 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
} }
} }
if end.Number.Cmp(begin.Number) < 0 {
return nil, nil
}
ancestor, mainChain, err := f.findCommonAncestor(ctx, begin, end) ancestor, mainChain, err := f.findCommonAncestor(ctx, begin, end)
if err != nil { if err != nil {
return nil, err return nil, err
@ -287,7 +291,7 @@ func (f *Filter) indexedLogs(ctx context.Context, begin, end uint64) ([]*types.L
func (f *Filter) unindexedLogs(ctx context.Context, begin, end common.Hash) ([]*types.Log, error) { func (f *Filter) unindexedLogs(ctx context.Context, begin, end common.Hash) ([]*types.Log, error) {
var logs []*types.Log var logs []*types.Log
for begin != end { for {
header, err := f.backend.HeaderByHash(ctx, end) header, err := f.backend.HeaderByHash(ctx, end)
if header == nil || err != nil { if header == nil || err != nil {
return logs, err return logs, err
@ -297,6 +301,9 @@ func (f *Filter) unindexedLogs(ctx context.Context, begin, end common.Hash) ([]*
return logs, err return logs, err
} }
logs = append(logs, found...) logs = append(logs, found...)
if begin == end {
break
}
end = header.ParentHash end = header.ParentHash
} }
return logs, nil return logs, nil

File diff suppressed because one or more lines are too long

View file

@ -5431,6 +5431,12 @@ var methods = function () {
params: 0 params: 0
}); });
var getLogs = new Method({
name: 'getLogs',
call: 'eth_getLogs',
params: 1
})
return [ return [
getBalance, getBalance,
getStorageAt, getStorageAt,
@ -5454,7 +5460,8 @@ var methods = function () {
compileLLL, compileLLL,
compileSerpent, compileSerpent,
submitWork, submitWork,
getWork getWork,
getLogs
]; ];
}; };