Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jianoaix committed Mar 25, 2024
1 parent 715a5cb commit d925390
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions node/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package node
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"slices"
Expand All @@ -29,16 +30,16 @@ 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)
}
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)
}
quorumsToRegister, err := operator.getQuorumIdsToRegister(ctx, transactor)
if err != nil {
return fmt.Errorf("failed to get quorum ids to register: %w", err)
}
if !operator.RegisterNodeAtStart {
// For operator-initiated registration, the supplied quorums must be not registered yet.
if len(quorumsToRegister) != len(operator.QuorumIDs) {
return errors.New("quorums to register must be not registered yet")
}
}
if len(quorumsToRegister) == 0 {
return nil
}
Expand Down Expand Up @@ -109,32 +110,23 @@ func UpdateOperatorSocket(ctx context.Context, transactor core.Transactor, socke
return transactor.UpdateOperatorSocket(ctx, socket)
}

// 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)
// 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)
if err != nil {
return nil, fmt.Errorf("failed to get registered quorum ids for an operator: %w", err)
}

quorumIdsNotRegistered := make([]core.QuorumID, 0)
for _, quorumID := range quorumIDs {
quorumIdsToRegister := make([]core.QuorumID, 0, len(c.QuorumIDs))
for _, quorumID := range c.QuorumIDs {
if !slices.Contains(registeredQuorumIds, quorumID) {
quorumIdsNotRegistered = append(quorumIdsNotRegistered, quorumID)
quorumIdsToRegister = append(quorumIdsToRegister, 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
}

0 comments on commit d925390

Please sign in to comment.