github.com/hyperion-hyn/go-ethereum@v2.4.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  
    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  		// if it's a future block, we will handle it again after the duration
    86  		if err == consensus.ErrFutureBlock {
    87  			logger.Info("Proposed block will be handled in the future", "err", err, "duration", duration)
    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  			logger.Warn("Failed to verify proposal", "err", err, "duration", duration)
    97  			c.sendNextRoundChange()
    98  		}
    99  		return err
   100  	}
   101  
   102  	// Here is about to accept the PRE-PREPARE
   103  	if c.state == StateAcceptRequest {
   104  		// Send ROUND CHANGE if the locked proposal and the received proposal are different
   105  		if c.current.IsHashLocked() {
   106  			if preprepare.Proposal.Hash() == c.current.GetLockedHash() {
   107  				// Broadcast COMMIT and enters Prepared state directly
   108  				c.acceptPreprepare(preprepare)
   109  				c.setState(StatePrepared)
   110  				c.sendCommit()
   111  			} else {
   112  				// Send round change
   113  				c.sendNextRoundChange()
   114  			}
   115  		} else {
   116  			// Either
   117  			//   1. the locked proposal and the received proposal match
   118  			//   2. we have no locked proposal
   119  			c.acceptPreprepare(preprepare)
   120  			c.setState(StatePreprepared)
   121  			c.sendPrepare()
   122  		}
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func (c *core) acceptPreprepare(preprepare *istanbul.Preprepare) {
   129  	c.consensusTimestamp = time.Now()
   130  	c.current.SetPreprepare(preprepare)
   131  }