github.com/iotexproject/iotex-core@v1.14.1-rc1/consensus/scheme/rolldpos/endorsedconsensusmessage_test.go (about)

     1  // Copyright (c) 2019 IoTeX
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package rolldpos
     7  
     8  import (
     9  	"bytes"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/iotexproject/iotex-core/blockchain/block"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/iotexproject/iotex-core/endorsement"
    17  	"github.com/iotexproject/iotex-core/test/identityset"
    18  )
    19  
    20  func TestEndorsedConsensusMessage(t *testing.T) {
    21  	require := require.New(t)
    22  	hash := []byte("abcdefg")
    23  	sig := []byte("signature")
    24  	priKey := identityset.PrivateKey(0)
    25  	vote := NewConsensusVote(hash, PROPOSAL)
    26  	now := time.Now()
    27  	en := endorsement.NewEndorsement(
    28  		now,
    29  		priKey.PublicKey(),
    30  		sig,
    31  	)
    32  	endorsedMessage := NewEndorsedConsensusMessage(10, vote, en)
    33  	pb, err := endorsedMessage.Proto()
    34  	require.NoError(err)
    35  	cem := &EndorsedConsensusMessage{}
    36  	require.NoError(cem.LoadProto(pb, block.NewDeserializer(0)))
    37  	require.Equal(uint64(10), cem.Height())
    38  	cvote, ok := cem.Document().(*ConsensusVote)
    39  	require.True(ok)
    40  	require.Equal(PROPOSAL, cvote.Topic())
    41  	require.Equal(0, bytes.Compare(hash, cvote.BlockHash()))
    42  	cen := cem.Endorsement()
    43  	require.Equal(0, bytes.Compare(sig, cen.Signature()))
    44  	require.True(now.Equal(cen.Timestamp()))
    45  	require.Equal(priKey.PublicKey().HexString(), en.Endorser().HexString())
    46  }