github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/footer_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     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 block
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/golang/protobuf/ptypes/timestamp"
    13  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    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 TestConvertToBlockFooterPb(t *testing.T) {
    21  	require := require.New(t)
    22  	footer := &Footer{nil, time.Now()}
    23  	blockFooter, err := footer.ConvertToBlockFooterPb()
    24  	require.NoError(err)
    25  	require.NotNil(blockFooter)
    26  	require.Equal(0, len(blockFooter.Endorsements))
    27  
    28  	footer = makeFooter()
    29  	blockFooter, err = footer.ConvertToBlockFooterPb()
    30  	require.NoError(err)
    31  	require.NotNil(blockFooter)
    32  	require.Equal(1, len(blockFooter.Endorsements))
    33  }
    34  
    35  func TestConvertFromBlockFooterPb(t *testing.T) {
    36  	require := require.New(t)
    37  	ts := &timestamp.Timestamp{Seconds: 10, Nanos: 10}
    38  	footerPb := &iotextypes.BlockFooter{
    39  		Endorsements: nil,
    40  		Timestamp:    ts,
    41  	}
    42  	footer := &Footer{}
    43  	require.NoError(footer.ConvertFromBlockFooterPb(footerPb))
    44  }
    45  
    46  func TestSerDesFooter(t *testing.T) {
    47  	require := require.New(t)
    48  	footer := &Footer{nil, time.Now()}
    49  	ser, err := footer.Serialize()
    50  	require.NoError(err)
    51  	require.NoError(footer.Deserialize(ser))
    52  	require.Equal(0, len(footer.endorsements))
    53  
    54  	footer = makeFooter()
    55  	require.NoError(err)
    56  	ser, err = footer.Serialize()
    57  	require.NoError(err)
    58  	require.NoError(footer.Deserialize(ser))
    59  	require.Equal(1, len(footer.endorsements))
    60  }
    61  
    62  func makeFooter() (f *Footer) {
    63  	endors := make([]*endorsement.Endorsement, 0)
    64  	endor := endorsement.NewEndorsement(time.Now(), identityset.PrivateKey(27).PublicKey(), nil)
    65  	endors = append(endors, endor)
    66  	f = &Footer{endors, time.Now()}
    67  	return
    68  }