7 "github.com/davecgh/go-spew/spew"
9 "github.com/vapor/consensus"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/bc/types"
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)},
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)},
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)},
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)},
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)},
42 func TestTransactionMessage(t *testing.T) {
43 for _, tx := range txs {
44 txMsg, err := NewTransactionMessage(tx)
46 t.Fatalf("create tx msg err:%s", err)
49 gotTx, err := txMsg.GetTransaction()
51 t.Fatalf("get txs from txsMsg err:%s", err)
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))
59 func TestTransactionsMessage(t *testing.T) {
60 txsMsg, err := NewTransactionsMessage(txs)
62 t.Fatalf("create txs msg err:%s", err)
65 gotTxs, err := txsMsg.GetTransactions()
67 t.Fatalf("get txs from txsMsg err:%s", err)
70 if len(gotTxs) != len(txs) {
71 t.Fatal("txs msg test err: number of txs not match ")
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))
81 var testBlock = &types.Block{
82 BlockHeader: types.BlockHeader{
85 Timestamp: 1528945000000,
86 BlockCommitment: types.BlockCommitment{
87 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
88 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
93 func TestBlockMessage(t *testing.T) {
94 blockMsg, err := NewBlockMessage(testBlock)
96 t.Fatalf("create new block msg err:%s", err)
99 gotBlock, err := blockMsg.GetBlock()
101 t.Fatalf("got block err:%s", err)
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))
108 blockMsg.RawBlock[1] = blockMsg.RawBlock[1] + 0x1
109 _, err = blockMsg.GetBlock()
111 t.Fatalf("get mine block err")
115 var testHeaders = []*types.BlockHeader{
119 Timestamp: 1528945000000,
120 BlockCommitment: types.BlockCommitment{
121 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
122 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
128 Timestamp: 1528945000000,
129 BlockCommitment: types.BlockCommitment{
130 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
131 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
137 Timestamp: 1528945000000,
138 BlockCommitment: types.BlockCommitment{
139 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
140 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
145 func TestHeadersMessage(t *testing.T) {
146 headersMsg, err := NewHeadersMessage(testHeaders)
148 t.Fatalf("create headers msg err:%s", err)
151 gotHeaders, err := headersMsg.GetHeaders()
153 t.Fatalf("got headers err:%s", err)
156 if !reflect.DeepEqual(gotHeaders, testHeaders) {
157 t.Errorf("headers msg test err: got %s\nwant %s", spew.Sdump(gotHeaders), spew.Sdump(testHeaders))
161 func TestGetBlockMessage(t *testing.T) {
162 getBlockMsg := GetBlockMessage{RawHash: [32]byte{0x01}}
163 gotHash := getBlockMsg.GetHash()
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))
170 type testGetHeadersMessage struct {
171 blockLocator []*bc.Hash
175 func TestGetHeadersMessage(t *testing.T) {
176 testMsg := testGetHeadersMessage{
177 blockLocator: []*bc.Hash{{V0: 0x01}, {V0: 0x02}, {V0: 0x03}},
178 stopHash: &bc.Hash{V0: 0xaa, V2: 0x55},
180 getHeadersMsg := NewGetHeadersMessage(testMsg.blockLocator, testMsg.stopHash)
181 gotBlockLocator := getHeadersMsg.GetBlockLocator()
182 gotStopHash := getHeadersMsg.GetStopHash()
184 if !reflect.DeepEqual(testMsg.blockLocator, gotBlockLocator) {
185 t.Errorf("get headers msg test err: got %s\nwant %s", spew.Sdump(gotBlockLocator), spew.Sdump(testMsg.blockLocator))
188 if !reflect.DeepEqual(testMsg.stopHash, gotStopHash) {
189 t.Errorf("get headers msg test err: got %s\nwant %s", spew.Sdump(gotStopHash), spew.Sdump(testMsg.stopHash))
193 var testBlocks = []*types.Block{
195 BlockHeader: types.BlockHeader{
198 Timestamp: 1528945000000,
199 BlockCommitment: types.BlockCommitment{
200 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
201 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
206 BlockHeader: types.BlockHeader{
209 Timestamp: 1528945000000,
210 BlockCommitment: types.BlockCommitment{
211 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
212 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
218 func TestBlocksMessage(t *testing.T) {
219 blocksMsg, err := NewBlocksMessage(testBlocks)
221 t.Fatalf("create blocks msg err:%s", err)
223 gotBlocks, err := blocksMsg.GetBlocks()
225 t.Fatalf("get blocks err:%s", err)
228 for _, gotBlock := range gotBlocks {
229 if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
230 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
235 func TestStatusMessage(t *testing.T) {
236 statusResponseMsg := NewStatusMessage(&testBlock.BlockHeader)
237 gotHash := statusResponseMsg.GetHash()
238 if !reflect.DeepEqual(*gotHash, testBlock.Hash()) {
239 t.Errorf("status response msg test err: got %s\nwant %s", spew.Sdump(*gotHash), spew.Sdump(testBlock.Hash()))