From 678fd4462edfc64670e12009fb46472c29600d7d Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Fri, 19 Jul 2024 08:55:01 -0600 Subject: [PATCH] Use default rpcclient config when unmarshalling JSON --- util/rpcclient/rpcclient.go | 7 +++++++ util/rpcclient/rpcclient_test.go | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/util/rpcclient/rpcclient.go b/util/rpcclient/rpcclient.go index e171e6e99b..be5825a28d 100644 --- a/util/rpcclient/rpcclient.go +++ b/util/rpcclient/rpcclient.go @@ -44,6 +44,13 @@ func (c *ClientConfig) Validate() error { return err } +func (c *ClientConfig) UnmarshalJSON(data []byte) error { + // Use DefaultClientConfig for default values when unmarshalling JSON + *c = DefaultClientConfig + type clientConfigWithoutCustomUnmarshal ClientConfig + return json.Unmarshal(data, (*clientConfigWithoutCustomUnmarshal)(c)) +} + type ClientConfigFetcher func() *ClientConfig var TestClientConfig = ClientConfig{ diff --git a/util/rpcclient/rpcclient_test.go b/util/rpcclient/rpcclient_test.go index ae7e4fc226..1a7da54774 100644 --- a/util/rpcclient/rpcclient_test.go +++ b/util/rpcclient/rpcclient_test.go @@ -2,13 +2,17 @@ package rpcclient import ( "context" + "encoding/json" "errors" + "regexp" "sync/atomic" "testing" "time" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/rpc" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/offchainlabs/nitro/util/testhelpers" ) @@ -204,6 +208,21 @@ func TestIsAlreadyKnownError(t *testing.T) { } } +func TestUnmarshalClientConfig(t *testing.T) { + exampleJson := `[{"jwtsecret":"/tmp/nitro-val.jwt","url":"http://127.0.0.10:52000"}, {"jwtsecret":"/tmp/nitro-val.jwt","url":"http://127.0.0.10:52001"}]` + var clientConfigs []ClientConfig + Require(t, json.Unmarshal([]byte(exampleJson), &clientConfigs)) + expectedClientConfigs := []ClientConfig{DefaultClientConfig, DefaultClientConfig} + expectedClientConfigs[0].JWTSecret = "/tmp/nitro-val.jwt" + expectedClientConfigs[0].URL = "http://127.0.0.10:52000" + expectedClientConfigs[1].JWTSecret = "/tmp/nitro-val.jwt" + expectedClientConfigs[1].URL = "http://127.0.0.10:52001" + // Ensure the configs are equivalent to the expected configs, ignoring the retryErrors regexp as cmp can't compare it + if diff := cmp.Diff(expectedClientConfigs, clientConfigs, cmpopts.IgnoreTypes(®exp.Regexp{})); diff != "" { + t.Errorf("unmarshalling example JSON unexpected diff:\n%s", diff) + } +} + func Require(t *testing.T, err error, printables ...interface{}) { t.Helper() testhelpers.RequireImpl(t, err, printables...)