OSDN Git Service

new repo
[bytom/vapor.git] / protocol / bc / types / bytom / types / block_header_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "strings"
7         "testing"
8
9         "github.com/davecgh/go-spew/spew"
10
11         "github.com/vapor/encoding/blockchain"
12         "github.com/vapor/testutil"
13 )
14
15 func TestBlockHeader(t *testing.T) {
16         blockHeader := &BlockHeader{
17                 Version:           1,
18                 Height:            432234,
19                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
20                 Timestamp:         1522908275,
21                 Nonce:             34342,
22                 Bits:              2305843009222082559,
23                 BlockCommitment: BlockCommitment{
24                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
25                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
26                 },
27         }
28
29         wantHex := strings.Join([]string{
30                 "01",     // serialization flags
31                 "01",     // version
32                 "eab01a", // block height
33                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
34                 "f3f896d605", // timestamp
35                 "40",         // commitment extensible field length
36                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
37                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
38                 "a68c02",             // nonce
39                 "ffffff838080808020", // bits
40         }, "")
41
42         gotHex := testutil.Serialize(t, blockHeader)
43         want, err := hex.DecodeString(wantHex)
44         if err != nil {
45                 t.Fatal(err)
46         }
47
48         if !bytes.Equal(gotHex, want) {
49                 t.Errorf("empty block header bytes = %x want %x", gotHex, want)
50         }
51
52         gotBlockHeader := BlockHeader{}
53         if _, err := gotBlockHeader.readFrom(blockchain.NewReader(want)); err != nil {
54                 t.Fatal(err)
55         }
56
57         if !testutil.DeepEqual(gotBlockHeader, *blockHeader) {
58                 t.Errorf("got:\n%s\nwant:\n%s", spew.Sdump(gotBlockHeader), spew.Sdump(*blockHeader))
59         }
60 }