prepare.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "reflect"
  19. "github.com/ethereum/go-ethereum/consensus/istanbul"
  20. istanbulcommon "github.com/ethereum/go-ethereum/consensus/istanbul/common"
  21. ibfttypes "github.com/ethereum/go-ethereum/consensus/istanbul/ibft/types"
  22. )
  23. func (c *core) sendPrepare() {
  24. logger := c.logger.New("state", c.state)
  25. sub := c.current.Subject()
  26. encodedSubject, err := ibfttypes.Encode(sub)
  27. if err != nil {
  28. logger.Error("Failed to encode", "subject", sub)
  29. return
  30. }
  31. c.broadcast(&ibfttypes.Message{
  32. Code: ibfttypes.MsgPrepare,
  33. Msg: encodedSubject,
  34. })
  35. }
  36. func (c *core) handlePrepare(msg *ibfttypes.Message, src istanbul.Validator) error {
  37. // Decode PREPARE message
  38. var prepare *istanbul.Subject
  39. err := msg.Decode(&prepare)
  40. if err != nil {
  41. return istanbulcommon.ErrFailedDecodePrepare
  42. }
  43. if err := c.checkMessage(ibfttypes.MsgPrepare, prepare.View); err != nil {
  44. return err
  45. }
  46. // If it is locked, it can only process on the locked block.
  47. // Passing verifyPrepare and checkMessage implies it is processing on the locked block since it was verified in the Preprepared state.
  48. if err := c.verifyPrepare(prepare, src); err != nil {
  49. return err
  50. }
  51. c.acceptPrepare(msg, src)
  52. // Change to Prepared state if we've received enough PREPARE messages or it is locked
  53. // and we are in earlier state before Prepared state.
  54. if ((c.current.IsHashLocked() && prepare.Digest == c.current.GetLockedHash()) || c.current.GetPrepareOrCommitSize() >= c.QuorumSize()) &&
  55. c.state.Cmp(ibfttypes.StatePrepared) < 0 {
  56. c.current.LockHash()
  57. c.setState(ibfttypes.StatePrepared)
  58. c.sendCommit()
  59. }
  60. return nil
  61. }
  62. // verifyPrepare verifies if the received PREPARE message is equivalent to our subject
  63. func (c *core) verifyPrepare(prepare *istanbul.Subject, src istanbul.Validator) error {
  64. logger := c.logger.New("from", src, "state", c.state)
  65. sub := c.current.Subject()
  66. if !reflect.DeepEqual(prepare, sub) {
  67. logger.Warn("Inconsistent subjects between PREPARE and proposal", "expected", sub, "got", prepare)
  68. return istanbulcommon.ErrInconsistentSubject
  69. }
  70. return nil
  71. }
  72. func (c *core) acceptPrepare(msg *ibfttypes.Message, src istanbul.Validator) error {
  73. logger := c.logger.New("from", src, "state", c.state)
  74. // Add the PREPARE message to current round state
  75. if err := c.current.Prepares.Add(msg); err != nil {
  76. logger.Error("Failed to add PREPARE message to round state", "msg", msg, "err", err)
  77. return err
  78. }
  79. return nil
  80. }