prepare.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "github.com/ethereum/go-ethereum/common/hexutil"
  19. qbfttypes "github.com/ethereum/go-ethereum/consensus/istanbul/qbft/types"
  20. "github.com/ethereum/go-ethereum/rlp"
  21. )
  22. // broadcastPrepare is called after receiving PRE-PREPARE from proposer node
  23. // It
  24. // - creates a PREPARE message
  25. // - broadcast PREPARE message to other validators
  26. func (c *core) broadcastPrepare() {
  27. logger := c.currentLogger(true, nil)
  28. // Create PREPARE message from the current proposal
  29. sub := c.current.Subject()
  30. prepare := qbfttypes.NewPrepare(sub.View.Sequence, sub.View.Round, sub.Digest)
  31. prepare.SetSource(c.Address())
  32. // Sign Message
  33. encodedPayload, err := prepare.EncodePayloadForSigning()
  34. if err != nil {
  35. withMsg(logger, prepare).Error("QBFT: failed to encode payload of PREPARE message", "err", err)
  36. return
  37. }
  38. signature, err := c.backend.Sign(encodedPayload)
  39. if err != nil {
  40. withMsg(logger, prepare).Error("QBFT: failed to sign PREPARE message", "err", err)
  41. return
  42. }
  43. prepare.SetSignature(signature)
  44. // RLP-encode message
  45. payload, err := rlp.EncodeToBytes(&prepare)
  46. if err != nil {
  47. withMsg(logger, prepare).Error("QBFT: failed to encode PREPARE message", "err", err)
  48. return
  49. }
  50. withMsg(logger, prepare).Info("QBFT: broadcast PREPARE message", "payload", hexutil.Encode(payload))
  51. // Broadcast RLP-encoded message
  52. if err = c.backend.Broadcast(c.valSet, prepare.Code(), payload); err != nil {
  53. withMsg(logger, prepare).Error("QBFT: failed to broadcast PREPARE message", "err", err)
  54. return
  55. }
  56. }
  57. // handlePrepare is called when receiving a PREPARE message
  58. // It
  59. // - validates PREPARE message digest matches the current block proposal
  60. // - accumulates valid PREPARE message until reaching quorum
  61. // - when quorum is reached update states to "Prepared" and broadcast COMMIT
  62. func (c *core) handlePrepare(prepare *qbfttypes.Prepare) error {
  63. logger := c.currentLogger(true, prepare).New()
  64. logger.Info("QBFT: handle PREPARE message", "prepares.count", c.current.QBFTPrepares.Size(), "quorum", c.QuorumSize())
  65. // Check digest
  66. if prepare.Digest != c.current.Proposal().Hash() {
  67. logger.Error("QBFT: invalid PREPARE message digest")
  68. return errInvalidMessage
  69. }
  70. // Save PREPARE messages
  71. if err := c.current.QBFTPrepares.Add(prepare); err != nil {
  72. logger.Error("QBFT: failed to save PREPARE message", "err", err)
  73. return err
  74. }
  75. logger = logger.New("prepares.count", c.current.QBFTPrepares.Size(), "quorum", c.QuorumSize())
  76. // Change to "Prepared" state if we've received quorum of PREPARE messages
  77. // and we are in earlier state than "Prepared"
  78. if (c.current.QBFTPrepares.Size() >= c.QuorumSize()) && c.state.Cmp(StatePrepared) < 0 {
  79. logger.Info("QBFT: received quorum of PREPARE messages")
  80. // Accumulates PREPARE messages
  81. c.current.preparedRound = c.currentView().Round
  82. c.QBFTPreparedPrepares = make([]*qbfttypes.Prepare, 0)
  83. for _, m := range c.current.QBFTPrepares.Values() {
  84. c.QBFTPreparedPrepares = append(
  85. c.QBFTPreparedPrepares,
  86. qbfttypes.NewPrepareWithSigAndSource(
  87. m.View().Sequence, m.View().Round, m.(*qbfttypes.Prepare).Digest, m.Signature(), m.Source()))
  88. }
  89. if c.current.Proposal() != nil && c.current.Proposal().Hash() == prepare.Digest {
  90. logger.Debug("QBFT: PREPARE message matches proposal", "proposal", c.current.Proposal().Hash(), "prepare", prepare.Digest)
  91. c.current.preparedBlock = c.current.Proposal()
  92. }
  93. c.setState(StatePrepared)
  94. c.broadcastCommit()
  95. } else {
  96. logger.Debug("QBFT: accepted PREPARE messages")
  97. }
  98. return nil
  99. }