7 "github.com/davecgh/go-spew/spew"
9 "github.com/bytom/vapor/consensus"
10 "github.com/bytom/vapor/protocol/bc"
11 "github.com/bytom/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
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},
182 getHeadersMsg := NewGetHeadersMessage(testMsg.blockLocator, testMsg.stopHash, testMsg.skip)
183 gotBlockLocator := getHeadersMsg.GetBlockLocator()
184 gotStopHash := getHeadersMsg.GetStopHash()
185 gotSkip := getHeadersMsg.GetSkip()
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))
191 if !reflect.DeepEqual(testMsg.stopHash, gotStopHash) {
192 t.Errorf("get headers msg test err: amount:got %d\nwant %d", gotStopHash, testMsg.stopHash)
195 if !reflect.DeepEqual(testMsg.skip, gotSkip) {
196 t.Errorf("get headers msg test err: skip:got %d\nwant %d", gotSkip, testMsg.skip)
200 var testBlocks = []*types.Block{
202 BlockHeader: types.BlockHeader{
205 Timestamp: 1528945000000,
206 BlockCommitment: types.BlockCommitment{
207 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
208 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
213 BlockHeader: types.BlockHeader{
216 Timestamp: 1528945000000,
217 BlockCommitment: types.BlockCommitment{
218 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
219 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
225 func TestBlocksMessage(t *testing.T) {
226 blocksMsg, err := NewBlocksMessage(testBlocks)
228 t.Fatalf("create blocks msg err:%s", err)
230 gotBlocks, err := blocksMsg.GetBlocks()
232 t.Fatalf("get blocks err:%s", err)
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))
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()))
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()))