Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc.Client.CallContext incorrectly unmarshalling null results #26700

Closed
JasonYuan869 opened this issue Feb 15, 2023 · 1 comment
Closed

rpc.Client.CallContext incorrectly unmarshalling null results #26700

JasonYuan869 opened this issue Feb 15, 2023 · 1 comment
Labels

Comments

@JasonYuan869
Copy link
Contributor

System information

Geth version: v1.10.26 (but also in master branch)

Expected behaviour

When {"result": null} is returned from a JSON RPC call in rpc.CallContext, it should unmarshal into a json.RawMessage as json.RawMessage("null").

Actual behaviour

It actually gets unmarshalled as nil.

Steps to reproduce the behaviour

  1. Create a local blockchain private network and expose it with --http or --ws
  2. Set RPC_ENDPOINT environment variable to the URL of the network
  3. The following code attempts to retrieve a transaction receipt for an invalid hash, which should set result to json.RawMessage("null")
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/ethereum/go-ethereum/rpc"
)

func main() {
	client, err := rpc.Dial(os.Getenv("RPC_ENDPOINT"))
	if err != nil {
		panic(err)
	}
	defer client.Close()

	var result json.RawMessage
	invalidHash := "0x1234567890123456789012345678901234567890123456789012345678901234"

	err = client.Call(&result, "eth_getTransactionReceipt", invalidHash)
	if err != nil {
		panic(err)
	}

	if result == nil {
		panic("result should not be nil")
	} else {
		fmt.Printf("result: %s", result)
	}
}
  1. The go code panics since result is nil
  2. Observe that running curl against the endpoint with the same RPC call properly returns the JSON field "result": null.

Fix

The error seems to be caused by a redundant reference operator in rpc.Client.CallContext(), when the JSON unmarshaller is called.

return json.Unmarshal(resp.Result, &result)

Line 321 already checks that result is a pointer, so there shouldn’t be any need to reference it again.

go-ethereum/rpc/client.go

Lines 321 to 323 in dbd6c13

if result != nil && reflect.TypeOf(result).Kind() != reflect.Ptr {
return fmt.Errorf("call result parameter must be pointer or nil interface: %v", result)
}

Upon further investigation, it seems that nested pointers will be set to nil specifically when decoding null values (https://go.dev/src/encoding/json/decode.go#L424).

@JasonYuan869
Copy link
Contributor Author

Closed in #26723

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant