OSDN Git Service

new repo
[bytom/vapor.git] / protocol / bc / types / block_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "encoding/json"
7         "strings"
8         "testing"
9
10         "github.com/davecgh/go-spew/spew"
11
12         "github.com/vapor/consensus"
13         "github.com/vapor/protocol/bc"
14         "github.com/vapor/testutil"
15 )
16
17 func TestBlock(t *testing.T) {
18         cases := []struct {
19                 block *Block
20                 hex   string
21                 hash  bc.Hash
22         }{
23                 {
24                         block: &Block{
25                                 BlockHeader: BlockHeader{
26                                         Version: 1,
27                                         Height:  1,
28                                 },
29                                 Transactions: []*Tx{},
30                         },
31                         hex: strings.Join([]string{
32                                 "03", // serialization flags
33                                 "01", // version
34                                 "01", // block height
35                                 "0000000000000000000000000000000000000000000000000000000000000000", // prev block hash
36                                 "00", // timestamp
37                                 "40", // commitment extensible field length
38                                 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
39                                 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
40                                 "00", // nonce
41                                 "00", // bits
42                                 "00", // num transactions
43                         }, ""),
44                         hash: testutil.MustDecodeHash("9609d2e45760f34cbc6c6d948c3fb9b6d7b61552d9d17fdd5b7d0cb5d2e67244"),
45                 },
46                 {
47                         block: &Block{
48                                 BlockHeader: BlockHeader{
49                                         Version:           1,
50                                         Height:            432234,
51                                         PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
52                                         Timestamp:         1522908275,
53                                         Nonce:             34342,
54                                         Bits:              2305843009222082559,
55                                         BlockCommitment: BlockCommitment{
56                                                 TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
57                                                 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
58                                         },
59                                 },
60                                 Transactions: []*Tx{
61                                         NewTx(TxData{
62                                                 Version:        1,
63                                                 SerializedSize: uint64(261),
64                                                 TimeRange:      654,
65                                                 Inputs: []*TxInput{
66                                                         NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
67                                                         NewSpendInput([][]byte{[]byte("arguments3"), []byte("arguments4")}, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), *consensus.BTMAssetID, 254354, 3, []byte("spendProgram")),
68                                                 },
69                                                 Outputs: []*TxOutput{
70                                                         NewTxOutput(testutil.MustDecodeAsset("a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf"), 254354, []byte("true")),
71                                                 },
72                                         }),
73                                         NewTx(TxData{
74                                                 Version:        1,
75                                                 SerializedSize: uint64(108),
76                                                 Inputs: []*TxInput{
77                                                         NewCoinbaseInput([]byte("arbitrary")),
78                                                 },
79                                                 Outputs: []*TxOutput{
80                                                         NewTxOutput(*consensus.BTMAssetID, 254354, []byte("true")),
81                                                         NewTxOutput(*consensus.BTMAssetID, 254354, []byte("false")),
82                                                 },
83                                         }),
84                                 },
85                         },
86                         hex: strings.Join([]string{
87                                 "03",     // serialization flags
88                                 "01",     // version
89                                 "eab01a", // block height
90                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
91                                 "f3f896d605", // timestamp
92                                 "40",         // commitment extensible field length
93                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
94                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
95                                 "a68c02",             // nonce
96                                 "ffffff838080808020", // bits
97                                 "02",                 // num transactions
98                                 "07018e0502012a00056e6f6e6365a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf92c30f380f6173736574446566696e6974696f6e010f69737375616e636550726f6772616d020a617267756d656e7473310a617267756d656e74733201540152fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f03010c7370656e6450726f6772616d17020a617267756d656e7473330a617267756d656e747334010129a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf92c30f01047472756500",
99                                 "07010001010b020961726269747261727900020129ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f01047472756500012affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f010566616c736500",
100                         }, ""),
101                         hash: testutil.MustDecodeHash("86c833711a6a6b59864708d9dbae7869ba10782e3e7b1c7fc9fe3514899fec80"),
102                 },
103         }
104
105         for i, test := range cases {
106                 got := testutil.Serialize(t, test.block)
107                 want, err := hex.DecodeString(test.hex)
108                 if err != nil {
109                         t.Fatal(err)
110                 }
111
112                 if !bytes.Equal(got, want) {
113                         t.Errorf("test %d: bytes = %x want %x", i, got, want)
114                 }
115
116                 blockHash := test.block.Hash()
117                 if blockHash != test.hash {
118                         t.Errorf("test %d: hash = %s want %s", i, blockHash.String(), test.hash.String())
119                 }
120
121                 blockJSON, err := json.Marshal(test.block)
122                 if err != nil {
123                         t.Errorf("test %d: error marshaling block to json: %s", i, err)
124                 }
125
126                 blockFromJSON := Block{}
127                 if err := json.Unmarshal(blockJSON, &blockFromJSON); err != nil {
128                         t.Errorf("test %d: error unmarshaling block from json: %s", i, err)
129                 }
130                 if !testutil.DeepEqual(*test.block, blockFromJSON) {
131                         t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(blockFromJSON), spew.Sdump(*test.block))
132                 }
133         }
134 }