-
Notifications
You must be signed in to change notification settings - Fork 19
/
engine.go
343 lines (296 loc) · 11.7 KB
/
engine.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package protocolupgrade
import (
"context"
"errors"
"fmt"
"sort"
"sync"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/txn"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
"github.com/blang/semver"
"github.com/cenkalti/backoff"
"github.com/golang/protobuf/proto"
)
type protocolUpgradeProposal struct {
blockHeight uint64
vegaReleaseTag string
accepted map[string]struct{}
}
func protocolUpgradeProposalID(upgradeBlockHeight uint64, vegaReleaseTag string) string {
return fmt.Sprintf("%v@%v", vegaReleaseTag, upgradeBlockHeight)
}
// TrimReleaseTag removes 'v' or 'V' at the beginning of the tag if present.
func TrimReleaseTag(tag string) string {
if len(tag) == 0 {
return tag
}
switch tag[0] {
case 'v', 'V':
return tag[1:]
default:
return tag
}
}
func (p *protocolUpgradeProposal) approvers() []string {
accepted := make([]string, 0, len(p.accepted))
for k := range p.accepted {
accepted = append(accepted, k)
}
sort.Strings(accepted)
return accepted
}
type ValidatorTopology interface {
IsTendermintValidator(pubkey string) bool
IsSelfTendermintValidator() bool
GetVotingPower(pubkey string) int64
GetTotalVotingPower() int64
}
type Commander interface {
CommandSync(ctx context.Context, cmd txn.Command, payload proto.Message, f func(error), bo *backoff.ExponentialBackOff)
}
type Broker interface {
Send(event events.Event)
}
type Engine struct {
log *logging.Logger
config Config
broker Broker
topology ValidatorTopology
hashKeys []string
currentVersion string
currentBlockHeight uint64
activeProposals map[string]*protocolUpgradeProposal
events map[string]*eventspb.ProtocolUpgradeEvent
lock sync.RWMutex
requiredMajority num.Decimal
upgradeStatus *types.UpgradeStatus
}
func New(log *logging.Logger, config Config, broker Broker, topology ValidatorTopology, version string) *Engine {
log = log.Named(namedLogger)
log.SetLevel(config.Level.Get())
return &Engine{
activeProposals: map[string]*protocolUpgradeProposal{},
events: map[string]*eventspb.ProtocolUpgradeEvent{},
log: log,
config: config,
broker: broker,
topology: topology,
hashKeys: []string{(&types.PayloadProtocolUpgradeProposals{}).Key()},
upgradeStatus: &types.UpgradeStatus{},
currentVersion: version,
}
}
func (e *Engine) OnRequiredMajorityChanged(_ context.Context, requiredMajority num.Decimal) error {
e.requiredMajority = requiredMajority
return nil
}
func (e *Engine) IsValidProposal(ctx context.Context, pk string, upgradeBlockHeight uint64, vegaReleaseTag string) error {
if !e.topology.IsTendermintValidator(pk) {
// not a tendermint validator, so we don't care about their intention
return errors.New("only tendermint validator can propose a protocol upgrade")
}
if upgradeBlockHeight == 0 {
return errors.New("upgrade block out of range")
}
if upgradeBlockHeight <= e.currentBlockHeight {
return errors.New("upgrade block earlier than current block height")
}
newv, err := semver.Parse(TrimReleaseTag(vegaReleaseTag))
if err != nil {
err = fmt.Errorf("invalid protocol version for upgrade received: version (%s), %w", vegaReleaseTag, err)
e.log.Error("", logging.Error(err))
return err
}
if semver.MustParse(TrimReleaseTag(e.currentVersion)).GT(newv) {
return errors.New("upgrade version is too old")
}
return nil
}
// UpgradeProposal records the intention of a validator to upgrade the protocol to a release tag at block height.
func (e *Engine) UpgradeProposal(ctx context.Context, pk string, upgradeBlockHeight uint64, vegaReleaseTag string) error {
e.lock.RLock()
defer e.lock.RUnlock()
e.log.Debug("Adding protocol upgrade proposal",
logging.String("validatorPubKey", pk),
logging.Uint64("upgradeBlockHeight", upgradeBlockHeight),
logging.String("vegaReleaseTag", vegaReleaseTag),
logging.String("currentVersion", e.currentVersion),
)
if err := e.IsValidProposal(ctx, pk, upgradeBlockHeight, vegaReleaseTag); err != nil {
return err
}
ID := protocolUpgradeProposalID(upgradeBlockHeight, vegaReleaseTag)
// if the proposed upgrade version is different from the current version we create a new proposal and keep it
// if it is the same as the current version, this is taken as a signal to withdraw previous vote for another proposal - in this case the validator will have no vote for no proposal.
if vegaReleaseTag != e.currentVersion {
// if it's the first time we see this ID, generate an active proposal entry
if _, ok := e.activeProposals[ID]; !ok {
e.activeProposals[ID] = &protocolUpgradeProposal{
blockHeight: upgradeBlockHeight,
vegaReleaseTag: vegaReleaseTag,
accepted: map[string]struct{}{},
}
}
active := e.activeProposals[ID]
active.accepted[pk] = struct{}{}
e.sendAndKeepEvent(ctx, ID, active)
e.log.Debug("Successfully added protocol upgrade proposal",
logging.String("validatorPubKey", pk),
logging.Uint64("upgradeBlockHeight", upgradeBlockHeight),
logging.String("vegaReleaseTag", vegaReleaseTag),
)
}
activeIDs := make([]string, 0, len(e.activeProposals))
for k := range e.activeProposals {
activeIDs = append(activeIDs, k)
}
sort.Strings(activeIDs)
// each validator can only have one vote so if we got a vote for a different release than they voted for before, we remove that vote
for _, activeID := range activeIDs {
if activeID == ID {
continue
}
activeProposal := e.activeProposals[activeID]
// if there is a vote for another proposal from the pk, remove it and send an update
if _, ok := activeProposal.accepted[pk]; ok {
delete(activeProposal.accepted, pk)
e.sendAndKeepEvent(ctx, activeID, activeProposal)
e.log.Debug("Removed validator vote from previous proposal",
logging.String("validatorPubKey", pk),
logging.Uint64("upgradeBlockHeight", activeProposal.blockHeight),
logging.String("vegaReleaseTag", activeProposal.vegaReleaseTag),
)
}
if len(activeProposal.accepted) == 0 {
delete(e.activeProposals, activeID)
delete(e.events, activeID)
e.log.Debug("Removed previous upgrade proposal",
logging.String("validatorPubKey", pk),
logging.Uint64("upgradeBlockHeight", activeProposal.blockHeight),
logging.String("vegaReleaseTag", activeProposal.vegaReleaseTag),
)
}
}
return nil
}
func (e *Engine) sendAndKeepEvent(ctx context.Context, ID string, activeProposal *protocolUpgradeProposal) {
status := eventspb.ProtocolUpgradeProposalStatus_PROTOCOL_UPGRADE_PROPOSAL_STATUS_PENDING
if len(activeProposal.approvers()) == 0 {
status = eventspb.ProtocolUpgradeProposalStatus_PROTOCOL_UPGRADE_PROPOSAL_STATUS_REJECTED
}
evt := events.NewProtocolUpgradeProposalEvent(ctx, activeProposal.blockHeight, activeProposal.vegaReleaseTag, activeProposal.approvers(), status)
evtProto := evt.Proto()
e.events[ID] = &evtProto
e.broker.Send(evt)
}
// TimeForUpgrade is called by abci at the beginning of the block (before calling begin block of the engine) - if a block height for upgrade is set and is equal
// to the last block's height then return true.
func (e *Engine) TimeForUpgrade() bool {
e.lock.RLock()
defer e.lock.RUnlock()
return e.upgradeStatus.AcceptedReleaseInfo != nil && e.currentBlockHeight-e.upgradeStatus.AcceptedReleaseInfo.UpgradeBlockHeight == 0
}
func (e *Engine) isAccepted(p *protocolUpgradeProposal) bool {
// if the block is already behind us or we've already accepted a proposal return false
if p.blockHeight < e.currentBlockHeight {
return false
}
totalVotingPower := e.topology.GetTotalVotingPower()
if totalVotingPower <= 0 {
return false
}
totalD := num.DecimalFromInt64(totalVotingPower)
ratio := num.DecimalZero()
for k := range p.accepted {
ratio = ratio.Add(num.DecimalFromInt64(e.topology.GetVotingPower(k)).Div(totalD))
}
return ratio.GreaterThan(e.requiredMajority)
}
func (e *Engine) getProposalIDs() []string {
proposalIDs := make([]string, 0, len(e.activeProposals))
for k := range e.activeProposals {
proposalIDs = append(proposalIDs, k)
}
sort.Strings(proposalIDs)
return proposalIDs
}
// BeginBlock is called at the beginning of the block, to mark the current block height and check if there are proposals that are accepted/rejected.
// If there is more than one active proposal that is accepted (unlikely) we choose the one with the earliest upgrade block.
func (e *Engine) BeginBlock(ctx context.Context, blockHeight uint64) {
e.lock.Lock()
e.currentBlockHeight = blockHeight
e.lock.Unlock()
var accepted *protocolUpgradeProposal
for _, ID := range e.getProposalIDs() {
pup := e.activeProposals[ID]
if e.isAccepted(pup) {
if accepted == nil || accepted.blockHeight > pup.blockHeight {
accepted = pup
}
} else {
if blockHeight >= pup.blockHeight {
delete(e.activeProposals, ID)
delete(e.events, ID)
e.log.Info("protocol upgrade rejected", logging.String("vega-release-tag", pup.vegaReleaseTag), logging.Uint64("upgrade-block-height", pup.blockHeight))
e.broker.Send(events.NewProtocolUpgradeProposalEvent(ctx, pup.blockHeight, pup.vegaReleaseTag, pup.approvers(), eventspb.ProtocolUpgradeProposalStatus_PROTOCOL_UPGRADE_PROPOSAL_STATUS_REJECTED))
}
}
}
e.lock.Lock()
if accepted != nil {
e.upgradeStatus.AcceptedReleaseInfo = &types.ReleaseInfo{
VegaReleaseTag: accepted.vegaReleaseTag,
UpgradeBlockHeight: accepted.blockHeight,
}
} else {
e.upgradeStatus.AcceptedReleaseInfo = &types.ReleaseInfo{}
}
e.lock.Unlock()
}
// Cleanup is called by the abci before the final snapshot is taken to clear remaining state. It emits events for the accepted and rejected proposals.
func (e *Engine) Cleanup(ctx context.Context) {
e.lock.Lock()
defer e.lock.Unlock()
for _, ID := range e.getProposalIDs() {
pup := e.activeProposals[ID]
status := eventspb.ProtocolUpgradeProposalStatus_PROTOCOL_UPGRADE_PROPOSAL_STATUS_APPROVED
if !e.isAccepted(pup) {
e.log.Info("protocol upgrade rejected", logging.String("vega-release-tag", pup.vegaReleaseTag), logging.Uint64("upgrade-block-height", pup.blockHeight))
status = eventspb.ProtocolUpgradeProposalStatus_PROTOCOL_UPGRADE_PROPOSAL_STATUS_REJECTED
}
e.broker.Send(events.NewProtocolUpgradeProposalEvent(ctx, pup.blockHeight, pup.vegaReleaseTag, pup.approvers(), status))
delete(e.activeProposals, ID)
delete(e.events, ID)
}
}
// SetReadyForUpgrade is called by abci after a snapshot has been taken and the process is ready to be shutdown.
func (e *Engine) SetReadyForUpgrade() {
e.lock.Lock()
defer e.lock.Unlock()
if int(e.currentBlockHeight)-int(e.upgradeStatus.AcceptedReleaseInfo.UpgradeBlockHeight) != 0 {
e.log.Panic("can only call SetReadyForUpgrade at the block of the block height for upgrade", logging.Uint64("block-height", e.currentBlockHeight), logging.Int("block-height-for-upgrade", int(e.upgradeStatus.AcceptedReleaseInfo.UpgradeBlockHeight)))
}
e.log.Info("marking vega as ready to shut down")
e.upgradeStatus.ReadyToUpgrade = true
}
// GetUpgradeStatus is an RPC call that returns the status of an upgrade.
func (e *Engine) GetUpgradeStatus() types.UpgradeStatus {
e.lock.RLock()
defer e.lock.RUnlock()
return *e.upgradeStatus
}