Skip to content

Commit

Permalink
Merge pull request #2508 from OffchainLabs/default-rpc-client-config
Browse files Browse the repository at this point in the history
Use default rpcclient config when unmarshalling JSON
  • Loading branch information
PlasmaPower authored Jul 19, 2024
2 parents 16434a2 + 678fd44 commit 656898d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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

0 comments on commit 656898d

Please sign in to comment.