github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/consensus/istanbul/types/prepare.go (about)

     1  package qbfttypes
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"math/big"
     7  
     8  	"github.com/electroneum/electroneum-sc/common"
     9  	"github.com/electroneum/electroneum-sc/rlp"
    10  )
    11  
    12  // A QBFT PREPARE message.
    13  type Prepare struct {
    14  	CommonPayload
    15  	Digest common.Hash
    16  }
    17  
    18  func NewPrepare(sequence *big.Int, round *big.Int, digest common.Hash) *Prepare {
    19  	return &Prepare{
    20  		CommonPayload: CommonPayload{
    21  			code:     PrepareCode,
    22  			Sequence: sequence,
    23  			Round:    round,
    24  		},
    25  		Digest: digest,
    26  	}
    27  }
    28  
    29  func NewPrepareWithSigAndSource(sequence *big.Int, round *big.Int, digest common.Hash, signature []byte, source common.Address) *Prepare {
    30  	prepare := NewPrepare(sequence, round, digest)
    31  	prepare.signature = signature
    32  	prepare.source = source
    33  	return prepare
    34  }
    35  
    36  func (p *Prepare) String() string {
    37  	return fmt.Sprintf("Prepare {seq=%v, round=%v, digest=%v}", p.Sequence, p.Round, p.Digest.Hex())
    38  }
    39  
    40  func (p *Prepare) EncodePayloadForSigning() ([]byte, error) {
    41  	return rlp.EncodeToBytes(
    42  		[]interface{}{
    43  			p.Code(),
    44  			[]interface{}{p.Sequence, p.Round, p.Digest},
    45  		})
    46  }
    47  
    48  func (p *Prepare) EncodeRLP(w io.Writer) error {
    49  	return rlp.Encode(
    50  		w,
    51  		[]interface{}{
    52  			[]interface{}{
    53  				p.Sequence,
    54  				p.Round,
    55  				p.Digest},
    56  			p.signature,
    57  		})
    58  }
    59  
    60  func (p *Prepare) DecodeRLP(stream *rlp.Stream) error {
    61  	var message struct {
    62  		Payload struct {
    63  			Sequence *big.Int
    64  			Round    *big.Int
    65  			Digest   common.Hash
    66  		}
    67  		Signature []byte
    68  	}
    69  	if err := stream.Decode(&message); err != nil {
    70  		return err
    71  	}
    72  	p.code = PrepareCode
    73  	p.Sequence = message.Payload.Sequence
    74  	p.Round = message.Payload.Round
    75  	p.Digest = message.Payload.Digest
    76  	p.signature = message.Signature
    77  	return nil
    78  }