mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-19 19:30:44 +00:00
ethclient: omit empty address filter field in toFilterArg
Previously, an empty (non-nil) []common.Address{} slice caused the
"address" field to be serialized as an empty JSON array [] in the
RPC request. Some Ethereum-compatible nodes (e.g. Hedera) reject this
empty array with an error.
According to the JSON-RPC specification the "address" field is
optional. Align with the spec by only including the field when at
least one address is present, treating []Address{} the same as nil.
Add a test case "with empty addresses slice" to TestToFilterArg
to prevent regression.
This commit is contained in:
parent
82fad31540
commit
c638faaf34
2 changed files with 21 additions and 1 deletions
|
|
@ -498,7 +498,11 @@ func (ec *Client) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuer
|
||||||
|
|
||||||
func toFilterArg(q ethereum.FilterQuery) (interface{}, error) {
|
func toFilterArg(q ethereum.FilterQuery) (interface{}, error) {
|
||||||
arg := map[string]interface{}{}
|
arg := map[string]interface{}{}
|
||||||
if q.Addresses != nil {
|
// Only include "address" when there are actual address filters.
|
||||||
|
// An empty slice is treated the same as nil (no filter), and omitting
|
||||||
|
// the field avoids sending "address":[] to nodes that reject empty arrays
|
||||||
|
// (e.g. Hedera, some non-Geth implementations).
|
||||||
|
if len(q.Addresses) > 0 {
|
||||||
arg["address"] = q.Addresses
|
arg["address"] = q.Addresses
|
||||||
}
|
}
|
||||||
if q.Topics != nil {
|
if q.Topics != nil {
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,22 @@ func TestToFilterArg(t *testing.T) {
|
||||||
},
|
},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
// empty Addresses slice must be treated same as nil:
|
||||||
|
// the "address" field must be omitted so that non-Geth nodes
|
||||||
|
// (e.g. Hedera) do not reject the request with an error.
|
||||||
|
"with empty addresses slice",
|
||||||
|
ethereum.FilterQuery{
|
||||||
|
Addresses: []common.Address{},
|
||||||
|
FromBlock: big.NewInt(1),
|
||||||
|
ToBlock: big.NewInt(2),
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"fromBlock": "0x1",
|
||||||
|
"toBlock": "0x2",
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"without BlockHash",
|
"without BlockHash",
|
||||||
ethereum.FilterQuery{
|
ethereum.FilterQuery{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue