Skip to content

Commit 1d96752

Browse files
authored
refactor: refactor network url transformer (#259)
This refactor changes the URL Transform Load Option to accept a simple transform function, and moves the logic of which URLs to transform to the config itself. This simplifies the caller's API and avoids exposing the internal config structure to the caller.
1 parent f8876aa commit 1d96752

File tree

3 files changed

+95
-36
lines changed

3 files changed

+95
-36
lines changed

.changeset/cute-tigers-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"chainlink-deployments-framework": minor
3+
---
4+
5+
[BREAKING] The Load function of Network Config has been changed to simplify the URL transformation option

engine/cld/config/network/config.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,32 @@ func ChainFamilyFilter(chainFamily string) NetworkFilter {
134134
}
135135
}
136136

137+
// transformRPCURLs transforms the RPC URLs of the networks in the config.
138+
func (c *Config) transformRPCURLs(transform URLTransformer) {
139+
for k, n := range c.networks {
140+
// Transform the RPC URLS
141+
for i, rpc := range n.RPCs {
142+
rpc.HTTPURL = transform(rpc.HTTPURL)
143+
rpc.WSURL = transform(rpc.WSURL)
144+
145+
n.RPCs[i] = rpc
146+
}
147+
148+
// Transform EVM Metadata URLs
149+
md, err := DecodeMetadata[EVMMetadata](n.Metadata)
150+
if err != nil {
151+
continue // skip this network since metadata cannot be decoded
152+
}
153+
154+
md.AnvilConfig.ArchiveHTTPURL = transform(md.AnvilConfig.ArchiveHTTPURL)
155+
n.Metadata = md
156+
157+
// Update the network with the modifications. We need to do this the network is a value
158+
// type, so we need to update the map with the new network.
159+
c.networks[k] = n
160+
}
161+
}
162+
137163
// Load loads configuration from the specified file paths, and merges them into a single Config.
138164
//
139165
// It accepts load options to customize the loading behavior.
@@ -162,8 +188,8 @@ func Load(filePaths []string, opts ...LoadOption) (*Config, error) {
162188
}
163189

164190
// Apply the URL transformer if one is provided.
165-
if loadCfg.URLTransformer != nil {
166-
loadCfg.URLTransformer(cfg)
191+
if loadCfg.RPCURLTransformer != nil {
192+
cfg.transformRPCURLs(loadCfg.RPCURLTransformer)
167193
}
168194

169195
return cfg, nil
@@ -172,17 +198,18 @@ func Load(filePaths []string, opts ...LoadOption) (*Config, error) {
172198
// LoadOption defines a function that modifies the load configuration.
173199
type LoadOption func(*loadConfig)
174200

175-
// URLTransformer is a function that transforms the URLs in the Config.
176-
type URLTransformer func(*Config)
177-
178201
// loadConfig holds the configuration for loading the config.
179202
type loadConfig struct {
180-
URLTransformer URLTransformer
203+
RPCURLTransformer URLTransformer
181204
}
182205

183-
// WithURLTransform allows setting a custom URL transformer for the config.
184-
func WithURLTransform(t URLTransformer) func(opts *loadConfig) {
206+
// URLTransformer is a function that transforms a URL.
207+
type URLTransformer func(string) string
208+
209+
// WithRPCURLTransformer is a load option that transforms the RPC URLs of the networks in the
210+
// config.
211+
func WithRPCURLTransformer(t URLTransformer) LoadOption {
185212
return func(opts *loadConfig) {
186-
opts.URLTransformer = t
213+
opts.RPCURLTransformer = t
187214
}
188215
}

engine/cld/config/network/config_test.go

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -686,43 +686,70 @@ networks:
686686
- type: "mainnet"
687687
chain_selector: 1
688688
rpcs:
689-
- rpc_name: "test_rpc"
690-
preferred_url_scheme: "http"
691-
http_url: "https://test.rpc"
692-
ws_url: "wss://test.rpc"`
689+
- rpc_name: "test_rpc"
690+
preferred_url_scheme: "http"
691+
http_url: "https://test.rpc"
692+
ws_url: "wss://test.rpc"
693+
metadata:
694+
anvil_config:
695+
archive_http_url: "https://test.archive.rpc"
696+
`
693697

694698
tmpFile := filepath.Join(tmpDir, "test.yaml")
695699
err := os.WriteFile(tmpFile, []byte(yamlContent), 0600)
696700
require.NoError(t, err, "Failed to create test file")
697701

698-
// Test URL transformer
699-
transformer := func(cfg *Config) {
700-
for _, network := range cfg.Networks() {
701-
for j, rpc := range network.RPCs {
702-
rpc.HTTPURL = strings.Replace(rpc.HTTPURL, "test", "test2", 1)
703-
rpc.WSURL = strings.Replace(rpc.WSURL, "test", "test2", 1)
704-
network.RPCs[j] = rpc
705-
}
706-
}
702+
// Simple URL transformer
703+
transformFunc := func(url string) string {
704+
return strings.Replace(url, "test", "test2", 1)
707705
}
708706

709-
got, err := Load([]string{tmpFile}, WithURLTransform(transformer))
710-
require.NoError(t, err, "Load() should not return an error")
711-
712-
want := NewConfig([]Network{
707+
tests := []struct {
708+
name string
709+
givePath string
710+
giveURLTransformer URLTransformer
711+
want *Config
712+
wantErr string
713+
}{
713714
{
714-
Type: "mainnet",
715-
ChainSelector: 1,
716-
RPCs: []RPC{
715+
name: "transform RPC URLs",
716+
givePath: tmpFile,
717+
giveURLTransformer: transformFunc,
718+
want: NewConfig([]Network{
717719
{
718-
RPCName: "test_rpc",
719-
PreferredURLScheme: "http",
720-
HTTPURL: "https://test2.rpc",
721-
WSURL: "wss://test2.rpc",
720+
Type: "mainnet",
721+
ChainSelector: 1,
722+
RPCs: []RPC{
723+
{
724+
RPCName: "test_rpc",
725+
PreferredURLScheme: "http",
726+
HTTPURL: "https://test2.rpc",
727+
WSURL: "wss://test2.rpc",
728+
},
729+
},
730+
Metadata: EVMMetadata{
731+
AnvilConfig: &AnvilConfig{
732+
ArchiveHTTPURL: "https://test2.archive.rpc",
733+
},
734+
},
722735
},
723-
},
736+
}),
724737
},
725-
})
738+
}
739+
740+
for _, tt := range tests {
741+
t.Run(tt.name, func(t *testing.T) {
742+
t.Parallel()
743+
744+
got, err := Load([]string{tt.givePath}, WithRPCURLTransformer(tt.giveURLTransformer))
745+
require.NoError(t, err)
726746

727-
assert.Equal(t, want, got)
747+
if tt.wantErr != "" {
748+
require.Error(t, err)
749+
} else {
750+
require.NoError(t, err)
751+
assert.Equal(t, tt.want, got)
752+
}
753+
})
754+
}
728755
}

0 commit comments

Comments
 (0)