OSDN Git Service

ts to ms (#71)
[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:         1522908275000,
23                 BlockCommitment: BlockCommitment{
24                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
25                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
26                 },
27                 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
28         }
29
30         wantHex := strings.Join([]string{
31                 "01",     // serialization flags
32                 "01",     // version
33                 "eab01a", // block height
34                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
35                 "b8c2a0a3a92c", // timestamp
36                 "40",           // commitment extensible field length
37                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
38                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
39                 "040102beef", //BlockWitness
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 }
61
62 func TestMarshalBlockHeader(t *testing.T) {
63         cases := []struct {
64                 blockHeader *BlockHeader
65                 wantHex     string
66                 wantError   error
67         }{
68                 {
69                         blockHeader: &BlockHeader{
70                                 Version:           1,
71                                 Height:            10000,
72                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
73                                 Timestamp:         1528945000000,
74                                 BlockCommitment: BlockCommitment{
75                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
76                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
77                                 },
78                                 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
79                         },
80                         wantHex: strings.Join([]string{
81                                 "01",   // serialization flags
82                                 "01",   // version
83                                 "904e", // block height
84                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
85                                 "c0fce4e1bf2c", // timestamp
86                                 "40",           // commitment extensible field length
87                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
88                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
89                                 "040102beef", //BlockWitness
90                         }, ""),
91                 },
92                 {
93                         blockHeader: &BlockHeader{
94                                 Version:           1,
95                                 Height:            9223372036854775808, // Height > MaxInt64(9223372036854775807)
96                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
97                                 Timestamp:         1528945000000,
98                                 BlockCommitment: BlockCommitment{
99                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
100                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
101                                 },
102                                 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
103                         },
104                         wantError: blockchain.ErrRange,
105                 },
106                 {
107                         blockHeader: &BlockHeader{
108                                 Version:           1,
109                                 Height:            10000,
110                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
111                                 Timestamp:         9223372036854775808, // Timestamp > MaxInt64(9223372036854775807)
112                                 BlockCommitment: BlockCommitment{
113                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
114                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
115                                 },
116                                 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
117                         },
118                         wantError: blockchain.ErrRange,
119                 },
120                 {
121                         blockHeader: &BlockHeader{
122                                 Version:           1,
123                                 Height:            9223372036854775807, // MaxInt64(9223372036854775807)
124                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
125                                 Timestamp:         1528945000000,
126                                 BlockWitness:      BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
127                         },
128                         wantHex: strings.Join([]string{
129                                 "01",                 // serialization flags
130                                 "01",                 // version
131                                 "ffffffffffffffff7f", // block height
132                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
133                                 "c0fce4e1bf2c", // timestamp
134                                 "40",           // commitment extensible field length
135                                 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
136                                 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
137                                 "040102beef", //BlockWitness
138                         }, ""),
139                 },
140                 {
141                         blockHeader: &BlockHeader{
142                                 Version:           1,
143                                 Height:            9223372036854775807, // MaxInt64(9223372036854775807)
144                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
145                                 Timestamp:         1528945000000,
146                                 BlockWitness:      BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, []byte{0xab, 0xcd}, []byte{0xcd, 0x68}}},
147                         },
148                         wantHex: strings.Join([]string{
149                                 "01",                 // serialization flags
150                                 "01",                 // version
151                                 "ffffffffffffffff7f", // block height
152                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
153                                 "c0fce4e1bf2c", // timestamp
154                                 "40",           // commitment extensible field length
155                                 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
156                                 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
157                                 "0a0302beef02abcd02cd68", //BlockWitness
158                         }, ""),
159                 },
160                 {
161                         blockHeader: &BlockHeader{
162                                 Version:           1,
163                                 Height:            9223372036854775807, // MaxInt64(9223372036854775807)
164                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
165                                 Timestamp:         1528945000000,
166                                 BlockWitness:      BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, nil, []byte{0xcd, 0x68}}},
167                         },
168                         wantHex: strings.Join([]string{
169                                 "01",                 // serialization flags
170                                 "01",                 // version
171                                 "ffffffffffffffff7f", // block height
172                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
173                                 "c0fce4e1bf2c", // timestamp
174                                 "40",           // commitment extensible field length
175                                 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
176                                 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
177                                 "080302beef0002cd68", //BlockWitness
178                         }, ""),
179                 },
180                 {
181                         blockHeader: &BlockHeader{
182                                 Version:           1,
183                                 Height:            9223372036854775807, // MaxInt64(9223372036854775807)
184                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
185                                 Timestamp:         1528945000000,
186                                 BlockWitness:      BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, []byte{}, []byte{0xcd, 0x68}}},
187                         },
188                         wantHex: strings.Join([]string{
189                                 "01",                 // serialization flags
190                                 "01",                 // version
191                                 "ffffffffffffffff7f", // block height
192                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
193                                 "c0fce4e1bf2c", // timestamp
194                                 "40",           // commitment extensible field length
195                                 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
196                                 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
197                                 "080302beef0002cd68", //BlockWitness
198                         }, ""),
199                 },
200                 {
201                         blockHeader: &BlockHeader{
202                                 Version:           1,
203                                 Height:            9223372036854775807, // MaxInt64(9223372036854775807)
204                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
205                                 Timestamp:         1528945000000,
206                                 BlockWitness:      BlockWitness{Witness: [][]byte{}},
207                         },
208                         wantHex: strings.Join([]string{
209                                 "01",                 // serialization flags
210                                 "01",                 // version
211                                 "ffffffffffffffff7f", // block height
212                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
213                                 "c0fce4e1bf2c", // timestamp
214                                 "40",           // commitment extensible field length
215                                 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
216                                 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
217                                 "0100", //BlockWitness
218                         }, ""),
219                 },
220         }
221
222         for i, test := range cases {
223                 got, err := test.blockHeader.MarshalText()
224                 if err != nil && err != test.wantError {
225                         t.Errorf("test %d: got error = %x, want = %x", i, err, test.wantError)
226                 } else if err != nil && err == test.wantError {
227                         continue
228                 }
229
230                 if string(got) != test.wantHex {
231                         t.Errorf("test %d: got strbytes = %s, want %s", i, string(got), test.wantHex)
232                 }
233
234                 resultBlockHeader := &BlockHeader{}
235                 if err := resultBlockHeader.UnmarshalText(got); err != nil {
236                         t.Fatal(err)
237                 }
238
239                 if !testutil.DeepEqual(*resultBlockHeader, *test.blockHeader) {
240                         t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(*resultBlockHeader), spew.Sdump(*test.blockHeader))
241                 }
242         }
243 }
244
245 func TestUnmarshalBlockHeader(t *testing.T) {
246         cases := []struct {
247                 hexBlockHeader  string
248                 wantBlockHeader *BlockHeader
249                 wantError       error
250         }{
251                 {
252                         hexBlockHeader: strings.Join([]string{
253                                 "01",   // serialization flags (SerBlockHeader = 01)
254                                 "01",   // version
255                                 "904e", // block height
256                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
257                                 "c0fce4e1bf2c", // timestamp
258                                 "40",           // commitment extensible field length
259                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
260                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
261                                 "040102beef", //BlockWitness
262                         }, ""),
263                         wantBlockHeader: &BlockHeader{
264                                 Version:           1,
265                                 Height:            10000,
266                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
267                                 Timestamp:         1528945000000,
268                                 BlockCommitment: BlockCommitment{
269                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
270                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
271                                 },
272                                 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
273                         },
274                 },
275                 {
276                         hexBlockHeader: strings.Join([]string{
277                                 "03",   // serialization flags (SerBlockFull = 03)
278                                 "01",   // version
279                                 "904e", // block height
280                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
281                                 "c0fce4e1bf2c", // timestamp
282                                 "40",           // commitment extensible field length
283                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
284                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
285                                 "040102beef", //BlockWitness
286                         }, ""),
287                         wantBlockHeader: &BlockHeader{
288                                 Version:           1,
289                                 Height:            10000,
290                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
291                                 Timestamp:         1528945000000,
292                                 BlockCommitment: BlockCommitment{
293                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
294                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
295                                 },
296                                 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
297                         },
298                 },
299                 {
300                         hexBlockHeader: strings.Join([]string{
301                                 "02",   // serialization flags (SerBlockTransactions = 02)
302                                 "01",   // version
303                                 "904e", // block height
304                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
305                                 "c0fce4e1bf2c", // timestamp
306                                 "40",           // commitment extensible field length
307                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
308                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
309                         }, ""),
310                         wantError: fmt.Errorf("unsupported serialization flags 0x02"),
311                 },
312                 {
313                         hexBlockHeader: strings.Join([]string{
314                                 "01",  // serialization flags
315                                 "01",  // version
316                                 "908", // block height (error with odd length)
317                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
318                                 "c0fce4e1bf2c", // timestamp
319                                 "40",           // commitment extensible field length
320                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
321                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
322                         }, ""),
323                         wantError: hex.ErrLength,
324                 },
325                 {
326                         hexBlockHeader: strings.Join([]string{
327                                 "01",                 // serialization flags
328                                 "01",                 // version
329                                 "ffffffffffffffffff", // block height
330                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
331                                 "c0fce4e1bf2c", // timestamp
332                                 "40",           // commitment extensible field length
333                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
334                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
335                                 "040102beef", //BlockWitness
336                         }, ""),
337                         wantError: errors.New("binary: varint overflows a 64-bit integer"),
338                 },
339                 {
340                         hexBlockHeader: strings.Join([]string{
341                                 "01",                 // serialization flags
342                                 "01",                 // version
343                                 "ffffffffffffffff7f", // block height
344                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
345                                 "c0fce4e1bf2c", // timestamp
346                                 "40",           // commitment extensible field length
347                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
348                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a14",   // tx status hash
349                         }, ""),
350                         wantError: fmt.Errorf("unexpected EOF"),
351                 },
352                 {
353                         hexBlockHeader: strings.Join([]string{
354                                 "01",                 // serialization flags
355                                 "01",                 // version
356                                 "ffffffffffffffff7f", // block height
357                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
358                                 "c0fce4e1bf2c", // timestamp
359                                 "40",           // commitment extensible field length
360                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
361                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
362                                 "040102beef", //BlockWitness
363                         }, ""),
364                         wantBlockHeader: &BlockHeader{
365                                 Version:           1,
366                                 Height:            9223372036854775807, // MaxInt64(9223372036854775807)
367                                 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
368                                 Timestamp:         1528945000000,
369                                 BlockCommitment: BlockCommitment{
370                                         TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
371                                         TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
372                                 },
373                                 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
374                         },
375                 },
376         }
377
378         for i, test := range cases {
379                 resultBlockHeader := &BlockHeader{}
380                 err := resultBlockHeader.UnmarshalText([]byte(test.hexBlockHeader))
381                 if err != nil && err.Error() != test.wantError.Error() {
382                         t.Errorf("test %d: got error = %s, want = %s", i, err.Error(), test.wantError.Error())
383                 } else if err != nil && err.Error() == test.wantError.Error() {
384                         continue
385                 }
386                 if !testutil.DeepEqual(*resultBlockHeader, *test.wantBlockHeader) {
387                         t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(*resultBlockHeader), spew.Sdump(*test.wantBlockHeader))
388                 }
389         }
390 }