OSDN Git Service

2136dd8293f801afcb34609940b58907073ff09f
[bytom/bytom.git] / protocol / txpool_test.go
1 package protocol
2
3 import (
4         "testing"
5
6         "github.com/bytom/consensus"
7         "github.com/bytom/protocol/bc/types"
8 )
9
10 func TestTxPool(t *testing.T) {
11         p := NewTxPool()
12
13         txA := mockCoinbaseTx(1000, 6543)
14         txB := mockCoinbaseTx(2000, 2324)
15         txC := mockCoinbaseTx(3000, 9322)
16
17         p.AddTransaction(txA, false, 1000, 5000000000)
18         if !p.IsTransactionInPool(&txA.ID) {
19                 t.Errorf("fail to find added txA in tx pool")
20         } else {
21                 i, _ := p.GetTransaction(&txA.ID)
22                 if i.Height != 1000 || i.Fee != 5000000000 || i.FeePerKB != 5000000000 {
23                         t.Errorf("incorrect data of TxDesc structure")
24                 }
25         }
26
27         if p.IsTransactionInPool(&txB.ID) {
28                 t.Errorf("shouldn't find txB in tx pool")
29         }
30         p.AddTransaction(txB, false, 1, 5000000000)
31         if !p.IsTransactionInPool(&txB.ID) {
32                 t.Errorf("shouldn find txB in tx pool")
33         }
34
35         if p.Count() != 2 {
36                 t.Errorf("get wrong number of tx in the pool")
37         }
38         p.RemoveTransaction(&txB.ID)
39         if p.IsTransactionInPool(&txB.ID) {
40                 t.Errorf("shouldn't find txB in tx pool")
41         }
42
43         p.AddErrCache(&txC.ID, nil)
44         if !p.IsTransactionInErrCache(&txC.ID) {
45                 t.Errorf("shouldn find txC in tx err cache")
46         }
47         if !p.HaveTransaction(&txC.ID) {
48                 t.Errorf("shouldn find txC in tx err cache")
49         }
50 }
51
52 func mockCoinbaseTx(serializedSize uint64, amount uint64) *types.Tx {
53         oldTx := &types.TxData{
54                 SerializedSize: serializedSize,
55                 Outputs: []*types.TxOutput{
56                         types.NewTxOutput(*consensus.BTMAssetID, amount, []byte{1}),
57                 },
58         }
59
60         return &types.Tx{
61                 TxData: *oldTx,
62                 Tx:     types.MapTx(oldTx),
63         }
64 }