8 "github.com/vapor/encoding/blockchain"
9 "github.com/vapor/testutil"
12 func TestReadWriteBlockWitness(t *testing.T) {
18 bw: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
19 hexString: "0102beef",
22 bw: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, []byte{0xab, 0xcd}, []byte{0xcd, 0x68}}},
23 hexString: "0302beef02abcd02cd68",
26 bw: BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, nil, []byte{0xcd, 0x68}}},
27 hexString: "0302beef0002cd68",
30 bw: BlockWitness{Witness: [][]byte{[]byte{}}},
39 for _, c := range cases {
41 buffer := bytes.NewBuffer(buff)
42 if err := c.bw.writeTo(buffer); err != nil {
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)
52 if err := bc.readFrom(blockchain.NewReader(buffer.Bytes())); err != nil {
56 if !testutil.DeepEqual(*bc, c.bw) {
57 t.Errorf("test read block commitment fail, got:%v, want:%v", *bc, c.bw)
62 func TestBlockWitnessSet(t *testing.T) {
70 bw: BlockWitness{Witness: [][]byte{}},
72 data: []byte{0x01, 0x02, 0x03, 0x04},
73 want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}},
76 bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}},
78 data: []byte{0x01, 0x01, 0x01, 0x01},
79 want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x01, 0x01, 0x01}}},
82 bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}},
84 data: []byte{0x04, 0x04, 0x04, 0x04},
85 want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, nil, nil, []byte{0x04, 0x04, 0x04, 0x04}}},
89 for i, c := range cases {
91 newbw.Set(c.index, c.data)
92 if !testutil.DeepEqual(c.want, newbw) {
93 t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want)
98 func TestBlockWitnessDelete(t *testing.T) {
105 bw: BlockWitness{Witness: [][]byte{}},
107 want: BlockWitness{Witness: [][]byte{}},
110 bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}},
112 want: BlockWitness{Witness: [][]byte{[]byte{}}},
115 bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}},
117 want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{}}},
120 bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}},
122 want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}},
126 for i, c := range cases {
128 newbw.Delete(c.index)
129 if !testutil.DeepEqual(c.want, newbw) {
130 t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want)