OSDN Git Service

fix votetx for validation (#75)
[bytom/vapor.git] / netsync / message_test.go
1 package netsync
2
3 import (
4         "reflect"
5         "testing"
6
7         "github.com/davecgh/go-spew/spew"
8
9         "github.com/vapor/protocol/bc"
10         "github.com/vapor/protocol/bc/types"
11 )
12
13 var testBlock = &types.Block{
14         BlockHeader: types.BlockHeader{
15                 Version:   1,
16                 Height:    0,
17                 Timestamp: 1528945000000,
18                 BlockCommitment: types.BlockCommitment{
19                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
20                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
21                 },
22         },
23 }
24
25 func TestBlockMessage(t *testing.T) {
26         blockMsg, err := NewBlockMessage(testBlock)
27         if err != nil {
28                 t.Fatalf("create new block msg err:%s", err)
29         }
30
31         gotBlock, err := blockMsg.GetBlock()
32         if err != nil {
33                 t.Fatalf("got block err:%s", err)
34         }
35
36         if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
37                 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
38         }
39
40         blockMsg.RawBlock[1] = blockMsg.RawBlock[1] + 0x1
41         _, err = blockMsg.GetBlock()
42         if err == nil {
43                 t.Fatalf("get mine block err")
44         }
45 }
46
47 var testHeaders = []*types.BlockHeader{
48         {
49                 Version:   1,
50                 Height:    0,
51                 Timestamp: 1528945000000,
52                 BlockCommitment: types.BlockCommitment{
53                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
54                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
55                 },
56         },
57         {
58                 Version:   1,
59                 Height:    1,
60                 Timestamp: 1528945000000,
61                 BlockCommitment: types.BlockCommitment{
62                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
63                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
64                 },
65         },
66         {
67                 Version:   1,
68                 Height:    3,
69                 Timestamp: 1528945000000,
70                 BlockCommitment: types.BlockCommitment{
71                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
72                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
73                 },
74         },
75 }
76
77 func TestHeadersMessage(t *testing.T) {
78         headersMsg, err := NewHeadersMessage(testHeaders)
79         if err != nil {
80                 t.Fatalf("create headers msg err:%s", err)
81         }
82
83         gotHeaders, err := headersMsg.GetHeaders()
84         if err != nil {
85                 t.Fatalf("got headers err:%s", err)
86         }
87
88         if !reflect.DeepEqual(gotHeaders, testHeaders) {
89                 t.Errorf("headers msg test err: got %s\nwant %s", spew.Sdump(gotHeaders), spew.Sdump(testHeaders))
90         }
91 }
92
93 func TestGetBlockMessage(t *testing.T) {
94         getBlockMsg := GetBlockMessage{RawHash: [32]byte{0x01}}
95         gotHash := getBlockMsg.GetHash()
96
97         if !reflect.DeepEqual(gotHash.Byte32(), getBlockMsg.RawHash) {
98                 t.Errorf("get block msg test err: got %s\nwant %s", spew.Sdump(gotHash.Byte32()), spew.Sdump(getBlockMsg.RawHash))
99         }
100 }
101
102 type testGetHeadersMessage struct {
103         blockLocator []*bc.Hash
104         stopHash     *bc.Hash
105 }
106
107 func TestGetHeadersMessage(t *testing.T) {
108         testMsg := testGetHeadersMessage{
109                 blockLocator: []*bc.Hash{{V0: 0x01}, {V0: 0x02}, {V0: 0x03}},
110                 stopHash:     &bc.Hash{V0: 0xaa, V2: 0x55},
111         }
112         getHeadersMsg := NewGetHeadersMessage(testMsg.blockLocator, testMsg.stopHash)
113         gotBlockLocator := getHeadersMsg.GetBlockLocator()
114         gotStopHash := getHeadersMsg.GetStopHash()
115
116         if !reflect.DeepEqual(testMsg.blockLocator, gotBlockLocator) {
117                 t.Errorf("get headers msg test err: got %s\nwant %s", spew.Sdump(gotBlockLocator), spew.Sdump(testMsg.blockLocator))
118         }
119
120         if !reflect.DeepEqual(testMsg.stopHash, gotStopHash) {
121                 t.Errorf("get headers msg test err: got %s\nwant %s", spew.Sdump(gotStopHash), spew.Sdump(testMsg.stopHash))
122         }
123 }
124
125 var testBlocks = []*types.Block{
126         {
127                 BlockHeader: types.BlockHeader{
128                         Version:   1,
129                         Height:    0,
130                         Timestamp: 1528945000000,
131                         BlockCommitment: types.BlockCommitment{
132                                 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
133                                 TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
134                         },
135                 },
136         },
137         {
138                 BlockHeader: types.BlockHeader{
139                         Version:   1,
140                         Height:    0,
141                         Timestamp: 1528945000000,
142                         BlockCommitment: types.BlockCommitment{
143                                 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
144                                 TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
145                         },
146                 },
147         },
148 }
149
150 func TestBlocksMessage(t *testing.T) {
151         blocksMsg, err := NewBlocksMessage(testBlocks)
152         if err != nil {
153                 t.Fatalf("create blocks msg err:%s", err)
154         }
155         gotBlocks, err := blocksMsg.GetBlocks()
156         if err != nil {
157                 t.Fatalf("get blocks err:%s", err)
158         }
159
160         for _, gotBlock := range gotBlocks {
161                 if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
162                         t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
163                 }
164         }
165 }
166
167 func TestStatusMessage(t *testing.T) {
168         statusResponseMsg := NewStatusMessage(&testBlock.BlockHeader)
169         gotHash := statusResponseMsg.GetHash()
170         if !reflect.DeepEqual(*gotHash, testBlock.Hash()) {
171                 t.Errorf("status response msg test err: got %s\nwant %s", spew.Sdump(*gotHash), spew.Sdump(testBlock.Hash()))
172         }
173 }