-
Notifications
You must be signed in to change notification settings - Fork 38
/
process_update.go
141 lines (112 loc) · 3.6 KB
/
process_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package governance
import (
"encoding/binary"
"encoding/hex"
"sort"
"strings"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs"
"go.uber.org/zap"
)
const (
alphabetUpdateIDPrefix = "AlphabetUpdate"
)
func (gp *Processor) processAlphabetSync(txHash util.Uint256) {
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
}
gp.log.Info("alphabet list has been changed, starting update",
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("can't vote for side chain committee",
zap.String("error", err.Error()))
}
// 2. Update NeoFSAlphabet role in the sidechain.
innerRing, err := gp.irFetcher.InnerRingKeys()
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)
gp.log.Info("update of the inner ring list",
zap.String("before", prettyKeys(innerRing)),
zap.String("after", prettyKeys(newInnerRing)),
)
updPrm := client.UpdateAlphabetListPrm{}
updPrm.SetList(newInnerRing)
updPrm.SetHash(txHash)
err = gp.morphClient.UpdateNeoFSAlphabetList(updPrm)
if err != nil {
gp.log.Error("can't update inner ring list with new alphabet keys",
zap.String("error", err.Error()))
}
}
}
// 3. Update notary role in the sidechain.
updPrm := client.UpdateNotaryListPrm{}
updPrm.SetList(newAlphabet)
updPrm.SetHash(txHash)
err = gp.morphClient.UpdateNotaryList(updPrm)
if err != nil {
gp.log.Error("can't update list of notary nodes in side chain",
zap.String("error", err.Error()))
}
// 4. Update NeoFS contract in the mainnet.
id := binary.LittleEndian.AppendUint64([]byte(alphabetUpdateIDPrefix), gp.epochState.EpochCounter())
prm := neofscontract.AlphabetUpdatePrm{}
prm.SetID(id)
prm.SetPubs(newAlphabet)
err = gp.neofsClient.AlphabetUpdate(prm)
if err != nil {
gp.log.Error("can't update list of alphabet nodes in neofs contract",
zap.String("error", err.Error()))
}
gp.log.Info("finished alphabet list update")
}
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)
}