From 90a82cbc749361588c122a4839fc524871a3cbec Mon Sep 17 00:00:00 2001 From: Bolek <1416262+bolekk@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:27:08 -0800 Subject: [PATCH] [Keystone][Deployment] Minor refactor to expose RegisterNodes publicly (#15529) --- deployment/keystone/deploy.go | 80 ++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/deployment/keystone/deploy.go b/deployment/keystone/deploy.go index 2aa26312eae..dc61e0491b6 100644 --- a/deployment/keystone/deploy.go +++ b/deployment/keystone/deploy.go @@ -256,13 +256,13 @@ func ConfigureRegistry(ctx context.Context, lggr logger.Logger, req ConfigureCon lggr.Infow("registered node operators", "nops", nopsResp.Nops) // register nodes - nodesResp, err := registerNodes(lggr, ®isterNodesRequest{ - registry: registry, - chain: registryChain, - nopToNodeIDs: nopsToNodeIDs, - donToNodes: donToNodes, - donToCapabilities: capabilitiesResp.DonToCapabilities, - nops: nopsResp.Nops, + nodesResp, err := RegisterNodes(lggr, &RegisterNodesRequest{ + Env: req.Env, + RegistryChainSelector: req.RegistryChainSel, + NopToNodeIDs: nopsToNodeIDs, + DonToNodes: donToNodes, + DonToCapabilities: capabilitiesResp.DonToCapabilities, + Nops: nopsResp.Nops, }) if err != nil { return nil, fmt.Errorf("failed to register nodes: %w", err) @@ -445,6 +445,21 @@ type RegisteredCapability struct { ID [32]byte } +func FromCapabilitiesRegistryCapability(cap *kcr.CapabilitiesRegistryCapability, e deployment.Environment, registryChainSelector uint64) (*RegisteredCapability, error) { + registry, _, err := GetRegistryContract(&e, registryChainSelector, e.ExistingAddresses) + if err != nil { + return nil, fmt.Errorf("failed to get registry: %w", err) + } + id, err := registry.GetHashedCapabilityId(&bind.CallOpts{}, cap.LabelledName, cap.Version) + if err != nil { + return nil, fmt.Errorf("failed to call GetHashedCapabilityId for capability %v: %w", cap, err) + } + return &RegisteredCapability{ + CapabilitiesRegistryCapability: *cap, + ID: id, + }, nil +} + // RegisterCapabilities add computes the capability id, adds it to the registry and associates the registered capabilities with appropriate don(s) func RegisterCapabilities(lggr logger.Logger, req RegisterCapabilitiesRequest) (*RegisterCapabilitiesResponse, error) { if len(req.DonToCapabilities) == 0 { @@ -629,34 +644,39 @@ func DecodeErr(encodedABI string, err error) error { } // register nodes -type registerNodesRequest struct { - registry *kcr.CapabilitiesRegistry - chain deployment.Chain - nopToNodeIDs map[kcr.CapabilitiesRegistryNodeOperator][]string - donToNodes map[string][]deployment.Node - donToCapabilities map[string][]RegisteredCapability - nops []*kcr.CapabilitiesRegistryNodeOperatorAdded +type RegisterNodesRequest struct { + Env *deployment.Environment + RegistryChainSelector uint64 + NopToNodeIDs map[kcr.CapabilitiesRegistryNodeOperator][]string + DonToNodes map[string][]deployment.Node + DonToCapabilities map[string][]RegisteredCapability + Nops []*kcr.CapabilitiesRegistryNodeOperatorAdded } -type registerNodesResponse struct { +type RegisterNodesResponse struct { nodeIDToParams map[string]kcr.CapabilitiesRegistryNodeParams } // registerNodes registers the nodes with the registry. it assumes that the deployer key in the Chain // can sign the transactions update the contract state // TODO: 467 refactor to support MCMS. Specifically need to separate the call data generation from the actual contract call -func registerNodes(lggr logger.Logger, req *registerNodesRequest) (*registerNodesResponse, error) { +func RegisterNodes(lggr logger.Logger, req *RegisterNodesRequest) (*RegisterNodesResponse, error) { + registry, registryChain, err := GetRegistryContract(req.Env, req.RegistryChainSelector, req.Env.ExistingAddresses) + if err != nil { + return nil, fmt.Errorf("failed to get registry: %w", err) + } + var count int - for _, nodes := range req.nopToNodeIDs { + for _, nodes := range req.NopToNodeIDs { count += len(nodes) } lggr.Infow("registering nodes...", "len", count) nodeToRegisterNop := make(map[string]*kcr.CapabilitiesRegistryNodeOperatorAdded) - for _, nop := range req.nops { + for _, nop := range req.Nops { n := kcr.CapabilitiesRegistryNodeOperator{ Name: nop.Name, Admin: nop.Admin, } - nodeIDs := req.nopToNodeIDs[n] + nodeIDs := req.NopToNodeIDs[n] for _, nodeID := range nodeIDs { _, exists := nodeToRegisterNop[nodeID] if !exists { @@ -666,7 +686,7 @@ func registerNodes(lggr logger.Logger, req *registerNodesRequest) (*registerNode } // TODO: deduplicate everywhere - registryChainID, err := chainsel.ChainIdFromSelector(req.chain.Selector) + registryChainID, err := chainsel.ChainIdFromSelector(registryChain.Selector) if err != nil { return nil, err } @@ -676,10 +696,10 @@ func registerNodes(lggr logger.Logger, req *registerNodesRequest) (*registerNode } nodeIDToParams := make(map[string]kcr.CapabilitiesRegistryNodeParams) - for don, nodes := range req.donToNodes { - caps, ok := req.donToCapabilities[don] + for don, nodes := range req.DonToNodes { + caps, ok := req.DonToCapabilities[don] if !ok { - return nil, fmt.Errorf("capabilities not found for node operator %s", don) + return nil, fmt.Errorf("capabilities not found for don %s", don) } var hashedCapabilityIds [][32]byte for _, cap := range caps { @@ -700,7 +720,7 @@ func registerNodes(lggr logger.Logger, req *registerNodesRequest) (*registerNode if !ok { evmCC, exists := n.SelToOCRConfig[registryChainDetails] if !exists { - return nil, fmt.Errorf("config for selector not found on node: %v", req.chain.Selector) + return nil, fmt.Errorf("config for selector %v not found on node (id: %s, name: %s)", registryChain.Selector, n.NodeID, n.Name) } var signer [32]byte copy(signer[:], evmCC.OnchainPublicKey) @@ -738,8 +758,8 @@ func registerNodes(lggr logger.Logger, req *registerNodesRequest) (*registerNode for _, v := range nodeIDToParams { uniqueNodeParams = append(uniqueNodeParams, v) } - lggr.Debugw("unique node params to add", "count", len(uniqueNodeParams)) - tx, err := req.registry.AddNodes(req.chain.DeployerKey, uniqueNodeParams) + lggr.Debugw("unique node params to add", "count", len(uniqueNodeParams), "params", uniqueNodeParams) + tx, err := registry.AddNodes(registryChain.DeployerKey, uniqueNodeParams) if err != nil { err = DecodeErr(kcr.CapabilitiesRegistryABI, err) // no typed errors in the abi, so we have to do string matching @@ -749,7 +769,7 @@ func registerNodes(lggr logger.Logger, req *registerNodesRequest) (*registerNode } lggr.Warn("nodes already exist, falling back to 1-by-1") for _, singleNodeParams := range uniqueNodeParams { - tx, err = req.registry.AddNodes(req.chain.DeployerKey, []kcr.CapabilitiesRegistryNodeParams{singleNodeParams}) + tx, err = registry.AddNodes(registryChain.DeployerKey, []kcr.CapabilitiesRegistryNodeParams{singleNodeParams}) if err != nil { err = DecodeErr(kcr.CapabilitiesRegistryABI, err) if strings.Contains(err.Error(), "NodeAlreadyExists") { @@ -759,7 +779,7 @@ func registerNodes(lggr logger.Logger, req *registerNodesRequest) (*registerNode return nil, fmt.Errorf("failed to call AddNode for node with p2pid %v: %w", singleNodeParams.P2pId, err) } // 1-by-1 tx is pending and we need to wait for it to be mined - _, err = req.chain.Confirm(tx) + _, err = registryChain.Confirm(tx) if err != nil { return nil, fmt.Errorf("failed to confirm AddNode of p2pid node %v transaction %s: %w", singleNodeParams.P2pId, tx.Hash().String(), err) } @@ -767,12 +787,12 @@ func registerNodes(lggr logger.Logger, req *registerNodesRequest) (*registerNode } } else { // the bulk add tx is pending and we need to wait for it to be mined - _, err = req.chain.Confirm(tx) + _, err = registryChain.Confirm(tx) if err != nil { return nil, fmt.Errorf("failed to confirm AddNode confirm transaction %s: %w", tx.Hash().String(), err) } } - return ®isterNodesResponse{ + return &RegisterNodesResponse{ nodeIDToParams: nodeIDToParams, }, nil }