OSDN Git Service

rename (#465)
[bytom/vapor.git] / protocol / bc / types / block_commitment_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "testing"
7
8         "github.com/bytom/vapor/encoding/blockchain"
9         "github.com/bytom/vapor/testutil"
10 )
11
12 func TestReadWriteBlockCommitment(t *testing.T) {
13         cases := []struct {
14                 bc        BlockCommitment
15                 hexString string
16         }{
17                 {
18                         bc: BlockCommitment{
19                                 TransactionsMerkleRoot: testutil.MustDecodeHash("35a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad2"),
20                                 TransactionStatusHash:  testutil.MustDecodeHash("6978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a"),
21                         },
22                         hexString: "35a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad26978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a",
23                 },
24                 {
25                         bc: BlockCommitment{
26                                 TransactionsMerkleRoot: testutil.MustDecodeHash("8ec3ee7589f95eee9b534f71fcd37142bcc839a0dbfe78124df9663827b90c35"),
27                                 TransactionStatusHash:  testutil.MustDecodeHash("011bd3380852b2946df507e0c6234222c559eec8f545e4bc58a89e960892259b"),
28                         },
29                         hexString: "8ec3ee7589f95eee9b534f71fcd37142bcc839a0dbfe78124df9663827b90c35011bd3380852b2946df507e0c6234222c559eec8f545e4bc58a89e960892259b",
30                 },
31         }
32
33         for _, c := range cases {
34                 buff := []byte{}
35                 buffer := bytes.NewBuffer(buff)
36                 if err := c.bc.writeTo(buffer); err != nil {
37                         t.Fatal(err)
38                 }
39
40                 hexString := hex.EncodeToString(buffer.Bytes())
41                 if hexString != c.hexString {
42                         t.Errorf("test write block commitment fail, got:%s, want:%s", hexString, c.hexString)
43                 }
44
45                 bc := &BlockCommitment{}
46                 if err := bc.readFrom(blockchain.NewReader(buffer.Bytes())); err != nil {
47                         t.Fatal(err)
48                 }
49
50                 if !testutil.DeepEqual(*bc, c.bc) {
51                         t.Errorf("test read block commitment fail, got:%v, want:%v", *bc, c.bc)
52                 }
53         }
54 }