From aeb4bbc51e22286f966ccc3a519a142de4e14f72 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Thu, 30 Mar 2023 10:35:16 +0300 Subject: [PATCH] [#185] ir: Refactor alphabet update Resolve funlen linter for processAlphabetSync method Signed-off-by: Dmitrii Stepanov --- .../processors/governance/process_update.go | 156 ++++++++++-------- 1 file changed, 85 insertions(+), 71 deletions(-) diff --git a/pkg/innerring/processors/governance/process_update.go b/pkg/innerring/processors/governance/process_update.go index c9764fee4..3504e7a53 100644 --- a/pkg/innerring/processors/governance/process_update.go +++ b/pkg/innerring/processors/governance/process_update.go @@ -18,7 +18,6 @@ const ( alphabetUpdateIDPrefix = "AlphabetUpdate" ) -// nolint: funlen func (gp *Processor) processAlphabetSync(txHash util.Uint256) { if !gp.alphabetState.IsAlphabet() { gp.log.Info("non alphabet mode, ignore alphabet sync") @@ -69,79 +68,13 @@ func (gp *Processor) processAlphabetSync(txHash util.Uint256) { } // 2. Update NeoFSAlphabet role in the sidechain. - innerRing, err := gp.irFetcher.InnerRingKeys() - if err != nil { - gp.log.Error("can't fetch inner ring list from side chain", - zap.String("error", err.Error())) - } else { - newInnerRing, err := updateInnerRing(innerRing, sidechainAlphabet, newAlphabet) - if err != nil { - gp.log.Error("can't create new inner ring list with new alphabet keys", - zap.String("error", err.Error())) - } else { - sort.Sort(newInnerRing) + gp.updateNeoFSAlphabetRoleInSidechain(sidechainAlphabet, newAlphabet, txHash) - gp.log.Info("update of the inner ring list", - zap.String("before", prettyKeys(innerRing)), - zap.String("after", prettyKeys(newInnerRing)), - ) - - if gp.notaryDisabled { - updPrm := nmClient.UpdateIRPrm{} - - updPrm.SetKeys(newInnerRing) - updPrm.SetHash(txHash) - - err = gp.netmapClient.UpdateInnerRing(updPrm) - } else { - updPrm := client.UpdateAlphabetListPrm{} - - updPrm.SetList(newInnerRing) - updPrm.SetHash(txHash) - - err = gp.morphClient.UpdateNeoFSAlphabetList(updPrm) - } - - if err != nil { - gp.log.Error("can't update inner ring list with new alphabet keys", - zap.String("error", err.Error())) - } - } - } - - if !gp.notaryDisabled { - // 3. Update notary role in the sidechain. - - updPrm := client.UpdateNotaryListPrm{} - - updPrm.SetList(newAlphabet) - updPrm.SetHash(txHash) - - err = gp.morphClient.UpdateNotaryList(updPrm) - if err != nil { - gp.log.Error("can't update list of notary nodes in side chain", - zap.String("error", err.Error())) - } - } + // 3. Update notary role in the sidechain. + gp.updateNotaryRoleInSidechain(newAlphabet, txHash) // 4. Update FrostFS contract in the mainnet. - epoch := gp.epochState.EpochCounter() - - buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, epoch) - - id := append([]byte(alphabetUpdateIDPrefix), buf...) - - prm := frostfscontract.AlphabetUpdatePrm{} - - prm.SetID(id) - prm.SetPubs(newAlphabet) - - err = gp.frostfsClient.AlphabetUpdate(prm) - if err != nil { - gp.log.Error("can't update list of alphabet nodes in frostfs contract", - zap.String("error", err.Error())) - } + gp.updateFrostFSContractInMainnet(newAlphabet) gp.log.Info("finished alphabet list update") } @@ -157,3 +90,84 @@ func prettyKeys(keys keys.PublicKeys) string { return strings.TrimRight(sb.String(), delimiter) } + +func (gp *Processor) updateNeoFSAlphabetRoleInSidechain(sidechainAlphabet, newAlphabet keys.PublicKeys, txHash util.Uint256) { + innerRing, err := gp.irFetcher.InnerRingKeys() + if err != nil { + gp.log.Error("can't fetch inner ring list from side chain", + zap.String("error", err.Error())) + return + } + + newInnerRing, err := updateInnerRing(innerRing, sidechainAlphabet, newAlphabet) + if err != nil { + gp.log.Error("can't create new inner ring list with new alphabet keys", + zap.String("error", err.Error())) + return + } + + sort.Sort(newInnerRing) + + gp.log.Info("update of the inner ring list", + zap.String("before", prettyKeys(innerRing)), + zap.String("after", prettyKeys(newInnerRing)), + ) + + if gp.notaryDisabled { + updPrm := nmClient.UpdateIRPrm{} + + updPrm.SetKeys(newInnerRing) + updPrm.SetHash(txHash) + + err = gp.netmapClient.UpdateInnerRing(updPrm) + } else { + updPrm := client.UpdateAlphabetListPrm{} + + updPrm.SetList(newInnerRing) + updPrm.SetHash(txHash) + + err = gp.morphClient.UpdateNeoFSAlphabetList(updPrm) + } + + if err != nil { + gp.log.Error("can't update inner ring list with new alphabet keys", + zap.String("error", err.Error())) + } +} + +func (gp *Processor) updateNotaryRoleInSidechain(newAlphabet keys.PublicKeys, txHash util.Uint256) { + if gp.notaryDisabled { + return + } + + updPrm := client.UpdateNotaryListPrm{} + + updPrm.SetList(newAlphabet) + updPrm.SetHash(txHash) + + err := gp.morphClient.UpdateNotaryList(updPrm) + if err != nil { + gp.log.Error("can't update list of notary nodes in side chain", + zap.String("error", err.Error())) + } +} + +func (gp *Processor) updateFrostFSContractInMainnet(newAlphabet keys.PublicKeys) { + epoch := gp.epochState.EpochCounter() + + buf := make([]byte, 8) + binary.LittleEndian.PutUint64(buf, epoch) + + id := append([]byte(alphabetUpdateIDPrefix), buf...) + + prm := frostfscontract.AlphabetUpdatePrm{} + + prm.SetID(id) + prm.SetPubs(newAlphabet) + + err := gp.frostfsClient.AlphabetUpdate(prm) + if err != nil { + gp.log.Error("can't update list of alphabet nodes in frostfs contract", + zap.String("error", err.Error())) + } +}