types.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 istanbul
  17. import (
  18. "fmt"
  19. "io"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // Proposal supports retrieving height and serialized block to be used during Istanbul consensus.
  26. type Proposal interface {
  27. // Number retrieves the sequence number of this proposal.
  28. Number() *big.Int
  29. // Hash retrieves the hash of this proposal.
  30. Hash() common.Hash
  31. EncodeRLP(w io.Writer) error
  32. DecodeRLP(s *rlp.Stream) error
  33. }
  34. var _ Proposal = &types.Block{}
  35. type Request struct {
  36. Proposal Proposal
  37. }
  38. // View includes a round number and a sequence number.
  39. // Sequence is the block number we'd like to commit.
  40. // Each round has a number and is composed by 3 steps: preprepare, prepare and commit.
  41. //
  42. // If the given block is not accepted by validators, a round change will occur
  43. // and the validators start a new round with round+1.
  44. type View struct {
  45. Round *big.Int
  46. Sequence *big.Int
  47. }
  48. // EncodeRLP serializes b into the Ethereum RLP format.
  49. func (v *View) EncodeRLP(w io.Writer) error {
  50. return rlp.Encode(w, []interface{}{v.Round, v.Sequence})
  51. }
  52. // DecodeRLP implements rlp.Decoder, and load the consensus fields from a RLP stream.
  53. func (v *View) DecodeRLP(s *rlp.Stream) error {
  54. var view struct {
  55. Round *big.Int
  56. Sequence *big.Int
  57. }
  58. if err := s.Decode(&view); err != nil {
  59. return err
  60. }
  61. v.Round, v.Sequence = view.Round, view.Sequence
  62. return nil
  63. }
  64. func (v *View) String() string {
  65. return fmt.Sprintf("{Round: %d, Sequence: %d}", v.Round.Uint64(), v.Sequence.Uint64())
  66. }
  67. // Cmp compares v and y and returns:
  68. // -1 if v < y
  69. // 0 if v == y
  70. // +1 if v > y
  71. func (v *View) Cmp(y *View) int {
  72. if v.Sequence.Cmp(y.Sequence) != 0 {
  73. return v.Sequence.Cmp(y.Sequence)
  74. }
  75. if v.Round.Cmp(y.Round) != 0 {
  76. return v.Round.Cmp(y.Round)
  77. }
  78. return 0
  79. }
  80. type Preprepare struct {
  81. View *View
  82. Proposal Proposal
  83. }
  84. // EncodeRLP serializes b into the Ethereum RLP format.
  85. func (b *Preprepare) EncodeRLP(w io.Writer) error {
  86. return rlp.Encode(w, []interface{}{b.View, b.Proposal})
  87. }
  88. // DecodeRLP implements rlp.Decoder, and load the consensus fields from a RLP stream.
  89. func (b *Preprepare) DecodeRLP(s *rlp.Stream) error {
  90. var preprepare struct {
  91. View *View
  92. Proposal *types.Block
  93. }
  94. if err := s.Decode(&preprepare); err != nil {
  95. return err
  96. }
  97. b.View, b.Proposal = preprepare.View, preprepare.Proposal
  98. return nil
  99. }
  100. type Subject struct {
  101. View *View
  102. Digest common.Hash
  103. }
  104. // EncodeRLP serializes b into the Ethereum RLP format.
  105. func (b *Subject) EncodeRLP(w io.Writer) error {
  106. return rlp.Encode(w, []interface{}{b.View, b.Digest})
  107. }
  108. // DecodeRLP implements rlp.Decoder, and load the consensus fields from a RLP stream.
  109. func (b *Subject) DecodeRLP(s *rlp.Stream) error {
  110. var subject struct {
  111. View *View
  112. Digest common.Hash
  113. }
  114. if err := s.Decode(&subject); err != nil {
  115. return err
  116. }
  117. b.View, b.Digest = subject.View, subject.Digest
  118. return nil
  119. }
  120. func (b *Subject) String() string {
  121. return fmt.Sprintf("{View: %v, Digest: %v}", b.View, b.Digest.String())
  122. }