-
Notifications
You must be signed in to change notification settings - Fork 13
/
consensus_new_round.go
124 lines (111 loc) · 4.21 KB
/
consensus_new_round.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
package consensus
import (
"fmt"
cstypes "github.com/okx/okbchain/libs/tendermint/consensus/types"
"github.com/okx/okbchain/libs/tendermint/types"
tmtime "github.com/okx/okbchain/libs/tendermint/types/time"
)
//-----------------------------------------------------------------------------
// State functions
// Used internally by handleTimeout and handleMsg to make state transitions
// Enter: `timeoutNewHeight` by startTime (R0PrevoteTime+timeoutCommit),
//
// or, if SkipTimeoutCommit==true, after receiving all precommits from (height,round-1)
//
// Enter: `timeoutPrecommits` after any +2/3 precommits from (height,round-1)
// Enter: +2/3 precommits for nil at (height,round-1)
// Enter: +2/3 prevotes any or +2/3 precommits for block or any from (height, round)
// NOTE: cs.StartTime was already set for height.
func (cs *State) enterNewRound(height int64, round int) {
logger := cs.Logger.With("height", height, "round", round)
if cs.Height != height || round < cs.Round || (cs.Round == round && cs.Step != cstypes.RoundStepNewHeight) {
logger.Debug(fmt.Sprintf(
"enterNewRound(%v/%v): Invalid args. Current step: %v/%v/%v",
height,
round,
cs.Height,
cs.Round,
cs.Step))
return
}
cs.doNewRound(height, round, false, nil)
}
func (cs *State) doNewRound(height int64, round int, avc bool, val *types.Validator) {
logger := cs.Logger.With("height", height, "round", round)
cs.initNewHeight()
if !avc {
if now := tmtime.Now(); cs.StartTime.After(now) {
logger.Info("Need to set a buffer and log message here for sanity.", "startTime", cs.StartTime, "now", now)
}
logger.Info(fmt.Sprintf("enterNewRound(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
// Increment validators if necessary
validators := cs.Validators
if cs.Round < round {
validators = validators.Copy()
validators.IncrementProposerPriority(round - cs.Round)
}
cs.Validators = validators
cs.Votes.SetRound(round + 1) // also track next round (round+1) to allow round-skipping
} else {
cs.trc.Pin("NewRoundVC-%d", round)
logger.Info(fmt.Sprintf("enterNewRoundAVC(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))
cs.Validators.Proposer = val
if cs.Votes.Round() == 0 {
cs.Votes.SetRound(1) // also track next round (round+1) to allow round-skipping
}
}
// Setup new round
// we don't fire newStep for this step,
// but we fire an event, so update the round step first
cs.updateRoundStep(round, cstypes.RoundStepNewRound)
cs.HasVC = avc
if round == 0 {
// We've already reset these upon new height,
// and meanwhile we might have received a proposal
// for round 0.
} else {
logger.Info("Resetting Proposal info")
cs.Proposal = nil
cs.ProposalBlock = nil
cs.ProposalBlockParts = nil
}
cs.TriggeredTimeoutPrecommit = false
cs.eventBus.PublishEventNewRound(cs.NewRoundEvent())
cs.metrics.Rounds.Set(float64(round))
// Wait for txs to be available in the mempool
// before we enterPropose in round 0. If the last block changed the app hash,
// we may need an empty "proof" block, and enterPropose immediately.
waitForTxs := cs.config.WaitForTxs() && round == 0 && !cs.needProofBlock(height)
if waitForTxs {
if cs.config.CreateEmptyBlocksInterval > 0 {
cs.scheduleTimeout(cs.config.CreateEmptyBlocksInterval, height, round,
cstypes.RoundStepNewRound)
}
} else {
cs.enterPropose(height, round)
}
}
func (cs *State) enterNewRoundAVC(height int64, round int, val *types.Validator) {
logger := cs.Logger.With("height", height, "round", round)
if round != 0 || cs.Round != 0 || cs.Height != height {
logger.Debug(fmt.Sprintf(
"enterNewRoundAVC(%v/%v): Invalid args. Current step: %v/%v/%v",
height,
round,
cs.Height,
cs.Round,
cs.Step))
return
}
cs.doNewRound(height, round, true, val)
}
// Enter: `timeoutNewHeight` by startTime (after timeoutCommit),
func (cs *State) enterNewHeight(height int64) {
cs.Logger.Info("enterNewHeight", "vcMsg", cs.vcMsg, "proposer", cs.Validators.Proposer.Address)
if GetActiveVC() && cs.vcMsg != nil && cs.vcMsg.Validate(height, cs.Validators.Proposer.Address) {
_, val := cs.Validators.GetByAddress(cs.vcMsg.NewProposer)
cs.enterNewRoundAVC(height, 0, val)
} else {
cs.enterNewRound(height, 0)
}
}