-
Notifications
You must be signed in to change notification settings - Fork 22
/
topology_key_rotate.go
230 lines (188 loc) · 6.97 KB
/
topology_key_rotate.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package validators
import (
"context"
"errors"
"fmt"
"sort"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/logging"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)
var (
ErrTargetBlockHeightMustBeGreaterThanCurrentHeight = errors.New("target block height must be greater then current block")
ErrNewVegaPubKeyIndexMustBeGreaterThenCurrentPubKeyIndex = errors.New("a new vega public key index must be greather then current public key index")
ErrInvalidVegaPubKeyForNode = errors.New("current vega public key is invalid for node")
ErrNodeAlreadyHasPendingKeyRotation = errors.New("node already has a pending key rotation")
ErrCurrentPubKeyHashDoesNotMatch = errors.New("current public key hash does not match")
)
type PendingKeyRotation struct {
BlockHeight uint64
NodeID string
NewPubKey string
NewKeyIndex uint32
}
type pendingKeyRotation struct {
newPubKey string
newKeyIndex uint32
}
// pendingKeyRotationMapping maps a block height => node id => new pending key rotation.
type pendingKeyRotationMapping map[uint64]map[string]pendingKeyRotation
func (pr pendingKeyRotationMapping) getSortedNodeIDsPerHeight(height uint64) []string {
rotationsPerHeight := pr[height]
if len(rotationsPerHeight) == 0 {
return nil
}
nodeIDs := make([]string, 0, len(rotationsPerHeight))
for nodeID := range rotationsPerHeight {
nodeIDs = append(nodeIDs, nodeID)
}
sort.Strings(nodeIDs)
return nodeIDs
}
func (t *Topology) hasPendingKeyRotation(nodeID string) bool {
for _, rotationsPerNodeID := range t.pendingPubKeyRotations {
if _, ok := rotationsPerNodeID[nodeID]; ok {
return true
}
}
return false
}
func (t *Topology) AddKeyRotate(ctx context.Context, nodeID string, currentBlockHeight uint64, kr *commandspb.KeyRotateSubmission) error {
t.mu.Lock()
defer t.mu.Unlock()
t.log.Debug("Adding key rotation",
logging.String("nodeID", nodeID),
logging.Uint64("currentBlockHeight", currentBlockHeight),
logging.Uint64("targetBlock", kr.TargetBlock),
logging.String("currentPubKeyHash", kr.CurrentPubKeyHash),
)
node, ok := t.validators[nodeID]
if !ok {
return fmt.Errorf("failed to add key rotate for non existing node %q", nodeID)
}
if t.hasPendingKeyRotation(nodeID) {
return ErrNodeAlreadyHasPendingKeyRotation
}
if currentBlockHeight > kr.TargetBlock {
return ErrTargetBlockHeightMustBeGreaterThanCurrentHeight
}
if node.data.VegaPubKeyIndex >= kr.NewPubKeyIndex {
return ErrNewVegaPubKeyIndexMustBeGreaterThenCurrentPubKeyIndex
}
hashedVegaPubKey, err := node.data.HashVegaPubKey()
if err != nil {
return err
}
if hashedVegaPubKey != kr.CurrentPubKeyHash {
return ErrCurrentPubKeyHashDoesNotMatch
}
if _, ok = t.pendingPubKeyRotations[kr.TargetBlock]; !ok {
t.pendingPubKeyRotations[kr.TargetBlock] = map[string]pendingKeyRotation{}
}
t.pendingPubKeyRotations[kr.TargetBlock][nodeID] = pendingKeyRotation{
newPubKey: kr.NewPubKey,
newKeyIndex: kr.NewPubKeyIndex,
}
t.log.Debug("Successfully added key rotation to pending key rotations",
logging.String("nodeID", nodeID),
logging.Uint64("currentBlockHeight", currentBlockHeight),
logging.Uint64("targetBlock", kr.TargetBlock),
)
return nil
}
func (t *Topology) GetPendingKeyRotation(blockHeight uint64, nodeID string) *PendingKeyRotation {
t.mu.RLock()
defer t.mu.RUnlock()
if _, ok := t.pendingPubKeyRotations[blockHeight]; !ok {
return nil
}
if pkr, ok := t.pendingPubKeyRotations[blockHeight][nodeID]; ok {
return &PendingKeyRotation{
BlockHeight: blockHeight,
NodeID: nodeID,
NewPubKey: pkr.newPubKey,
NewKeyIndex: pkr.newKeyIndex,
}
}
return nil
}
func (t *Topology) GetAllPendingKeyRotations() []*PendingKeyRotation {
t.mu.RLock()
defer t.mu.RUnlock()
pkrs := make([]*PendingKeyRotation, 0, len(t.pendingPubKeyRotations)*2)
blockHeights := make([]uint64, 0, len(t.pendingPubKeyRotations))
for blockHeight := range t.pendingPubKeyRotations {
blockHeights = append(blockHeights, blockHeight)
}
sort.Slice(blockHeights, func(i, j int) bool { return blockHeights[i] < blockHeights[j] })
for _, blockHeight := range blockHeights {
rotations := t.pendingPubKeyRotations[blockHeight]
nodeIDs := make([]string, 0, len(rotations))
for nodeID := range rotations {
nodeIDs = append(nodeIDs, nodeID)
}
sort.Strings(nodeIDs)
for _, nodeID := range nodeIDs {
r := rotations[nodeID]
pkrs = append(pkrs, &PendingKeyRotation{
BlockHeight: blockHeight,
NodeID: nodeID,
NewPubKey: r.newPubKey,
NewKeyIndex: r.newKeyIndex,
})
}
}
return pkrs
}
func (t *Topology) keyRotationBeginBlockLocked(ctx context.Context) {
t.log.Debug("Trying to apply pending key rotations", logging.Uint64("currentBlockHeight", t.currentBlockHeight))
// key swaps should run in deterministic order
nodeIDs := t.pendingPubKeyRotations.getSortedNodeIDsPerHeight(t.currentBlockHeight)
if len(nodeIDs) == 0 {
return
}
t.log.Debug("Applying pending key rotations", logging.Strings("nodeIDs", nodeIDs))
for _, nodeID := range nodeIDs {
data, ok := t.validators[nodeID]
if !ok {
// this should actually happen if validator was removed due to poor performance
t.log.Error("failed to rotate Vega key due to non present validator", logging.String("nodeID", nodeID))
continue
}
oldPubKey := data.data.VegaPubKey
rotation := t.pendingPubKeyRotations[t.currentBlockHeight][nodeID]
data.data.VegaPubKey = rotation.newPubKey
data.data.VegaPubKeyIndex = rotation.newKeyIndex
t.validators[nodeID] = data
t.notifyKeyChange(ctx, oldPubKey, rotation.newPubKey)
t.broker.Send(events.NewVegaKeyRotationEvent(ctx, nodeID, oldPubKey, rotation.newPubKey, t.currentBlockHeight))
t.log.Debug("Applied key rotation",
logging.String("nodeID", nodeID),
logging.String("oldPubKey", oldPubKey),
logging.String("newPubKey", rotation.newPubKey),
)
}
delete(t.pendingPubKeyRotations, t.currentBlockHeight)
}
func (t *Topology) NotifyOnKeyChange(fns ...func(ctx context.Context, oldPubKey, newPubKey string)) {
t.pubKeyChangeListeners = append(t.pubKeyChangeListeners, fns...)
}
func (t *Topology) notifyKeyChange(ctx context.Context, oldPubKey, newPubKey string) {
for _, f := range t.pubKeyChangeListeners {
f(ctx, oldPubKey, newPubKey)
}
}