Skip to content

Commit

Permalink
[Keystone][Deployment] Minor refactor to expose RegisterNodes publicly (
Browse files Browse the repository at this point in the history
  • Loading branch information
bolekk authored and jhweintraub committed Dec 6, 2024
1 parent 20b99bd commit 2fb6511
Showing 1 changed file with 50 additions and 30 deletions.
80 changes: 50 additions & 30 deletions deployment/keystone/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &registerNodesRequest{
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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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") {
Expand All @@ -759,20 +779,20 @@ 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)
}
lggr.Debugw("registered node", "p2pid", singleNodeParams.P2pId)
}
} 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 &registerNodesResponse{
return &RegisterNodesResponse{
nodeIDToParams: nodeIDToParams,
}, nil
}
Expand Down

0 comments on commit 2fb6511

Please sign in to comment.