OSDN Git Service

init delete the pow related (#55)
[bytom/vapor.git] / protocol / bc / types / block_header_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "fmt"
7         "strings"
8         "testing"
9
10         "github.com/davecgh/go-spew/spew"
11
12         "github.com/vapor/encoding/blockchain"
13         "github.com/vapor/errors"
14         "github.com/vapor/testutil"
15 )
16
17 func TestBlockHeader(t *testing.T) {
18         blockHeader := &BlockHeader{
19                 Version:           1,
20                 Height:            432234,
21                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
22                 Timestamp:         1522908275,
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         }, "")
39
40         gotHex := testutil.Serialize(t, blockHeader)
41         want, err := hex.DecodeString(wantHex)
42         if err != nil {
43                 t.Fatal(err)
44         }
45
46         if !bytes.Equal(gotHex, want) {
47                 t.Errorf("empty block header bytes = %x want %x", gotHex, want)
48         }
49
50         gotBlockHeader := BlockHeader{}
51         if _, err := gotBlockHeader.readFrom(blockchain.NewReader(want)); err != nil {
52                 t.Fatal(err)
53         }
54
55         if !testutil.DeepEqual(gotBlockHeader, *blockHeader) {
56                 t.Errorf("got:\n%s\nwant:\n%s", spew.Sdump(gotBlockHeader), spew.Sdump(*blockHeader))
57         }
58 }
59
60 func TestMarshalBlockHeader(t *testing.T) {
61         cases := []struct {
62                 blockHeader *BlockHeader
63                 wantHex     string
64                 wantError   error
65         }{
66                 {
67                         blockHeader: &BlockHeader{
68                                 Version:           1,
69                                 Height:            10000,
70                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
71                                 Timestamp:         1528945000,
72                                 BlockCommitment: BlockCommitment{
73                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
74                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
75                                 },
76                         },
77                         wantHex: strings.Join([]string{
78                                 "01",   // serialization flags
79                                 "01",   // version
80                                 "904e", // block height
81                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
82                                 "e8b287d905", // timestamp
83                                 "40",         // commitment extensible field length
84                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
85                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
86                         }, ""),
87                 },
88                 {
89                         blockHeader: &BlockHeader{
90                                 Version:           1,
91                                 Height:            9223372036854775808, // Height > MaxInt64(9223372036854775807)
92                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
93                                 Timestamp:         1528945000,
94                                 BlockCommitment: BlockCommitment{
95                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
96                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
97                                 },
98                         },
99                         wantError: blockchain.ErrRange,
100                 },
101                 {
102                         blockHeader: &BlockHeader{
103                                 Version:           1,
104                                 Height:            10000,
105                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
106                                 Timestamp:         9223372036854775808, // Timestamp > MaxInt64(9223372036854775807)
107                                 BlockCommitment: BlockCommitment{
108                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
109                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
110                                 },
111                         },
112                         wantError: blockchain.ErrRange,
113                 },
114                 {
115                         blockHeader: &BlockHeader{
116                                 Version:           1,
117                                 Height:            9223372036854775807, // MaxInt64(9223372036854775807)
118                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
119                                 Timestamp:         1528945000,
120                         },
121                         wantHex: strings.Join([]string{
122                                 "01",                 // serialization flags
123                                 "01",                 // version
124                                 "ffffffffffffffff7f", // block height
125                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
126                                 "e8b287d905", // timestamp
127                                 "40",         // commitment extensible field length
128                                 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
129                                 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
130                         }, ""),
131                 },
132         }
133
134         for i, test := range cases {
135                 got, err := test.blockHeader.MarshalText()
136                 if err != nil && err != test.wantError {
137                         t.Errorf("test %d: got error = %x, want = %x", i, err, test.wantError)
138                 } else if err != nil && err == test.wantError {
139                         continue
140                 }
141
142                 if string(got) != test.wantHex {
143                         t.Errorf("test %d: got strbytes = %s, want %s", i, string(got), test.wantHex)
144                 }
145
146                 resultBlockHeader := &BlockHeader{}
147                 if err := resultBlockHeader.UnmarshalText(got); err != nil {
148                         t.Fatal(err)
149                 }
150
151                 if !testutil.DeepEqual(*resultBlockHeader, *test.blockHeader) {
152                         t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(*resultBlockHeader), spew.Sdump(*test.blockHeader))
153                 }
154         }
155 }
156
157 func TestUnmarshalBlockHeader(t *testing.T) {
158         cases := []struct {
159                 hexBlockHeader  string
160                 wantBlockHeader *BlockHeader
161                 wantError       error
162         }{
163                 {
164                         hexBlockHeader: strings.Join([]string{
165                                 "01",   // serialization flags (SerBlockHeader = 01)
166                                 "01",   // version
167                                 "904e", // block height
168                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
169                                 "e8b287d905", // timestamp
170                                 "40",         // commitment extensible field length
171                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
172                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
173                         }, ""),
174                         wantBlockHeader: &BlockHeader{
175                                 Version:           1,
176                                 Height:            10000,
177                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
178                                 Timestamp:         1528945000,
179                                 BlockCommitment: BlockCommitment{
180                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
181                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
182                                 },
183                         },
184                 },
185                 {
186                         hexBlockHeader: strings.Join([]string{
187                                 "03",   // serialization flags (SerBlockFull = 03)
188                                 "01",   // version
189                                 "904e", // block height
190                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
191                                 "e8b287d905", // timestamp
192                                 "40",         // commitment extensible field length
193                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
194                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
195                         }, ""),
196                         wantBlockHeader: &BlockHeader{
197                                 Version:           1,
198                                 Height:            10000,
199                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
200                                 Timestamp:         1528945000,
201                                 BlockCommitment: BlockCommitment{
202                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
203                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
204                                 },
205                         },
206                 },
207                 {
208                         hexBlockHeader: strings.Join([]string{
209                                 "02",   // serialization flags (SerBlockTransactions = 02)
210                                 "01",   // version
211                                 "904e", // block height
212                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
213                                 "e8b287d905", // timestamp
214                                 "40",         // commitment extensible field length
215                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
216                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
217                         }, ""),
218                         wantError: fmt.Errorf("unsupported serialization flags 0x02"),
219                 },
220                 {
221                         hexBlockHeader: strings.Join([]string{
222                                 "01",  // serialization flags
223                                 "01",  // version
224                                 "908", // block height (error with odd length)
225                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
226                                 "e8b287d905", // timestamp
227                                 "40",         // commitment extensible field length
228                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
229                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
230                         }, ""),
231                         wantError: hex.ErrLength,
232                 },
233                 {
234                         hexBlockHeader: strings.Join([]string{
235                                 "01",                 // serialization flags
236                                 "01",                 // version
237                                 "ffffffffffffffffff", // block height
238                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
239                                 "e8b287d905", // timestamp
240                                 "40",         // commitment extensible field length
241                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
242                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
243                         }, ""),
244                         wantError: errors.New("binary: varint overflows a 64-bit integer"),
245                 },
246                 {
247                         hexBlockHeader: strings.Join([]string{
248                                 "01",                 // serialization flags
249                                 "01",                 // version
250                                 "ffffffffffffffff7f", // block height
251                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
252                                 "e8b287d905", // timestamp
253                                 "40",         // commitment extensible field length
254                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
255                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a14",   // tx status hash
256                         }, ""),
257                         wantError: fmt.Errorf("unexpected EOF"),
258                 },
259                 {
260                         hexBlockHeader: strings.Join([]string{
261                                 "01",                 // serialization flags
262                                 "01",                 // version
263                                 "ffffffffffffffff7f", // block height
264                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
265                                 "e8b287d905", // timestamp
266                                 "40",         // commitment extensible field length
267                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
268                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
269                         }, ""),
270                         wantBlockHeader: &BlockHeader{
271                                 Version:           1,
272                                 Height:            9223372036854775807, // MaxInt64(9223372036854775807)
273                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
274                                 Timestamp:         1528945000,
275                                 BlockCommitment: BlockCommitment{
276                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
277                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
278                                 },
279                         },
280                 },
281         }
282
283         for i, test := range cases {
284                 resultBlockHeader := &BlockHeader{}
285                 err := resultBlockHeader.UnmarshalText([]byte(test.hexBlockHeader))
286                 if err != nil && err.Error() != test.wantError.Error() {
287                         t.Errorf("test %d: got error = %s, want = %s", i, err.Error(), test.wantError.Error())
288                 } else if err != nil && err.Error() == test.wantError.Error() {
289                         continue
290                 }
291                 if !testutil.DeepEqual(*resultBlockHeader, *test.wantBlockHeader) {
292                         t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(*resultBlockHeader), spew.Sdump(*test.wantBlockHeader))
293                 }
294         }
295 }