github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/consensus/podc/types.go (about) 1 package podc 2 3 import ( 4 "fmt" 5 "io" 6 "math/big" 7 "github.com/ethereum/go-ethereum/common" 8 "github.com/ethereum/go-ethereum/core/types" 9 "github.com/ethereum/go-ethereum/rlp" 10 ) 11 12 // Proposal supports retrieving height(of Block) and serialized block to be used during PoDC consensus. 13 // Proposal block ? 14 type Proposal interface { 15 // Number retrieves the sequence number of this proposal. 16 Number() *big.Int 17 18 // Hash retrieves the hash of this proposal. 19 Hash() common.Hash 20 21 EncodeRLP(w io.Writer) error 22 23 DecodeRLP(s *rlp.Stream) error 24 25 String() string 26 } 27 28 type Request struct { 29 Proposal Proposal 30 } 31 // Qmanager data exchange format 32 // Proposal supports retrieving height and serialized block to be used during Istanbul consensus. 33 type ProposalQman interface { 34 // Number retrieves the sequence number of this proposal. 35 Number() *big.Int 36 37 // Hash retrieves the hash of this proposal. 38 Hash() common.Hash 39 40 EncodeRLP(w io.Writer) error 41 42 DecodeRLP(s *rlp.Stream) error 43 44 String() string 45 } 46 47 type RequestQman struct { 48 Proposal ProposalQman 49 } 50 51 // View includes a round number and a sequence number. 52 // Sequence is the block number we'd like to commit. 53 // Each round has a number and is composed by 3 steps: preprepare, prepare and commit. 54 // 55 // If the given block is not accepted by validators, a round change will occur 56 // and the validators start a new round with round+1. 57 type View struct { 58 Round *big.Int 59 Sequence *big.Int 60 } 61 62 // EncodeRLP serializes b into the Ethereum RLP format. 63 func (v *View) EncodeRLP(w io.Writer) error { 64 return rlp.Encode(w, []interface{}{v.Round, v.Sequence}) 65 } 66 67 // DecodeRLP implements rlp.Decoder, and load the consensus fields from a RLP stream. 68 func (v *View) DecodeRLP(s *rlp.Stream) error { 69 var view struct { 70 Round *big.Int 71 Sequence *big.Int 72 } 73 74 if err := s.Decode(&view); err != nil { 75 return err 76 } 77 v.Round, v.Sequence = view.Round, view.Sequence 78 return nil 79 } 80 81 func (v *View) String() string { 82 return fmt.Sprintf("{Round: %d, Sequence: %d}", v.Round.Uint64(), v.Sequence.Uint64()) 83 } 84 85 // Cmp compares v and y and returns: 86 // -1 if v < y 87 // 0 if v == y 88 // +1 if v > y 89 func (v *View) Cmp(y *View) int { 90 if v.Sequence.Cmp(y.Sequence) != 0 { 91 return v.Sequence.Cmp(y.Sequence) 92 } 93 if v.Round.Cmp(y.Round) != 0 { 94 return v.Round.Cmp(y.Round) 95 } 96 return 0 97 } 98 99 100 type Preprepare struct { 101 View *View 102 Proposal Proposal 103 } 104 type D_select struct { 105 // TODO: define Coordinator struct like 106 View *View 107 Proposal Proposal 108 } 109 type D_commit struct { 110 View *View 111 Proposal Proposal 112 } 113 114 115 // EncodeRLP serializes b into the Ethereum RLP format. 116 func (b *Preprepare) EncodeRLP(w io.Writer) error { 117 return rlp.Encode(w, []interface{}{b.View, b.Proposal}) 118 } 119 120 // DecodeRLP implements rlp.Decoder, and load the consensus fields from a RLP stream. 121 func (b *Preprepare) DecodeRLP(s *rlp.Stream) error { 122 var preprepare struct { 123 View *View 124 Proposal *types.Block 125 } 126 127 if err := s.Decode(&preprepare); err != nil { 128 return err 129 } 130 b.View, b.Proposal = preprepare.View, preprepare.Proposal 131 132 return nil 133 } 134 135 type Subject struct { 136 View *View 137 Digest common.Hash 138 } 139 140 // EncodeRLP serializes b into the Ethereum RLP format. 141 func (b *Subject) EncodeRLP(w io.Writer) error { 142 return rlp.Encode(w, []interface{}{b.View, b.Digest}) 143 } 144 145 // DecodeRLP implements rlp.Decoder, and load the consensus fields from a RLP stream. 146 func (b *Subject) DecodeRLP(s *rlp.Stream) error { 147 var subject struct { 148 View *View 149 Digest common.Hash 150 } 151 152 if err := s.Decode(&subject); err != nil { 153 return err 154 } 155 b.View, b.Digest = subject.View, subject.Digest 156 return nil 157 } 158 159 func (b *Subject) String() string { 160 return fmt.Sprintf("{View: %v, Digest: %v}", b.View, b.Digest.String()) 161 }