7 "github.com/davecgh/go-spew/spew"
9 "github.com/bytom/bytom/consensus"
10 "github.com/bytom/bytom/protocol/bc"
11 "github.com/bytom/bytom/protocol/bc/types"
12 "github.com/bytom/bytom/testcontrol"
15 var txs = []*types.Tx{
16 types.NewTx(types.TxData{
17 SerializedSize: uint64(52),
18 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
19 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 5000, nil)},
21 types.NewTx(types.TxData{
22 SerializedSize: uint64(53),
23 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02})},
24 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 5000, nil)},
26 types.NewTx(types.TxData{
27 SerializedSize: uint64(54),
28 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})},
29 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 5000, nil)},
31 types.NewTx(types.TxData{
32 SerializedSize: uint64(54),
33 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})},
34 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 2000, nil)},
36 types.NewTx(types.TxData{
37 SerializedSize: uint64(54),
38 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})},
39 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 10000, nil)},
43 func TestTransactionMessage(t *testing.T) {
44 if testcontrol.IgnoreTestTemporary {
48 for _, tx := range txs {
49 txMsg, err := NewTransactionMessage(tx)
51 t.Fatalf("create tx msg err:%s", err)
54 gotTx, err := txMsg.GetTransaction()
56 t.Fatalf("get txs from txsMsg err:%s", err)
58 if !reflect.DeepEqual(*tx.Tx, *gotTx.Tx) {
59 t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(tx.Tx), spew.Sdump(gotTx.Tx))
64 func TestTransactionsMessage(t *testing.T) {
65 if testcontrol.IgnoreTestTemporary {
69 txsMsg, err := NewTransactionsMessage(txs)
71 t.Fatalf("create txs msg err:%s", err)
74 gotTxs, err := txsMsg.GetTransactions()
76 t.Fatalf("get txs from txsMsg err:%s", err)
79 if len(gotTxs) != len(txs) {
80 t.Fatal("txs msg test err: number of txs not match ")
83 for i, tx := range txs {
84 if !reflect.DeepEqual(tx.Tx, gotTxs[i].Tx) {
85 t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(tx.Tx), spew.Sdump(gotTxs[i].Tx))
90 var testBlock = &types.Block{
91 BlockHeader: types.BlockHeader{
94 Timestamp: 1528945000000,
95 BlockCommitment: types.BlockCommitment{
96 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
98 SupLinks: types.SupLinks{},
102 func TestBlockMessage(t *testing.T) {
103 blockMsg, err := NewBlockMessage(testBlock)
105 t.Fatalf("create new block msg err:%s", err)
108 gotBlock, err := blockMsg.GetBlock()
110 t.Fatalf("got block err:%s", err)
113 if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
114 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
117 blockMsg.RawBlock[1] = blockMsg.RawBlock[1] + 0x1
118 _, err = blockMsg.GetBlock()
120 t.Fatalf("get mine block err")
124 var testHeaders = []*types.BlockHeader{
128 Timestamp: 1528945000000,
129 BlockCommitment: types.BlockCommitment{
130 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
132 SupLinks: types.SupLinks{},
137 Timestamp: 1528945000000,
138 BlockCommitment: types.BlockCommitment{
139 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
141 SupLinks: types.SupLinks{},
146 Timestamp: 1528945000000,
147 BlockCommitment: types.BlockCommitment{
148 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
150 SupLinks: types.SupLinks{},
154 func TestHeadersMessage(t *testing.T) {
155 headersMsg, err := NewHeadersMessage(testHeaders)
157 t.Fatalf("create headers msg err:%s", err)
160 gotHeaders, err := headersMsg.GetHeaders()
162 t.Fatalf("got headers err:%s", err)
165 if !reflect.DeepEqual(gotHeaders, testHeaders) {
166 t.Errorf("headers msg test err: got %s\nwant %s", spew.Sdump(gotHeaders), spew.Sdump(testHeaders))
170 func TestGetBlockMessage(t *testing.T) {
171 getBlockMsg := GetBlockMessage{RawHash: [32]byte{0x01}}
172 gotHash := getBlockMsg.GetHash()
174 if !reflect.DeepEqual(gotHash.Byte32(), getBlockMsg.RawHash) {
175 t.Errorf("get block msg test err: got %s\nwant %s", spew.Sdump(gotHash.Byte32()), spew.Sdump(getBlockMsg.RawHash))
179 type testGetHeadersMessage struct {
180 blockLocator []*bc.Hash
185 func TestGetHeadersMessage(t *testing.T) {
186 testMsg := testGetHeadersMessage{
187 blockLocator: []*bc.Hash{{V0: 0x01}, {V0: 0x02}, {V0: 0x03}},
188 stopHash: &bc.Hash{V0: 0x01},
191 getHeadersMsg := NewGetHeadersMessage(testMsg.blockLocator, testMsg.stopHash, testMsg.skip)
192 gotBlockLocator := getHeadersMsg.GetBlockLocator()
193 gotStopHash := getHeadersMsg.GetStopHash()
194 gotSkip := getHeadersMsg.GetSkip()
196 if !reflect.DeepEqual(testMsg.blockLocator, gotBlockLocator) {
197 t.Errorf("get headers msg test err: got %s\nwant %s", spew.Sdump(gotBlockLocator), spew.Sdump(testMsg.blockLocator))
200 if !reflect.DeepEqual(testMsg.stopHash, gotStopHash) {
201 t.Errorf("get headers msg test err: amount:got %d\nwant %d", gotStopHash, testMsg.stopHash)
204 if !reflect.DeepEqual(testMsg.skip, gotSkip) {
205 t.Errorf("get headers msg test err: skip:got %d\nwant %d", gotSkip, testMsg.skip)
209 var testBlocks = []*types.Block{
211 BlockHeader: types.BlockHeader{
214 Timestamp: 1528945000000,
215 BlockCommitment: types.BlockCommitment{
216 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
221 BlockHeader: types.BlockHeader{
224 Timestamp: 1528945000000,
225 BlockCommitment: types.BlockCommitment{
226 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
232 func TestBlocksMessage(t *testing.T) {
233 blocksMsg, err := NewBlocksMessage(testBlocks)
235 t.Fatalf("create blocks msg err:%s", err)
237 gotBlocks, err := blocksMsg.GetBlocks()
239 t.Fatalf("get blocks err:%s", err)
242 for _, gotBlock := range gotBlocks {
243 if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
244 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
249 func TestStatusMessage(t *testing.T) {
250 statusResponseMsg := NewStatusMessage(&testBlock.BlockHeader, &testBlock.BlockHeader)
251 gotBestHash := statusResponseMsg.GetBestHash()
252 if !reflect.DeepEqual(*gotBestHash, testBlock.Hash()) {
253 t.Errorf("status response msg test err: got %s\nwant %s", spew.Sdump(*gotBestHash), spew.Sdump(testBlock.Hash()))
255 gotIrreversibleHash := statusResponseMsg.GetIrreversibleHash()
256 if !reflect.DeepEqual(*gotIrreversibleHash, testBlock.Hash()) {
257 t.Errorf("status response msg test err: got %s\nwant %s", spew.Sdump(*gotIrreversibleHash), spew.Sdump(testBlock.Hash()))