OSDN Git Service

fix utxo valid height (#174)
[bytom/vapor.git] / protocol / bc / types / block_witness_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "testing"
7
8         "github.com/vapor/encoding/blockchain"
9         "github.com/vapor/testutil"
10 )
11
12 func TestReadWriteBlockWitness(t *testing.T) {
13         cases := []struct {
14                 bw        BlockWitness
15                 hexString string
16         }{
17                 {
18                         bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
19                         hexString: "0102beef",
20                 },
21                 {
22                         bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, []byte{0xab, 0xcd}, []byte{0xcd, 0x68}}},
23                         hexString: "0302beef02abcd02cd68",
24                 },
25                 {
26                         bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, nil, []byte{0xcd, 0x68}}},
27                         hexString: "0302beef0002cd68",
28                 },
29                 {
30                         bw:        BlockWitness{Witness: [][]byte{[]byte{}}},
31                         hexString: "0100",
32                 },
33                 {
34                         bw:        BlockWitness{},
35                         hexString: "00",
36                 },
37         }
38
39         for _, c := range cases {
40                 buff := []byte{}
41                 buffer := bytes.NewBuffer(buff)
42                 if err := c.bw.writeTo(buffer); err != nil {
43                         t.Fatal(err)
44                 }
45
46                 hexString := hex.EncodeToString(buffer.Bytes())
47                 if hexString != c.hexString {
48                         t.Errorf("test write block commitment fail, got:%s, want:%s", hexString, c.hexString)
49                 }
50
51                 bc := &BlockWitness{}
52                 if err := bc.readFrom(blockchain.NewReader(buffer.Bytes())); err != nil {
53                         t.Fatal(err)
54                 }
55
56                 if !testutil.DeepEqual(*bc, c.bw) {
57                         t.Errorf("test read block commitment fail, got:%v, want:%v", *bc, c.bw)
58                 }
59         }
60 }