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
|
|
|
|
2024-10-21 09:21:01 +00:00
|
|
|
func (gp *Processor) processAlphabetSync(ctx context.Context, txHash util.Uint256) bool {
|
2024-10-21 13:27:28 +00:00
|
|
|
if !gp.alphabetState.IsAlphabet(ctx) {
|
2024-10-21 09:21:01 +00:00
|
|
|
gp.log.Info(ctx, 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 09:21:01 +00:00
|
|
|
gp.log.Error(ctx, 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 09:21:01 +00:00
|
|
|
gp.log.Error(ctx, 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 09:21:01 +00:00
|
|
|
gp.log.Error(ctx, 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 09:21:01 +00:00
|
|
|
gp.log.Info(ctx, logs.GovernanceNoGovernanceUpdateAlphabetListHasNotBeenChanged)
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 09:21:01 +00:00
|
|
|
gp.log.Info(ctx, 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.
|
2024-10-21 09:21:01 +00:00
|
|
|
err = gp.voter.VoteForSidechainValidator(ctx, votePrm)
|
2021-03-25 13:23:53 +00:00
|
|
|
if err != nil {
|
2024-10-21 09:21:01 +00:00
|
|
|
gp.log.Error(ctx, 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.
|
2024-10-21 13:27:28 +00:00
|
|
|
gp.updateNeoFSAlphabetRoleInSidechain(ctx, sidechainAlphabet, newAlphabet, txHash)
|
2023-03-30 07:35:16 +00:00
|
|
|
|
|
|
|
// 3. Update notary role in the sidechain.
|
2024-10-21 13:27:28 +00:00
|
|
|
gp.updateNotaryRoleInSidechain(ctx, newAlphabet, txHash)
|
2023-03-30 07:35:16 +00:00
|
|
|
|
|
|
|
// 4. Update FrostFS contract in the mainnet.
|
2024-10-21 13:27:28 +00:00
|
|
|
gp.updateFrostFSContractInMainnet(ctx, newAlphabet)
|
2023-03-30 07:35:16 +00:00
|
|
|
|
2024-10-21 09:21:01 +00:00
|
|
|
gp.log.Info(ctx, 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)
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (gp *Processor) updateNeoFSAlphabetRoleInSidechain(ctx context.Context, 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 13:27:28 +00:00
|
|
|
gp.log.Error(ctx, 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 13:27:28 +00:00
|
|
|
gp.log.Error(ctx, logs.GovernanceCantCreateNewInnerRingListWithNewAlphabetKeys,
|
2023-03-30 07:35:16 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(newInnerRing)
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
gp.log.Info(ctx, 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
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
if err = gp.morphClient.UpdateNeoFSAlphabetList(ctx, updPrm); err != nil {
|
|
|
|
gp.log.Error(ctx, logs.GovernanceCantUpdateInnerRingListWithNewAlphabetKeys,
|
2023-03-30 07:35:16 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (gp *Processor) updateNotaryRoleInSidechain(ctx context.Context, newAlphabet keys.PublicKeys, txHash util.Uint256) {
|
2023-03-30 07:35:16 +00:00
|
|
|
updPrm := client.UpdateNotaryListPrm{}
|
|
|
|
|
|
|
|
updPrm.SetList(newAlphabet)
|
|
|
|
updPrm.SetHash(txHash)
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
err := gp.morphClient.UpdateNotaryList(ctx, updPrm)
|
2023-03-30 07:35:16 +00:00
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
gp.log.Error(ctx, logs.GovernanceCantUpdateListOfNotaryNodesInSideChain,
|
2023-03-30 07:35:16 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (gp *Processor) updateFrostFSContractInMainnet(ctx context.Context, 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)
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
err := gp.frostfsClient.AlphabetUpdate(ctx, prm)
|
2021-03-25 13:23:53 +00:00
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
gp.log.Error(ctx, logs.GovernanceCantUpdateListOfAlphabetNodesInFrostfsContract,
|
2021-03-25 13:23:53 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
2021-09-29 16:54:49 +00:00
|
|
|
}
|