github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/consensus/istanbul/types/preprepare.go (about) 1 package qbfttypes 2 3 import ( 4 "fmt" 5 "io" 6 "math/big" 7 8 "github.com/electroneum/electroneum-sc/consensus/istanbul" 9 "github.com/electroneum/electroneum-sc/core/types" 10 "github.com/electroneum/electroneum-sc/rlp" 11 ) 12 13 type Preprepare struct { 14 CommonPayload 15 Proposal istanbul.Proposal 16 JustificationRoundChanges []*SignedRoundChangePayload 17 JustificationPrepares []*Prepare 18 } 19 20 func NewPreprepare(sequence *big.Int, round *big.Int, proposal istanbul.Proposal) *Preprepare { 21 return &Preprepare{ 22 CommonPayload: CommonPayload{ 23 code: PreprepareCode, 24 Sequence: sequence, 25 Round: round, 26 }, 27 Proposal: proposal, 28 } 29 } 30 31 func (m *Preprepare) EncodePayloadForSigning() ([]byte, error) { 32 return rlp.EncodeToBytes( 33 []interface{}{ 34 m.Code(), 35 []interface{}{m.Sequence, m.Round, m.Proposal}, 36 }) 37 } 38 39 func (m *Preprepare) EncodeRLP(w io.Writer) error { 40 return rlp.Encode( 41 w, 42 []interface{}{ 43 []interface{}{ 44 []interface{}{m.Sequence, m.Round, m.Proposal}, 45 m.signature, 46 }, 47 []interface{}{ 48 m.JustificationRoundChanges, 49 m.JustificationPrepares, 50 }, 51 }) 52 } 53 54 func (m *Preprepare) DecodeRLP(stream *rlp.Stream) error { 55 var message struct { 56 SignedPayload struct { 57 Payload struct { 58 Sequence *big.Int 59 Round *big.Int 60 Proposal *types.Block 61 } 62 Signature []byte 63 } 64 Justification struct { 65 RoundChanges []*SignedRoundChangePayload 66 Prepares []*Prepare 67 } 68 } 69 if err := stream.Decode(&message); err != nil { 70 return err 71 } 72 m.code = PreprepareCode 73 m.Sequence = message.SignedPayload.Payload.Sequence 74 m.Round = message.SignedPayload.Payload.Round 75 m.Proposal = message.SignedPayload.Payload.Proposal 76 m.signature = message.SignedPayload.Signature 77 m.JustificationPrepares = message.Justification.Prepares 78 m.JustificationRoundChanges = message.Justification.RoundChanges 79 return nil 80 } 81 82 func (m *Preprepare) String() string { 83 return fmt.Sprintf("code: %d, sequence: %d, round: %d, proposal: %v", m.code, m.Sequence, m.Round, m.Proposal.Hash().Hex()) 84 }