github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/consensus/istanbul/ibft/core/prepare.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  	"reflect"
    21  
    22  	"github.com/kisexp/xdchain/consensus/istanbul"
    23  	istanbulcommon "github.com/kisexp/xdchain/consensus/istanbul/common"
    24  	ibfttypes "github.com/kisexp/xdchain/consensus/istanbul/ibft/types"
    25  )
    26  
    27  func (c *core) sendPrepare() {
    28  	logger := c.logger.New("state", c.state)
    29  
    30  	sub := c.current.Subject()
    31  	encodedSubject, err := ibfttypes.Encode(sub)
    32  	if err != nil {
    33  		logger.Error("Failed to encode", "subject", sub)
    34  		return
    35  	}
    36  	c.broadcast(&ibfttypes.Message{
    37  		Code: ibfttypes.MsgPrepare,
    38  		Msg:  encodedSubject,
    39  	})
    40  }
    41  
    42  func (c *core) handlePrepare(msg *ibfttypes.Message, src istanbul.Validator) error {
    43  	// Decode PREPARE message
    44  	var prepare *istanbul.Subject
    45  	err := msg.Decode(&prepare)
    46  	if err != nil {
    47  		return istanbulcommon.ErrFailedDecodePrepare
    48  	}
    49  
    50  	if err := c.checkMessage(ibfttypes.MsgPrepare, prepare.View); err != nil {
    51  		return err
    52  	}
    53  
    54  	// If it is locked, it can only process on the locked block.
    55  	// Passing verifyPrepare and checkMessage implies it is processing on the locked block since it was verified in the Preprepared state.
    56  	if err := c.verifyPrepare(prepare, src); err != nil {
    57  		return err
    58  	}
    59  
    60  	c.acceptPrepare(msg, src)
    61  
    62  	// Change to Prepared state if we've received enough PREPARE messages or it is locked
    63  	// and we are in earlier state before Prepared state.
    64  	if ((c.current.IsHashLocked() && prepare.Digest == c.current.GetLockedHash()) || c.current.GetPrepareOrCommitSize() >= c.QuorumSize()) &&
    65  		c.state.Cmp(ibfttypes.StatePrepared) < 0 {
    66  		c.current.LockHash()
    67  		c.setState(ibfttypes.StatePrepared)
    68  		c.sendCommit()
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  // verifyPrepare verifies if the received PREPARE message is equivalent to our subject
    75  func (c *core) verifyPrepare(prepare *istanbul.Subject, src istanbul.Validator) error {
    76  	logger := c.logger.New("from", src, "state", c.state)
    77  
    78  	sub := c.current.Subject()
    79  	if !reflect.DeepEqual(prepare, sub) {
    80  		logger.Warn("Inconsistent subjects between PREPARE and proposal", "expected", sub, "got", prepare)
    81  		return istanbulcommon.ErrInconsistentSubject
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func (c *core) acceptPrepare(msg *ibfttypes.Message, src istanbul.Validator) error {
    88  	logger := c.logger.New("from", src, "state", c.state)
    89  
    90  	// Add the PREPARE message to current round state
    91  	if err := c.current.Prepares.Add(msg); err != nil {
    92  		logger.Error("Failed to add PREPARE message to round state", "msg", msg, "err", err)
    93  		return err
    94  	}
    95  
    96  	return nil
    97  }