forked from TrueCloudLab/frostfs-node
160 lines
4.6 KiB
Go
160 lines
4.6 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(txHash util.Uint256) bool {
|
|
if !gp.alphabetState.IsAlphabet() {
|
|
gp.log.Info(context.Background(), logs.GovernanceNonAlphabetModeIgnoreAlphabetSync)
|
|
return true
|
|
}
|
|
|
|
mainnetAlphabet, err := gp.mainnetClient.NeoFSAlphabetList()
|
|
if err != nil {
|
|
gp.log.Error(context.Background(), logs.GovernanceCantFetchAlphabetListFromMainNet,
|
|
zap.String("error", err.Error()))
|
|
return false
|
|
}
|
|
|
|
sidechainAlphabet, err := gp.morphClient.Committee()
|
|
if err != nil {
|
|
gp.log.Error(context.Background(), logs.GovernanceCantFetchAlphabetListFromSideChain,
|
|
zap.String("error", err.Error()))
|
|
return false
|
|
}
|
|
|
|
newAlphabet, err := newAlphabetList(sidechainAlphabet, mainnetAlphabet)
|
|
if err != nil {
|
|
gp.log.Error(context.Background(), logs.GovernanceCantMergeAlphabetListsFromMainNetAndSideChain,
|
|
zap.String("error", err.Error()))
|
|
return false
|
|
}
|
|
|
|
if newAlphabet == nil {
|
|
gp.log.Info(context.Background(), logs.GovernanceNoGovernanceUpdateAlphabetListHasNotBeenChanged)
|
|
return true
|
|
}
|
|
|
|
gp.log.Info(context.Background(), 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(votePrm)
|
|
if err != nil {
|
|
gp.log.Error(context.Background(), logs.GovernanceCantVoteForSideChainCommittee,
|
|
zap.String("error", err.Error()))
|
|
}
|
|
|
|
// 2. Update NeoFSAlphabet role in the sidechain.
|
|
gp.updateNeoFSAlphabetRoleInSidechain(sidechainAlphabet, newAlphabet, txHash)
|
|
|
|
// 3. Update notary role in the sidechain.
|
|
gp.updateNotaryRoleInSidechain(newAlphabet, txHash)
|
|
|
|
// 4. Update FrostFS contract in the mainnet.
|
|
gp.updateFrostFSContractInMainnet(newAlphabet)
|
|
|
|
gp.log.Info(context.Background(), 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(sidechainAlphabet, newAlphabet keys.PublicKeys, txHash util.Uint256) {
|
|
innerRing, err := gp.irFetcher.InnerRingKeys()
|
|
if err != nil {
|
|
gp.log.Error(context.Background(), logs.GovernanceCantFetchInnerRingListFromSideChain,
|
|
zap.String("error", err.Error()))
|
|
return
|
|
}
|
|
|
|
newInnerRing, err := updateInnerRing(innerRing, sidechainAlphabet, newAlphabet)
|
|
if err != nil {
|
|
gp.log.Error(context.Background(), logs.GovernanceCantCreateNewInnerRingListWithNewAlphabetKeys,
|
|
zap.String("error", err.Error()))
|
|
return
|
|
}
|
|
|
|
sort.Sort(newInnerRing)
|
|
|
|
gp.log.Info(context.Background(), 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(updPrm); err != nil {
|
|
gp.log.Error(context.Background(), logs.GovernanceCantUpdateInnerRingListWithNewAlphabetKeys,
|
|
zap.String("error", err.Error()))
|
|
}
|
|
}
|
|
|
|
func (gp *Processor) updateNotaryRoleInSidechain(newAlphabet keys.PublicKeys, txHash util.Uint256) {
|
|
updPrm := client.UpdateNotaryListPrm{}
|
|
|
|
updPrm.SetList(newAlphabet)
|
|
updPrm.SetHash(txHash)
|
|
|
|
err := gp.morphClient.UpdateNotaryList(updPrm)
|
|
if err != nil {
|
|
gp.log.Error(context.Background(), logs.GovernanceCantUpdateListOfNotaryNodesInSideChain,
|
|
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(context.Background(), logs.GovernanceCantUpdateListOfAlphabetNodesInFrostfsContract,
|
|
zap.String("error", err.Error()))
|
|
}
|
|
}
|