-
Notifications
You must be signed in to change notification settings - Fork 85
/
instance.go
439 lines (384 loc) · 14.8 KB
/
instance.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package instance
import (
"context"
"encoding/hex"
"fmt"
"sync"
"time"
specqbft "github.com/bloxapp/ssv-spec/qbft"
spectypes "github.com/bloxapp/ssv-spec/types"
"github.com/pkg/errors"
"go.uber.org/atomic"
"go.uber.org/zap"
beaconprotocol "github.com/bloxapp/ssv/protocol/v1/blockchain/beacon"
"github.com/bloxapp/ssv/protocol/v1/message"
protcolp2p "github.com/bloxapp/ssv/protocol/v1/p2p"
"github.com/bloxapp/ssv/protocol/v1/qbft"
"github.com/bloxapp/ssv/protocol/v1/qbft/instance/forks"
"github.com/bloxapp/ssv/protocol/v1/qbft/instance/leader"
"github.com/bloxapp/ssv/protocol/v1/qbft/instance/msgcont"
msgcontinmem "github.com/bloxapp/ssv/protocol/v1/qbft/instance/msgcont/inmem"
"github.com/bloxapp/ssv/protocol/v1/qbft/instance/roundtimer"
"github.com/bloxapp/ssv/protocol/v1/qbft/pipelines"
qbftstorage "github.com/bloxapp/ssv/protocol/v1/qbft/storage"
"github.com/bloxapp/ssv/protocol/v1/qbft/validation/signedmsg"
)
// Options defines option attributes for the Instance
type Options struct {
Logger *zap.Logger
ValidatorShare *beaconprotocol.Share
Network protcolp2p.Network
LeaderSelector leader.Selector
Config *qbft.InstanceConfig
Identifier []byte
Height specqbft.Height
// RequireMinPeers flag to require minimum peers before starting an instance
// useful for tests where we want (sometimes) to avoid networking
RequireMinPeers bool
// Fork sets the current fork to apply on instance
Fork forks.Fork
SSVSigner spectypes.SSVSigner
ChangeRoundStore qbftstorage.ChangeRoundStore
}
// Instance defines the instance attributes
type Instance struct {
ValidatorShare *beaconprotocol.Share
State *qbft.State
network protcolp2p.Network
LeaderSelector leader.Selector
Config *qbft.InstanceConfig
roundTimer *roundtimer.RoundTimer
Logger *zap.Logger
fork forks.Fork
SsvSigner spectypes.SSVSigner
// messages
ContainersMap map[specqbft.MessageType]msgcont.MessageContainer
decidedMsg *specqbft.SignedMessage
// channels
stageChangedChan chan qbft.RoundState
// flags
stopped atomic.Bool
initialized bool
// locks
runInitOnce *sync.Once
runStopOnce *sync.Once
processChangeRoundQuorumOnce *sync.Once
processPrepareQuorumOnce *sync.Once
processCommitQuorumOnce *sync.Once
lastChangeRoundMsgLock sync.RWMutex
stageChanCloseChan sync.Mutex
changeRoundStore qbftstorage.ChangeRoundStore
ctx context.Context
cancelCtx context.CancelFunc
}
// NewInstanceWithState used for testing, not PROD!
func NewInstanceWithState(state *qbft.State) Instancer {
return &Instance{
State: state,
}
}
// NewInstance is the constructor of Instance
func NewInstance(opts *Options) Instancer {
messageID := message.ToMessageID(opts.Identifier)
metricsIBFTStage.WithLabelValues(messageID.GetRoleType().String(), hex.EncodeToString(messageID.GetPubKey())).Set(float64(qbft.RoundStateNotStarted))
logger := opts.Logger.With(zap.Uint64("instance height", uint64(opts.Height)))
ctx, cancelCtx := context.WithCancel(context.Background())
ret := &Instance{
ctx: ctx,
cancelCtx: cancelCtx,
ValidatorShare: opts.ValidatorShare,
State: GenerateState(opts),
network: opts.Network,
LeaderSelector: opts.LeaderSelector,
Config: opts.Config,
Logger: logger,
SsvSigner: opts.SSVSigner,
roundTimer: roundtimer.New(ctx, logger.With(zap.String("who", "RoundTimer"))),
// locks
runInitOnce: &sync.Once{},
runStopOnce: &sync.Once{},
processChangeRoundQuorumOnce: &sync.Once{},
processPrepareQuorumOnce: &sync.Once{},
processCommitQuorumOnce: &sync.Once{},
lastChangeRoundMsgLock: sync.RWMutex{},
stageChanCloseChan: sync.Mutex{},
changeRoundStore: opts.ChangeRoundStore,
stopped: *atomic.NewBool(false),
}
ret.ContainersMap = map[specqbft.MessageType]msgcont.MessageContainer{
specqbft.ProposalMsgType: msgcontinmem.New(uint64(opts.ValidatorShare.ThresholdSize()), uint64(opts.ValidatorShare.PartialThresholdSize())),
specqbft.PrepareMsgType: msgcontinmem.New(uint64(opts.ValidatorShare.ThresholdSize()), uint64(opts.ValidatorShare.PartialThresholdSize())),
specqbft.CommitMsgType: msgcontinmem.New(uint64(opts.ValidatorShare.ThresholdSize()), uint64(opts.ValidatorShare.PartialThresholdSize())),
specqbft.RoundChangeMsgType: msgcontinmem.New(uint64(opts.ValidatorShare.ThresholdSize()), uint64(opts.ValidatorShare.PartialThresholdSize())),
}
ret.setFork(opts.Fork)
return ret
}
// Init must be called before start can be
func (i *Instance) Init() {
i.runInitOnce.Do(func() {
go i.startRoundTimerLoop()
i.initialized = true
i.Logger.Debug("iBFT instance init finished")
})
}
// GetState returns instance state
func (i *Instance) GetState() *qbft.State {
return i.State
}
// Containers returns map of containers
func (i *Instance) Containers() map[specqbft.MessageType]msgcont.MessageContainer {
return i.ContainersMap
}
// Start implements the Algorithm 1 IBFTController pseudocode for process pi: constants, state variables, and ancillary procedures
// procedure Start(λ, value)
// λi ← λ
// ri ← 1
// pri ← ⊥
// pvi ← ⊥
// inputValue i ← value
// if leader(hi, ri) = pi then
// broadcast ⟨PROPOSAL, λi, ri, inputV aluei⟩ message
// set timer to running and expire after t(ri)
func (i *Instance) Start(inputValue []byte) error {
if !i.initialized {
return errors.New("instance not initialized")
}
if inputValue == nil {
return errors.New("input value is nil")
}
messageID := message.ToMessageID(i.GetState().GetIdentifier())
i.Logger.Info("Node is starting iBFT instance", zap.String("identifier", hex.EncodeToString(i.GetState().GetIdentifier())))
i.GetState().InputValue.Store(inputValue)
i.GetState().Round.Store(specqbft.Round(1)) // start from 1
i.GetState().Stage.Store(int32(qbft.RoundStateReady)) // for the queue to process by state only from this point
metricsIBFTStage.WithLabelValues(messageID.GetRoleType().String(), hex.EncodeToString(messageID.GetPubKey())).Set(float64(qbft.RoundStateReady))
metricsIBFTRound.WithLabelValues(messageID.GetRoleType().String(), hex.EncodeToString(messageID.GetPubKey())).Set(1)
i.Logger.Debug("state", zap.Uint64("round", uint64(i.GetState().GetRound())))
if i.IsLeader() {
go func() {
i.Logger.Info("Node is leader for round 1")
//i.ProcessStageChange(qbft.RoundStateProposal) we need to process the proposal msg in order to broadcast to prepare msg
// LeaderProposalDelaySeconds waits to let other nodes complete their instance start or round change.
// Waiting will allow a more stable msg receiving for all parties.
time.Sleep(time.Duration(i.Config.LeaderProposalDelaySeconds))
msg, err := i.GenerateProposalMessage(&specqbft.ProposalData{
Data: i.GetState().GetInputValue(),
})
if err != nil {
i.Logger.Warn("failed to generate proposal message", zap.Error(err))
return
}
if err := i.SignAndBroadcast(&msg); err != nil {
i.Logger.Error("could not broadcast proposal", zap.Error(err))
}
}()
}
i.ResetRoundTimer() // TODO could be race condition with message process?
return nil
}
// Stop will trigger a stopped for the entire instance
func (i *Instance) Stop() {
// stop can be run just once
i.runStopOnce.Do(func() {
i.stop()
i.cancelCtx()
})
}
// stop stops the instance
func (i *Instance) stop() {
i.Logger.Info("stopping iBFT instance...")
i.Logger.Debug("STOPPING IBFTController -> set stopped to true")
i.stopped.Store(true)
i.Logger.Debug("STOPPING IBFTController -> kill round timer")
i.roundTimer.Kill()
i.Logger.Debug("STOPPING IBFTController -> stopped round timer")
i.ProcessStageChange(qbft.RoundStateStopped)
i.Logger.Debug("STOPPING IBFTController -> round stage set stopped")
// stop stage chan
if i.stageChangedChan != nil {
i.Logger.Debug("STOPPING IBFTController -> lock stage chan")
i.stageChanCloseChan.Lock() // in order to prevent from sending to a close chan
i.Logger.Debug("STOPPING IBFTController -> closing stage changed chan")
close(i.stageChangedChan)
i.Logger.Debug("STOPPING IBFTController -> closed stageChangedChan")
i.stageChangedChan = nil
i.Logger.Debug("STOPPING IBFTController -> stageChangedChan nilled")
i.stageChanCloseChan.Unlock()
i.Logger.Debug("STOPPING IBFTController -> stageChangedChan chan unlocked")
}
i.Logger.Info("stopped iBFT instance")
}
// Stopped is stopping queue work
func (i *Instance) Stopped() bool {
return i.stopped.Load()
}
// ProcessMsg will process the message
func (i *Instance) ProcessMsg(msg *specqbft.SignedMessage) (bool, error) {
if err := msg.Validate(); err != nil {
return false, errors.Wrap(err, "invalid signed message")
}
var p pipelines.SignedMessagePipeline
switch msg.Message.MsgType {
case specqbft.ProposalMsgType:
p = i.ProposalMsgPipeline()
case specqbft.PrepareMsgType:
p = i.PrepareMsgPipeline()
case specqbft.CommitMsgType:
p = i.CommitMsgPipeline()
case specqbft.RoundChangeMsgType:
p = i.ChangeRoundMsgPipeline()
default:
i.Logger.Warn("undefined message type", zap.Any("msg", msg))
return false, fmt.Errorf("undefined message type")
}
if err := p.Run(msg); err != nil {
if errors.Is(err, signedmsg.ErrWrongRound) {
i.Logger.Debug(fmt.Sprintf("message type %d, round (%d) does not equal state round (%d)", msg.Message.MsgType, msg.Message.Round, i.GetState().GetRound()))
}
return false, err
}
if i.GetState().Stage.Load() == int32(qbft.RoundStateDecided) {
return true, nil
}
return false, nil
}
// BumpRound is used to set bump round by 1
func (i *Instance) BumpRound() {
i.bumpToRound(i.GetState().GetRound() + 1)
}
func (i *Instance) bumpToRound(round specqbft.Round) {
i.processChangeRoundQuorumOnce = &sync.Once{}
i.processPrepareQuorumOnce = &sync.Once{}
newRound := round
i.GetState().Round.Store(newRound)
messageID := message.ToMessageID(i.GetState().GetIdentifier())
metricsIBFTRound.WithLabelValues(messageID.GetRoleType().String(), hex.EncodeToString(messageID.GetPubKey())).Set(float64(newRound))
}
// ProcessStageChange set the state's round state and pushed the new state into the state channel
func (i *Instance) ProcessStageChange(stage qbft.RoundState) {
// in order to prevent race condition between timer timeout and decided state. once decided we need to prevent any other new state
currentStage := i.GetState().Stage.Load()
if currentStage == int32(qbft.RoundStateStopped) {
return
}
if currentStage == int32(qbft.RoundStateDecided) && stage != qbft.RoundStateStopped {
return
}
messageID := message.ToMessageID(i.GetState().GetIdentifier())
metricsIBFTStage.WithLabelValues(messageID.GetRoleType().String(), hex.EncodeToString(messageID.GetPubKey())).Set(float64(stage))
i.GetState().Stage.Store(int32(stage))
// blocking send to channel
i.stageChanCloseChan.Lock()
defer i.stageChanCloseChan.Unlock()
if i.stageChangedChan != nil {
i.stageChangedChan <- stage
}
}
// GetStageChan returns a RoundState channel added to the stateChangesChans array
func (i *Instance) GetStageChan() chan qbft.RoundState {
if i.stageChangedChan == nil {
i.stageChangedChan = make(chan qbft.RoundState, 1) // buffer of 1 in order to support process stop stage right after decided
}
return i.stageChangedChan
}
// SignAndBroadcast checks and adds the signed message to the appropriate round state type
func (i *Instance) SignAndBroadcast(msg *specqbft.Message) error {
i.Logger.Debug("broadcasting consensus msg",
zap.Int("type", int(msg.MsgType)),
zap.Int64("height", int64(msg.Height)),
zap.Int64("round", int64(msg.Round)),
)
pk, err := i.ValidatorShare.OperatorSharePubKey()
if err != nil {
return errors.Wrap(err, "could not find operator pk for signing msg")
}
sigByts, err := i.SsvSigner.SignRoot(msg, spectypes.QBFTSignatureType, pk.Serialize())
if err != nil {
return err
}
signedMessage := &specqbft.SignedMessage{
Message: msg,
Signature: sigByts,
Signers: []spectypes.OperatorID{i.ValidatorShare.NodeID},
}
// used for instance fast change round catchup
if msg.MsgType == specqbft.RoundChangeMsgType {
i.setLastChangeRoundMsg(signedMessage)
}
encodedMsg, err := signedMessage.Encode()
if err != nil {
return errors.New("could not encode message")
}
ssvMsg := spectypes.SSVMessage{
MsgType: spectypes.SSVConsensusMsgType,
MsgID: message.ToMessageID(i.GetState().GetIdentifier()),
Data: encodedMsg,
}
if i.network != nil {
return i.network.Broadcast(ssvMsg)
}
return errors.New("no networking, could not broadcast msg")
}
func (i *Instance) setLastChangeRoundMsg(msg *specqbft.SignedMessage) {
_ = i.changeRoundStore.SaveLastChangeRoundMsg(msg)
}
//// GetLastChangeRoundMsg returns the latest broadcasted msg from the instance
//func (i *Instance) GetLastChangeRoundMsg() *specqbft.SignedMessage {
// err := i.changeRoundStore.GetLastChangeRoundMsg()
//}
// CommittedAggregatedMsg returns a signed message for the state's committed value with the max known signatures
func (i *Instance) CommittedAggregatedMsg() (*specqbft.SignedMessage, error) {
if i.GetState() == nil {
return nil, errors.New("missing instance state")
}
if i.decidedMsg != nil {
return i.decidedMsg, nil
}
return nil, errors.New("missing decided message")
}
// GetCommittedAggSSVMessage returns ssv msg with message.SSVDecidedMsgType and the agg commit signed msg
func (i *Instance) GetCommittedAggSSVMessage() (spectypes.SSVMessage, error) {
decidedMsg, err := i.CommittedAggregatedMsg()
if err != nil {
return spectypes.SSVMessage{}, err
}
encodedAgg, err := decidedMsg.Encode()
if err != nil {
return spectypes.SSVMessage{}, errors.Wrap(err, "failed to encode agg message")
}
ssvMsg := spectypes.SSVMessage{
MsgType: spectypes.SSVDecidedMsgType,
MsgID: message.ToMessageID(i.GetState().GetIdentifier()),
Data: encodedAgg,
}
return ssvMsg, nil
}
func (i *Instance) setFork(fork forks.Fork) {
if fork == nil {
return
}
i.fork = fork
//i.fork.Apply(i)
}
// GenerateState return generated state
func GenerateState(opts *Options) *qbft.State {
var identifier, height, round, preparedRound, preparedValue, iv, proposalReceivedForCurrentRound atomic.Value
height.Store(opts.Height)
round.Store(specqbft.Round(0))
identifier.Store(opts.Identifier[:])
preparedRound.Store(specqbft.Round(0))
preparedValue.Store([]byte(nil))
iv.Store([]byte{})
proposalReceivedForCurrentRound.Store((*specqbft.SignedMessage)(nil))
return &qbft.State{
Stage: *atomic.NewInt32(int32(qbft.RoundStateNotStarted)),
Identifier: identifier,
Height: height,
InputValue: iv,
Round: round,
PreparedRound: preparedRound,
PreparedValue: preparedValue,
ProposalAcceptedForCurrentRound: proposalReceivedForCurrentRound,
}
}