/
consensus_main_routine.go
409 lines (358 loc) · 12.6 KB
/
consensus_main_routine.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
package consensus
import (
"bytes"
"fmt"
"reflect"
"runtime/debug"
"time"
cfg "github.com/brc20-collab/brczero/libs/tendermint/config"
cstypes "github.com/brc20-collab/brczero/libs/tendermint/consensus/types"
"github.com/brc20-collab/brczero/libs/tendermint/libs/fail"
"github.com/brc20-collab/brczero/libs/tendermint/types"
tmtime "github.com/brc20-collab/brczero/libs/tendermint/types/time"
)
//-----------------------------------------
// the main go routines
// receiveRoutine handles messages which may cause state transitions.
// it's argument (n) is the number of messages to process before exiting - use 0 to run forever
// It keeps the RoundState and is the only thing that updates it.
// Updates (state transitions) happen on timeouts, complete proposals, and 2/3 majorities.
// State must be locked before any internal state is updated.
func (cs *State) receiveRoutine(maxSteps int) {
onExit := func(cs *State) {
// NOTE: the internalMsgQueue may have signed messages from our
// priv_val that haven't hit the WAL, but its ok because
// priv_val tracks LastSig
// close wal now that we're done writing to it
cs.wal.Stop()
cs.wal.Wait()
close(cs.done)
cs.done = nil
}
defer func() {
if r := recover(); r != nil {
cs.Logger.Error("CONSENSUS FAILURE!!!", "err", r, "stack", string(debug.Stack()))
// stop gracefully
//
// NOTE: We most probably shouldn't be running any further when there is
// some unexpected panic. Some unknown error happened, and so we don't
// know if that will result in the validator signing an invalid thing. It
// might be worthwhile to explore a mechanism for manual resuming via
// some console or secure RPC system, but for now, halting the chain upon
// unexpected consensus bugs sounds like the better option.
onExit(cs)
}
}()
for {
if maxSteps > 0 {
if cs.nSteps >= maxSteps {
cs.Logger.Info("reached max steps. exiting receive routine")
cs.nSteps = 0
return
}
}
rs := cs.RoundState
var mi msgInfo
select {
case <-cs.txNotifier.TxsAvailable():
cs.handleTxsAvailable()
case mi = <-cs.peerMsgQueue:
// handles proposals, block parts, votes
// may generate internal events (votes, complete proposals, 2/3 majorities)
if cs.handleMsg(mi) {
cs.wal.Write(mi)
}
case mi = <-cs.internalMsgQueue:
err := cs.wal.WriteSync(mi) // NOTE: fsync
if err != nil {
panic(fmt.Sprintf("Failed to write %v msg to consensus wal due to %v. Check your FS and restart the node", mi, err))
}
if _, ok := mi.Msg.(*VoteMessage); ok {
// we actually want to simulate failing during
// the previous WriteSync, but this isn't easy to do.
// Equivalent would be to fail here and manually remove
// some bytes from the end of the wal.
fail.Fail() // XXX
}
// handles proposals, block parts, votes
cs.handleMsg(mi)
case ti := <-cs.timeoutTicker.Chan(): // tockChan:
cs.wal.Write(ti)
// if the timeout is relevant to the rs
// go to the next step
cs.handleTimeout(ti, rs)
case <-cs.Quit():
onExit(cs)
return
}
}
}
func (cs *State) handleAVCProposal(proposal *types.Proposal) {
if !GetActiveVC() ||
cs.Height != proposal.Height || cs.Round != proposal.Round ||
len(cs.taskResultChan) == 0 {
return
}
res := cs.getPreBlockResult(proposal.Height)
if res == nil {
cs.Logger.Error("handleAVCProposal get block nil", "cs height", cs.Height, "proposal height", proposal.Height)
return
}
if !bytes.Equal(proposal.BlockID.PartsHeader.Hash, res.blockParts.Header().Hash) || proposal.Height != res.block.Height {
return
}
cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""})
for i := 0; i < res.blockParts.Total(); i++ {
part := res.blockParts.GetPart(i)
cs.sendInternalMessage(msgInfo{&BlockPartMessage{cs.Height, cs.Round, part}, ""})
}
}
// state transitions on complete-proposal, 2/3-any, 2/3-one
func (cs *State) handleMsg(mi msgInfo) (added bool) {
cs.mtx.Lock()
defer cs.mtx.Unlock()
var (
err error
)
msg, peerID := mi.Msg, mi.PeerID
switch msg := msg.(type) {
case *ProposeResponseMessage:
cs.handleAVCProposal(msg.Proposal)
case *ViewChangeMessage:
if !GetActiveVC() {
return
}
// no need to handle duplicate vcMsg
if cs.vcMsg != nil && cs.vcMsg.Height >= msg.Height {
return
}
// enterNewHeight use cs.vcMsg
if msg.Height == cs.Height+1 {
cs.vcMsg = msg
cs.Logger.Info("handle vcMsg", "height", cs.Height, "vcMsg", cs.vcMsg)
} else if msg.Height == cs.Height {
// ApplyBlock of height-1 has finished
// at this height, it has enterNewHeight
// vc immediately
cs.vcMsg = msg
cs.Logger.Info("handle vcMsg", "height", cs.Height, "vcMsg", cs.vcMsg)
if cs.Step != cstypes.RoundStepNewHeight && cs.Round == 0 {
_, val := cs.Validators.GetByAddress(msg.NewProposer)
cs.enterNewRoundAVC(cs.Height, 0, val)
}
}
case *ProposalMessage:
// will not cause transition.
// once proposal is set, we can receive block parts
if added, err = cs.setProposal(msg.Proposal); added {
cs.handleAVCProposal(msg.Proposal)
}
case *BlockPartMessage:
// if avc and has 2/3 votes, it can use the blockPartsHeader from votes
if cs.HasVC && cs.ProposalBlockParts == nil && cs.Round == 0 {
prevotes := cs.Votes.Prevotes(cs.Round)
blockID, hasTwoThirds := prevotes.TwoThirdsMajority()
if hasTwoThirds && !blockID.IsZero() {
cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartsHeader)
}
}
// if the proposal is complete, we'll enterPrevote or tryFinalizeCommit
added, err = cs.addProposalBlockPart(msg, peerID)
// We unlock here to yield to any routines that need to read the the RoundState.
// Previously, this code held the lock from the point at which the final block
// part was received until the block executed against the application.
// This prevented the reactor from being able to retrieve the most updated
// version of the RoundState. The reactor needs the updated RoundState to
// gossip the now completed block.
//
// This code can be further improved by either always operating on a copy
// of RoundState and only locking when switching out State's copy of
// RoundState with the updated copy or by emitting RoundState events in
// more places for routines depending on it to listen for.
cs.mtx.Unlock()
cs.mtx.Lock()
if added && cs.ProposalBlockParts.IsComplete() && err == nil {
cs.handleCompleteProposal(msg.Height)
}
if added {
cs.statsMsgQueue <- mi
}
if err != nil && msg.Round != cs.Round {
cs.Logger.Debug(
"Received block part from wrong round",
"height",
cs.Height,
"csRound",
cs.Round,
"blockRound",
msg.Round)
err = nil
}
case *VoteMessage:
// attempt to add the vote and dupeout the validator if its a duplicate signature
// if the vote gives us a 2/3-any or 2/3-one, we transition
added, err = cs.tryAddVote(msg.Vote, peerID)
if added {
cs.statsMsgQueue <- mi
}
// if err == ErrAddingVote {
// TODO: punish peer
// We probably don't want to stop the peer here. The vote does not
// necessarily comes from a malicious peer but can be just broadcasted by
// a typical peer.
// https://github.com/tendermint/tendermint/issues/1281
// }
// NOTE: the vote is broadcast to peers by the reactor listening
// for vote events
// TODO: If rs.Height == vote.Height && rs.Round < vote.Round,
// the peer is sending us CatchupCommit precommits.
// We could make note of this and help filter in broadcastHasVoteMessage().
default:
cs.Logger.Error("Unknown msg type", "type", reflect.TypeOf(msg))
return
}
if err != nil { // nolint:staticcheck
// Causes TestReactorValidatorSetChanges to timeout
// https://github.com/tendermint/tendermint/issues/3406
// cs.Logger.Error("Error with msg", "height", cs.Height, "round", cs.Round,
// "peer", peerID, "err", err, "msg", msg)
}
return
}
func (cs *State) handleTimeout(ti timeoutInfo, rs cstypes.RoundState) {
cs.Logger.Debug("Received tock", "timeout", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step)
// timeouts must be for current height, round, step
if ti.Height != rs.Height || ti.Round < rs.Round || (ti.Round == rs.Round && ti.Step < rs.Step) {
cs.Logger.Debug("Ignoring tock because we're ahead", "height", rs.Height, "round", rs.Round, "step", rs.Step)
return
}
// the timeout will now cause a state transition
cs.mtx.Lock()
defer cs.mtx.Unlock()
switch ti.Step {
case cstypes.RoundStepNewHeight:
// NewRound event fired from enterNewRound.
// XXX: should we fire timeout here (for timeout commit)?
cs.enterNewHeight(ti.Height)
case cstypes.RoundStepNewRound:
cs.enterPropose(ti.Height, 0)
case cstypes.RoundStepPropose:
cs.eventBus.PublishEventTimeoutPropose(cs.RoundStateEvent())
cs.enterPrevote(ti.Height, ti.Round)
case cstypes.RoundStepPrevoteWait:
cs.eventBus.PublishEventTimeoutWait(cs.RoundStateEvent())
cs.enterPrecommit(ti.Height, ti.Round)
case cstypes.RoundStepPrecommitWait:
cs.eventBus.PublishEventTimeoutWait(cs.RoundStateEvent())
cs.enterPrecommit(ti.Height, ti.Round)
cs.enterNewRound(ti.Height, ti.Round+1)
default:
panic(fmt.Sprintf("Invalid timeout step: %v", ti.Step))
}
}
// enterNewRound(height, 0) at cs.StartTime.
func (cs *State) scheduleRound0(rs *cstypes.RoundState) {
overDuration := tmtime.Now().Sub(cs.StartTime)
if overDuration < 0 {
overDuration = 0
}
sleepDuration := cfg.DynamicConfig.GetCsTimeoutCommit() - overDuration
if sleepDuration < 0 {
sleepDuration = 0
}
if !cs.config.Waiting {
sleepDuration = 0
}
if GetActiveVC() && cs.privValidator != nil {
select {
case cs.preBlockTaskChan <- &preBlockTask{cs.Height, sleepDuration}:
default:
}
}
cs.scheduleTimeout(sleepDuration, rs.Height, 0, cstypes.RoundStepNewHeight)
}
// requestForProposer FireEvent to broadcast ProposeRequestMessage
func (cs *State) requestForProposer(prMsg ProposeRequestMessage) {
if signature, err := cs.privValidator.SignBytes(prMsg.SignBytes()); err == nil {
prMsg.Signature = signature
cs.evsw.FireEvent(types.EventProposeRequest, &prMsg)
} else {
cs.Logger.Error("requestForProposer", "err", err)
}
}
// Attempt to schedule a timeout (by sending timeoutInfo on the tickChan)
func (cs *State) scheduleTimeout(duration time.Duration, height int64, round int, step cstypes.RoundStepType) {
cs.timeoutTicker.ScheduleTimeout(timeoutInfo{Duration: duration, Height: height, Round: round, Step: step})
}
// send a msg into the receiveRoutine regarding our own proposal, block part, or vote
func (cs *State) sendInternalMessage(mi msgInfo) {
select {
case cs.internalMsgQueue <- mi:
default:
// NOTE: using the go-routine means our votes can
// be processed out of order.
// TODO: use CList here for strict determinism and
// attempt push to internalMsgQueue in receiveRoutine
cs.Logger.Info("Internal msg queue is full. Using a go-routine")
go func() { cs.internalMsgQueue <- mi }()
}
}
func (cs *State) handleTxsAvailable() {
cs.mtx.Lock()
defer cs.mtx.Unlock()
// We only need to do this for round 0.
if cs.Round != 0 {
return
}
switch cs.Step {
case cstypes.RoundStepNewHeight: // timeoutCommit phase
//todo when only brczeroData of mempool, it's only be called by block commit.
//if cs.needProofBlock(cs.Height) {
// // enterPropose will be called by enterNewRound
// return
//}
// +1ms to ensure RoundStepNewRound timeout always happens after RoundStepNewHeight
timeoutCommit := cs.StartTime.Sub(tmtime.Now()) + 1*time.Millisecond
cs.scheduleTimeout(timeoutCommit, cs.Height, 0, cstypes.RoundStepNewRound)
case cstypes.RoundStepNewRound: // after timeoutCommit
cs.enterPropose(cs.Height, 0)
}
}
func (cs *State) preMakeBlockRoutine() {
for {
select {
case task := <-cs.preBlockTaskChan:
if task.height == cs.Height {
cs.preMakeBlock(task.height, task.duration)
}
case <-cs.Quit():
return
}
}
}
func (cs *State) isValidator() bool {
if cs.privValidator == nil || cs.privValidatorPubKey == nil {
return false
}
if !cs.Validators.HasAddress(cs.privValidatorPubKey.Address()) {
return false
}
return true
}
func (cs *State) rpcDeliverTxs(btcHeight int64) {
if cs.isValidator() {
return
}
for h := btcHeight; h <= btcHeight+5; h++ {
brczeroData, err := cs.blockExec.GetZeroDataByBTCHeight(h)
if err != nil || brczeroData.IsConfirmed || brczeroData.Delivered {
continue
}
mockBlock, _ := cs.createMockBlock(h, brczeroData)
// when DeliverTx, the stores(mpt and iavl) use Set()/Delete() and the cache the kv
types.RpcFlag = types.RpcDeliverTxsMode
_, _ = cs.blockExec.DeliverTxsForBrczeroRpc(mockBlock)
types.RpcFlag = types.RpcDefaultMode
cs.blockExec.SetZeroDataDelivered(h, true)
}
}