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

Use default rpcclient config when unmarshalling JSON #2508

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions util/rpcclient/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
19 changes: 19 additions & 0 deletions util/rpcclient/rpcclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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(&regexp.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...)
Expand Down
Loading