request.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. package core
  17. import (
  18. "time"
  19. "github.com/ethereum/go-ethereum/consensus/istanbul"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. "github.com/ethereum/go-ethereum/log"
  22. )
  23. // handleRequest is called by proposer in reaction to `miner.Seal()`
  24. // (this is the starting of the QBFT validation process)
  25. // It
  26. // - validates block proposal is not empty and number correspond to the current sequence
  27. // - creates and send PRE-PREPARE message to other validators
  28. func (c *core) handleRequest(request *Request) error {
  29. logger := c.currentLogger(true, nil)
  30. logger.Info("QBFT: handle block proposal request")
  31. if err := c.checkRequestMsg(request); err != nil {
  32. if err == errInvalidMessage {
  33. logger.Error("QBFT: invalid request")
  34. return err
  35. }
  36. logger.Error("QBFT: unexpected request", "err", err, "number", request.Proposal.Number(), "hash", request.Proposal.Hash())
  37. return err
  38. }
  39. c.current.pendingRequest = request
  40. if c.state == StateAcceptRequest {
  41. c.newRoundMutex.Lock()
  42. defer c.newRoundMutex.Unlock()
  43. if c.newRoundTimer != nil {
  44. c.newRoundTimer.Stop()
  45. c.newRoundTimer = nil
  46. }
  47. delay := time.Duration(0)
  48. block, ok := request.Proposal.(*types.Block)
  49. if ok && len(block.Transactions()) == 0 { // if empty block
  50. config := c.config.GetConfig(c.current.Sequence())
  51. if config.EmptyBlockPeriod > config.BlockPeriod {
  52. log.Info("EmptyBlockPeriod detected adding delay to request", "EmptyBlockPeriod", config.EmptyBlockPeriod, "BlockTime", block.Time())
  53. delay = time.Duration(config.EmptyBlockPeriod-config.BlockPeriod) * time.Second
  54. }
  55. }
  56. c.newRoundTimer = time.AfterFunc(delay, func() {
  57. c.newRoundTimer = nil
  58. // Start ROUND-CHANGE timer
  59. c.newRoundChangeTimer()
  60. // Send PRE-PREPARE message to other validators
  61. c.sendPreprepareMsg(request)
  62. })
  63. }
  64. return nil
  65. }
  66. // check request state
  67. // return errInvalidMessage if the message is invalid
  68. // return errFutureMessage if the sequence of proposal is larger than current sequence
  69. // return errOldMessage if the sequence of proposal is smaller than current sequence
  70. func (c *core) checkRequestMsg(request *Request) error {
  71. if request == nil || request.Proposal == nil {
  72. return errInvalidMessage
  73. }
  74. if c := c.current.sequence.Cmp(request.Proposal.Number()); c > 0 {
  75. return errOldMessage
  76. } else if c < 0 {
  77. return errFutureMessage
  78. } else {
  79. return nil
  80. }
  81. }
  82. func (c *core) storeRequestMsg(request *Request) {
  83. logger := c.currentLogger(true, nil).New("proposal.number", request.Proposal.Number(), "proposal.hash", request.Proposal.Hash())
  84. logger.Trace("QBFT: store block proposal request for future treatment")
  85. c.pendingRequestsMu.Lock()
  86. defer c.pendingRequestsMu.Unlock()
  87. c.pendingRequests.Push(request, float32(-request.Proposal.Number().Int64()))
  88. }
  89. // processPendingRequests is called each time QBFT state is re-initialized
  90. // it lookup over pending requests and re-input its so they can be treated
  91. func (c *core) processPendingRequests() {
  92. c.pendingRequestsMu.Lock()
  93. defer c.pendingRequestsMu.Unlock()
  94. logger := c.currentLogger(true, nil)
  95. logger.Debug("QBFT: lookup for pending block proposal requests")
  96. for !(c.pendingRequests.Empty()) {
  97. m, prio := c.pendingRequests.Pop()
  98. r, ok := m.(*Request)
  99. if !ok {
  100. logger.Error("QBFT: malformed pending block proposal request, skip", "msg", m)
  101. continue
  102. }
  103. // Push back if it's a future message
  104. err := c.checkRequestMsg(r)
  105. if err != nil {
  106. if err == errFutureMessage {
  107. logger.Trace("QBFT: stop looking up for pending block proposal request")
  108. c.pendingRequests.Push(m, prio)
  109. break
  110. }
  111. logger.Trace("QBFT: skip pending invalid block proposal request", "number", r.Proposal.Number(), "hash", r.Proposal.Hash(), "err", err)
  112. continue
  113. }
  114. logger.Debug("QBFT: found pending block proposal request", "proposal.number", r.Proposal.Number(), "proposal.hash", r.Proposal.Hash())
  115. go c.sendEvent(istanbul.RequestEvent{
  116. Proposal: r.Proposal,
  117. })
  118. }
  119. }