github.com/pfcoder/quorum@v2.0.3-0.20180501191142-d4a1b0958135+incompatible/consensus/istanbul/core/preprepare.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "time" 21 22 "github.com/ethereum/go-ethereum/consensus" 23 "github.com/ethereum/go-ethereum/consensus/istanbul" 24 ) 25 26 func (c *core) sendPreprepare(request *istanbul.Request) { 27 logger := c.logger.New("state", c.state) 28 29 // If I'm the proposer and I have the same sequence with the proposal 30 if c.current.Sequence().Cmp(request.Proposal.Number()) == 0 && c.isProposer() { 31 curView := c.currentView() 32 preprepare, err := Encode(&istanbul.Preprepare{ 33 View: curView, 34 Proposal: request.Proposal, 35 }) 36 if err != nil { 37 logger.Error("Failed to encode", "view", curView) 38 return 39 } 40 41 c.broadcast(&message{ 42 Code: msgPreprepare, 43 Msg: preprepare, 44 }) 45 } 46 } 47 48 func (c *core) handlePreprepare(msg *message, src istanbul.Validator) error { 49 logger := c.logger.New("from", src, "state", c.state) 50 51 // Decode PRE-PREPARE 52 var preprepare *istanbul.Preprepare 53 err := msg.Decode(&preprepare) 54 if err != nil { 55 return errFailedDecodePreprepare 56 } 57 58 // Ensure we have the same view with the PRE-PREPARE message 59 // If it is old message, see if we need to broadcast COMMIT 60 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.HasPropsal(preprepare.Proposal.Hash(), preprepare.Proposal.Number()) { 70 c.sendCommitForOldBlock(preprepare.View, preprepare.Proposal.Hash()) 71 return nil 72 } 73 } 74 return err 75 } 76 77 // Check if the message comes from current proposer 78 if !c.valSet.IsProposer(src.Address()) { 79 logger.Warn("Ignore preprepare messages from non-proposer") 80 return errNotFromProposer 81 } 82 83 // Verify the proposal we received 84 if duration, err := c.backend.Verify(preprepare.Proposal); err != nil { 85 logger.Warn("Failed to verify proposal", "err", err, "duration", duration) 86 // if it's a future block, we will handle it again after the duration 87 if err == consensus.ErrFutureBlock { 88 c.stopFuturePreprepareTimer() 89 c.futurePreprepareTimer = time.AfterFunc(duration, func() { 90 c.sendEvent(backlogEvent{ 91 src: src, 92 msg: msg, 93 }) 94 }) 95 } else { 96 c.sendNextRoundChange() 97 } 98 return err 99 } 100 101 // Here is about to accept the PRE-PREPARE 102 if c.state == StateAcceptRequest { 103 // Send ROUND CHANGE if the locked proposal and the received proposal are different 104 if c.current.IsHashLocked() { 105 if preprepare.Proposal.Hash() == c.current.GetLockedHash() { 106 // Broadcast COMMIT and enters Prepared state directly 107 c.acceptPreprepare(preprepare) 108 c.setState(StatePrepared) 109 c.sendCommit() 110 } else { 111 // Send round change 112 c.sendNextRoundChange() 113 } 114 } else { 115 // Either 116 // 1. the locked proposal and the received proposal match 117 // 2. we have no locked proposal 118 c.acceptPreprepare(preprepare) 119 c.setState(StatePreprepared) 120 c.sendPrepare() 121 } 122 } 123 124 return nil 125 } 126 127 func (c *core) acceptPreprepare(preprepare *istanbul.Preprepare) { 128 c.consensusTimestamp = time.Now() 129 c.current.SetPreprepare(preprepare) 130 }