mobile, ethclient, core/types: enable filtering on pending/latest logs

* wrap rpc.BlockNumber consts for mobile, for use with FilterQuery
* Log - remove 'required' from TxIndex and Index, can be nil for pending
balance
This commit is contained in:
Yossi Rizgan 2017-11-12 15:42:57 +02:00
parent 0131bd6ff9
commit 47ff71bb9b
4 changed files with 29 additions and 12 deletions

View file

@ -10,6 +10,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
) )
var _ = (*logMarshaling)(nil)
func (l Log) MarshalJSON() ([]byte, error) { func (l Log) MarshalJSON() ([]byte, error) {
type Log struct { type Log struct {
Address common.Address `json:"address" gencodec:"required"` Address common.Address `json:"address" gencodec:"required"`
@ -17,9 +19,9 @@ func (l Log) MarshalJSON() ([]byte, error) {
Data hexutil.Bytes `json:"data" gencodec:"required"` Data hexutil.Bytes `json:"data" gencodec:"required"`
BlockNumber hexutil.Uint64 `json:"blockNumber"` BlockNumber hexutil.Uint64 `json:"blockNumber"`
TxHash common.Hash `json:"transactionHash" gencodec:"required"` TxHash common.Hash `json:"transactionHash" gencodec:"required"`
TxIndex hexutil.Uint `json:"transactionIndex" gencodec:"required"` TxIndex hexutil.Uint `json:"transactionIndex"`
BlockHash common.Hash `json:"blockHash"` BlockHash common.Hash `json:"blockHash"`
Index hexutil.Uint `json:"logIndex" gencodec:"required"` Index hexutil.Uint `json:"logIndex"`
Removed bool `json:"removed"` Removed bool `json:"removed"`
} }
var enc Log var enc Log
@ -42,9 +44,9 @@ func (l *Log) UnmarshalJSON(input []byte) error {
Data hexutil.Bytes `json:"data" gencodec:"required"` Data hexutil.Bytes `json:"data" gencodec:"required"`
BlockNumber *hexutil.Uint64 `json:"blockNumber"` BlockNumber *hexutil.Uint64 `json:"blockNumber"`
TxHash *common.Hash `json:"transactionHash" gencodec:"required"` TxHash *common.Hash `json:"transactionHash" gencodec:"required"`
TxIndex *hexutil.Uint `json:"transactionIndex" gencodec:"required"` TxIndex *hexutil.Uint `json:"transactionIndex"`
BlockHash *common.Hash `json:"blockHash"` BlockHash *common.Hash `json:"blockHash"`
Index *hexutil.Uint `json:"logIndex" gencodec:"required"` Index *hexutil.Uint `json:"logIndex"`
Removed *bool `json:"removed"` Removed *bool `json:"removed"`
} }
var dec Log var dec Log
@ -70,17 +72,15 @@ func (l *Log) UnmarshalJSON(input []byte) error {
return errors.New("missing required field 'transactionHash' for Log") return errors.New("missing required field 'transactionHash' for Log")
} }
l.TxHash = *dec.TxHash l.TxHash = *dec.TxHash
if dec.TxIndex == nil { if dec.TxIndex != nil {
return errors.New("missing required field 'transactionIndex' for Log") l.TxIndex = uint(*dec.TxIndex)
} }
l.TxIndex = uint(*dec.TxIndex)
if dec.BlockHash != nil { if dec.BlockHash != nil {
l.BlockHash = *dec.BlockHash l.BlockHash = *dec.BlockHash
} }
if dec.Index == nil { if dec.Index != nil {
return errors.New("missing required field 'logIndex' for Log") l.Index = uint(*dec.Index)
} }
l.Index = uint(*dec.Index)
if dec.Removed != nil { if dec.Removed != nil {
l.Removed = *dec.Removed l.Removed = *dec.Removed
} }

View file

@ -45,11 +45,11 @@ type Log struct {
// hash of the transaction // hash of the transaction
TxHash common.Hash `json:"transactionHash" gencodec:"required"` TxHash common.Hash `json:"transactionHash" gencodec:"required"`
// index of the transaction in the block // index of the transaction in the block
TxIndex uint `json:"transactionIndex" gencodec:"required"` TxIndex uint `json:"transactionIndex"`
// hash of the block in which the transaction was included // hash of the block in which the transaction was included
BlockHash common.Hash `json:"blockHash"` BlockHash common.Hash `json:"blockHash"`
// index of the log in the receipt // index of the log in the receipt
Index uint `json:"logIndex" gencodec:"required"` Index uint `json:"logIndex"`
// The Removed field is true if this log was reverted due to a chain reorganisation. // The Removed field is true if this log was reverted due to a chain reorganisation.
// You must pay attention to this field if you receive logs through a filter query. // You must pay attention to this field if you receive logs through a filter query.

View file

@ -257,6 +257,14 @@ func toBlockNumArg(number *big.Int) string {
if number == nil { if number == nil {
return "latest" return "latest"
} }
switch number.Int64() {
case rpc.PendingBlockNumber.Int64():
return "pending"
case rpc.LatestBlockNumber.Int64():
return "latest"
case rpc.EarliestBlockNumber.Int64():
return "earliest"
}
return hexutil.EncodeBig(number) return hexutil.EncodeBig(number)
} }

View file

@ -23,6 +23,15 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
)
const (
PendingBlockNumber = int(rpc.PendingBlockNumber)
LatestBlockNumber = int(rpc.LatestBlockNumber)
EarliestBlockNumber = int(rpc.EarliestBlockNumber)
) )
// EthereumClient provides access to the Ethereum APIs. // EthereumClient provides access to the Ethereum APIs.