OSDN Git Service

Peer add announces new block message num limit
[bytom/vapor.git] / netsync / messages / chain_msg_test.go
1 package messages
2
3 import (
4         "reflect"
5         "testing"
6
7         "github.com/davecgh/go-spew/spew"
8
9         "github.com/vapor/consensus"
10         "github.com/vapor/protocol/bc"
11         "github.com/vapor/protocol/bc/types"
12 )
13
14 var txs = []*types.Tx{
15         types.NewTx(types.TxData{
16                 SerializedSize: uint64(52),
17                 Inputs:         []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
18                 Outputs:        []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 5000, nil)},
19         }),
20         types.NewTx(types.TxData{
21                 SerializedSize: uint64(53),
22                 Inputs:         []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02})},
23                 Outputs:        []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 5000, nil)},
24         }),
25         types.NewTx(types.TxData{
26                 SerializedSize: uint64(54),
27                 Inputs:         []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})},
28                 Outputs:        []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 5000, nil)},
29         }),
30         types.NewTx(types.TxData{
31                 SerializedSize: uint64(54),
32                 Inputs:         []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})},
33                 Outputs:        []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 2000, nil)},
34         }),
35         types.NewTx(types.TxData{
36                 SerializedSize: uint64(54),
37                 Inputs:         []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})},
38                 Outputs:        []*types.TxOutput{types.NewIntraChainOutput(*consensus.BTMAssetID, 10000, nil)},
39         }),
40 }
41
42 func TestTransactionMessage(t *testing.T) {
43         for _, tx := range txs {
44                 txMsg, err := NewTransactionMessage(tx)
45                 if err != nil {
46                         t.Fatalf("create tx msg err:%s", err)
47                 }
48
49                 gotTx, err := txMsg.GetTransaction()
50                 if err != nil {
51                         t.Fatalf("get txs from txsMsg err:%s", err)
52                 }
53                 if !reflect.DeepEqual(*tx.Tx, *gotTx.Tx) {
54                         t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(tx.Tx), spew.Sdump(gotTx.Tx))
55                 }
56         }
57 }
58
59 func TestTransactionsMessage(t *testing.T) {
60         txsMsg, err := NewTransactionsMessage(txs)
61         if err != nil {
62                 t.Fatalf("create txs msg err:%s", err)
63         }
64
65         gotTxs, err := txsMsg.GetTransactions()
66         if err != nil {
67                 t.Fatalf("get txs from txsMsg err:%s", err)
68         }
69
70         if len(gotTxs) != len(txs) {
71                 t.Fatal("txs msg test err: number of txs not match ")
72         }
73
74         for i, tx := range txs {
75                 if !reflect.DeepEqual(tx.Tx, gotTxs[i].Tx) {
76                         t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(tx.Tx), spew.Sdump(gotTxs[i].Tx))
77                 }
78         }
79 }
80
81 var testBlock = &types.Block{
82         BlockHeader: types.BlockHeader{
83                 Version:   1,
84                 Height:    0,
85                 Timestamp: 1528945000000,
86                 BlockCommitment: types.BlockCommitment{
87                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
88                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
89                 },
90         },
91 }
92
93 func TestBlockMessage(t *testing.T) {
94         blockMsg, err := NewBlockMessage(testBlock)
95         if err != nil {
96                 t.Fatalf("create new block msg err:%s", err)
97         }
98
99         gotBlock, err := blockMsg.GetBlock()
100         if err != nil {
101                 t.Fatalf("got block err:%s", err)
102         }
103
104         if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
105                 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
106         }
107
108         blockMsg.RawBlock[1] = blockMsg.RawBlock[1] + 0x1
109         _, err = blockMsg.GetBlock()
110         if err == nil {
111                 t.Fatalf("get mine block err")
112         }
113 }
114
115 var testHeaders = []*types.BlockHeader{
116         {
117                 Version:   1,
118                 Height:    0,
119                 Timestamp: 1528945000000,
120                 BlockCommitment: types.BlockCommitment{
121                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
122                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
123                 },
124         },
125         {
126                 Version:   1,
127                 Height:    1,
128                 Timestamp: 1528945000000,
129                 BlockCommitment: types.BlockCommitment{
130                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
131                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
132                 },
133         },
134         {
135                 Version:   1,
136                 Height:    3,
137                 Timestamp: 1528945000000,
138                 BlockCommitment: types.BlockCommitment{
139                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
140                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
141                 },
142         },
143 }
144
145 func TestHeadersMessage(t *testing.T) {
146         headersMsg, err := NewHeadersMessage(testHeaders)
147         if err != nil {
148                 t.Fatalf("create headers msg err:%s", err)
149         }
150
151         gotHeaders, err := headersMsg.GetHeaders()
152         if err != nil {
153                 t.Fatalf("got headers err:%s", err)
154         }
155
156         if !reflect.DeepEqual(gotHeaders, testHeaders) {
157                 t.Errorf("headers msg test err: got %s\nwant %s", spew.Sdump(gotHeaders), spew.Sdump(testHeaders))
158         }
159 }
160
161 func TestGetBlockMessage(t *testing.T) {
162         getBlockMsg := GetBlockMessage{RawHash: [32]byte{0x01}}
163         gotHash := getBlockMsg.GetHash()
164
165         if !reflect.DeepEqual(gotHash.Byte32(), getBlockMsg.RawHash) {
166                 t.Errorf("get block msg test err: got %s\nwant %s", spew.Sdump(gotHash.Byte32()), spew.Sdump(getBlockMsg.RawHash))
167         }
168 }
169
170 type testGetHeadersMessage struct {
171         blockLocator []*bc.Hash
172         stopHash     *bc.Hash
173         skip         uint64
174 }
175
176 func TestGetHeadersMessage(t *testing.T) {
177         testMsg := testGetHeadersMessage{
178                 blockLocator: []*bc.Hash{{V0: 0x01}, {V0: 0x02}, {V0: 0x03}},
179                 stopHash:     &bc.Hash{V0: 0x01},
180                 skip:         888,
181         }
182         getHeadersMsg := NewGetHeadersMessage(testMsg.blockLocator, testMsg.stopHash, testMsg.skip)
183         gotBlockLocator := getHeadersMsg.GetBlockLocator()
184         gotStopHash := getHeadersMsg.GetStopHash()
185         gotSkip := getHeadersMsg.GetSkip()
186
187         if !reflect.DeepEqual(testMsg.blockLocator, gotBlockLocator) {
188                 t.Errorf("get headers msg test err: got %s\nwant %s", spew.Sdump(gotBlockLocator), spew.Sdump(testMsg.blockLocator))
189         }
190
191         if !reflect.DeepEqual(testMsg.stopHash, gotStopHash) {
192                 t.Errorf("get headers msg test err: amount:got %d\nwant %d", gotStopHash, testMsg.stopHash)
193         }
194
195         if !reflect.DeepEqual(testMsg.skip, gotSkip) {
196                 t.Errorf("get headers msg test err: skip:got %d\nwant %d", gotSkip, testMsg.skip)
197         }
198 }
199
200 var testBlocks = []*types.Block{
201         {
202                 BlockHeader: types.BlockHeader{
203                         Version:   1,
204                         Height:    0,
205                         Timestamp: 1528945000000,
206                         BlockCommitment: types.BlockCommitment{
207                                 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
208                                 TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
209                         },
210                 },
211         },
212         {
213                 BlockHeader: types.BlockHeader{
214                         Version:   1,
215                         Height:    0,
216                         Timestamp: 1528945000000,
217                         BlockCommitment: types.BlockCommitment{
218                                 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
219                                 TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
220                         },
221                 },
222         },
223 }
224
225 func TestBlocksMessage(t *testing.T) {
226         blocksMsg, err := NewBlocksMessage(testBlocks)
227         if err != nil {
228                 t.Fatalf("create blocks msg err:%s", err)
229         }
230         gotBlocks, err := blocksMsg.GetBlocks()
231         if err != nil {
232                 t.Fatalf("get blocks err:%s", err)
233         }
234
235         for _, gotBlock := range gotBlocks {
236                 if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
237                         t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
238                 }
239         }
240 }
241
242 func TestStatusMessage(t *testing.T) {
243         statusResponseMsg := NewStatusMessage(&testBlock.BlockHeader, &testBlock.BlockHeader)
244         gotBestHash := statusResponseMsg.GetBestHash()
245         if !reflect.DeepEqual(*gotBestHash, testBlock.Hash()) {
246                 t.Errorf("status response msg test err: got %s\nwant %s", spew.Sdump(*gotBestHash), spew.Sdump(testBlock.Hash()))
247         }
248         gotIrreversibleHash := statusResponseMsg.GetIrreversibleHash()
249         if !reflect.DeepEqual(*gotIrreversibleHash, testBlock.Hash()) {
250                 t.Errorf("status response msg test err: got %s\nwant %s", spew.Sdump(*gotIrreversibleHash), spew.Sdump(testBlock.Hash()))
251         }
252 }