Skip to content

Commit

Permalink
Extracts hydration functionality into a common package
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzaldysanchez committed Nov 19, 2024
1 parent 8107d68 commit 1e7b80e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 78 deletions.
83 changes: 83 additions & 0 deletions deployment/common/changeset/capability_registry.go
Original file line number Diff line number Diff line change
@@ -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
}
82 changes: 4 additions & 78 deletions deployment/common/view/v1_0/capreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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)
Expand Down

0 comments on commit 1e7b80e

Please sign in to comment.