From 47ff71bb9bebf348b1aebb1e841937cffcaf804f Mon Sep 17 00:00:00 2001 From: Yossi Rizgan Date: Sun, 12 Nov 2017 15:42:57 +0200 Subject: [PATCH] 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 --- core/types/gen_log_json.go | 20 ++++++++++---------- core/types/log.go | 4 ++-- ethclient/ethclient.go | 8 ++++++++ mobile/ethclient.go | 9 +++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/core/types/gen_log_json.go b/core/types/gen_log_json.go index 92c699c2a7..da29fbee85 100644 --- a/core/types/gen_log_json.go +++ b/core/types/gen_log_json.go @@ -10,6 +10,8 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" ) +var _ = (*logMarshaling)(nil) + func (l Log) MarshalJSON() ([]byte, error) { type Log struct { Address common.Address `json:"address" gencodec:"required"` @@ -17,9 +19,9 @@ func (l Log) MarshalJSON() ([]byte, error) { Data hexutil.Bytes `json:"data" gencodec:"required"` BlockNumber hexutil.Uint64 `json:"blockNumber"` TxHash common.Hash `json:"transactionHash" gencodec:"required"` - TxIndex hexutil.Uint `json:"transactionIndex" gencodec:"required"` + TxIndex hexutil.Uint `json:"transactionIndex"` BlockHash common.Hash `json:"blockHash"` - Index hexutil.Uint `json:"logIndex" gencodec:"required"` + Index hexutil.Uint `json:"logIndex"` Removed bool `json:"removed"` } var enc Log @@ -42,9 +44,9 @@ func (l *Log) UnmarshalJSON(input []byte) error { Data hexutil.Bytes `json:"data" gencodec:"required"` BlockNumber *hexutil.Uint64 `json:"blockNumber"` TxHash *common.Hash `json:"transactionHash" gencodec:"required"` - TxIndex *hexutil.Uint `json:"transactionIndex" gencodec:"required"` + TxIndex *hexutil.Uint `json:"transactionIndex"` BlockHash *common.Hash `json:"blockHash"` - Index *hexutil.Uint `json:"logIndex" gencodec:"required"` + Index *hexutil.Uint `json:"logIndex"` Removed *bool `json:"removed"` } var dec Log @@ -70,17 +72,15 @@ func (l *Log) UnmarshalJSON(input []byte) error { return errors.New("missing required field 'transactionHash' for Log") } l.TxHash = *dec.TxHash - if dec.TxIndex == nil { - return errors.New("missing required field 'transactionIndex' for Log") + if dec.TxIndex != nil { + l.TxIndex = uint(*dec.TxIndex) } - l.TxIndex = uint(*dec.TxIndex) if dec.BlockHash != nil { l.BlockHash = *dec.BlockHash } - if dec.Index == nil { - return errors.New("missing required field 'logIndex' for Log") + if dec.Index != nil { + l.Index = uint(*dec.Index) } - l.Index = uint(*dec.Index) if dec.Removed != nil { l.Removed = *dec.Removed } diff --git a/core/types/log.go b/core/types/log.go index be5de38da7..daa01ec8c2 100644 --- a/core/types/log.go +++ b/core/types/log.go @@ -45,11 +45,11 @@ type Log struct { // hash of the transaction TxHash common.Hash `json:"transactionHash" gencodec:"required"` // 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 BlockHash common.Hash `json:"blockHash"` // 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. // You must pay attention to this field if you receive logs through a filter query. diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 7f73ab113c..cb5f7c4675 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -257,6 +257,14 @@ func toBlockNumArg(number *big.Int) string { if number == nil { 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) } diff --git a/mobile/ethclient.go b/mobile/ethclient.go index 7f31a89989..28e7984900 100644 --- a/mobile/ethclient.go +++ b/mobile/ethclient.go @@ -23,6 +23,15 @@ import ( "github.com/ethereum/go-ethereum/core/types" "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.