forked from TrueCloudLab/frostfs-node
Aleksey Savchuk
f0c43c8d80
Use `zap.Error` instead of `zap.String` for logging errors: change all expressions like `zap.String("error", err.Error())` or `zap.String("err", err.Error())` to `zap.Error(err)`. Leave similar expressions with other messages unchanged, for example, `zap.String("last_error", lastErr.Error())` or `zap.String("reason", ctx.Err().Error())`. This change was made by applying the following patch: ```diff @@ var err expression @@ -zap.String("error", err.Error()) +zap.Error(err) @@ var err expression @@ -zap.String("err", err.Error()) +zap.Error(err) ``` Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
160 lines
4.4 KiB
Go
160 lines
4.4 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
frostfscontract "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/frostfs"
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
alphabetUpdateIDPrefix = "AlphabetUpdate"
|
|
)
|
|
|
|
func (gp *Processor) processAlphabetSync(ctx context.Context, txHash util.Uint256) bool {
|
|
if !gp.alphabetState.IsAlphabet(ctx) {
|
|
gp.log.Info(ctx, logs.GovernanceNonAlphabetModeIgnoreAlphabetSync)
|
|
return true
|
|
}
|
|
|
|
mainnetAlphabet, err := gp.mainnetClient.NeoFSAlphabetList()
|
|
if err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantFetchAlphabetListFromMainNet,
|
|
zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
sidechainAlphabet, err := gp.morphClient.Committee()
|
|
if err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantFetchAlphabetListFromSideChain,
|
|
zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
newAlphabet, err := newAlphabetList(sidechainAlphabet, mainnetAlphabet)
|
|
if err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantMergeAlphabetListsFromMainNetAndSideChain,
|
|
zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
if newAlphabet == nil {
|
|
gp.log.Info(ctx, logs.GovernanceNoGovernanceUpdateAlphabetListHasNotBeenChanged)
|
|
return true
|
|
}
|
|
|
|
gp.log.Info(ctx, logs.GovernanceAlphabetListHasBeenChangedStartingUpdate,
|
|
zap.String("side_chain_alphabet", prettyKeys(sidechainAlphabet)),
|
|
zap.String("new_alphabet", prettyKeys(newAlphabet)),
|
|
)
|
|
|
|
votePrm := VoteValidatorPrm{
|
|
Validators: newAlphabet,
|
|
Hash: &txHash,
|
|
}
|
|
|
|
// 1. Vote to sidechain committee via alphabet contracts.
|
|
err = gp.voter.VoteForSidechainValidator(ctx, votePrm)
|
|
if err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantVoteForSideChainCommittee,
|
|
zap.Error(err))
|
|
}
|
|
|
|
// 2. Update NeoFSAlphabet role in the sidechain.
|
|
gp.updateNeoFSAlphabetRoleInSidechain(ctx, sidechainAlphabet, newAlphabet, txHash)
|
|
|
|
// 3. Update notary role in the sidechain.
|
|
gp.updateNotaryRoleInSidechain(ctx, newAlphabet, txHash)
|
|
|
|
// 4. Update FrostFS contract in the mainnet.
|
|
gp.updateFrostFSContractInMainnet(ctx, newAlphabet)
|
|
|
|
gp.log.Info(ctx, logs.GovernanceFinishedAlphabetListUpdate)
|
|
|
|
return true
|
|
}
|
|
|
|
func prettyKeys(keys keys.PublicKeys) string {
|
|
const delimiter = ","
|
|
|
|
sb := strings.Builder{}
|
|
for _, key := range keys {
|
|
sb.WriteString(hex.EncodeToString(key.Bytes()))
|
|
sb.WriteString(delimiter)
|
|
}
|
|
|
|
return strings.TrimRight(sb.String(), delimiter)
|
|
}
|
|
|
|
func (gp *Processor) updateNeoFSAlphabetRoleInSidechain(ctx context.Context, sidechainAlphabet, newAlphabet keys.PublicKeys, txHash util.Uint256) {
|
|
innerRing, err := gp.irFetcher.InnerRingKeys()
|
|
if err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantFetchInnerRingListFromSideChain,
|
|
zap.Error(err))
|
|
return
|
|
}
|
|
|
|
newInnerRing, err := updateInnerRing(innerRing, sidechainAlphabet, newAlphabet)
|
|
if err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantCreateNewInnerRingListWithNewAlphabetKeys,
|
|
zap.Error(err))
|
|
return
|
|
}
|
|
|
|
sort.Sort(newInnerRing)
|
|
|
|
gp.log.Info(ctx, logs.GovernanceUpdateOfTheInnerRingList,
|
|
zap.String("before", prettyKeys(innerRing)),
|
|
zap.String("after", prettyKeys(newInnerRing)),
|
|
)
|
|
|
|
updPrm := client.UpdateAlphabetListPrm{}
|
|
updPrm.SetList(newInnerRing)
|
|
updPrm.SetHash(txHash)
|
|
|
|
if err = gp.morphClient.UpdateNeoFSAlphabetList(ctx, updPrm); err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantUpdateInnerRingListWithNewAlphabetKeys,
|
|
zap.Error(err))
|
|
}
|
|
}
|
|
|
|
func (gp *Processor) updateNotaryRoleInSidechain(ctx context.Context, newAlphabet keys.PublicKeys, txHash util.Uint256) {
|
|
updPrm := client.UpdateNotaryListPrm{}
|
|
|
|
updPrm.SetList(newAlphabet)
|
|
updPrm.SetHash(txHash)
|
|
|
|
err := gp.morphClient.UpdateNotaryList(ctx, updPrm)
|
|
if err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantUpdateListOfNotaryNodesInSideChain,
|
|
zap.Error(err))
|
|
}
|
|
}
|
|
|
|
func (gp *Processor) updateFrostFSContractInMainnet(ctx context.Context, 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(ctx, prm)
|
|
if err != nil {
|
|
gp.log.Error(ctx, logs.GovernanceCantUpdateListOfAlphabetNodesInFrostfsContract,
|
|
zap.Error(err))
|
|
}
|
|
}
|