github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/header_test.go (about)

     1  package flow_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/fxamacker/cbor/v2"
     9  	"github.com/onflow/crypto"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/vmihailenco/msgpack/v4"
    13  
    14  	"github.com/onflow/flow-go/consensus/hotstuff/helper"
    15  	"github.com/onflow/flow-go/model/encoding/rlp"
    16  	"github.com/onflow/flow-go/model/flow"
    17  	"github.com/onflow/flow-go/utils/unittest"
    18  )
    19  
    20  func TestHeaderEncodingJSON(t *testing.T) {
    21  	header := unittest.BlockHeaderFixture()
    22  	headerID := header.ID()
    23  	data, err := json.Marshal(header)
    24  	require.NoError(t, err)
    25  	var decoded flow.Header
    26  	err = json.Unmarshal(data, &decoded)
    27  	require.NoError(t, err)
    28  	decodedID := decoded.ID()
    29  	assert.Equal(t, headerID, decodedID)
    30  	assert.Equal(t, *header, decoded)
    31  }
    32  
    33  func TestHeaderFingerprint(t *testing.T) {
    34  	header := unittest.BlockHeaderFixture()
    35  	header.LastViewTC = helper.MakeTC()
    36  	headerID := header.ID()
    37  	data := header.Fingerprint()
    38  	var decoded struct {
    39  		ChainID            flow.ChainID
    40  		ParentID           flow.Identifier
    41  		Height             uint64
    42  		PayloadHash        flow.Identifier
    43  		Timestamp          uint64
    44  		View               uint64
    45  		ParentView         uint64
    46  		ParentVoterIndices []byte
    47  		ParentVoterSigData crypto.Signature
    48  		ProposerID         flow.Identifier
    49  		LastViewTC         interface{}
    50  	}
    51  	rlp.NewMarshaler().MustUnmarshal(data, &decoded)
    52  	decHeader := &flow.Header{
    53  		ChainID:            decoded.ChainID,
    54  		ParentID:           decoded.ParentID,
    55  		Height:             decoded.Height,
    56  		PayloadHash:        decoded.PayloadHash,
    57  		Timestamp:          time.Unix(0, int64(decoded.Timestamp)).UTC(),
    58  		View:               decoded.View,
    59  		ParentView:         decoded.ParentView,
    60  		ParentVoterIndices: decoded.ParentVoterIndices,
    61  		ParentVoterSigData: decoded.ParentVoterSigData,
    62  		ProposerID:         decoded.ProposerID,
    63  		ProposerSigData:    header.ProposerSigData, // since this field is not encoded/decoded, just set it to the original value to pass test
    64  		LastViewTC:         header.LastViewTC,
    65  	}
    66  	decodedID := decHeader.ID()
    67  	assert.Equal(t, headerID, decodedID)
    68  	assert.Equal(t, *header, *decHeader)
    69  }
    70  
    71  func TestHeaderEncodingMsgpack(t *testing.T) {
    72  	header := unittest.BlockHeaderFixture()
    73  	headerID := header.ID()
    74  	data, err := msgpack.Marshal(header)
    75  	require.NoError(t, err)
    76  	var decoded flow.Header
    77  	err = msgpack.Unmarshal(data, &decoded)
    78  	require.NoError(t, err)
    79  	decodedID := decoded.ID()
    80  	assert.Equal(t, headerID, decodedID)
    81  	assert.Equal(t, *header, decoded)
    82  }
    83  
    84  func TestHeaderEncodingCBOR(t *testing.T) {
    85  	header := unittest.BlockHeaderFixture()
    86  	headerID := header.ID()
    87  	data, err := cbor.Marshal(header)
    88  	require.NoError(t, err)
    89  	var decoded flow.Header
    90  	err = cbor.Unmarshal(data, &decoded)
    91  	require.NoError(t, err)
    92  	decodedID := decoded.ID()
    93  	assert.Equal(t, headerID, decodedID)
    94  	assert.Equal(t, *header, decoded)
    95  }
    96  
    97  func TestNonUTCTimestampSameHashAsUTC(t *testing.T) {
    98  	header := unittest.BlockHeaderFixture()
    99  	headerID := header.ID()
   100  	loc := time.FixedZone("UTC-8", -8*60*60)
   101  	header.Timestamp = header.Timestamp.In(loc)
   102  	checkedID := header.ID()
   103  	assert.Equal(t, headerID, checkedID)
   104  }