2021-03-25 13:23:53 +00:00
|
|
|
package governance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
frostfscontract "github.com/TrueCloudLab/frostfs-node/pkg/morph/client/neofs"
|
|
|
|
nmClient "github.com/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
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
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
func (gp *Processor) processAlphabetSync(txHash util.Uint256) {
|
2021-03-25 13:23:53 +00:00
|
|
|
if !gp.alphabetState.IsAlphabet() {
|
|
|
|
gp.log.Info("non alphabet mode, ignore alphabet sync")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mainnetAlphabet, err := gp.mainnetClient.NeoFSAlphabetList()
|
|
|
|
if err != nil {
|
|
|
|
gp.log.Error("can't fetch alphabet list from main net",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sidechainAlphabet, err := gp.morphClient.Committee()
|
|
|
|
if err != nil {
|
|
|
|
gp.log.Error("can't fetch alphabet list from side chain",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newAlphabet, err := newAlphabetList(sidechainAlphabet, mainnetAlphabet)
|
|
|
|
if err != nil {
|
|
|
|
gp.log.Error("can't merge alphabet lists from main net and side chain",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if newAlphabet == nil {
|
|
|
|
gp.log.Info("no governance update, alphabet list has not been changed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:54:49 +00:00
|
|
|
gp.log.Info("alphabet list has been changed, starting update",
|
|
|
|
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 {
|
|
|
|
gp.log.Error("can't vote for side chain committee",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// 2. Update NeoFSAlphabet role in the sidechain.
|
2021-07-22 10:04:37 +00:00
|
|
|
innerRing, err := gp.irFetcher.InnerRingKeys()
|
2021-03-25 13:23:53 +00:00
|
|
|
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)
|
|
|
|
|
2021-09-29 16:54:49 +00:00
|
|
|
gp.log.Info("update of the inner ring list",
|
|
|
|
zap.String("before", prettyKeys(innerRing)),
|
|
|
|
zap.String("after", prettyKeys(newInnerRing)),
|
|
|
|
)
|
|
|
|
|
2021-04-29 13:40:34 +00:00
|
|
|
if gp.notaryDisabled {
|
2022-01-31 11:58:55 +00:00
|
|
|
updPrm := nmClient.UpdateIRPrm{}
|
2021-11-10 11:05:51 +00:00
|
|
|
|
|
|
|
updPrm.SetKeys(newInnerRing)
|
|
|
|
updPrm.SetHash(txHash)
|
|
|
|
|
|
|
|
err = gp.netmapClient.UpdateInnerRing(updPrm)
|
2021-04-29 13:40:34 +00:00
|
|
|
} else {
|
2021-11-10 11:05:51 +00:00
|
|
|
updPrm := client.UpdateAlphabetListPrm{}
|
|
|
|
|
|
|
|
updPrm.SetList(newInnerRing)
|
|
|
|
updPrm.SetHash(txHash)
|
|
|
|
|
|
|
|
err = gp.morphClient.UpdateNeoFSAlphabetList(updPrm)
|
2021-04-29 13:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:53 +00:00
|
|
|
if err != nil {
|
|
|
|
gp.log.Error("can't update inner ring list with new alphabet keys",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:40:34 +00:00
|
|
|
if !gp.notaryDisabled {
|
2022-04-21 11:28:05 +00:00
|
|
|
// 3. Update notary role in the sidechain.
|
2021-11-10 11:05:51 +00:00
|
|
|
|
|
|
|
updPrm := client.UpdateNotaryListPrm{}
|
|
|
|
|
|
|
|
updPrm.SetList(newAlphabet)
|
|
|
|
updPrm.SetHash(txHash)
|
|
|
|
|
|
|
|
err = gp.morphClient.UpdateNotaryList(updPrm)
|
2021-04-29 13:40:34 +00:00
|
|
|
if err != nil {
|
|
|
|
gp.log.Error("can't update list of notary nodes in side chain",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// 4. Update NeoFS contract in the mainnet.
|
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)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
err = gp.frostfsClient.AlphabetUpdate(prm)
|
2021-03-25 13:23:53 +00:00
|
|
|
if err != nil {
|
2022-12-23 17:35:35 +00:00
|
|
|
gp.log.Error("can't update list of alphabet nodes in frostfs contract",
|
2021-03-25 13:23:53 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
gp.log.Info("finished alphabet list update")
|
|
|
|
}
|
2021-09-29 16:54:49 +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)
|
|
|
|
}
|