Skip to content

Commit

Permalink
refactor(keystone/changeset): move OCR3 selector func and add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
MStreet3 committed Dec 24, 2024
1 parent 0b428a8 commit 4d594bd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
34 changes: 4 additions & 30 deletions deployment/keystone/changeset/internal/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

capabilities_registry "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/capabilities_registry_1_1_0"
kf "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/forwarder_1_0_0"
ocr3_capability "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/ocr3_capability_1_0_0"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
)
Expand Down Expand Up @@ -324,8 +323,9 @@ func ConfigureOCR3Contract(env *deployment.Environment, chainSel uint64, dons []
return fmt.Errorf("failed to get contract set for chain %d", chainSel)
}

contract, err := getOCR3Contract(contracts.OCR3, nil)
contract, err := contracts.GetOCR3Contract(nil)
if err != nil {
env.Logger.Errorf("failed to get OCR3 contract: %s", err)
return fmt.Errorf("failed to get OCR3 contract: %w", err)
}

Expand Down Expand Up @@ -382,8 +382,9 @@ func ConfigureOCR3ContractFromJD(env *deployment.Environment, cfg ConfigureOCR3C
return nil, fmt.Errorf("failed to get contract set for chain %d", cfg.ChainSel)
}

contract, err := getOCR3Contract(contracts.OCR3, cfg.Address)
contract, err := contracts.GetOCR3Contract(cfg.Address)
if err != nil {
env.Logger.Errorf("%sfailed to get OCR3 contract at %s : %s", prefix, cfg.Address, err)
return nil, fmt.Errorf("failed to get OCR3 contract: %w", err)
}

Expand Down Expand Up @@ -969,30 +970,3 @@ func configureForwarder(lggr logger.Logger, chain deployment.Chain, contractSet
}
return opMap, nil
}

// getOCR3Contract returns the OCR3 contract from the contract set. By default, it returns the only
// contract in the set if there is no address specified. If an address is specified, it returns the
// contract with that address. If the address is specified but not found in the contract set, it returns
// an error.
func getOCR3Contract(contracts map[common.Address]*ocr3_capability.OCR3Capability, addr *common.Address) (*ocr3_capability.OCR3Capability, error) {
// Fail if the OCR3 contract address is unspecified and there are multiple OCR3 contracts
if addr == nil && len(contracts) > 1 {
return nil, errors.New("OCR contract address is unspecified")
}

// Use the first OCR3 contract if the address is unspecified
if addr == nil && len(contracts) == 1 {
// use the first OCR3 contract
for _, c := range contracts {
return c, nil
}
}

// Select the OCR3 contract by address
if contract, ok := contracts[*addr]; ok {
return contract, nil
}

// Fail if the OCR3 contract address is specified but not found in the contract set
return nil, fmt.Errorf("OCR3 contract address %s not found in contract set", *addr)
}
37 changes: 37 additions & 0 deletions deployment/keystone/changeset/internal/state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -65,6 +66,10 @@ func (cs ContractSet) View() (view.KeystoneChainView, error) {
return out, nil
}

func (cs ContractSet) GetOCR3Contract(addr *common.Address) (*ocr3_capability.OCR3Capability, error) {
return getOCR3Contract(cs.OCR3, addr)
}

func GetContractSets(lggr logger.Logger, req *GetContractSetsRequest) (*GetContractSetsResponse, error) {
resp := &GetContractSetsResponse{
ContractSets: make(map[uint64]ContractSet),
Expand Down Expand Up @@ -128,3 +133,35 @@ func loadContractSet(lggr logger.Logger, chain deployment.Chain, addresses map[s
}
return &out, nil
}

// getOCR3Contract returns the OCR3 contract from the contract set. By default, it returns the only
// contract in the set if there is no address specified. If an address is specified, it returns the
// contract with that address. If the address is specified but not found in the contract set, it returns
// an error.
func getOCR3Contract(contracts map[common.Address]*ocr3_capability.OCR3Capability, addr *common.Address) (*ocr3_capability.OCR3Capability, error) {
// Fail if the OCR3 contract address is unspecified and there are multiple OCR3 contracts
if addr == nil && len(contracts) > 1 {
return nil, errors.New("OCR contract address is unspecified")
}

// Use the first OCR3 contract if the address is unspecified
if addr == nil && len(contracts) == 1 {
// use the first OCR3 contract
for _, c := range contracts {
return c, nil
}
}

// Select the OCR3 contract by address
if contract, ok := contracts[*addr]; ok {
return contract, nil
}

addrSet := make([]string, 0, len(contracts))
for a := range contracts {
addrSet = append(addrSet, a.String())
}

// Fail if the OCR3 contract address is specified but not found in the contract set
return nil, fmt.Errorf("OCR3 contract address %s not found in contract set %v", *addr, addrSet)
}

0 comments on commit 4d594bd

Please sign in to comment.