github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/header_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 "fmt" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/iotexproject/go-pkgs/hash" 15 "github.com/iotexproject/iotex-core/test/identityset" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestHeader(t *testing.T) { 20 require := require.New(t) 21 header := getHeader() 22 require.Equal(uint32(1), header.Version()) 23 require.Equal(uint64(2), header.Height()) 24 ti, err := time.Parse("2006-Jan-02", "2019-Feb-03") 25 require.NoError(err) 26 require.Equal(ti, header.Timestamp()) 27 expected := "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" 28 require.True(isEqual(expected, header.PrevHash())) 29 require.True(isEqual(expected, header.TxRoot())) 30 require.True(isEqual(expected, header.DeltaStateDigest())) 31 require.Equal("04755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf33", header.PublicKey().HexString()) 32 require.True(isEqual(expected, header.ReceiptRoot())) 33 require.True(isEqual("39f9a57253c8396601394ca504ff0cd648adefbd1d0728e9e77fd211e34c5258", header.HashBlock())) 34 require.NotNil(header.Proto()) 35 require.NotNil(header.BlockHeaderCoreProto()) 36 require.Equal("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms", header.ProducerAddress()) 37 } 38 func TestSerDesHeadrer(t *testing.T) { 39 require := require.New(t) 40 h := getHeader() 41 ser, err := h.Serialize() 42 require.NoError(err) 43 require.NotNil(ser) 44 header := &Header{} 45 require.NoError(header.Deserialize(ser)) 46 require.Equal(uint32(1), header.Version()) 47 require.Equal(uint64(2), header.Height()) 48 ti, err := time.Parse("2006-Jan-02", "2019-Feb-03") 49 require.NoError(err) 50 require.Equal(ti, header.Timestamp()) 51 expected := "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" 52 require.True(isEqual(expected, header.PrevHash())) 53 require.True(isEqual(expected, header.TxRoot())) 54 require.True(isEqual(expected, header.DeltaStateDigest())) 55 require.Equal("04755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf33", header.PublicKey().HexString()) 56 require.True(isEqual(expected, header.ReceiptRoot())) 57 require.True(isEqual("39f9a57253c8396601394ca504ff0cd648adefbd1d0728e9e77fd211e34c5258", header.HashBlock())) 58 require.NotNil(header.Proto()) 59 require.NotNil(header.BlockHeaderCoreProto()) 60 require.Equal("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms", header.ProducerAddress()) 61 } 62 func getHeader() *Header { 63 ti, err := time.Parse("2006-Jan-02", "2019-Feb-03") 64 if err != nil { 65 return nil 66 } 67 h := &Header{ 68 version: 1, 69 height: 2, 70 timestamp: ti, 71 prevBlockHash: hash.Hash256b([]byte("")), 72 txRoot: hash.Hash256b([]byte("")), 73 deltaStateDigest: hash.Hash256b([]byte("")), 74 receiptRoot: hash.Hash256b([]byte("")), 75 blockSig: nil, 76 pubkey: identityset.PrivateKey(27).PublicKey(), 77 } 78 return h 79 } 80 func isEqual(expected string, hash hash.Hash256) bool { 81 h := fmt.Sprintf("%x", hash[:]) 82 return strings.EqualFold(expected, h) 83 }