From 715a5cb4bf940fb97af5de65bad89b451db3a044 Mon Sep 17 00:00:00 2001 From: Jian Xiao Date: Mon, 25 Mar 2024 22:10:51 +0000 Subject: [PATCH] Fix the quorumID diffing for registartion from node start --- node/node.go | 16 ++++++------ node/operator.go | 54 ++++++++++++++++++++++++++--------------- node/plugin/cmd/main.go | 15 ++++++------ 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/node/node.go b/node/node.go index e2d59cf75..9ebbd6025 100644 --- a/node/node.go +++ b/node/node.go @@ -192,13 +192,14 @@ func (n *Node) Start(ctx context.Context) error { return fmt.Errorf("NewClient: cannot parse private key: %w", err) } operator := &Operator{ - Address: crypto.PubkeyToAddress(privateKey.PublicKey).Hex(), - Socket: socket, - Timeout: 10 * time.Second, - PrivKey: privateKey, - KeyPair: n.KeyPair, - OperatorId: n.Config.ID, - QuorumIDs: n.Config.QuorumIDList, + Address: crypto.PubkeyToAddress(privateKey.PublicKey).Hex(), + Socket: socket, + Timeout: 10 * time.Second, + PrivKey: privateKey, + KeyPair: n.KeyPair, + OperatorId: n.Config.ID, + QuorumIDs: n.Config.QuorumIDList, + RegisterNodeAtStart: n.Config.RegisterNodeAtStart, } churnerClient := NewChurnerClient(n.Config.ChurnerUrl, n.Config.UseSecureGrpc, n.Config.Timeout, n.Logger) err = RegisterOperator(ctx, operator, n.Transactor, churnerClient, n.Logger) @@ -212,7 +213,6 @@ func (n *Node) Start(ctx context.Context) error { } else { n.Logger.Infof("The node has started but the network with chainID %s is not supported yet", n.ChainID.String()) } - } n.CurrentSocket = socket diff --git a/node/operator.go b/node/operator.go index a6cd282a8..9c37b4a68 100644 --- a/node/operator.go +++ b/node/operator.go @@ -14,13 +14,14 @@ import ( ) type Operator struct { - Address string - Socket string - Timeout time.Duration - PrivKey *ecdsa.PrivateKey - KeyPair *core.KeyPair - OperatorId core.OperatorID - QuorumIDs []core.QuorumID + Address string + Socket string + Timeout time.Duration + PrivKey *ecdsa.PrivateKey + KeyPair *core.KeyPair + OperatorId core.OperatorID + QuorumIDs []core.QuorumID + RegisterNodeAtStart bool } // RegisterOperator operator registers the operator with the given public key for the given quorum IDs. @@ -28,7 +29,13 @@ func RegisterOperator(ctx context.Context, operator *Operator, transactor core.T if len(operator.QuorumIDs) > 1+core.MaxQuorumID { return fmt.Errorf("cannot provide more than %d quorums", 1+core.MaxQuorumID) } - quorumsToRegister, err := operator.getQuorumIdsToRegister(ctx, transactor) + var quorumsToRegister []core.QuorumID + var err error + if operator.RegisterNodeAtStart { + quorumsToRegister, err = GetQuorumIdsNotRegistered(ctx, transactor, operator.OperatorId, operator.QuorumIDs) + } else { + quorumsToRegister, err = operator.getQuorumIdsToRegister(ctx, transactor) + } if err != nil { return fmt.Errorf("failed to get quorum ids to register: %w", err) } @@ -102,25 +109,32 @@ func UpdateOperatorSocket(ctx context.Context, transactor core.Transactor, socke return transactor.UpdateOperatorSocket(ctx, socket) } -// getQuorumIdsToRegister returns the quorum ids that the operator is not registered in. -func (c *Operator) getQuorumIdsToRegister(ctx context.Context, transactor core.Transactor) ([]core.QuorumID, error) { - if len(c.QuorumIDs) == 0 { - return nil, fmt.Errorf("an operator should be in at least one quorum to be useful") - } - - registeredQuorumIds, err := transactor.GetRegisteredQuorumIdsForOperator(ctx, c.OperatorId) +// GetQuorumIdsNotRegistered returns the quorums from quorumIDs that are not yet registered for +// the given operator. +func GetQuorumIdsNotRegistered(ctx context.Context, transactor core.Transactor, operatorId core.OperatorID, quorumIDs []core.QuorumID) ([]core.QuorumID, error) { + registeredQuorumIds, err := transactor.GetRegisteredQuorumIdsForOperator(ctx, operatorId) if err != nil { return nil, fmt.Errorf("failed to get registered quorum ids for an operator: %w", err) } - quorumIdsToRegister := make([]core.QuorumID, 0, len(c.QuorumIDs)) - for _, quorumID := range c.QuorumIDs { + quorumIdsNotRegistered := make([]core.QuorumID, 0) + for _, quorumID := range quorumIDs { if !slices.Contains(registeredQuorumIds, quorumID) { - quorumIdsToRegister = append(quorumIdsToRegister, quorumID) - } else { - return nil, fmt.Errorf("the operator already registered for quorum %d", quorumID) + quorumIdsNotRegistered = append(quorumIdsNotRegistered, quorumID) } } + return quorumIdsNotRegistered, nil +} + +// getQuorumIdsToRegister returns the quorum ids that the operator is not registered in. +func (c *Operator) getQuorumIdsToRegister(ctx context.Context, transactor core.Transactor) ([]core.QuorumID, error) { + if len(c.QuorumIDs) == 0 { + return nil, fmt.Errorf("an operator should be in at least one quorum to be useful") + } + quorumIdsToRegister, err := GetQuorumIdsNotRegistered(ctx, transactor, c.OperatorId, c.QuorumIDs) + if err != nil { + return nil, fmt.Errorf("failed to get quorums to register an operator: %w", err) + } return quorumIdsToRegister, nil } diff --git a/node/plugin/cmd/main.go b/node/plugin/cmd/main.go index 020fd4ca7..8627038f9 100644 --- a/node/plugin/cmd/main.go +++ b/node/plugin/cmd/main.go @@ -118,13 +118,14 @@ func pluginOps(ctx *cli.Context) { } operator := &node.Operator{ - Address: sk.Address.Hex(), - Socket: socket, - Timeout: 10 * time.Second, - PrivKey: sk.PrivateKey, - KeyPair: keyPair, - OperatorId: keyPair.GetPubKeyG1().GetOperatorID(), - QuorumIDs: config.QuorumIDList, + Address: sk.Address.Hex(), + Socket: socket, + Timeout: 10 * time.Second, + PrivKey: sk.PrivateKey, + KeyPair: keyPair, + OperatorId: keyPair.GetPubKeyG1().GetOperatorID(), + QuorumIDs: config.QuorumIDList, + RegisterNodeAtStart: false, } churnerClient := node.NewChurnerClient(config.ChurnerUrl, true, operator.Timeout, logger) if config.Operation == "opt-in" {