From 07d5cfbf0a5e92a51459a43fe4021ceccae428b3 Mon Sep 17 00:00:00 2001 From: sammy007 Date: Thu, 4 Jun 2020 17:41:31 +0500 Subject: [PATCH 1/2] Serialize negative Bigint as "pending" in ethclient --- core/types/gen_log_json.go | 18 ++++++++---------- core/types/log.go | 4 ++-- ethclient/ethclient.go | 3 +++ ethclient/ethclient_test.go | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/core/types/gen_log_json.go b/core/types/gen_log_json.go index 6e94339478f0..90e1c14d9017 100644 --- a/core/types/gen_log_json.go +++ b/core/types/gen_log_json.go @@ -20,9 +20,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 @@ -46,9 +46,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 @@ -74,17 +74,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 006f62bbf6f6..88274e39dae0 100644 --- a/core/types/log.go +++ b/core/types/log.go @@ -44,11 +44,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 block - 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 a60d73223116..bbe4cb2dd966 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -282,6 +282,9 @@ func toBlockNumArg(number *big.Int) string { if number == nil { return "latest" } + if number.Sign() < 0 { + return "pending" + } return hexutil.EncodeBig(number) } diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 3576d4870e66..518a38788e57 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -97,6 +97,22 @@ func TestToFilterArg(t *testing.T) { }, nil, }, + { + "with negative fromBlock and negative toBlock", + ethereum.FilterQuery{ + Addresses: addresses, + FromBlock: big.NewInt(-1), + ToBlock: big.NewInt(-1), + Topics: [][]common.Hash{}, + }, + map[string]interface{}{ + "address": addresses, + "fromBlock": "pending", + "toBlock": "pending", + "topics": [][]common.Hash{}, + }, + nil, + }, { "with blockhash", ethereum.FilterQuery{ From 856bb171f268162e458cf1698581929e44a00bb0 Mon Sep 17 00:00:00 2001 From: Adam Schmideg Date: Tue, 21 Jul 2020 09:36:21 +0200 Subject: [PATCH 2/2] only index -1 as pending --- ethclient/ethclient.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index bbe4cb2dd966..bc0305fc229d 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -282,7 +282,8 @@ func toBlockNumArg(number *big.Int) string { if number == nil { return "latest" } - if number.Sign() < 0 { + pending := big.NewInt(-1) + if number.Cmp(pending) == 0 { return "pending" } return hexutil.EncodeBig(number)