From 1e7b80e8da9a60f439f615a40ae8a26195ce2e6e Mon Sep 17 00:00:00 2001 From: vyzaldysanchez Date: Tue, 19 Nov 2024 13:02:30 -0400 Subject: [PATCH] Extracts hydration functionality into a `common` package --- .../common/changeset/capability_registry.go | 83 +++++++++++++++++++ deployment/common/view/v1_0/capreg.go | 82 +----------------- 2 files changed, 87 insertions(+), 78 deletions(-) create mode 100644 deployment/common/changeset/capability_registry.go diff --git a/deployment/common/changeset/capability_registry.go b/deployment/common/changeset/capability_registry.go new file mode 100644 index 00000000000..0397759b36e --- /dev/null +++ b/deployment/common/changeset/capability_registry.go @@ -0,0 +1,83 @@ +package changeset + +import ( + "fmt" + + "github.com/smartcontractkit/chainlink/deployment" + "github.com/smartcontractkit/chainlink/deployment/common/view/v1_0" + "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/capabilities_registry" + "github.com/smartcontractkit/chainlink/v2/core/logger" +) + +type HydrateConfig struct { + lggr logger.Logger + ChainID uint64 +} + +// HydrateCapabilityRegistry deploys a new capabilities registry contract and hydrates it with the provided data. +func HydrateCapabilityRegistry(v v1_0.CapabilityRegistryView, env deployment.Environment, cfg HydrateConfig) (*deployment.ContractDeploy[*capabilities_registry.CapabilitiesRegistry], error) { + chain, ok := env.Chains[cfg.ChainID] + if !ok { + return nil, fmt.Errorf("chain with id %d not found", cfg.ChainID) + } + deployedContract, err := deployment.DeployContract( + cfg.lggr, chain, env.ExistingAddresses, + func(chain deployment.Chain) deployment.ContractDeploy[*capabilities_registry.CapabilitiesRegistry] { + crAddr, tx, cr, err2 := capabilities_registry.DeployCapabilitiesRegistry( + chain.DeployerKey, + chain.Client, + ) + return deployment.ContractDeploy[*capabilities_registry.CapabilitiesRegistry]{ + Address: crAddr, Contract: cr, Tx: tx, Err: err2, + Tv: deployment.NewTypeAndVersion("CapabilitiesRegistry", deployment.Version1_0_0), + } + }, + ) + if err != nil { + return nil, fmt.Errorf("failed to deploy contract: %w", err) + } + + nodesParams, err := v.NodesToNodesParams() + if err != nil { + return nil, fmt.Errorf("failed to convert nodes to nodes params: %w", err) + } + tx, err := deployedContract.Contract.AddNodes(chain.DeployerKey, nodesParams) + if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { + return nil, fmt.Errorf("failed to add nodes: %w", err) + } + + capabilitiesParams := v.CapabilitiesToCapabilitiesParams() + tx, err = deployedContract.Contract.AddCapabilities(chain.DeployerKey, capabilitiesParams) + if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { + return nil, fmt.Errorf("failed to add capabilities: %w", err) + } + + nopsParams := v.NopsToNopsParams() + for _, nop := range v.Nops { + nopsParams = append(nopsParams, capabilities_registry.CapabilitiesRegistryNodeOperator{ + Admin: nop.Admin, + Name: nop.Name, + }) + } + tx, err = deployedContract.Contract.AddNodeOperators(chain.DeployerKey, nopsParams) + if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { + return nil, fmt.Errorf("failed to add node operators: %w", err) + } + + for _, don := range v.Dons { + cfgs, err := v.CapabilityConfigToCapabilityConfigParams(don) + if err != nil { + return nil, fmt.Errorf("failed to convert capability configurations to capability configuration params: %w", err) + } + var peerIds [][32]byte + for _, id := range don.NodeP2PIds { + peerIds = append(peerIds, id) + } + tx, err = deployedContract.Contract.AddDON(chain.DeployerKey, peerIds, cfgs, don.IsPublic, don.AcceptsWorkflows, don.F) + if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { + return nil, fmt.Errorf("failed to add don: %w", err) + } + } + + return deployedContract, nil +} diff --git a/deployment/common/view/v1_0/capreg.go b/deployment/common/view/v1_0/capreg.go index ceb1217f57a..bc19f33048b 100644 --- a/deployment/common/view/v1_0/capreg.go +++ b/deployment/common/view/v1_0/capreg.go @@ -10,10 +10,8 @@ import ( "github.com/ethereum/go-ethereum/common" - "github.com/smartcontractkit/chainlink/deployment" "github.com/smartcontractkit/chainlink/deployment/common/view/types" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/capabilities_registry" - "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey" ) @@ -142,79 +140,7 @@ func (v CapabilityRegistryView) DonDenormalizedView() ([]DonDenormalizedView, er return out, nil } -type HydrateConfig struct { - lggr logger.Logger - ChainID uint64 -} - -func (v CapabilityRegistryView) Hydrate(env deployment.Environment, cfg HydrateConfig) (*deployment.ContractDeploy[*capabilities_registry.CapabilitiesRegistry], error) { - chain, ok := env.Chains[cfg.ChainID] - if !ok { - return nil, fmt.Errorf("chain with id %d not found", cfg.ChainID) - } - deployedContract, err := deployment.DeployContract( - cfg.lggr, chain, env.ExistingAddresses, - func(chain deployment.Chain) deployment.ContractDeploy[*capabilities_registry.CapabilitiesRegistry] { - crAddr, tx, cr, err2 := capabilities_registry.DeployCapabilitiesRegistry( - chain.DeployerKey, - chain.Client, - ) - return deployment.ContractDeploy[*capabilities_registry.CapabilitiesRegistry]{ - Address: crAddr, Contract: cr, Tx: tx, Err: err2, - Tv: deployment.NewTypeAndVersion("CapabilitiesRegistry", deployment.Version1_0_0), - } - }, - ) - if err != nil { - return nil, fmt.Errorf("failed to deploy contract: %w", err) - } - - nodesParams, err := v.nodesToNodesParams() - if err != nil { - return nil, fmt.Errorf("failed to convert nodes to nodes params: %w", err) - } - tx, err := deployedContract.Contract.AddNodes(chain.DeployerKey, nodesParams) - if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { - return nil, fmt.Errorf("failed to add nodes: %w", err) - } - - capabilitiesParams := v.capabilitiesToCapabilitiesParams() - tx, err = deployedContract.Contract.AddCapabilities(chain.DeployerKey, capabilitiesParams) - if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { - return nil, fmt.Errorf("failed to add capabilities: %w", err) - } - - nopsParams := v.nopsToNopsParams() - for _, nop := range v.Nops { - nopsParams = append(nopsParams, capabilities_registry.CapabilitiesRegistryNodeOperator{ - Admin: nop.Admin, - Name: nop.Name, - }) - } - tx, err = deployedContract.Contract.AddNodeOperators(chain.DeployerKey, nopsParams) - if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { - return nil, fmt.Errorf("failed to add node operators: %w", err) - } - - for _, don := range v.Dons { - cfgs, err := v.capabilityConfigToCapabilityConfigParams(don) - if err != nil { - return nil, fmt.Errorf("failed to convert capability configurations to capability configuration params: %w", err) - } - var peerIds [][32]byte - for _, id := range don.NodeP2PIds { - peerIds = append(peerIds, id) - } - tx, err = deployedContract.Contract.AddDON(chain.DeployerKey, peerIds, cfgs, don.IsPublic, don.AcceptsWorkflows, don.F) - if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { - return nil, fmt.Errorf("failed to add don: %w", err) - } - } - - return deployedContract, nil -} - -func (v CapabilityRegistryView) nodesToNodesParams() ([]capabilities_registry.CapabilitiesRegistryNodeParams, error) { +func (v CapabilityRegistryView) NodesToNodesParams() ([]capabilities_registry.CapabilitiesRegistryNodeParams, error) { var nodesParams []capabilities_registry.CapabilitiesRegistryNodeParams for _, node := range v.Nodes { signer, err := hexTo32Bytes(node.Signer) @@ -245,7 +171,7 @@ func (v CapabilityRegistryView) nodesToNodesParams() ([]capabilities_registry.Ca return nodesParams, nil } -func (v CapabilityRegistryView) capabilitiesToCapabilitiesParams() []capabilities_registry.CapabilitiesRegistryCapability { +func (v CapabilityRegistryView) CapabilitiesToCapabilitiesParams() []capabilities_registry.CapabilitiesRegistryCapability { var capabilitiesParams []capabilities_registry.CapabilitiesRegistryCapability for _, capability := range v.Capabilities { capabilitiesParams = append(capabilitiesParams, capabilities_registry.CapabilitiesRegistryCapability{ @@ -259,7 +185,7 @@ func (v CapabilityRegistryView) capabilitiesToCapabilitiesParams() []capabilitie return capabilitiesParams } -func (v CapabilityRegistryView) nopsToNopsParams() []capabilities_registry.CapabilitiesRegistryNodeOperator { +func (v CapabilityRegistryView) NopsToNopsParams() []capabilities_registry.CapabilitiesRegistryNodeOperator { var nopsParams []capabilities_registry.CapabilitiesRegistryNodeOperator for _, nop := range v.Nops { nopsParams = append(nopsParams, capabilities_registry.CapabilitiesRegistryNodeOperator{ @@ -270,7 +196,7 @@ func (v CapabilityRegistryView) nopsToNopsParams() []capabilities_registry.Capab return nopsParams } -func (v CapabilityRegistryView) capabilityConfigToCapabilityConfigParams(don DonView) ([]capabilities_registry.CapabilitiesRegistryCapabilityConfiguration, error) { +func (v CapabilityRegistryView) CapabilityConfigToCapabilityConfigParams(don DonView) ([]capabilities_registry.CapabilitiesRegistryCapabilityConfiguration, error) { var cfgs []capabilities_registry.CapabilitiesRegistryCapabilityConfiguration for _, cfg := range don.CapabilityConfigurations { cid, err := hexTo32Bytes(cfg.ID)