OSDN Git Service

Merge pull request #935 from Bytom/dev
[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         "github.com/bytom/protocol/vm/vmutil"
9 )
10
11 func TestTxPool(t *testing.T) {
12         p := NewTxPool()
13
14         txA := mockCoinbaseTx(1000, 6543)
15         txB := mockCoinbaseTx(2000, 2324)
16         txC := mockCoinbaseTx(3000, 9322)
17
18         p.AddTransaction(txA, false, 1000, 5000000000)
19         if !p.IsTransactionInPool(&txA.ID) {
20                 t.Errorf("fail to find added txA in tx pool")
21         } else {
22                 i, _ := p.GetTransaction(&txA.ID)
23                 if i.Height != 1000 || i.Fee != 5000000000 || i.FeePerKB != 5000000000 {
24                         t.Errorf("incorrect data of TxDesc structure")
25                 }
26         }
27
28         if p.IsTransactionInPool(&txB.ID) {
29                 t.Errorf("shouldn't find txB in tx pool")
30         }
31         p.AddTransaction(txB, false, 1, 5000000000)
32         if !p.IsTransactionInPool(&txB.ID) {
33                 t.Errorf("shouldn find txB in tx pool")
34         }
35
36         if p.Count() != 2 {
37                 t.Errorf("get wrong number of tx in the pool")
38         }
39         p.RemoveTransaction(&txB.ID)
40         if p.IsTransactionInPool(&txB.ID) {
41                 t.Errorf("shouldn't find txB in tx pool")
42         }
43
44         p.AddErrCache(&txC.ID, nil)
45         if !p.IsTransactionInErrCache(&txC.ID) {
46                 t.Errorf("shouldn find txC in tx err cache")
47         }
48         if !p.HaveTransaction(&txC.ID) {
49                 t.Errorf("shouldn find txC in tx err cache")
50         }
51 }
52
53 func mockCoinbaseTx(serializedSize uint64, amount uint64) *types.Tx {
54         cp, _ := vmutil.DefaultCoinbaseProgram()
55         oldTx := &types.TxData{
56                 SerializedSize: serializedSize,
57                 Inputs: []*types.TxInput{
58                         types.NewCoinbaseInput(nil),
59                 },
60                 Outputs: []*types.TxOutput{
61                         types.NewTxOutput(*consensus.BTMAssetID, amount, cp),
62                 },
63         }
64         return &types.Tx{
65                 TxData: *oldTx,
66                 Tx:     types.MapTx(oldTx),
67         }
68 }