github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/pbft/core/request.go (about) 1 // Copyright 2020 The go-simplechain Authors 2 // This file is part of the go-simplechain library. 3 // 4 // The go-simplechain 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-simplechain 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-simplechain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "github.com/bigzoro/my_simplechain/consensus/pbft" 21 "github.com/bigzoro/my_simplechain/log" 22 ) 23 24 func (c *core) handleRequest(request *pbft.Request) error { 25 logger := c.logger.New("state", c.state, "seq", c.current.sequence) 26 log.Report("> handleRequest") 27 28 if err := c.checkRequestMsg(request); err != nil { 29 if err == errInvalidMessage { 30 logger.Warn("invalid request") 31 return err 32 } 33 logger.Warn("unexpected request", "err", err, "number", request.Proposal.Number(), "hash", request.Proposal.PendingHash()) 34 return err 35 } 36 37 logger.Trace("handleRequest", "number", request.Proposal.Number(), "hash", request.Proposal.PendingHash()) 38 39 c.current.pendingRequest = request 40 if c.state == StateAcceptRequest { 41 c.sendPreprepare(request) 42 } 43 return nil 44 } 45 46 // check request state 47 // return errInvalidMessage if the message is invalid 48 // return errFutureMessage if the sequence of proposal is larger than current sequence 49 // return errOldMessage if the sequence of proposal is smaller than current sequence 50 func (c *core) checkRequestMsg(request *pbft.Request) error { 51 if request == nil || request.Proposal == nil { 52 return errInvalidMessage 53 } 54 55 if c := c.current.sequence.Cmp(request.Proposal.Number()); c > 0 { 56 return errOldMessage 57 } else if c < 0 { 58 return errFutureMessage 59 } else { 60 return nil 61 } 62 } 63 64 func (c *core) storeRequestMsg(request *pbft.Request) { 65 logger := c.logger.New("state", c.state) 66 67 //logger.Trace("Store future request", "number", request.Proposal.Number(), "hash", request.Proposal.Hash()) 68 logger.Trace("Store future request", "number", request.Proposal.Number(), "hash", request.Proposal.PendingHash()) 69 70 c.pendingRequestsMu.Lock() 71 defer c.pendingRequestsMu.Unlock() 72 73 c.pendingRequests.Push(request, -request.Proposal.Number().Int64()) 74 } 75 76 func (c *core) processPendingRequests() { 77 c.pendingRequestsMu.Lock() 78 defer c.pendingRequestsMu.Unlock() 79 80 for !(c.pendingRequests.Empty()) { 81 m, prio := c.pendingRequests.Pop() 82 r, ok := m.(*pbft.Request) 83 if !ok { 84 c.logger.Warn("Malformed request, skip", "msg", m) 85 continue 86 } 87 // Push back if it's a future message 88 err := c.checkRequestMsg(r) 89 if err != nil { 90 if err == errFutureMessage { 91 //c.logger.Trace("Stop processing request", "number", r.Proposal.Number(), "hash", r.Proposal.Hash()) 92 c.logger.Trace("Stop processing request", "number", r.Proposal.Number(), "hash", r.Proposal.PendingHash()) 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 c.logger.Trace("Skip the pending request", "number", r.Proposal.Number(), "hash", r.Proposal.PendingHash(), "err", err) 98 continue 99 } 100 //c.logger.Trace("Post pending request", "number", r.Proposal.Number(), "hash", r.Proposal.Hash()) 101 c.logger.Trace("Post pending request", "number", r.Proposal.Number(), "hash", r.Proposal.PendingHash()) 102 103 go c.sendEvent(pbft.RequestEvent{ 104 Proposal: r.Proposal, 105 }) 106 } 107 }