Skip to content

Commit fea5979

Browse files
tailingchenmarkya0616
authored andcommitted
consensus/pbft: avoid to rollback to previous state
1 parent 89cf557 commit fea5979

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

consensus/pbft/core/commit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ func (c *core) handleCommit(msg *message, src pbft.Validator) error {
6262

6363
c.acceptCommit(msg, src)
6464

65-
if int64(c.current.Commits.Size()) > 2*c.F && c.state != StateCommitted {
65+
// if has proposal and receives enough commit messages,
66+
// it can chnage to StateCommitted directly to speed up the consensus process
67+
if int64(c.current.Commits.Size()) > 2*c.F && c.state.Cmp(StateCommitted) < 0 {
6668
c.commit()
6769
}
6870

consensus/pbft/core/prepare.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ func (c *core) handlePrepare(msg *message, src pbft.Validator) error {
6262

6363
c.acceptPrepare(msg, src)
6464

65-
// If 2f+1
66-
if int64(c.current.Prepares.Size()) > 2*c.F && c.state != StatePrepared {
65+
// change to StatePrepared if receving enough prepare messages
66+
// and the current state is at previous state
67+
if int64(c.current.Prepares.Size()) > 2*c.F && c.state.Cmp(StatePrepared) < 0 {
6768
c.setState(StatePrepared)
6869
c.sendCommit()
6970
}

consensus/pbft/core/types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ func (s State) String() string {
6060
}
6161
}
6262

63+
// Cmp compares s and y and returns:
64+
// -1 if s is the previous state of y
65+
// 0 if s and y are the same state
66+
// +1 if s is the next state of y
67+
func (s State) Cmp(y State) int {
68+
if uint64(s) < uint64(y) {
69+
return -1
70+
}
71+
if uint64(s) > uint64(y) {
72+
return 1
73+
}
74+
return 0
75+
}
76+
6377
const (
6478
msgPreprepare uint64 = iota
6579
msgPrepare

0 commit comments

Comments
 (0)