github.com/okex/exchain@v1.8.0/libs/tendermint/mempool/exchain_pending_pool_test.go (about) 1 package mempool 2 3 import ( 4 "fmt" 5 "math/big" 6 "math/rand" 7 "strconv" 8 "testing" 9 "time" 10 11 abci "github.com/okex/exchain/libs/tendermint/abci/types" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestAddtx(t *testing.T) { 16 pool := newPendingPool(100, 3, 10, 10) 17 testCases := []struct { 18 Tx *mempoolTx 19 }{ 20 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("1"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(3780)}}}, 21 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("2"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(5853)}}}, 22 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("3"), from: "3", realTx: abci.MockTx{GasPrice: big.NewInt(8315)}}}, 23 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("4"), from: "4", realTx: abci.MockTx{GasPrice: big.NewInt(9526)}}}, 24 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("5"), from: "5", realTx: abci.MockTx{GasPrice: big.NewInt(9140)}}}, 25 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("6"), from: "6", realTx: abci.MockTx{GasPrice: big.NewInt(9227)}}}, 26 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("13"), from: "12", realTx: abci.MockTx{GasPrice: big.NewInt(2791), Nonce: 1}}}, 27 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("14"), from: "12", realTx: abci.MockTx{GasPrice: big.NewInt(2698), Nonce: 100}}}, 28 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("15"), from: "12", realTx: abci.MockTx{GasPrice: big.NewInt(2698), Nonce: 18446744073709551615}}}, 29 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("16"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(6925), Nonce: 100}}}, 30 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("17"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(2965), Nonce: 99}}}, 31 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("18"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(2791), Nonce: 98}}}, 32 } 33 34 for _, exInfo := range testCases { 35 pool.addTx(exInfo.Tx) 36 } 37 assert.Equal(t, len(testCases), pool.Size(), fmt.Sprintf("Expected to txs length %v but got %v", len(testCases), pool.Size())) 38 for _, exInfo := range testCases { 39 tx := pool.addressTxsMap[exInfo.Tx.from][exInfo.Tx.realTx.GetNonce()] 40 assert.Equal(t, tx, exInfo.Tx) 41 } 42 } 43 44 func TestAddtxRandom(t *testing.T) { 45 pool := newPendingPool(100000, 3, 10, 20000) 46 txCount := 10000 47 rand.Seed(time.Now().Unix()) 48 addrMap := map[int]string{ 49 0: "1234567", 50 1: "0x333", 51 2: "11111", 52 3: "test", 53 } 54 55 for i := 0; i < txCount; i++ { 56 nonce := rand.Intn(txCount) 57 addrIndex := nonce % len(addrMap) 58 tx := &mempoolTx{height: 1, gasWanted: 1, tx: []byte(strconv.Itoa(i)), from: addrMap[addrIndex], realTx: abci.MockTx{Nonce: uint64(nonce)}} 59 pool.addTx(tx) 60 txRes := pool.addressTxsMap[tx.from][tx.realTx.GetNonce()] 61 assert.Equal(t, tx, txRes) 62 } 63 assert.Equal(t, txCount, pool.Size(), fmt.Sprintf("Expected to txs length %v but got %v", txCount, pool.Size())) 64 } 65 66 func TestRemovetx(t *testing.T) { 67 pool := newPendingPool(100, 3, 10, 10) 68 txs := []struct { 69 Tx *mempoolTx 70 }{ 71 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("13"), from: "18", realTx: abci.MockTx{GasPrice: big.NewInt(2791), Nonce: 1}}}, 72 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("14"), from: "18", realTx: abci.MockTx{GasPrice: big.NewInt(2698), Nonce: 100}}}, 73 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("15"), from: "18", realTx: abci.MockTx{GasPrice: big.NewInt(2698), Nonce: 18446744073709551615}}}, 74 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("16"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(6925), Nonce: 100}}}, 75 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("17"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(2965), Nonce: 99}}}, 76 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("18"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(2791), Nonce: 98}}}, 77 } 78 testCases := []struct { 79 address string 80 nonce uint64 81 }{ 82 {"18", 100}, 83 {"nonexist", 0}, 84 {"", 0}, 85 {"18", 1000}, 86 {"14", 98}, 87 } 88 for _, exInfo := range txs { 89 pool.addTx(exInfo.Tx) 90 tx := pool.getTx(exInfo.Tx.from, exInfo.Tx.realTx.GetNonce()) 91 assert.Equal(t, tx, exInfo.Tx) 92 } 93 94 for _, tc := range testCases { 95 pool.removeTx(tc.address, tc.nonce) 96 res := pool.getTx(tc.address, tc.nonce) 97 assert.Nil(t, res) 98 } 99 } 100 func TestRemoveTxByHash(t *testing.T) { 101 pool := newPendingPool(100, 3, 10, 10) 102 txs := []struct { 103 Tx *mempoolTx 104 }{ 105 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("13"), from: "18", realTx: abci.MockTx{GasPrice: big.NewInt(2791), Nonce: 1}}}, 106 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("14"), from: "18", realTx: abci.MockTx{GasPrice: big.NewInt(2698), Nonce: 100}}}, 107 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("15"), from: "18", realTx: abci.MockTx{GasPrice: big.NewInt(2698), Nonce: 18446744073709551615}}}, 108 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("16"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(6925), Nonce: 100}}}, 109 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("17"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(2965), Nonce: 99}}}, 110 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("18"), from: "14", realTx: abci.MockTx{GasPrice: big.NewInt(2791), Nonce: 98}}}, 111 } 112 113 for _, exInfo := range txs { 114 pool.addTx(exInfo.Tx) 115 tx := pool.getTx(exInfo.Tx.from, exInfo.Tx.realTx.GetNonce()) 116 assert.Equal(t, tx, exInfo.Tx) 117 } 118 var height int64 = 0 119 for _, tc := range txs { 120 pool.removeTxByHash(txID(tc.Tx.tx, height)) 121 res := pool.getTx(tc.Tx.from, tc.Tx.realTx.GetNonce()) 122 assert.Nil(t, res) 123 } 124 } 125 126 func TestHandlePendingTx(t *testing.T) { 127 pool := newPendingPool(100, 3, 10, 10) 128 txs := []struct { 129 Tx *mempoolTx 130 }{ 131 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("2"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(3780), Nonce: 1}}}, 132 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("3"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(5315), Nonce: 2}}}, 133 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("4"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(4526), Nonce: 3}}}, 134 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("5"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(2140), Nonce: 4}}}, 135 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("6"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(4227), Nonce: 5}}}, 136 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("7"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(5315), Nonce: 2}}}, 137 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("8"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(4526), Nonce: 3}}}, 138 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("9"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(2140), Nonce: 5}}}, 139 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("10"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(4227), Nonce: 6}}}, 140 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("11"), from: "3", realTx: abci.MockTx{GasPrice: big.NewInt(4526), Nonce: 3}}}, 141 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("12"), from: "3", realTx: abci.MockTx{GasPrice: big.NewInt(2140), Nonce: 4}}}, 142 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("13"), from: "3", realTx: abci.MockTx{GasPrice: big.NewInt(4227), Nonce: 5}}}, 143 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("14"), from: "4", realTx: abci.MockTx{GasPrice: big.NewInt(3780), Nonce: 1}}}, 144 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("15"), from: "4", realTx: abci.MockTx{GasPrice: big.NewInt(5315), Nonce: 2}}}, 145 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("16"), from: "4", realTx: abci.MockTx{GasPrice: big.NewInt(4526), Nonce: 3}}}, 146 } 147 addressNonceTestCase := map[string]uint64{ 148 "1": 0, 149 "2": 1, 150 "3": 1, 151 "4": 2, 152 "non-exist": 0, 153 } 154 testCases := []struct { 155 address string 156 nonceExpected uint64 157 }{ 158 {"1", 1}, 159 {"2", 2}, 160 {"4", 3}, 161 } 162 for _, exInfo := range txs { 163 pool.addTx(exInfo.Tx) 164 } 165 assert.Equal(t, len(txs), pool.Size(), fmt.Sprintf("Expected to txs length %v but got %v", len(txs), 166 pool.Size())) 167 168 res := pool.handlePendingTx(addressNonceTestCase) 169 for _, tc := range testCases { 170 assert.Equal(t, tc.nonceExpected, res[tc.address], fmt.Sprintf("Expected tx nonce %v for address %s, but got %d", tc.nonceExpected, tc.address, 171 res[tc.address])) 172 } 173 } 174 175 func TestHandlePeriodCounter(t *testing.T) { 176 177 pool := newPendingPool(100, 3, 10, 10) 178 txs := []struct { 179 Tx *mempoolTx 180 }{ 181 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("1"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(3780), Nonce: 1}}}, 182 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("2"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(5315), Nonce: 2}}}, 183 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("3"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(4526), Nonce: 3}}}, 184 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("4"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(2140), Nonce: 4}}}, 185 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("5"), from: "1", realTx: abci.MockTx{GasPrice: big.NewInt(4227), Nonce: 5}}}, 186 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("6"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(5315), Nonce: 2}}}, 187 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("7"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(4526), Nonce: 3}}}, 188 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("8"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(2140), Nonce: 5}}}, 189 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("9"), from: "2", realTx: abci.MockTx{GasPrice: big.NewInt(4227), Nonce: 6}}}, 190 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("10"), from: "3", realTx: abci.MockTx{GasPrice: big.NewInt(4526), Nonce: 3}}}, 191 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("11"), from: "3", realTx: abci.MockTx{GasPrice: big.NewInt(2140), Nonce: 4}}}, 192 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("12"), from: "3", realTx: abci.MockTx{GasPrice: big.NewInt(4227), Nonce: 5}}}, 193 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("13"), from: "4", realTx: abci.MockTx{GasPrice: big.NewInt(3780), Nonce: 1}}}, 194 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("14"), from: "4", realTx: abci.MockTx{GasPrice: big.NewInt(5315), Nonce: 2}}}, 195 {&mempoolTx{height: 1, gasWanted: 1, tx: []byte("15"), from: "4", realTx: abci.MockTx{GasPrice: big.NewInt(4526), Nonce: 3}}}, 196 } 197 for _, exInfo := range txs { 198 pool.addTx(exInfo.Tx) 199 } 200 for i := 0; i < pool.reserveBlocks; i++ { 201 pool.handlePeriodCounter() 202 } 203 assert.Equal(t, len(txs), pool.Size()) 204 pool.handlePeriodCounter() 205 assert.Equal(t, 0, pool.Size()) 206 }