refactor!: refactor LoadOffchainClient arguments#437
Conversation
🦋 Changeset detectedLatest commit: b238a07 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| } | ||
|
|
||
| return offchain.LoadOffchainClient(ctx, domain, envKey, cfg, lggr, true) | ||
| return offchain.LoadOffchainClient(ctx, domain, cfg.Offchain.JobDistributor, |
There was a problem hiding this comment.
We do not allow skipping initializing JD here, otherwise the JD commands wouldn't work
| @@ -1,4 +1,4 @@ | |||
| package internal | |||
| package credentials | |||
4de36f2 to
cd4b723
Compare
| } | ||
|
|
||
| if endpoints.WSRPC == "" || endpoints.GRPC == "" { | ||
| return nil, ErrEndpointsRequired |
There was a problem hiding this comment.
Instead of skipping as previously implemented, we return a sentinel error to let the caller decide how they wish to proceed. There are valid cases where you may want to fail entirely instead of skipping the client load. (See the JD commands case)
| ) | ||
|
|
||
| if domain.Key() == enginedomain.MustGetDomain("keystone").Key() && endpoints.WSRPC == "" { | ||
| // TODO: Remove this domain specific check |
There was a problem hiding this comment.
We need to check if this legacy code is still actually needed and remove it in a future PR. This would allow us to clean up the function arguments even more.
| if loadCfg.tokenSource != nil { | ||
| oauth = loadCfg.tokenSource // Used for injecting a mock token source for testing. | ||
| } else { |
There was a problem hiding this comment.
I don't love this because it modifies the code to support testing, but not sure if there is a better way to handle these in unit tests
| } | ||
|
|
||
| // withOffchainProvider sets the offchain provider for the offchain client. Used for testing. | ||
| func withOffchainProvider(provider foffchain.Provider) LoadOffchainClientOpt { |
There was a problem hiding this comment.
Private functional option because it is only used for testing
There was a problem hiding this comment.
should this be in the go doc comment itself? Else in future when someone else reads it, they will ask why is this unexported?
| func withOffchainProvider(provider foffchain.Provider) LoadOffchainClientOpt { | ||
| return func(c *loadConfig) { | ||
| c.provider = provider | ||
| } | ||
| } | ||
|
|
||
| // withTokenSource sets the oauth2 token source for the offchain client. Used for testing. | ||
| func withTokenSource(tokenSource oauth2.TokenSource) LoadOffchainClientOpt { |
There was a problem hiding this comment.
Private functional option because it is only used for testing
|
|
||
| oauth = source | ||
| } | ||
| creds := credentials.GetCredsForEnv(env) |
There was a problem hiding this comment.
This is now provided as a functional option. The caller can decide which creds they want to use.
| if tt.expectPanic { | ||
| assert.Panics(t, func() { | ||
| _, err := LoadFork(t.Context(), tt.domain, tt.env, tt.blockNumbers, tt.options...) | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| return | ||
| } | ||
|
|
There was a problem hiding this comment.
This was panicking because JD was initialized with nil and accessing it caused a panic.
Since we return an error instead we test for the error instead
cd4b723 to
d0d8004
Compare
| LoadOffchainClient(ctx, domain, "testnet", config, logger, false) | ||
|
|
||
| // New | ||
| LoadOffchainClient(ctx, domain, config.Offchain.JobDistributor, | ||
| WithLogger(logger), | ||
| WithDryRun(true), // Note: inverted! |
| // When true, the offchain client will perform read operations but no write operations. | ||
| func WithDryRun(dryRun bool) LoadOffchainClientOpt { | ||
| return func(c *loadConfig) { | ||
| c.dryRun = dryRun | ||
| } | ||
| } |
There was a problem hiding this comment.
i wonder if having the option alone should set it to true instead of accepting a boolean flag? Seems cleaner?
WithDryRun() // this means we want dry run
we never will use the WithDryRun(false) anyway since it is false by default
There was a problem hiding this comment.
I used the boolean flag here because it made the interface cleaner and I didn't have to collect options in it's own slice.
If I removed it the argument, the caller would have to do
opts := []Opt
if x == true {
opts = append(opts, WithDryRun())
}Instead with the boolean flag they can pass it in directly
Load(WithDryRun(x))
I think this is slightly cleaner, but can see an argument against it for a cleaner option.
| } | ||
|
|
||
| // withOffchainProvider sets the offchain provider for the offchain client. Used for testing. | ||
| func withOffchainProvider(provider foffchain.Provider) LoadOffchainClientOpt { |
There was a problem hiding this comment.
should this be in the go doc comment itself? Else in future when someone else reads it, they will ask why is this unexported?
[BREAKING CHANGE] Refactors the offchain client arguments to remove unnecessary arguments. Converts some of the arguments into functional options with defaults. Adds tests for the offchain client.
d0d8004 to
b238a07
Compare
|
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## chainlink-deployments-framework@0.49.0 ### Minor Changes - [#437](#437) [`2224427`](2224427) Thanks [@jkongie](https://github.com/jkongie)! - **[BREAKING]** Refactored `LoadOffchainClient` to use functional options ## Function Signature Changed **Before:** ```go func LoadOffchainClient(ctx, domain, env, config, logger, useRealBackends) ``` **After:** ```go func LoadOffchainClient(ctx, domain, cfg, ...opts) ``` ## Migration Required - `logger` → `WithLogger(logger)` option (optional, has default) - `useRealBackends` → `WithDryRun(!useRealBackends)`⚠️ **inverted logic** - `env` → `WithCredentials(creds)` option (optional, defaults to TLS) - `config` → `config.Offchain.JobDistributor` **Example:** ```go // Old LoadOffchainClient(ctx, domain, "testnet", config, logger, false) // New LoadOffchainClient(ctx, domain, config.Offchain.JobDistributor, WithLogger(logger), WithDryRun(true), // Note: inverted! ) ``` - [#428](#428) [`e172683`](e172683) Thanks [@jkongie](https://github.com/jkongie)! - Adds a test engine runtime for executing changesets in unit/integration tests - [#443](#443) [`9e6bc1d`](9e6bc1d) Thanks [@graham-chainlink](https://github.com/graham-chainlink)! - feat: introduce template-input command for generating YAML input This commit introduces a new template-input command that generates YAML input templates from Go struct types for durable pipeline changesets. The command uses reflection to analyze changeset input types and produces well-formatted YAML templates with type comments to guide users in creating valid input files. - [#440](#440) [`7f1af5d`](7f1af5d) Thanks [@RodrigoAD](https://github.com/RodrigoAD)! - add support for sui in mcms commands --------- Co-authored-by: app-token-issuer-engops[bot] <144731339+app-token-issuer-engops[bot]@users.noreply.github.com>


[BREAKING CHANGE] Refactors the offchain client arguments to remove unnecessary arguments. Converts some of the arguments into functional options with defaults.
Adds tests for the offchain client.