2021-03-25 13:23:53 +00:00
|
|
|
package governance
|
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2021-03-25 13:23:53 +00:00
|
|
|
"encoding/binary"
|
2021-09-29 16:54:49 +00:00
|
|
|
"encoding/hex"
|
2021-03-25 13:23:53 +00:00
|
|
|
"sort"
|
2021-09-29 16:54:49 +00:00
|
|
|
"strings"
|
2021-03-25 13:23:53 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
frostfscontract "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/frostfs"
|
2021-09-29 16:54:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-11-10 11:05:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-03-25 13:23:53 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-05-21 07:45:15 +00:00
|
|
|
const (
|
|
|
|
alphabetUpdateIDPrefix = "AlphabetUpdate"
|
|
|
|
)
|
2021-03-25 13:23:53 +00:00
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
func (gp *Processor) processAlphabetSync(txHash util.Uint256) bool {
|
2021-03-25 13:23:53 +00:00
|
|
|
if !gp.alphabetState.IsAlphabet() {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Info(context.Background(), logs.GovernanceNonAlphabetModeIgnoreAlphabetSync)
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mainnetAlphabet, err := gp.mainnetClient.NeoFSAlphabetList()
|
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantFetchAlphabetListFromMainNet,
|
2021-03-25 13:23:53 +00:00
|
|
|
zap.String("error", err.Error()))
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sidechainAlphabet, err := gp.morphClient.Committee()
|
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantFetchAlphabetListFromSideChain,
|
2021-03-25 13:23:53 +00:00
|
|
|
zap.String("error", err.Error()))
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newAlphabet, err := newAlphabetList(sidechainAlphabet, mainnetAlphabet)
|
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantMergeAlphabetListsFromMainNetAndSideChain,
|
2021-03-25 13:23:53 +00:00
|
|
|
zap.String("error", err.Error()))
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if newAlphabet == nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Info(context.Background(), logs.GovernanceNoGovernanceUpdateAlphabetListHasNotBeenChanged)
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Info(context.Background(), logs.GovernanceAlphabetListHasBeenChangedStartingUpdate,
|
2021-09-29 16:54:49 +00:00
|
|
|
zap.String("side_chain_alphabet", prettyKeys(sidechainAlphabet)),
|
|
|
|
zap.String("new_alphabet", prettyKeys(newAlphabet)),
|
|
|
|
)
|
2021-03-25 13:23:53 +00:00
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
votePrm := VoteValidatorPrm{
|
|
|
|
Validators: newAlphabet,
|
|
|
|
Hash: &txHash,
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// 1. Vote to sidechain committee via alphabet contracts.
|
2021-11-10 11:05:51 +00:00
|
|
|
err = gp.voter.VoteForSidechainValidator(votePrm)
|
2021-03-25 13:23:53 +00:00
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantVoteForSideChainCommittee,
|
2021-03-25 13:23:53 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// 2. Update NeoFSAlphabet role in the sidechain.
|
2023-03-30 07:35:16 +00:00
|
|
|
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)
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Info(context.Background(), logs.GovernanceFinishedAlphabetListUpdate)
|
2023-05-26 10:24:41 +00:00
|
|
|
|
|
|
|
return true
|
2023-03-30 07:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-07-22 10:04:37 +00:00
|
|
|
innerRing, err := gp.irFetcher.InnerRingKeys()
|
2021-03-25 13:23:53 +00:00
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantFetchInnerRingListFromSideChain,
|
2021-03-25 13:23:53 +00:00
|
|
|
zap.String("error", err.Error()))
|
2023-03-30 07:35:16 +00:00
|
|
|
return
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 07:35:16 +00:00
|
|
|
newInnerRing, err := updateInnerRing(innerRing, sidechainAlphabet, newAlphabet)
|
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantCreateNewInnerRingListWithNewAlphabetKeys,
|
2023-03-30 07:35:16 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(newInnerRing)
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Info(context.Background(), logs.GovernanceUpdateOfTheInnerRingList,
|
2023-03-30 07:35:16 +00:00
|
|
|
zap.String("before", prettyKeys(innerRing)),
|
|
|
|
zap.String("after", prettyKeys(newInnerRing)),
|
|
|
|
)
|
|
|
|
|
2023-05-18 08:19:08 +00:00
|
|
|
updPrm := client.UpdateAlphabetListPrm{}
|
|
|
|
updPrm.SetList(newInnerRing)
|
|
|
|
updPrm.SetHash(txHash)
|
2021-03-25 13:23:53 +00:00
|
|
|
|
2023-05-18 08:19:08 +00:00
|
|
|
if err = gp.morphClient.UpdateNeoFSAlphabetList(updPrm); err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantUpdateInnerRingListWithNewAlphabetKeys,
|
2023-03-30 07:35:16 +00:00
|
|
|
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 {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantUpdateListOfNotaryNodesInSideChain,
|
2023-03-30 07:35:16 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gp *Processor) updateFrostFSContractInMainnet(newAlphabet keys.PublicKeys) {
|
2021-03-25 13:23:53 +00:00
|
|
|
epoch := gp.epochState.EpochCounter()
|
|
|
|
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(buf, epoch)
|
|
|
|
|
|
|
|
id := append([]byte(alphabetUpdateIDPrefix), buf...)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
prm := frostfscontract.AlphabetUpdatePrm{}
|
2021-11-10 11:05:51 +00:00
|
|
|
|
|
|
|
prm.SetID(id)
|
|
|
|
prm.SetPubs(newAlphabet)
|
|
|
|
|
2023-03-30 07:35:16 +00:00
|
|
|
err := gp.frostfsClient.AlphabetUpdate(prm)
|
2021-03-25 13:23:53 +00:00
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
gp.log.Error(context.Background(), logs.GovernanceCantUpdateListOfAlphabetNodesInFrostfsContract,
|
2021-03-25 13:23:53 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
2021-09-29 16:54:49 +00:00
|
|
|
}
|