github.com/klaytn/klaytn@v1.12.1/consensus/istanbul/core/request.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from quorum/consensus/istanbul/core/request.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package core
    22  
    23  import (
    24  	"github.com/klaytn/klaytn/consensus/istanbul"
    25  )
    26  
    27  func (c *core) handleRequest(request *istanbul.Request) error {
    28  	logger := c.logger.NewWith("state", c.state, "seq", c.current.sequence)
    29  
    30  	if err := c.checkRequestMsg(request); err != nil {
    31  		if err == errInvalidMessage {
    32  			logger.Warn("invalid request")
    33  			return err
    34  		}
    35  		logger.Warn("unexpected request", "err", err, "number", request.Proposal.Number(), "hash", request.Proposal.Hash())
    36  		return err
    37  	}
    38  
    39  	logger.Trace("handleRequest", "number", request.Proposal.Number(), "hash", request.Proposal.Hash())
    40  
    41  	c.current.pendingRequest = request
    42  	if c.state == StateAcceptRequest {
    43  		c.sendPreprepare(request)
    44  	}
    45  	return nil
    46  }
    47  
    48  // check request state
    49  // return errInvalidMessage if the message is invalid
    50  // return errFutureMessage if the sequence of proposal is larger than current sequence
    51  // return errOldMessage if the sequence of proposal is smaller than current sequence
    52  func (c *core) checkRequestMsg(request *istanbul.Request) error {
    53  	if request == nil || request.Proposal == nil {
    54  		return errInvalidMessage
    55  	}
    56  
    57  	if c := c.current.sequence.Cmp(request.Proposal.Number()); c > 0 {
    58  		return errOldMessage
    59  	} else if c < 0 {
    60  		return errFutureMessage
    61  	} else {
    62  		return nil
    63  	}
    64  }
    65  
    66  func (c *core) storeRequestMsg(request *istanbul.Request) {
    67  	logger := c.logger.NewWith("state", c.state)
    68  
    69  	logger.Trace("Store future request", "number", request.Proposal.Number(), "hash", request.Proposal.Hash())
    70  
    71  	c.pendingRequestsMu.Lock()
    72  	defer c.pendingRequestsMu.Unlock()
    73  
    74  	c.pendingRequests.Push(request, -request.Proposal.Number().Int64())
    75  }
    76  
    77  func (c *core) processPendingRequests() {
    78  	c.pendingRequestsMu.Lock()
    79  	defer c.pendingRequestsMu.Unlock()
    80  
    81  	for !(c.pendingRequests.Empty()) {
    82  		m, prio := c.pendingRequests.Pop()
    83  		r, ok := m.(*istanbul.Request)
    84  		if !ok {
    85  			c.logger.Warn("Malformed request, skip", "msg", m)
    86  			continue
    87  		}
    88  		// Push back if it's a future message
    89  		err := c.checkRequestMsg(r)
    90  		if err != nil {
    91  			if err == errFutureMessage {
    92  				c.logger.Trace("Stop processing request", "number", r.Proposal.Number(), "hash", r.Proposal.Hash())
    93  				c.pendingRequests.Push(m, prio)
    94  				break
    95  			}
    96  			c.logger.Trace("Skip the pending request", "number", r.Proposal.Number(), "hash", r.Proposal.Hash(), "err", err)
    97  			continue
    98  		}
    99  		c.logger.Trace("Post pending request", "number", r.Proposal.Number(), "hash", r.Proposal.Hash())
   100  
   101  		go c.sendEvent(istanbul.RequestEvent{
   102  			Proposal: r.Proposal,
   103  		})
   104  	}
   105  }