Skip to content

Commit

Permalink
Fix the quorumID diffing for registartion from node start
Browse files Browse the repository at this point in the history
  • Loading branch information
jianoaix committed Mar 25, 2024
1 parent 292e203 commit 715a5cb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
16 changes: 8 additions & 8 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
54 changes: 34 additions & 20 deletions node/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@ 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.
func RegisterOperator(ctx context.Context, operator *Operator, transactor core.Transactor, churnerClient ChurnerClient, logger logging.Logger) error {
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)
}
Expand Down Expand Up @@ -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
}
15 changes: 8 additions & 7 deletions node/plugin/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 715a5cb

Please sign in to comment.