github.com/beyonderyue/gochain@v2.2.26+incompatible/core/tx_list_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "math/rand" 21 "testing" 22 23 "github.com/gochain-io/gochain/core/types" 24 "github.com/gochain-io/gochain/crypto" 25 ) 26 27 // Tests that transactions can be added to strict lists and list contents and 28 // nonce boundaries are correctly maintained. 29 func TestStrictTxListAdd(t *testing.T) { 30 // Generate a list of transactions to insert 31 key, _ := crypto.GenerateKey() 32 33 txs := make(types.Transactions, 1024) 34 for i := 0; i < len(txs); i++ { 35 txs[i] = transaction(uint64(i), 0, key) 36 } 37 // Insert the transactions in a random order 38 list := newTxList(true) 39 for _, v := range rand.Perm(len(txs)) { 40 list.Add(txs[v], DefaultTxPoolConfig.PriceBump) 41 } 42 // Verify internal state 43 if len(list.txs.items) != len(txs) { 44 t.Errorf("transaction count mismatch: have %d, want %d", len(list.txs.items), len(txs)) 45 } 46 for i, tx := range txs { 47 if list.txs.items[tx.Nonce()] != tx { 48 t.Errorf("item %d: transaction mismatch: have %v, want %v", i, list.txs.items[tx.Nonce()], tx) 49 } 50 } 51 } 52 53 func TestTxSortedMap_Cap(t *testing.T) { 54 txSortedMap := newTxSortedMap() 55 56 txs := make(types.Transactions, 1024) 57 key, _ := crypto.GenerateKey() 58 for i := 0; i < len(txs); i++ { 59 txs[i] = transaction(uint64(i), 0, key) 60 txSortedMap.Put(txs[i]) 61 } 62 63 var removed int 64 txSortedMap.Cap(500, func(*types.Transaction) { 65 removed++ 66 }) 67 if removed != 524 { 68 t.Fatalf("expected to remove 524 but got %d", removed) 69 } 70 71 removed = 0 72 txSortedMap.Cap(1, func(*types.Transaction) { 73 removed++ 74 }) 75 if removed != 499 { 76 t.Fatalf("expected to remove 499 but got %d", removed) 77 } 78 79 txSortedMap.cache = nil 80 81 removed = 0 82 txSortedMap.Cap(0, func(*types.Transaction) { 83 removed++ 84 }) 85 if removed != 1 { 86 t.Errorf("expected to remove 1 but got %d", removed) 87 } 88 89 if len(txSortedMap.items) > 0 || len(txSortedMap.cache) > 0 || len(*txSortedMap.index) > 0 { 90 t.Fatalf("expected empty txSortedMap but got %#v", txSortedMap) 91 } 92 } 93 94 func TestTxSortedMap_Ready(t *testing.T) { 95 txSortedMap := newTxSortedMap() 96 97 txs := make(types.Transactions, 10) 98 key, _ := crypto.GenerateKey() 99 for i := 1; i < 5; i++ { 100 txs[i] = transaction(uint64(i), 0, key) 101 txSortedMap.Put(txs[i]) 102 } 103 for i := 6; i < len(txs); i++ { 104 txs[i] = transaction(uint64(i), 0, key) 105 txSortedMap.Put(txs[i]) 106 } 107 108 var removed int 109 txSortedMap.Ready(0, func(*types.Transaction) { 110 removed++ 111 }) 112 if removed != 0 { 113 t.Fatalf("expected to remove none but got %d", removed) 114 } 115 116 txSortedMap.cache = nil 117 118 removed = 0 119 txSortedMap.Ready(1, func(*types.Transaction) { 120 removed++ 121 }) 122 if removed != 4 { 123 t.Fatalf("expected to remove 4 but got %d", removed) 124 } 125 126 removed = 0 127 txSortedMap.Ready(5, func(*types.Transaction) { 128 removed++ 129 }) 130 if removed != 0 { 131 t.Fatalf("expected to remove none but got %d", removed) 132 } 133 134 txSortedMap.cache = nil 135 136 removed = 0 137 txSortedMap.Ready(6, func(*types.Transaction) { 138 removed++ 139 }) 140 if removed != 4 { 141 t.Errorf("expected to remove 4 but got %d", removed) 142 } 143 144 if len(txSortedMap.items) > 0 || len(txSortedMap.cache) > 0 || len(*txSortedMap.index) > 0 { 145 t.Fatalf("expected empty txSortedMap but got %#v", txSortedMap) 146 } 147 } 148 149 func TestTxSortedMap_Forward(t *testing.T) { 150 txSortedMap := newTxSortedMap() 151 152 txs := make(types.Transactions, 10) 153 key, _ := crypto.GenerateKey() 154 for i := 1; i < 5; i++ { 155 txs[i] = transaction(uint64(i), 0, key) 156 txSortedMap.Put(txs[i]) 157 } 158 for i := 6; i < len(txs); i++ { 159 txs[i] = transaction(uint64(i), 0, key) 160 txSortedMap.Put(txs[i]) 161 } 162 163 var removed int 164 txSortedMap.Forward(1, func(*types.Transaction) { 165 removed++ 166 }) 167 if removed != 0 { 168 t.Fatalf("expected to remove none but got %d", removed) 169 } 170 171 removed = 0 172 txSortedMap.Forward(5, func(*types.Transaction) { 173 removed++ 174 }) 175 if removed != 4 { 176 t.Fatalf("expected to remove 4 but got %d", removed) 177 } 178 179 txSortedMap.cache = nil 180 181 removed = 0 182 txSortedMap.Forward(10, func(*types.Transaction) { 183 removed++ 184 }) 185 if removed != 4 { 186 t.Errorf("expected to remove 4 but got %d", removed) 187 } 188 189 if len(txSortedMap.items) > 0 || len(txSortedMap.cache) > 0 || len(*txSortedMap.index) > 0 { 190 t.Fatalf("expected empty txSortedMap but got %#v", txSortedMap) 191 } 192 } 193 194 func TestTxSortedMap_Filter(t *testing.T) { 195 txSortedMap := newTxSortedMap() 196 197 txs := make(types.Transactions, 10) 198 key, _ := crypto.GenerateKey() 199 for i := 1; i < 5; i++ { 200 txs[i] = transaction(uint64(i), 100, key) 201 txSortedMap.Put(txs[i]) 202 } 203 for i := 6; i < len(txs); i++ { 204 txs[i] = transaction(uint64(i), 200, key) 205 txSortedMap.Put(txs[i]) 206 } 207 208 var removed, invalid int 209 txSortedMap.Filter(func(tx *types.Transaction) bool { 210 return tx.Gas() == 100 211 }, false, func(*types.Transaction) { removed++ }, func(*types.Transaction) { invalid++ }) 212 213 if removed != 4 || invalid != 0 { 214 t.Fatalf("expected 4 removal and 0 invalid but got %d removals and %d invalid", removed, invalid) 215 } 216 217 removed, invalid = 0, 0 218 txSortedMap.Filter(func(tx *types.Transaction) bool { 219 return tx.Gas() == 200 220 }, true, func(*types.Transaction) { removed++ }, func(*types.Transaction) { invalid++ }) 221 if removed != 1 || invalid != 3 { 222 t.Fatalf("expected 1 removal and 3 invalid but got %d removals and %d invalid", removed, invalid) 223 } 224 225 if len(txSortedMap.items) > 0 || len(txSortedMap.cache) > 0 || len(*txSortedMap.index) > 0 { 226 t.Fatalf("expected empty txSortedMap but got %#v", txSortedMap) 227 } 228 } 229 230 func TestTxSortedMap_ForLast(t *testing.T) { 231 txSortedMap := newTxSortedMap() 232 233 txs := make(types.Transactions, 10) 234 key, _ := crypto.GenerateKey() 235 for i := 1; i < 5; i++ { 236 txs[i] = transaction(uint64(i), 100, key) 237 txSortedMap.Put(txs[i]) 238 } 239 for i := 6; i < len(txs); i++ { 240 txs[i] = transaction(uint64(i), 200, key) 241 txSortedMap.Put(txs[i]) 242 } 243 244 var cnt int 245 fn := func(*types.Transaction) { cnt++ } 246 txSortedMap.ForLast(2, fn) 247 if cnt != 2 { 248 t.Errorf("expected 2 but got %d", cnt) 249 } 250 251 txSortedMap.cache = nil 252 253 cnt = 0 254 txSortedMap.ForLast(4, fn) 255 if cnt != 4 { 256 t.Errorf("expected 6 but got %d", cnt) 257 } 258 259 txSortedMap.cache = nil 260 261 cnt = 0 262 txSortedMap.ForLast(2, fn) 263 if cnt != 2 { 264 t.Errorf("expected 8 but got %d", cnt) 265 } 266 267 if len(txSortedMap.items) > 0 || len(txSortedMap.cache) > 0 || len(*txSortedMap.index) > 0 { 268 t.Fatalf("expected empty txSortedMap but got %#v", txSortedMap) 269 } 270 }