Skip to content

Commit

Permalink
add dry option; return useful config (#15314)
Browse files Browse the repository at this point in the history
* add dry option; return useful config

* dry run logging

* cleanup

* rename

* fix broken refs
  • Loading branch information
krehermann authored Nov 19, 2024
1 parent 570c23f commit 7682edc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 37 deletions.
2 changes: 1 addition & 1 deletion core/scripts/keystone/src/01_deploy_contracts_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func deploy(

func setOCR3Config(
env helpers.Environment,
ocrConfig ksdeploy.Orc2drOracleConfig,
ocrConfig ksdeploy.OCR2OracleConfig,
artefacts string,
) {
loadedContracts, err := LoadDeployedContracts(artefacts)
Expand Down
2 changes: 1 addition & 1 deletion core/scripts/keystone/src/88_gen_ocr3_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func mustReadConfig(fileName string) (output ksdeploy.TopLevelConfigSource) {
return mustParseJSON[ksdeploy.TopLevelConfigSource](fileName)
}

func generateOCR3Config(nodeList string, configFile string, chainID int64, pubKeysPath string) ksdeploy.Orc2drOracleConfig {
func generateOCR3Config(nodeList string, configFile string, chainID int64, pubKeysPath string) ksdeploy.OCR2OracleConfig {
topLevelCfg := mustReadConfig(configFile)
cfg := topLevelCfg.OracleConfig
cfg.OCRSecrets = deployment.XXXGenerateTestOCRSecrets()
Expand Down
9 changes: 5 additions & 4 deletions deployment/keystone/changeset/deploy_ocr3.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ func DeployOCR3(env deployment.Environment, config interface{}) (deployment.Chan
return deployment.ChangesetOutput{AddressBook: ab}, nil
}

func ConfigureOCR3Contract(lggr logger.Logger, env deployment.Environment, ab deployment.AddressBook, registryChainSel uint64, nodes []string, cfg kslib.OracleConfigWithSecrets) (deployment.ChangesetOutput, error) {
err := kslib.ConfigureOCR3ContractFromJD(&env, registryChainSel, nodes, ab, &cfg)
func ConfigureOCR3Contract(lggr logger.Logger, env deployment.Environment, cfg kslib.ConfigureOCR3Config) (deployment.ChangesetOutput, error) {

_, err := kslib.ConfigureOCR3ContractFromJD(&env, cfg)
if err != nil {
return deployment.ChangesetOutput{}, fmt.Errorf("failed to configure OCR3Capability: %w", err)
}

return deployment.ChangesetOutput{AddressBook: ab}, nil
// does not create any new addresses
return deployment.ChangesetOutput{}, nil
}
56 changes: 40 additions & 16 deletions deployment/keystone/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,45 +423,65 @@ func ConfigureOCR3Contract(env *deployment.Environment, chainSel uint64, dons []
return nil
}

func ConfigureOCR3ContractFromJD(env *deployment.Environment, chainSel uint64, nodeIDs []string, addrBook deployment.AddressBook, cfg *OracleConfigWithSecrets) error {
registryChain, ok := env.Chains[chainSel]
type ConfigureOCR3Resp struct {
OCR2OracleConfig
}

type ConfigureOCR3Config struct {
ChainSel uint64
NodeIDs []string
OCR3Config *OracleConfigWithSecrets
DryRun bool
}

func ConfigureOCR3ContractFromJD(env *deployment.Environment, cfg ConfigureOCR3Config) (*ConfigureOCR3Resp, error) {
prefix := ""
if cfg.DryRun {
prefix = "DRY RUN: "
}
env.Logger.Infof("%sconfiguring OCR3 contract for chain %d", prefix, cfg.ChainSel)
registryChain, ok := env.Chains[cfg.ChainSel]
if !ok {
return fmt.Errorf("chain %d not found in environment", chainSel)
return nil, fmt.Errorf("chain %d not found in environment", cfg.ChainSel)
}
contractSetsResp, err := GetContractSets(env.Logger, &GetContractSetsRequest{
Chains: env.Chains,
AddressBook: addrBook,
AddressBook: env.ExistingAddresses,
})
if err != nil {
return fmt.Errorf("failed to get contract sets: %w", err)
return nil, fmt.Errorf("failed to get contract sets: %w", err)
}
contracts, ok := contractSetsResp.ContractSets[chainSel]
contracts, ok := contractSetsResp.ContractSets[cfg.ChainSel]
if !ok {
return fmt.Errorf("failed to get contract set for chain %d", chainSel)
return nil, fmt.Errorf("failed to get contract set for chain %d", cfg.ChainSel)
}
contract := contracts.OCR3
if contract == nil {
return fmt.Errorf("no ocr3 contract found for chain %d", chainSel)
return nil, fmt.Errorf("no ocr3 contract found for chain %d", cfg.ChainSel)
}
nodes, err := NodesFromJD("nodes", nodeIDs, env.Offchain)
nodes, err := NodesFromJD("nodes", cfg.NodeIDs, env.Offchain)
if err != nil {
return err
return nil, err
}
var ocr2nodes []*ocr2Node
for _, node := range nodes {
n, err := newOcr2NodeFromJD(&node, chainSel)
n, err := newOcr2NodeFromJD(&node, cfg.ChainSel)
if err != nil {
return fmt.Errorf("failed to create ocr2 node from clo node: %w", err)
return nil, fmt.Errorf("failed to create ocr2 node from clo node %v: %w", node, err)
}
ocr2nodes = append(ocr2nodes, n)
}
_, err = configureOCR3contract(configureOCR3Request{
cfg: cfg,
r, err := configureOCR3contract(configureOCR3Request{
cfg: cfg.OCR3Config,
chain: registryChain,
contract: contract,
nodes: ocr2nodes,
dryRun: cfg.DryRun,
})
return err
return &ConfigureOCR3Resp{
OCR2OracleConfig: r.ocrConfig,
}, nil

}

type registerCapabilitiesRequest struct {
Expand Down Expand Up @@ -965,9 +985,10 @@ type configureOCR3Request struct {
chain deployment.Chain
contract *kocr3.OCR3Capability
nodes []*ocr2Node
dryRun bool
}
type configureOCR3Response struct {
ocrConfig Orc2drOracleConfig
ocrConfig OCR2OracleConfig
}

func configureOCR3contract(req configureOCR3Request) (*configureOCR3Response, error) {
Expand All @@ -979,6 +1000,9 @@ func configureOCR3contract(req configureOCR3Request) (*configureOCR3Response, er
if err != nil {
return nil, fmt.Errorf("failed to generate OCR3 config: %w", err)
}
if req.dryRun {
return &configureOCR3Response{ocrConfig}, nil
}
tx, err := req.contract.SetConfig(req.chain.DeployerKey,
ocrConfig.Signers,
ocrConfig.Transmitters,
Expand Down
31 changes: 16 additions & 15 deletions deployment/keystone/ocr3config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ type NodeKeys struct {
EncryptionPublicKey string `json:"EncryptionPublicKey"`
}

type Orc2drOracleConfig struct {
// OCR2OracleConfig is the input configuration for an OCR2/3 contract.
type OCR2OracleConfig struct {
Signers [][]byte
Transmitters []common.Address
F uint8
Expand All @@ -77,7 +78,7 @@ type Orc2drOracleConfig struct {
OffchainConfig []byte
}

func (c Orc2drOracleConfig) MarshalJSON() ([]byte, error) {
func (c OCR2OracleConfig) MarshalJSON() ([]byte, error) {
alias := struct {
Signers []string
Transmitters []string
Expand Down Expand Up @@ -105,16 +106,16 @@ func (c Orc2drOracleConfig) MarshalJSON() ([]byte, error) {
return json.Marshal(alias)
}

func GenerateOCR3Config(cfg OracleConfigWithSecrets, nca []NodeKeys) (Orc2drOracleConfig, error) {
func GenerateOCR3Config(cfg OracleConfigWithSecrets, nca []NodeKeys) (OCR2OracleConfig, error) {
onchainPubKeys := [][]byte{}
allPubKeys := map[string]any{}
if cfg.OCRSecrets.IsEmpty() {
return Orc2drOracleConfig{}, errors.New("OCRSecrets is required")
return OCR2OracleConfig{}, errors.New("OCRSecrets is required")
}
for _, n := range nca {
// evm keys always required
if n.OCR2OnchainPublicKey == "" {
return Orc2drOracleConfig{}, errors.New("OCR2OnchainPublicKey is required")
return OCR2OracleConfig{}, errors.New("OCR2OnchainPublicKey is required")
}
ethPubKey := common.HexToAddress(n.OCR2OnchainPublicKey)
pubKeys := map[string]types.OnchainPublicKey{
Expand All @@ -124,7 +125,7 @@ func GenerateOCR3Config(cfg OracleConfigWithSecrets, nca []NodeKeys) (Orc2drOrac
if n.AptosOnchainPublicKey != "" {
aptosPubKey, err := hex.DecodeString(n.AptosOnchainPublicKey)
if err != nil {
return Orc2drOracleConfig{}, fmt.Errorf("failed to decode AptosOnchainPublicKey: %w", err)
return OCR2OracleConfig{}, fmt.Errorf("failed to decode AptosOnchainPublicKey: %w", err)
}
pubKeys[string(chaintype.Aptos)] = aptosPubKey
}
Expand All @@ -133,13 +134,13 @@ func GenerateOCR3Config(cfg OracleConfigWithSecrets, nca []NodeKeys) (Orc2drOrac
raw := hex.EncodeToString(key)
_, exists := allPubKeys[raw]
if exists {
return Orc2drOracleConfig{}, fmt.Errorf("Duplicate onchain public key: '%s'", raw)
return OCR2OracleConfig{}, fmt.Errorf("Duplicate onchain public key: '%s'", raw)
}
allPubKeys[raw] = struct{}{}
}
pubKey, err := ocrcommon.MarshalMultichainPublicKey(pubKeys)
if err != nil {
return Orc2drOracleConfig{}, fmt.Errorf("failed to marshal multichain public key: %w", err)
return OCR2OracleConfig{}, fmt.Errorf("failed to marshal multichain public key: %w", err)
}
onchainPubKeys = append(onchainPubKeys, pubKey)
}
Expand All @@ -148,13 +149,13 @@ func GenerateOCR3Config(cfg OracleConfigWithSecrets, nca []NodeKeys) (Orc2drOrac
for _, n := range nca {
pkBytes, err := hex.DecodeString(n.OCR2OffchainPublicKey)
if err != nil {
return Orc2drOracleConfig{}, fmt.Errorf("failed to decode OCR2OffchainPublicKey: %w", err)
return OCR2OracleConfig{}, fmt.Errorf("failed to decode OCR2OffchainPublicKey: %w", err)
}

pkBytesFixed := [ed25519.PublicKeySize]byte{}
nCopied := copy(pkBytesFixed[:], pkBytes)
if nCopied != ed25519.PublicKeySize {
return Orc2drOracleConfig{}, fmt.Errorf("wrong num elements copied from ocr2 offchain public key. expected %d but got %d", ed25519.PublicKeySize, nCopied)
return OCR2OracleConfig{}, fmt.Errorf("wrong num elements copied from ocr2 offchain public key. expected %d but got %d", ed25519.PublicKeySize, nCopied)
}

offchainPubKeysBytes = append(offchainPubKeysBytes, types.OffchainPublicKey(pkBytesFixed))
Expand All @@ -164,13 +165,13 @@ func GenerateOCR3Config(cfg OracleConfigWithSecrets, nca []NodeKeys) (Orc2drOrac
for _, n := range nca {
pkBytes, err := hex.DecodeString(n.OCR2ConfigPublicKey)
if err != nil {
return Orc2drOracleConfig{}, fmt.Errorf("failed to decode OCR2ConfigPublicKey: %w", err)
return OCR2OracleConfig{}, fmt.Errorf("failed to decode OCR2ConfigPublicKey: %w", err)
}

pkBytesFixed := [ed25519.PublicKeySize]byte{}
n := copy(pkBytesFixed[:], pkBytes)
if n != ed25519.PublicKeySize {
return Orc2drOracleConfig{}, fmt.Errorf("wrong num elements copied from ocr2 config public key. expected %d but got %d", ed25519.PublicKeySize, n)
return OCR2OracleConfig{}, fmt.Errorf("wrong num elements copied from ocr2 config public key. expected %d but got %d", ed25519.PublicKeySize, n)
}

configPubKeysBytes = append(configPubKeysBytes, types.ConfigEncryptionPublicKey(pkBytesFixed))
Expand Down Expand Up @@ -212,7 +213,7 @@ func GenerateOCR3Config(cfg OracleConfigWithSecrets, nca []NodeKeys) (Orc2drOrac
nil, // empty onChain config
)
if err != nil {
return Orc2drOracleConfig{}, fmt.Errorf("failed to generate contract config args: %w", err)
return OCR2OracleConfig{}, fmt.Errorf("failed to generate contract config args: %w", err)
}

var configSigners [][]byte
Expand All @@ -222,10 +223,10 @@ func GenerateOCR3Config(cfg OracleConfigWithSecrets, nca []NodeKeys) (Orc2drOrac

transmitterAddresses, err := evm.AccountToAddress(transmitters)
if err != nil {
return Orc2drOracleConfig{}, fmt.Errorf("failed to convert transmitters to addresses: %w", err)
return OCR2OracleConfig{}, fmt.Errorf("failed to convert transmitters to addresses: %w", err)
}

config := Orc2drOracleConfig{
config := OCR2OracleConfig{
Signers: configSigners,
Transmitters: transmitterAddresses,
F: f,
Expand Down

0 comments on commit 7682edc

Please sign in to comment.