10 "github.com/davecgh/go-spew/spew"
12 "github.com/vapor/encoding/blockchain"
13 "github.com/vapor/errors"
14 "github.com/vapor/testutil"
17 func TestBlockHeader(t *testing.T) {
18 blockHeader := &BlockHeader{
21 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
22 Timestamp: 1522908275000,
23 BlockCommitment: BlockCommitment{
24 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
25 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
27 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
30 wantHex := strings.Join([]string{
31 "01", // serialization flags
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
42 gotHex := testutil.Serialize(t, blockHeader)
43 want, err := hex.DecodeString(wantHex)
48 if !bytes.Equal(gotHex, want) {
49 t.Errorf("empty block header bytes = %x want %x", gotHex, want)
52 gotBlockHeader := BlockHeader{}
53 if _, err := gotBlockHeader.readFrom(blockchain.NewReader(want)); err != nil {
57 if !testutil.DeepEqual(gotBlockHeader, *blockHeader) {
58 t.Errorf("got:\n%s\nwant:\n%s", spew.Sdump(gotBlockHeader), spew.Sdump(*blockHeader))
62 func TestMarshalBlockHeader(t *testing.T) {
64 blockHeader *BlockHeader
69 blockHeader: &BlockHeader{
72 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
73 Timestamp: 1528945000000,
74 BlockCommitment: BlockCommitment{
75 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
76 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
78 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
80 wantHex: strings.Join([]string{
81 "01", // serialization flags
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
93 blockHeader: &BlockHeader{
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"),
102 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
104 wantError: blockchain.ErrRange,
107 blockHeader: &BlockHeader{
110 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
111 Timestamp: 9223372036854775808, // Timestamp > MaxInt64(9223372036854775807)
112 BlockCommitment: BlockCommitment{
113 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
114 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
116 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
118 wantError: blockchain.ErrRange,
121 blockHeader: &BlockHeader{
123 Height: 9223372036854775807, // MaxInt64(9223372036854775807)
124 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
125 Timestamp: 1528945000000,
126 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
128 wantHex: strings.Join([]string{
129 "01", // serialization flags
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
141 blockHeader: &BlockHeader{
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}}},
148 wantHex: strings.Join([]string{
149 "01", // serialization flags
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
161 blockHeader: &BlockHeader{
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}}},
168 wantHex: strings.Join([]string{
169 "01", // serialization flags
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
181 blockHeader: &BlockHeader{
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}}},
188 wantHex: strings.Join([]string{
189 "01", // serialization flags
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
201 blockHeader: &BlockHeader{
203 Height: 9223372036854775807, // MaxInt64(9223372036854775807)
204 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
205 Timestamp: 1528945000000,
206 BlockWitness: BlockWitness{Witness: [][]byte{}},
208 wantHex: strings.Join([]string{
209 "01", // serialization flags
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
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 {
230 if string(got) != test.wantHex {
231 t.Errorf("test %d: got strbytes = %s, want %s", i, string(got), test.wantHex)
234 resultBlockHeader := &BlockHeader{}
235 if err := resultBlockHeader.UnmarshalText(got); err != nil {
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))
245 func TestUnmarshalBlockHeader(t *testing.T) {
247 hexBlockHeader string
248 wantBlockHeader *BlockHeader
252 hexBlockHeader: strings.Join([]string{
253 "01", // serialization flags (SerBlockHeader = 01)
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
263 wantBlockHeader: &BlockHeader{
266 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
267 Timestamp: 1528945000000,
268 BlockCommitment: BlockCommitment{
269 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
270 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
272 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
276 hexBlockHeader: strings.Join([]string{
277 "03", // serialization flags (SerBlockFull = 03)
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
287 wantBlockHeader: &BlockHeader{
290 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
291 Timestamp: 1528945000000,
292 BlockCommitment: BlockCommitment{
293 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
294 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
296 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
300 hexBlockHeader: strings.Join([]string{
301 "02", // serialization flags (SerBlockTransactions = 02)
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
310 wantError: fmt.Errorf("unsupported serialization flags 0x02"),
313 hexBlockHeader: strings.Join([]string{
314 "01", // serialization flags
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
323 wantError: hex.ErrLength,
326 hexBlockHeader: strings.Join([]string{
327 "01", // serialization flags
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
337 wantError: errors.New("binary: varint overflows a 64-bit integer"),
340 hexBlockHeader: strings.Join([]string{
341 "01", // serialization flags
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
350 wantError: fmt.Errorf("unexpected EOF"),
353 hexBlockHeader: strings.Join([]string{
354 "01", // serialization flags
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
364 wantBlockHeader: &BlockHeader{
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"),
373 BlockWitness: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
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() {
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))