Skip to content

Commit e17a336

Browse files
authored
Merge pull request #134 from getamis/feature/consensus-stop
consensus/istanbul: send PREPARE instead of COMMIT after receiving PREPREPARE when we have locked proposal
2 parents dccfb94 + db9ca82 commit e17a336

File tree

2 files changed

+10
-50
lines changed

2 files changed

+10
-50
lines changed

consensus/istanbul/core/preprepare.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,6 @@ func (c *core) handlePreprepare(msg *message, src istanbul.Validator) error {
5858
// Ensure we have the same view with the preprepare message
5959
// If it is old message, see if we need to broadcast COMMIT
6060
if err := c.checkMessage(msgPreprepare, preprepare.View); err != nil {
61-
if err == errOldMessage {
62-
// Get validator set for the given proposal
63-
valSet := c.backend.ParentValidators(preprepare.Proposal).Copy()
64-
previousProposer := c.backend.GetProposer(preprepare.Proposal.Number().Uint64() - 1)
65-
valSet.CalcProposer(previousProposer, preprepare.View.Round.Uint64())
66-
// Broadcast COMMIT if it is an existing block
67-
// 1. The proposer needs to be a proposer matches the given (Sequence + Round)
68-
// 2. The given block must exist
69-
if valSet.IsProposer(src.Address()) && c.backend.HasBlock(preprepare.Proposal.Hash(), preprepare.Proposal.Number()) {
70-
c.sendCommitForOldBlock(preprepare.View, preprepare.Proposal.Hash())
71-
return nil
72-
}
73-
}
7461
return err
7562
}
7663

@@ -100,21 +87,14 @@ func (c *core) handlePreprepare(msg *message, src istanbul.Validator) error {
10087

10188
// Here is about to accept the preprepare
10289
if c.state == StateAcceptRequest {
103-
// If it is locked, it can only process on the locked block
104-
// Otherwise, broadcast PREPARE and enter Prepared state
105-
if c.current.IsHashLocked() {
106-
// Broadcast COMMIT directly if the proposal matches the locked block
107-
// Otherwise, send ROUND CHANGE
108-
if preprepare.Proposal.Hash() == c.current.GetLockedHash() {
109-
// Broadcast COMMIT and enters Prepared state directly
110-
c.acceptPreprepare(preprepare)
111-
c.setState(StatePrepared)
112-
c.sendCommit()
113-
} else {
114-
// Send round change
115-
c.sendNextRoundChange()
116-
}
90+
// Send ROUND CHANGE if the locked proposal and the received proposal are different
91+
if c.current.IsHashLocked() && preprepare.Proposal.Hash() != c.current.GetLockedHash() {
92+
// Send round change
93+
c.sendNextRoundChange()
11794
} else {
95+
// Either
96+
// 1. the locked proposal and the received proposal match
97+
// 2. we have no locked proposal
11898
c.acceptPreprepare(preprepare)
11999
c.setState(StatePreprepared)
120100
c.sendPrepare()

consensus/istanbul/core/preprepare_test.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestHandlePreprepare(t *testing.T) {
111111
false,
112112
},
113113
{
114-
// ErrInvalidMessage
114+
// errOldMessage
115115
func() *testSystem {
116116
sys := NewTestSystemWithBackend(N, F)
117117

@@ -130,26 +130,6 @@ func TestHandlePreprepare(t *testing.T) {
130130
errOldMessage,
131131
false,
132132
},
133-
{
134-
// ErrInvalidMessage
135-
func() *testSystem {
136-
sys := NewTestSystemWithBackend(N, F)
137-
138-
for i, backend := range sys.backends {
139-
c := backend.engine.(*core)
140-
c.valSet = backend.peers
141-
if i != 0 {
142-
c.state = StatePreprepared
143-
c.current.SetSequence(big.NewInt(10))
144-
c.current.SetRound(big.NewInt(10))
145-
}
146-
}
147-
return sys
148-
}(),
149-
makeBlock(5), //only height 5 will retrun true on backend.HasBlock, see testSystemBackend.HashBlock
150-
nil,
151-
true,
152-
},
153133
}
154134

155135
OUTER:
@@ -293,8 +273,8 @@ func TestHandlePreprepareWithLock(t *testing.T) {
293273
t.Errorf("error mismatch: have %v, want nil", err)
294274
}
295275
if test.proposal == test.lockProposal {
296-
if c.state != StatePrepared {
297-
t.Errorf("state mismatch: have %v, want %v", c.state, StatePrepared)
276+
if c.state != StatePreprepared {
277+
t.Errorf("state mismatch: have %v, want %v", c.state, StatePreprepared)
298278
}
299279
if !reflect.DeepEqual(curView, c.currentView()) {
300280
t.Errorf("view mismatch: have %v, want %v", c.currentView(), curView)

0 commit comments

Comments
 (0)