Skip to content

Commit

Permalink
deploy: share roles check code
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
  • Loading branch information
carpawell committed Jun 26, 2024
1 parent 9095b3e commit 5317901
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 63 deletions.
16 changes: 3 additions & 13 deletions deploy/alphabet.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,12 @@ func designateNeoFSAlphabet(ctx context.Context, prm initAlphabetPrm) error {

prm.logger.Info("checking NeoFS Alphabet role of the committee members...")

accsWithAlphabetRole, err := roleContract.GetDesignatedByRole(noderoles.NeoFSAlphabet, prm.monitor.currentHeight())
ok, err := checkRole(noderoles.NeoFSAlphabet, prm.blockchain, prm.monitor, prm.committee)
if err != nil {
prm.logger.Error("failed to check role of the committee, will try again later", zap.Error(err))
continue
return fmt.Errorf("role check: %w", err)
}

someoneWithoutRole := len(accsWithAlphabetRole) < len(prm.committee)
if !someoneWithoutRole {
for i := range prm.committee {
if !accsWithAlphabetRole.Contains(prm.committee[i]) {
someoneWithoutRole = true
break
}
}
}
if !someoneWithoutRole {
if ok {
prm.logger.Info("all committee members have a NeoFS Alphabet role")
return nil
}
Expand Down
60 changes: 25 additions & 35 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ func Deploy(ctx context.Context, prm Prm) error {
return fmt.Errorf("init blockchain monitor: %w", err)
}

rolesAreSet, err := checkCommitteeRoles(prm.Logger, prm.Blockchain, monitor, committee)
notaryRolesSet, alphaRolesSet, err := checkCommitteeRoles(prm.Blockchain, monitor, committee)
if err != nil {
return fmt.Errorf("pre-check committee roles: %w", err)
}
transfersDone := rolesAreSet // currently the same
if rolesAreSet {
transfersDone := notaryRolesSet // currently the same
if notaryRolesSet {
// if it is possible to transfer GAS before any network settings,
// it should be done as it speeds up expensive operations

Expand Down Expand Up @@ -277,7 +277,7 @@ func Deploy(ctx context.Context, prm Prm) error {

prm.Logger.Info("NNS contract successfully initialized on the chain", zap.Stringer("address", nnsOnChainAddress))

if !rolesAreSet {
if !notaryRolesSet {
prm.Logger.Info("enable Notary service for the committee...")

err = enableNotary(ctx, enableNotaryPrm{
Expand Down Expand Up @@ -318,7 +318,7 @@ func Deploy(ctx context.Context, prm Prm) error {
prm.Logger.Info("initial transfer to the committee successfully done")
}

if !rolesAreSet {
if !alphaRolesSet {
prm.Logger.Info("initializing NeoFS Alphabet...")

err = designateNeoFSAlphabet(ctx, initAlphabetPrm{
Expand Down Expand Up @@ -685,48 +685,38 @@ func neoFSRuntimeTransactionModifier(getBlockchainHeight func() uint32) actor.Tr
}
}

func checkCommitteeRoles(logger *zap.Logger, b Blockchain, m *blockchainMonitor, committee keys.PublicKeys) (bool, error) {
currHeight := m.currentHeight()

roleContract := rolemgmt.NewReader(invoker.New(b, nil))

currNotaries, err := roleContract.GetDesignatedByRole(noderoles.P2PNotary, currHeight)
// first is for notary, second is for alphabet.
func checkCommitteeRoles(b Blockchain, m *blockchainMonitor, committee keys.PublicKeys) (bool, bool, error) {
notaryRole, err := checkRole(noderoles.P2PNotary, b, m, committee)
if err != nil {
return false, fmt.Errorf("reading notary role: %w", err)
return false, false, fmt.Errorf("%s role check: %w", noderoles.P2PNotary, err)
}
sort.Sort(currNotaries)
if !keysAreEqual(currNotaries, committee) {
logger.Debug("notaries and committee mismatch",
zap.Stringers("notaries", currNotaries), zap.Stringers("committee", committee))

return false, nil
alphaRole, err := checkRole(noderoles.NeoFSAlphabet, b, m, committee)
if err != nil {
return false, false, fmt.Errorf("%s role check: %w", noderoles.NeoFSAlphabet, err)
}

currAlphas, err := roleContract.GetDesignatedByRole(noderoles.NeoFSAlphabet, currHeight)
return notaryRole, alphaRole, nil
}

func checkRole(role noderoles.Role, b Blockchain, m *blockchainMonitor, committee keys.PublicKeys) (bool, error) {
roleContract := rolemgmt.NewReader(invoker.New(b, nil))

currentRoles, err := roleContract.GetDesignatedByRole(role, m.currentHeight())
if err != nil {
return false, fmt.Errorf("reading alphabet role: %w", err)
return false, fmt.Errorf("reading %s role: %w", role, err)
}
sort.Sort(currAlphas)
if !keysAreEqual(currAlphas, committee) {
logger.Debug("alphabet and committee mismatch",
zap.Stringers("alphabet", currAlphas), zap.Stringers("committee", committee))

if len(currentRoles) < len(committee) {
return false, nil
}

return true, nil
}

func keysAreEqual(a, b keys.PublicKeys) bool {
if len(a) != len(b) {
return false
}

for i := range a {
if !a[i].Equal(b[i]) {
return false
for i := range committee {
if !currentRoles.Contains(committee[i]) {
return false, nil
}
}

return true
return true, nil
}
18 changes: 3 additions & 15 deletions deploy/notary.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,19 @@ func enableNotary(ctx context.Context, prm enableNotaryPrm) error {
}
}

roleContract := rolemgmt.NewReader(invoker.New(prm.blockchain, nil))

for ; ; err = prm.monitor.waitForNextBlock(ctx) {
if err != nil {
return fmt.Errorf("wait for Notary service to be enabled for the committee: %w", err)
}

prm.logger.Info("checking Notary role of the committee members...")

accsWithNotaryRole, err := roleContract.GetDesignatedByRole(noderoles.P2PNotary, prm.monitor.currentHeight())
ok, err := checkRole(noderoles.P2PNotary, prm.blockchain, prm.monitor, prm.committee)
if err != nil {
prm.logger.Error("failed to check role of the committee, will try again later", zap.Error(err))
continue
return fmt.Errorf("role check: %w", err)
}

someoneWithoutNotaryRole := len(accsWithNotaryRole) < len(prm.committee)
if !someoneWithoutNotaryRole {
for i := range prm.committee {
if !accsWithNotaryRole.Contains(prm.committee[i]) {
someoneWithoutNotaryRole = true
break
}
}
}
if !someoneWithoutNotaryRole {
if ok {
prm.logger.Info("all committee members have a Notary role")
return nil
}
Expand Down

0 comments on commit 5317901

Please sign in to comment.