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

     1  package qbfttypes
     2  
     3  import (
     4  	"io"
     5  	"math/big"
     6  
     7  	"github.com/electroneum/electroneum-sc/common"
     8  	"github.com/electroneum/electroneum-sc/rlp"
     9  )
    10  
    11  // A QBFT COMMIT message.
    12  type Commit struct {
    13  	CommonPayload
    14  	Digest     common.Hash
    15  	CommitSeal []byte
    16  }
    17  
    18  func NewCommit(sequence *big.Int, round *big.Int, digest common.Hash, seal []byte) *Commit {
    19  	return &Commit{
    20  		CommonPayload: CommonPayload{
    21  			code:     CommitCode,
    22  			Sequence: sequence,
    23  			Round:    round,
    24  		},
    25  		Digest:     digest,
    26  		CommitSeal: seal,
    27  	}
    28  }
    29  
    30  func (m *Commit) EncodePayloadForSigning() ([]byte, error) {
    31  	return rlp.EncodeToBytes(
    32  		[]interface{}{
    33  			m.Code(),
    34  			[]interface{}{m.Sequence, m.Round, m.Digest, m.CommitSeal},
    35  		})
    36  }
    37  
    38  func (m *Commit) EncodeRLP(w io.Writer) error {
    39  	return rlp.Encode(w, []interface{}{
    40  		[]interface{}{m.Sequence, m.Round, m.Digest, m.CommitSeal},
    41  		m.signature})
    42  }
    43  
    44  func (m *Commit) DecodeRLP(stream *rlp.Stream) error {
    45  	var message struct {
    46  		Payload struct {
    47  			Sequence   *big.Int
    48  			Round      *big.Int
    49  			Digest     common.Hash
    50  			CommitSeal []byte
    51  		}
    52  		Signature []byte
    53  	}
    54  	if err := stream.Decode(&message); err != nil {
    55  		return err
    56  	}
    57  	m.code = CommitCode
    58  	m.Sequence = message.Payload.Sequence
    59  	m.Round = message.Payload.Round
    60  	m.Digest = message.Payload.Digest
    61  	m.CommitSeal = message.Payload.CommitSeal
    62  	m.signature = message.Signature
    63  	return nil
    64  }