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