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"
"fmt"
"math/big"
"strings"
"sync"
"time"
@ -475,13 +476,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 *rpc.BlockNumber `json:"fromBlock"`
FromBlockHash *common.Hash `json:"fromBlockHash"`
ToBlock *rpc.BlockNumber `json:"toBlock"`
ToBlockHash *common.Hash `json:"toBlockHash"`
Addresses interface{} `json:"address"`
Topics []interface{} `json:"topics"`
BlockHash *common.Hash `json:"blockHash"`
FromBlock *string `json:"fromBlock"`
ToBlock *string `json:"toBlock"`
Addresses interface{} `json:"address"`
Topics []interface{} `json:"topics"`
}
var raw input
@ -497,16 +496,20 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
args.BlockHash = raw.BlockHash
} else {
if raw.FromBlock != nil {
args.FromBlock = big.NewInt(raw.FromBlock.Int64())
}
if raw.FromBlockHash != nil {
args.FromBlockHash = raw.FromBlockHash
if strings.HasPrefix(*raw.FromBlock, "0x") {
hash := common.HexToHash(*raw.FromBlock)
args.FromBlockHash = &hash
} else {
args.FromBlock.UnmarshalJSON([]byte(*raw.FromBlock))
}
}
if raw.ToBlock != nil {
args.ToBlock = big.NewInt(raw.ToBlock.Int64())
}
if raw.ToBlockHash != nil {
args.ToBlockHash = raw.ToBlockHash
if strings.HasPrefix(*raw.ToBlock, "0x") {
hash := common.HexToHash(*raw.ToBlock)
args.ToBlockHash = &hash
} 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)
if err != nil {
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) {
var logs []*types.Log
for begin != end {
for {
header, err := f.backend.HeaderByHash(ctx, end)
if header == nil || err != nil {
return logs, err
@ -297,6 +301,9 @@ func (f *Filter) unindexedLogs(ctx context.Context, begin, end common.Hash) ([]*
return logs, err
}
logs = append(logs, found...)
if begin == end {
break
}
end = header.ParentHash
}
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
});
var getLogs = new Method({
name: 'getLogs',
call: 'eth_getLogs',
params: 1
})
return [
getBalance,
getStorageAt,
@ -5454,7 +5460,8 @@ var methods = function () {
compileLLL,
compileSerpent,
submitWork,
getWork
getWork,
getLogs
];
};