From 52b3b29d71a2d1df747fbc9b25052fd96567e85d Mon Sep 17 00:00:00 2001 From: Christian Jackson-Gruber Date: Mon, 11 Nov 2024 14:12:31 -0800 Subject: [PATCH] Move Changeset to use a generic type for the config, to avoid a lot of reflection and casting --- deployment/ccip/changeset/home_chain.go | 9 +++------ deployment/ccip/changeset/initial_deploy.go | 8 ++------ deployment/changeset.go | 2 +- deployment/keystone/changeset/append_node_capbilities.go | 9 ++------- deployment/keystone/changeset/deploy_forwarder.go | 8 ++------ deployment/keystone/changeset/update_don.go | 7 +++---- .../keystone/changeset/update_node_capabilities.go | 9 +++------ 7 files changed, 16 insertions(+), 36 deletions(-) diff --git a/deployment/ccip/changeset/home_chain.go b/deployment/ccip/changeset/home_chain.go index 5fa5cab5b21..7d7f64a8bb8 100644 --- a/deployment/ccip/changeset/home_chain.go +++ b/deployment/ccip/changeset/home_chain.go @@ -13,14 +13,11 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/capabilities_registry" ) -var _ deployment.ChangeSet = DeployHomeChain +var _ deployment.ChangeSet[DeployHomeChainConfig] = DeployHomeChain // DeployHomeChain is a separate changeset because it is a standalone deployment performed once in home chain for the entire CCIP deployment. -func DeployHomeChain(env deployment.Environment, config interface{}) (deployment.ChangesetOutput, error) { - cfg, ok := config.(DeployHomeChainConfig) - if !ok { - return deployment.ChangesetOutput{}, deployment.ErrInvalidConfig - } +func DeployHomeChain(env deployment.Environment, cfg DeployHomeChainConfig) (deployment.ChangesetOutput, error) { + err := cfg.Validate() if err != nil { return deployment.ChangesetOutput{}, errors.Wrapf(deployment.ErrInvalidConfig, "%v", err) diff --git a/deployment/ccip/changeset/initial_deploy.go b/deployment/ccip/changeset/initial_deploy.go index f9d7caf44a3..de17834e8bd 100644 --- a/deployment/ccip/changeset/initial_deploy.go +++ b/deployment/ccip/changeset/initial_deploy.go @@ -8,13 +8,9 @@ import ( ccipdeployment "github.com/smartcontractkit/chainlink/deployment/ccip" ) -var _ deployment.ChangeSet = InitialDeploy +var _ deployment.ChangeSet[ccipdeployment.DeployCCIPContractConfig] = InitialDeploy -func InitialDeploy(env deployment.Environment, config interface{}) (deployment.ChangesetOutput, error) { - c, ok := config.(ccipdeployment.DeployCCIPContractConfig) - if !ok { - return deployment.ChangesetOutput{}, deployment.ErrInvalidConfig - } +func InitialDeploy(env deployment.Environment, c ccipdeployment.DeployCCIPContractConfig) (deployment.ChangesetOutput, error) { newAddresses := deployment.NewMemoryAddressBook() err := ccipdeployment.DeployCCIPContracts(env, newAddresses, c) if err != nil { diff --git a/deployment/changeset.go b/deployment/changeset.go index e6c0988e67e..abce4942203 100644 --- a/deployment/changeset.go +++ b/deployment/changeset.go @@ -18,7 +18,7 @@ var ( // Its recommended that changesets operate on a small number of chains (e.g. 1-3) // to reduce the risk of partial failures. // If the configuration is unexpected type or format, the changeset should return ErrInvalidConfig. -type ChangeSet func(e Environment, config interface{}) (ChangesetOutput, error) +type ChangeSet[C any] func(e Environment, config C) (ChangesetOutput, error) // ChangesetOutput is the output of a Changeset function. // Think of it like a state transition output. diff --git a/deployment/keystone/changeset/append_node_capbilities.go b/deployment/keystone/changeset/append_node_capbilities.go index ae654b7017d..e2892d87deb 100644 --- a/deployment/keystone/changeset/append_node_capbilities.go +++ b/deployment/keystone/changeset/append_node_capbilities.go @@ -8,19 +8,14 @@ import ( "github.com/smartcontractkit/chainlink/deployment/keystone/changeset/internal" ) -var _ deployment.ChangeSet = AppendNodeCapabilities +var _ deployment.ChangeSet[AppendNodeCapabilitiesRequest] = AppendNodeCapabilities // AppendNodeCapabilitiesRequest is a request to add capabilities to the existing capabilities of nodes in the registry type AppendNodeCapabilitiesRequest = MutateNodeCapabilitiesRequest // AppendNodeCapabilities adds any new capabilities to the registry, merges the new capabilities with the existing capabilities // of the node, and updates the nodes in the registry host the union of the new and existing capabilities. -func AppendNodeCapabilities(env deployment.Environment, config any) (deployment.ChangesetOutput, error) { - req, ok := config.(*AppendNodeCapabilitiesRequest) - if !ok { - return deployment.ChangesetOutput{}, fmt.Errorf("invalid config type") - } - +func AppendNodeCapabilities(env deployment.Environment, req AppendNodeCapabilitiesRequest) (deployment.ChangesetOutput, error) { cfg, err := req.convert(env) if err != nil { return deployment.ChangesetOutput{}, err diff --git a/deployment/keystone/changeset/deploy_forwarder.go b/deployment/keystone/changeset/deploy_forwarder.go index d6adbee0252..55ab0dcd86d 100644 --- a/deployment/keystone/changeset/deploy_forwarder.go +++ b/deployment/keystone/changeset/deploy_forwarder.go @@ -7,13 +7,9 @@ import ( kslib "github.com/smartcontractkit/chainlink/deployment/keystone" ) -var _ deployment.ChangeSet = DeployForwarder +var _ deployment.ChangeSet[uint64] = DeployForwarder -func DeployForwarder(env deployment.Environment, config interface{}) (deployment.ChangesetOutput, error) { - registryChainSel, ok := config.(uint64) - if !ok { - return deployment.ChangesetOutput{}, deployment.ErrInvalidConfig - } +func DeployForwarder(env deployment.Environment, registryChainSel uint64) (deployment.ChangesetOutput, error) { lggr := env.Logger // expect OCR3 to be deployed & capabilities registry regAddrs, err := env.ExistingAddresses.AddressesForChain(registryChainSel) diff --git a/deployment/keystone/changeset/update_don.go b/deployment/keystone/changeset/update_don.go index 1a535c5aa11..39f2745ebff 100644 --- a/deployment/keystone/changeset/update_don.go +++ b/deployment/keystone/changeset/update_don.go @@ -8,7 +8,7 @@ import ( kcr "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/capabilities_registry" ) -var _ deployment.ChangeSet = UpdateDon +var _ deployment.ChangeSet[UpdateDonRequest] = UpdateDon // CapabilityConfig is a struct that holds a capability and its configuration type CapabilityConfig = internal.CapabilityConfig @@ -22,9 +22,8 @@ type UpdateDonResponse struct { // UpdateDon updates the capabilities of a Don // This a complex action in practice that involves registering missing capabilities, adding the nodes, and updating // the capabilities of the DON -func UpdateDon(env deployment.Environment, cfg any) (deployment.ChangesetOutput, error) { - req := cfg.(*UpdateDonRequest) - _, err := internal.UpdateDon(env.Logger, req) +func UpdateDon(env deployment.Environment, req UpdateDonRequest) (deployment.ChangesetOutput, error) { + _, err := internal.UpdateDon(env.Logger, &req) if err != nil { return deployment.ChangesetOutput{}, fmt.Errorf("failed to update don: %w", err) } diff --git a/deployment/keystone/changeset/update_node_capabilities.go b/deployment/keystone/changeset/update_node_capabilities.go index 09cf351cc85..f9124893809 100644 --- a/deployment/keystone/changeset/update_node_capabilities.go +++ b/deployment/keystone/changeset/update_node_capabilities.go @@ -5,6 +5,7 @@ import ( "fmt" chainsel "github.com/smartcontractkit/chain-selectors" + "github.com/smartcontractkit/chainlink/deployment" "github.com/smartcontractkit/chainlink/deployment/environment/clo/models" kslib "github.com/smartcontractkit/chainlink/deployment/keystone" @@ -14,7 +15,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey" ) -var _ deployment.ChangeSet = UpdateNodeCapabilities +var _ deployment.ChangeSet[MutateNodeCapabilitiesRequest] = UpdateNodeCapabilities type P2PSignerEnc = internal.P2PSignerEnc @@ -84,11 +85,7 @@ func (req *MutateNodeCapabilitiesRequest) updateNodeCapabilitiesImplRequest(e de } // UpdateNodeCapabilities updates the capabilities of nodes in the registry -func UpdateNodeCapabilities(env deployment.Environment, config any) (deployment.ChangesetOutput, error) { - req, ok := config.(*MutateNodeCapabilitiesRequest) - if !ok { - return deployment.ChangesetOutput{}, fmt.Errorf("invalid config type. want %T, got %T", &MutateNodeCapabilitiesRequest{}, config) - } +func UpdateNodeCapabilities(env deployment.Environment, req MutateNodeCapabilitiesRequest) (deployment.ChangesetOutput, error) { c, err := req.updateNodeCapabilitiesImplRequest(env) if err != nil { return deployment.ChangesetOutput{}, fmt.Errorf("failed to convert request: %w", err)