github.com/ConsenSys/Quorum@v20.10.0+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  	// If I'm the proposer and I have the same sequence with the proposal
    29  	if c.current.Sequence().Cmp(request.Proposal.Number()) == 0 && c.IsProposer() {
    30  		curView := c.currentView()
    31  		preprepare, err := Encode(&istanbul.Preprepare{
    32  			View:     curView,
    33  			Proposal: request.Proposal,
    34  		})
    35  		if err != nil {
    36  			logger.Error("Failed to encode", "view", curView)
    37  			return
    38  		}
    39  		c.broadcast(&message{
    40  			Code: msgPreprepare,
    41  			Msg:  preprepare,
    42  		})
    43  	}
    44  }
    45  
    46  func (c *core) handlePreprepare(msg *message, src istanbul.Validator) error {
    47  	logger := c.logger.New("from", src, "state", c.state)
    48  
    49  	// Decode PRE-PREPARE
    50  	var preprepare *istanbul.Preprepare
    51  	err := msg.Decode(&preprepare)
    52  	if err != nil {
    53  		return errFailedDecodePreprepare
    54  	}
    55  
    56  	// Ensure we have the same view with the PRE-PREPARE message
    57  	// If it is old message, see if we need to broadcast COMMIT
    58  	if err := c.checkMessage(msgPreprepare, preprepare.View); err != nil {
    59  		if err == errOldMessage {
    60  			// Get validator set for the given proposal
    61  			valSet := c.backend.ParentValidators(preprepare.Proposal).Copy()
    62  			previousProposer := c.backend.GetProposer(preprepare.Proposal.Number().Uint64() - 1)
    63  			valSet.CalcProposer(previousProposer, preprepare.View.Round.Uint64())
    64  			// Broadcast COMMIT if it is an existing block
    65  			// 1. The proposer needs to be a proposer matches the given (Sequence + Round)
    66  			// 2. The given block must exist
    67  			if valSet.IsProposer(src.Address()) && c.backend.HasPropsal(preprepare.Proposal.Hash(), preprepare.Proposal.Number()) {
    68  				c.sendCommitForOldBlock(preprepare.View, preprepare.Proposal.Hash())
    69  				return nil
    70  			}
    71  		}
    72  		return err
    73  	}
    74  
    75  	// Check if the message comes from current proposer
    76  	if !c.valSet.IsProposer(src.Address()) {
    77  		logger.Warn("Ignore preprepare messages from non-proposer")
    78  		return errNotFromProposer
    79  	}
    80  
    81  	// Verify the proposal we received
    82  	if duration, err := c.backend.Verify(preprepare.Proposal); err != nil {
    83  		// if it's a future block, we will handle it again after the duration
    84  		if err == consensus.ErrFutureBlock {
    85  			logger.Info("Proposed block will be handled in the future", "err", err, "duration", duration)
    86  			c.stopFuturePreprepareTimer()
    87  			c.futurePreprepareTimer = time.AfterFunc(duration, func() {
    88  				c.sendEvent(backlogEvent{
    89  					src: src,
    90  					msg: msg,
    91  				})
    92  			})
    93  		} else {
    94  			logger.Warn("Failed to verify proposal", "err", err, "duration", duration)
    95  			c.sendNextRoundChange()
    96  		}
    97  		return err
    98  	}
    99  
   100  	// Here is about to accept the PRE-PREPARE
   101  	if c.state == StateAcceptRequest {
   102  		// Send ROUND CHANGE if the locked proposal and the received proposal are different
   103  		if c.current.IsHashLocked() {
   104  			if preprepare.Proposal.Hash() == c.current.GetLockedHash() {
   105  				// Broadcast COMMIT and enters Prepared state directly
   106  				c.acceptPreprepare(preprepare)
   107  				c.setState(StatePrepared)
   108  				c.sendCommit()
   109  			} else {
   110  				// Send round change
   111  				c.sendNextRoundChange()
   112  			}
   113  		} else {
   114  			// Either
   115  			//   1. the locked proposal and the received proposal match
   116  			//   2. we have no locked proposal
   117  			c.acceptPreprepare(preprepare)
   118  			c.setState(StatePreprepared)
   119  			c.sendPrepare()
   120  		}
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  func (c *core) acceptPreprepare(preprepare *istanbul.Preprepare) {
   127  	c.consensusTimestamp = time.Now()
   128  	c.current.SetPreprepare(preprepare)
   129  }