preprepare.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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"
  20. "github.com/ethereum/go-ethereum/consensus/istanbul"
  21. istanbulcommon "github.com/ethereum/go-ethereum/consensus/istanbul/common"
  22. ibfttypes "github.com/ethereum/go-ethereum/consensus/istanbul/ibft/types"
  23. )
  24. func (c *core) sendPreprepare(request *istanbul.Request) {
  25. logger := c.logger.New("state", c.state)
  26. // If I'm the proposer and I have the same sequence with the proposal
  27. if c.current.Sequence().Cmp(request.Proposal.Number()) == 0 && c.IsProposer() {
  28. curView := c.currentView()
  29. preprepare, err := ibfttypes.Encode(&istanbul.Preprepare{
  30. View: curView,
  31. Proposal: request.Proposal,
  32. })
  33. if err != nil {
  34. logger.Error("Failed to encode", "view", curView)
  35. return
  36. }
  37. c.broadcast(&ibfttypes.Message{
  38. Code: ibfttypes.MsgPreprepare,
  39. Msg: preprepare,
  40. })
  41. }
  42. }
  43. func (c *core) handlePreprepare(msg *ibfttypes.Message, src istanbul.Validator) error {
  44. logger := c.logger.New("from", src, "state", c.state)
  45. // Decode PRE-PREPARE
  46. var preprepare *istanbul.Preprepare
  47. err := msg.Decode(&preprepare)
  48. if err != nil {
  49. return istanbulcommon.ErrFailedDecodePreprepare
  50. }
  51. // Ensure we have the same view with the PRE-PREPARE message
  52. // If it is old message, see if we need to broadcast COMMIT
  53. if err := c.checkMessage(ibfttypes.MsgPreprepare, preprepare.View); err != nil {
  54. if err == istanbulcommon.ErrOldMessage {
  55. // Get validator set for the given proposal
  56. valSet := c.backend.ParentValidators(preprepare.Proposal).Copy()
  57. previousProposer := c.backend.GetProposer(preprepare.Proposal.Number().Uint64() - 1)
  58. valSet.CalcProposer(previousProposer, preprepare.View.Round.Uint64())
  59. // Broadcast COMMIT if it is an existing block
  60. // 1. The proposer needs to be a proposer matches the given (Sequence + Round)
  61. // 2. The given block must exist
  62. if valSet.IsProposer(src.Address()) && c.backend.HasPropsal(preprepare.Proposal.Hash(), preprepare.Proposal.Number()) {
  63. c.sendCommitForOldBlock(preprepare.View, preprepare.Proposal.Hash())
  64. return nil
  65. }
  66. }
  67. return err
  68. }
  69. // Check if the message comes from current proposer
  70. if !c.valSet.IsProposer(src.Address()) {
  71. logger.Warn("Ignore preprepare messages from non-proposer")
  72. return istanbulcommon.ErrNotFromProposer
  73. }
  74. // Verify the proposal we received
  75. if duration, err := c.backend.Verify(preprepare.Proposal); err != nil {
  76. // if it's a future block, we will handle it again after the duration
  77. if err == consensus.ErrFutureBlock {
  78. logger.Info("Proposed block will be handled in the future", "err", err, "duration", duration)
  79. c.stopFuturePreprepareTimer()
  80. c.futurePreprepareTimer = time.AfterFunc(duration, func() {
  81. c.sendEvent(backlogEvent{
  82. src: src,
  83. msg: msg,
  84. })
  85. })
  86. } else {
  87. logger.Warn("Failed to verify proposal", "err", err, "duration", duration)
  88. c.sendNextRoundChange()
  89. }
  90. return err
  91. }
  92. // Here is about to accept the PRE-PREPARE
  93. if c.state == ibfttypes.StateAcceptRequest {
  94. // Send ROUND CHANGE if the locked proposal and the received proposal are different
  95. if c.current.IsHashLocked() {
  96. if preprepare.Proposal.Hash() == c.current.GetLockedHash() {
  97. // Broadcast COMMIT and enters Prepared state directly
  98. c.acceptPreprepare(preprepare)
  99. c.setState(ibfttypes.StatePrepared)
  100. c.sendCommit()
  101. } else {
  102. // Send round change
  103. c.sendNextRoundChange()
  104. }
  105. } else {
  106. // Either
  107. // 1. the locked proposal and the received proposal match
  108. // 2. we have no locked proposal
  109. c.acceptPreprepare(preprepare)
  110. c.setState(ibfttypes.StatePreprepared)
  111. c.sendPrepare()
  112. }
  113. }
  114. return nil
  115. }
  116. func (c *core) acceptPreprepare(preprepare *istanbul.Preprepare) {
  117. c.consensusTimestamp = time.Now()
  118. c.current.SetPreprepare(preprepare)
  119. }