github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/core/tx_pool_test.go (about) 1 // Copyright 2015 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 "crypto/ecdsa" 21 "errors" 22 "fmt" 23 "io/ioutil" 24 "math/big" 25 "math/rand" 26 "os" 27 "testing" 28 "time" 29 30 "github.com/CommerciumBlockchain/go-commercium/common" 31 "github.com/CommerciumBlockchain/go-commercium/core/rawdb" 32 "github.com/CommerciumBlockchain/go-commercium/core/state" 33 "github.com/CommerciumBlockchain/go-commercium/core/types" 34 "github.com/CommerciumBlockchain/go-commercium/crypto" 35 "github.com/CommerciumBlockchain/go-commercium/event" 36 "github.com/CommerciumBlockchain/go-commercium/params" 37 "github.com/CommerciumBlockchain/go-commercium/trie" 38 ) 39 40 // testTxPoolConfig is a transaction pool configuration without stateful disk 41 // sideeffects used during testing. 42 var testTxPoolConfig TxPoolConfig 43 44 func init() { 45 testTxPoolConfig = DefaultTxPoolConfig 46 testTxPoolConfig.Journal = "" 47 } 48 49 type testBlockChain struct { 50 statedb *state.StateDB 51 chainHeadFeed *event.Feed 52 } 53 54 func (bc *testBlockChain) CurrentBlock() *types.Block { 55 return types.NewBlock(&types.Header{ 56 }, nil, nil, nil, new(trie.Trie)) 57 } 58 59 func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { 60 return bc.CurrentBlock() 61 } 62 63 func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) { 64 return bc.statedb, nil 65 } 66 67 func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription { 68 return bc.chainHeadFeed.Subscribe(ch) 69 } 70 71 func transaction(nonce uint64, key *ecdsa.PrivateKey) *types.Transaction { 72 return pricedTransaction(nonce, big.NewInt(1), key) 73 } 74 75 func pricedTransaction(nonce uint64, key *ecdsa.PrivateKey) *types.Transaction { 76 tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(100), nil), types.HomesteadSigner{}, key) 77 return tx 78 } 79 80 func pricedDataTransaction(nonce uint64, key *ecdsa.PrivateKey, bytes uint64) *types.Transaction { 81 data := make([]byte, bytes) 82 rand.Read(data) 83 84 tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(0), data), types.HomesteadSigner{}, key) 85 return tx 86 } 87 88 func setupTxPool() (*TxPool, *ecdsa.PrivateKey) { 89 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 90 blockchain := &testBlockChain{statedb, 10000000, new(event.Feed)} 91 92 key, _ := crypto.GenerateKey() 93 pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) 94 95 return pool, key 96 } 97 98 // validateTxPoolInternals checks various consistency invariants within the pool. 99 func validateTxPoolInternals(pool *TxPool) error { 100 pool.mu.RLock() 101 defer pool.mu.RUnlock() 102 103 // Ensure the total transaction set is consistent with pending + queued 104 pending, queued := pool.stats() 105 if total := pool.all.Count(); total != pending+queued { 106 return fmt.Errorf("total transaction count %d != %d pending + %d queued", total, pending, queued) 107 } 108 pool.priced.Reheap() 109 priced, remote := pool.priced.remotes.Len(), pool.all.RemoteCount() 110 if priced != remote { 111 return fmt.Errorf("total priced transaction count %d != %d", priced, remote) 112 } 113 // Ensure the next nonce to assign is the correct one 114 for addr, txs := range pool.pending { 115 // Find the last transaction 116 var last uint64 117 for nonce := range txs.txs.items { 118 if last < nonce { 119 last = nonce 120 } 121 } 122 if nonce := pool.pendingNonces.get(addr); nonce != last+1 { 123 return fmt.Errorf("pending nonce mismatch: have %v, want %v", nonce, last+1) 124 } 125 } 126 return nil 127 } 128 129 // validateEvents checks that the correct number of transaction addition events 130 // were fired on the pool's event feed. 131 func validateEvents(events chan NewTxsEvent, count int) error { 132 var received []*types.Transaction 133 134 for len(received) < count { 135 select { 136 case ev := <-events: 137 received = append(received, ev.Txs...) 138 case <-time.After(time.Second): 139 return fmt.Errorf("event #%d not fired", len(received)) 140 } 141 } 142 if len(received) > count { 143 return fmt.Errorf("more than %d events fired: %v", count, received[count:]) 144 } 145 select { 146 case ev := <-events: 147 return fmt.Errorf("more than %d events fired: %v", count, ev.Txs) 148 149 case <-time.After(50 * time.Millisecond): 150 // This branch should be "default", but it's a data race between goroutines, 151 // reading the event channel and pushing into it, so better wait a bit ensuring 152 // really nothing gets injected. 153 } 154 return nil 155 } 156 157 func deriveSender(tx *types.Transaction) (common.Address, error) { 158 return types.Sender(types.HomesteadSigner{}, tx) 159 } 160 161 type testChain struct { 162 *testBlockChain 163 address common.Address 164 trigger *bool 165 } 166 167 // testChain.State() is used multiple times to reset the pending state. 168 // when simulate is true it will create a state that indicates 169 // that tx0 and tx1 are included in the chain. 170 func (c *testChain) State() (*state.StateDB, error) { 171 // delay "state change" by one. The tx pool fetches the 172 // state multiple times and by delaying it a bit we simulate 173 // a state change between those fetches. 174 stdb := c.statedb 175 if *c.trigger { 176 c.statedb, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 177 // simulate that the new head block included tx0 and tx1 178 c.statedb.SetNonce(c.address, 2) 179 c.statedb.SetBalance(c.address, new(big.Int).SetUint64(params.Ether)) 180 *c.trigger = false 181 } 182 return stdb, nil 183 } 184 185 // This test simulates a scenario where a new block is imported during a 186 // state reset and tests whether the pending state is in sync with the 187 // block head event that initiated the resetState(). 188 func TestStateChangeDuringTransactionPoolReset(t *testing.T) { 189 t.Parallel() 190 191 var ( 192 key, _ = crypto.GenerateKey() 193 address = crypto.PubkeyToAddress(key.PublicKey) 194 statedb, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 195 trigger = false 196 ) 197 198 // setup pool with 2 transaction in it 199 statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether)) 200 blockchain := &testChain{&testBlockChain{statedb, 1000000000, new(event.Feed)}, address, &trigger} 201 202 tx0 := transaction(0, 100000, key) 203 tx1 := transaction(1, 100000, key) 204 205 pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) 206 defer pool.Stop() 207 208 nonce := pool.Nonce(address) 209 if nonce != 0 { 210 t.Fatalf("Invalid nonce, want 0, got %d", nonce) 211 } 212 213 pool.AddRemotesSync([]*types.Transaction{tx0, tx1}) 214 215 nonce = pool.Nonce(address) 216 if nonce != 2 { 217 t.Fatalf("Invalid nonce, want 2, got %d", nonce) 218 } 219 220 // trigger state change in the background 221 trigger = true 222 <-pool.requestReset(nil, nil) 223 224 _, err := pool.Pending() 225 if err != nil { 226 t.Fatalf("Could not fetch pending transactions: %v", err) 227 } 228 nonce = pool.Nonce(address) 229 if nonce != 2 { 230 t.Fatalf("Invalid nonce, want 2, got %d", nonce) 231 } 232 } 233 234 func TestInvalidTransactions(t *testing.T) { 235 t.Parallel() 236 237 pool, key := setupTxPool() 238 defer pool.Stop() 239 240 tx := transaction(0, 100, key) 241 from, _ := deriveSender(tx) 242 243 pool.currentState.AddBalance(from, big.NewInt(1)) 244 if err := pool.AddRemote(tx); !errors.Is(err, ErrInsufficientFunds) { 245 t.Error("expected", ErrInsufficientFunds) 246 } 247 248 balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(new(big.Int))) 249 pool.currentState.AddBalance(from, balance) 250 if err := pool.AddRemote(tx); !errors.Is(err) { 251 t.Error("expected", err) 252 } 253 254 pool.currentState.SetNonce(from, 1) 255 pool.currentState.AddBalance(from, big.NewInt(0xffffffffffffff)) 256 tx = transaction(0, 100000, key) 257 if err := pool.AddRemote(tx); !errors.Is(err, ErrNonceTooLow) { 258 t.Error("expected", ErrNonceTooLow) 259 } 260 261 tx = transaction(1, 100000, key) 262 pool.gasPrice = big.NewInt(1000) 263 if err := pool.AddRemote(tx); err != ErrUnderpriced { 264 t.Error("expected", ErrUnderpriced, "got", err) 265 } 266 if err := pool.AddLocal(tx); err != nil { 267 t.Error("expected", nil, "got", err) 268 } 269 } 270 271 func TestTransactionQueue(t *testing.T) { 272 t.Parallel() 273 274 pool, key := setupTxPool() 275 defer pool.Stop() 276 277 tx := transaction(0, 100, key) 278 from, _ := deriveSender(tx) 279 pool.currentState.AddBalance(from, big.NewInt(1000)) 280 <-pool.requestReset(nil, nil) 281 282 pool.enqueueTx(tx.Hash(), tx, false, true) 283 <-pool.requestPromoteExecutables(newAccountSet(pool.signer, from)) 284 if len(pool.pending) != 1 { 285 t.Error("expected valid txs to be 1 is", len(pool.pending)) 286 } 287 288 tx = transaction(1, 100, key) 289 from, _ = deriveSender(tx) 290 pool.currentState.SetNonce(from, 2) 291 pool.enqueueTx(tx.Hash(), tx, false, true) 292 293 <-pool.requestPromoteExecutables(newAccountSet(pool.signer, from)) 294 if _, ok := pool.pending[from].txs.items[tx.Nonce()]; ok { 295 t.Error("expected transaction to be in tx pool") 296 } 297 if len(pool.queue) > 0 { 298 t.Error("expected transaction queue to be empty. is", len(pool.queue)) 299 } 300 } 301 302 func TestTransactionQueue2(t *testing.T) { 303 t.Parallel() 304 305 pool, key := setupTxPool() 306 defer pool.Stop() 307 308 tx1 := transaction(0, 100, key) 309 tx2 := transaction(10, 100, key) 310 tx3 := transaction(11, 100, key) 311 from, _ := deriveSender(tx1) 312 pool.currentState.AddBalance(from, big.NewInt(1000)) 313 pool.reset(nil, nil) 314 315 pool.enqueueTx(tx1.Hash(), tx1, false, true) 316 pool.enqueueTx(tx2.Hash(), tx2, false, true) 317 pool.enqueueTx(tx3.Hash(), tx3, false, true) 318 319 pool.promoteExecutables([]common.Address{from}) 320 if len(pool.pending) != 1 { 321 t.Error("expected pending length to be 1, got", len(pool.pending)) 322 } 323 if pool.queue[from].Len() != 2 { 324 t.Error("expected len(queue) == 2, got", pool.queue[from].Len()) 325 } 326 } 327 328 func TestTransactionNegativeValue(t *testing.T) { 329 t.Parallel() 330 331 pool, key := setupTxPool() 332 defer pool.Stop() 333 334 tx, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(-1), 100, big.NewInt(1), nil), types.HomesteadSigner{}, key) 335 from, _ := deriveSender(tx) 336 pool.currentState.AddBalance(from, big.NewInt(1)) 337 if err := pool.AddRemote(tx); err != ErrNegativeValue { 338 t.Error("expected", ErrNegativeValue, "got", err) 339 } 340 } 341 342 func TestTransactionChainFork(t *testing.T) { 343 t.Parallel() 344 345 pool, key := setupTxPool() 346 defer pool.Stop() 347 348 addr := crypto.PubkeyToAddress(key.PublicKey) 349 resetState := func() { 350 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 351 statedb.AddBalance(addr, big.NewInt(100000000000000)) 352 353 pool.chain = &testBlockChain{statedb, 1000000, new(event.Feed)} 354 <-pool.requestReset(nil, nil) 355 } 356 resetState() 357 358 tx := transaction(0, 100000, key) 359 if _, err := pool.add(tx, false); err != nil { 360 t.Error("didn't expect error", err) 361 } 362 pool.removeTx(tx.Hash(), true) 363 364 // reset the pool's internal state 365 resetState() 366 if _, err := pool.add(tx, false); err != nil { 367 t.Error("didn't expect error", err) 368 } 369 } 370 371 func TestTransactionDoubleNonce(t *testing.T) { 372 t.Parallel() 373 374 pool, key := setupTxPool() 375 defer pool.Stop() 376 377 addr := crypto.PubkeyToAddress(key.PublicKey) 378 resetState := func() { 379 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 380 statedb.AddBalance(addr, big.NewInt(100000000000000)) 381 382 pool.chain = &testBlockChain{statedb, 1000000, new(event.Feed)} 383 <-pool.requestReset(nil, nil) 384 } 385 resetState() 386 387 signer := types.HomesteadSigner{} 388 tx1, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 100000, big.NewInt(1), nil), signer, key) 389 tx2, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 1000000, big.NewInt(2), nil), signer, key) 390 tx3, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 1000000, big.NewInt(1), nil), signer, key) 391 392 // Add the first two transaction, ensure higher priced stays only 393 if replace, err := pool.add(tx1, false); err != nil || replace { 394 t.Errorf("first transaction insert failed (%v) or reported replacement (%v)", err, replace) 395 } 396 if replace, err := pool.add(tx2, false); err != nil || !replace { 397 t.Errorf("second transaction insert failed (%v) or not reported replacement (%v)", err, replace) 398 } 399 <-pool.requestPromoteExecutables(newAccountSet(signer, addr)) 400 if pool.pending[addr].Len() != 1 { 401 t.Error("expected 1 pending transactions, got", pool.pending[addr].Len()) 402 } 403 if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() { 404 t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash()) 405 } 406 407 // Add the third transaction and ensure it's not saved (smaller price) 408 pool.add(tx3, false) 409 <-pool.requestPromoteExecutables(newAccountSet(signer, addr)) 410 if pool.pending[addr].Len() != 1 { 411 t.Error("expected 1 pending transactions, got", pool.pending[addr].Len()) 412 } 413 if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() { 414 t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash()) 415 } 416 // Ensure the total transaction count is correct 417 if pool.all.Count() != 1 { 418 t.Error("expected 1 total transactions, got", pool.all.Count()) 419 } 420 } 421 422 func TestTransactionMissingNonce(t *testing.T) { 423 t.Parallel() 424 425 pool, key := setupTxPool() 426 defer pool.Stop() 427 428 addr := crypto.PubkeyToAddress(key.PublicKey) 429 pool.currentState.AddBalance(addr, big.NewInt(100000000000000)) 430 tx := transaction(1, 100000, key) 431 if _, err := pool.add(tx, false); err != nil { 432 t.Error("didn't expect error", err) 433 } 434 if len(pool.pending) != 0 { 435 t.Error("expected 0 pending transactions, got", len(pool.pending)) 436 } 437 if pool.queue[addr].Len() != 1 { 438 t.Error("expected 1 queued transaction, got", pool.queue[addr].Len()) 439 } 440 if pool.all.Count() != 1 { 441 t.Error("expected 1 total transactions, got", pool.all.Count()) 442 } 443 } 444 445 func TestTransactionNonceRecovery(t *testing.T) { 446 t.Parallel() 447 448 const n = 10 449 pool, key := setupTxPool() 450 defer pool.Stop() 451 452 addr := crypto.PubkeyToAddress(key.PublicKey) 453 pool.currentState.SetNonce(addr, n) 454 pool.currentState.AddBalance(addr, big.NewInt(100000000000000)) 455 <-pool.requestReset(nil, nil) 456 457 tx := transaction(n, 100000, key) 458 if err := pool.AddRemote(tx); err != nil { 459 t.Error(err) 460 } 461 // simulate some weird re-order of transactions and missing nonce(s) 462 pool.currentState.SetNonce(addr, n-1) 463 <-pool.requestReset(nil, nil) 464 if fn := pool.Nonce(addr); fn != n-1 { 465 t.Errorf("expected nonce to be %d, got %d", n-1, fn) 466 } 467 } 468 469 // Tests that if an account runs out of funds, any pending and queued transactions 470 // are dropped. 471 func TestTransactionDropping(t *testing.T) { 472 t.Parallel() 473 474 // Create a test account and fund it 475 pool, key := setupTxPool() 476 defer pool.Stop() 477 478 account := crypto.PubkeyToAddress(key.PublicKey) 479 pool.currentState.AddBalance(account, big.NewInt(1000)) 480 481 // Add some pending and some queued transactions 482 var ( 483 tx0 = transaction(0, 100, key) 484 tx1 = transaction(1, 200, key) 485 tx2 = transaction(2, 300, key) 486 tx10 = transaction(10, 100, key) 487 tx11 = transaction(11, 200, key) 488 tx12 = transaction(12, 300, key) 489 ) 490 pool.all.Add(tx0, false) 491 pool.priced.Put(tx0, false) 492 pool.promoteTx(account, tx0.Hash(), tx0) 493 494 pool.all.Add(tx1, false) 495 pool.priced.Put(tx1, false) 496 pool.promoteTx(account, tx1.Hash(), tx1) 497 498 pool.all.Add(tx2, false) 499 pool.priced.Put(tx2, false) 500 pool.promoteTx(account, tx2.Hash(), tx2) 501 502 pool.enqueueTx(tx10.Hash(), tx10, false, true) 503 pool.enqueueTx(tx11.Hash(), tx11, false, true) 504 pool.enqueueTx(tx12.Hash(), tx12, false, true) 505 506 // Check that pre and post validations leave the pool as is 507 if pool.pending[account].Len() != 3 { 508 t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 3) 509 } 510 if pool.queue[account].Len() != 3 { 511 t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 3) 512 } 513 if pool.all.Count() != 6 { 514 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), 6) 515 } 516 <-pool.requestReset(nil, nil) 517 if pool.pending[account].Len() != 3 { 518 t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 3) 519 } 520 if pool.queue[account].Len() != 3 { 521 t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 3) 522 } 523 if pool.all.Count() != 6 { 524 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), 6) 525 } 526 // Reduce the balance of the account, and check that invalidated transactions are dropped 527 pool.currentState.AddBalance(account, big.NewInt(-650)) 528 <-pool.requestReset(nil, nil) 529 530 if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok { 531 t.Errorf("funded pending transaction missing: %v", tx0) 532 } 533 if _, ok := pool.pending[account].txs.items[tx1.Nonce()]; !ok { 534 t.Errorf("funded pending transaction missing: %v", tx0) 535 } 536 if _, ok := pool.pending[account].txs.items[tx2.Nonce()]; ok { 537 t.Errorf("out-of-fund pending transaction present: %v", tx1) 538 } 539 if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok { 540 t.Errorf("funded queued transaction missing: %v", tx10) 541 } 542 if _, ok := pool.queue[account].txs.items[tx11.Nonce()]; !ok { 543 t.Errorf("funded queued transaction missing: %v", tx10) 544 } 545 if _, ok := pool.queue[account].txs.items[tx12.Nonce()]; ok { 546 t.Errorf("out-of-fund queued transaction present: %v", tx11) 547 } 548 if pool.all.Count() != 4 { 549 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), 4) 550 } 551 // Reduce the block gas limit, check that invalidated transactions are dropped 552 pool.chain.(*testBlockChain).gasLimit = 100 553 <-pool.requestReset(nil, nil) 554 555 if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok { 556 t.Errorf("funded pending transaction missing: %v", tx0) 557 } 558 if _, ok := pool.pending[account].txs.items[tx1.Nonce()]; ok { 559 t.Errorf("over-gased pending transaction present: %v", tx1) 560 } 561 if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok { 562 t.Errorf("funded queued transaction missing: %v", tx10) 563 } 564 if _, ok := pool.queue[account].txs.items[tx11.Nonce()]; ok { 565 t.Errorf("over-gased queued transaction present: %v", tx11) 566 } 567 if pool.all.Count() != 2 { 568 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), 2) 569 } 570 } 571 572 // Tests that if a transaction is dropped from the current pending pool (e.g. out 573 // of fund), all consecutive (still valid, but not executable) transactions are 574 // postponed back into the future queue to prevent broadcasting them. 575 func TestTransactionPostponing(t *testing.T) { 576 t.Parallel() 577 578 // Create the pool to test the postponing with 579 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 580 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 581 582 pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) 583 defer pool.Stop() 584 585 // Create two test accounts to produce different gap profiles with 586 keys := make([]*ecdsa.PrivateKey, 2) 587 accs := make([]common.Address, len(keys)) 588 589 for i := 0; i < len(keys); i++ { 590 keys[i], _ = crypto.GenerateKey() 591 accs[i] = crypto.PubkeyToAddress(keys[i].PublicKey) 592 593 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(50100)) 594 } 595 // Add a batch consecutive pending transactions for validation 596 txs := []*types.Transaction{} 597 for i, key := range keys { 598 599 for j := 0; j < 100; j++ { 600 var tx *types.Transaction 601 if (i+j)%2 == 0 { 602 tx = transaction(uint64(j), 25000, key) 603 } else { 604 tx = transaction(uint64(j), 50000, key) 605 } 606 txs = append(txs, tx) 607 } 608 } 609 for i, err := range pool.AddRemotesSync(txs) { 610 if err != nil { 611 t.Fatalf("tx %d: failed to add transactions: %v", i, err) 612 } 613 } 614 // Check that pre and post validations leave the pool as is 615 if pending := pool.pending[accs[0]].Len() + pool.pending[accs[1]].Len(); pending != len(txs) { 616 t.Errorf("pending transaction mismatch: have %d, want %d", pending, len(txs)) 617 } 618 if len(pool.queue) != 0 { 619 t.Errorf("queued accounts mismatch: have %d, want %d", len(pool.queue), 0) 620 } 621 if pool.all.Count() != len(txs) { 622 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), len(txs)) 623 } 624 <-pool.requestReset(nil, nil) 625 if pending := pool.pending[accs[0]].Len() + pool.pending[accs[1]].Len(); pending != len(txs) { 626 t.Errorf("pending transaction mismatch: have %d, want %d", pending, len(txs)) 627 } 628 if len(pool.queue) != 0 { 629 t.Errorf("queued accounts mismatch: have %d, want %d", len(pool.queue), 0) 630 } 631 if pool.all.Count() != len(txs) { 632 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), len(txs)) 633 } 634 // Reduce the balance of the account, and check that transactions are reorganised 635 for _, addr := range accs { 636 pool.currentState.AddBalance(addr, big.NewInt(-1)) 637 } 638 <-pool.requestReset(nil, nil) 639 640 // The first account's first transaction remains valid, check that subsequent 641 // ones are either filtered out, or queued up for later. 642 if _, ok := pool.pending[accs[0]].txs.items[txs[0].Nonce()]; !ok { 643 t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txs[0]) 644 } 645 if _, ok := pool.queue[accs[0]].txs.items[txs[0].Nonce()]; ok { 646 t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txs[0]) 647 } 648 for i, tx := range txs[1:100] { 649 if i%2 == 1 { 650 if _, ok := pool.pending[accs[0]].txs.items[tx.Nonce()]; ok { 651 t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx) 652 } 653 if _, ok := pool.queue[accs[0]].txs.items[tx.Nonce()]; !ok { 654 t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx) 655 } 656 } else { 657 if _, ok := pool.pending[accs[0]].txs.items[tx.Nonce()]; ok { 658 t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx) 659 } 660 if _, ok := pool.queue[accs[0]].txs.items[tx.Nonce()]; ok { 661 t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx) 662 } 663 } 664 } 665 // The second account's first transaction got invalid, check that all transactions 666 // are either filtered out, or queued up for later. 667 if pool.pending[accs[1]] != nil { 668 t.Errorf("invalidated account still has pending transactions") 669 } 670 for i, tx := range txs[100:] { 671 if i%2 == 1 { 672 if _, ok := pool.queue[accs[1]].txs.items[tx.Nonce()]; !ok { 673 t.Errorf("tx %d: valid but future transaction missing from future queue: %v", 100+i, tx) 674 } 675 } else { 676 if _, ok := pool.queue[accs[1]].txs.items[tx.Nonce()]; ok { 677 t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", 100+i, tx) 678 } 679 } 680 } 681 if pool.all.Count() != len(txs)/2 { 682 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), len(txs)/2) 683 } 684 } 685 686 // Tests that if the transaction pool has both executable and non-executable 687 // transactions from an origin account, filling the nonce gap moves all queued 688 // ones into the pending pool. 689 func TestTransactionGapFilling(t *testing.T) { 690 t.Parallel() 691 692 // Create a test account and fund it 693 pool, key := setupTxPool() 694 defer pool.Stop() 695 696 account := crypto.PubkeyToAddress(key.PublicKey) 697 pool.currentState.AddBalance(account, big.NewInt(1000000)) 698 699 // Keep track of transaction events to ensure all executables get announced 700 events := make(chan NewTxsEvent, testTxPoolConfig.AccountQueue+5) 701 sub := pool.txFeed.Subscribe(events) 702 defer sub.Unsubscribe() 703 704 // Create a pending and a queued transaction with a nonce-gap in between 705 pool.AddRemotesSync([]*types.Transaction{ 706 transaction(0, 100000, key), 707 transaction(2, 100000, key), 708 }) 709 pending, queued := pool.Stats() 710 if pending != 1 { 711 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 1) 712 } 713 if queued != 1 { 714 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1) 715 } 716 if err := validateEvents(events, 1); err != nil { 717 t.Fatalf("original event firing failed: %v", err) 718 } 719 if err := validateTxPoolInternals(pool); err != nil { 720 t.Fatalf("pool internal state corrupted: %v", err) 721 } 722 // Fill the nonce gap and ensure all transactions become pending 723 if err := pool.addRemoteSync(transaction(1, 100000, key)); err != nil { 724 t.Fatalf("failed to add gapped transaction: %v", err) 725 } 726 pending, queued = pool.Stats() 727 if pending != 3 { 728 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3) 729 } 730 if queued != 0 { 731 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 732 } 733 if err := validateEvents(events, 2); err != nil { 734 t.Fatalf("gap-filling event firing failed: %v", err) 735 } 736 if err := validateTxPoolInternals(pool); err != nil { 737 t.Fatalf("pool internal state corrupted: %v", err) 738 } 739 } 740 741 // Tests that if the transaction count belonging to a single account goes above 742 // some threshold, the higher transactions are dropped to prevent DOS attacks. 743 func TestTransactionQueueAccountLimiting(t *testing.T) { 744 t.Parallel() 745 746 // Create a test account and fund it 747 pool, key := setupTxPool() 748 defer pool.Stop() 749 750 account := crypto.PubkeyToAddress(key.PublicKey) 751 pool.currentState.AddBalance(account, big.NewInt(1000000)) 752 753 // Keep queuing up transactions and make sure all above a limit are dropped 754 for i := uint64(1); i <= testTxPoolConfig.AccountQueue+5; i++ { 755 if err := pool.addRemoteSync(transaction(i, 100000, key)); err != nil { 756 t.Fatalf("tx %d: failed to add transaction: %v", i, err) 757 } 758 if len(pool.pending) != 0 { 759 t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), 0) 760 } 761 if i <= testTxPoolConfig.AccountQueue { 762 if pool.queue[account].Len() != int(i) { 763 t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), i) 764 } 765 } else { 766 if pool.queue[account].Len() != int(testTxPoolConfig.AccountQueue) { 767 t.Errorf("tx %d: queue limit mismatch: have %d, want %d", i, pool.queue[account].Len(), testTxPoolConfig.AccountQueue) 768 } 769 } 770 } 771 if pool.all.Count() != int(testTxPoolConfig.AccountQueue) { 772 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), testTxPoolConfig.AccountQueue) 773 } 774 } 775 776 // Tests that if the transaction count belonging to multiple accounts go above 777 // some threshold, the higher transactions are dropped to prevent DOS attacks. 778 // 779 // This logic should not hold for local transactions, unless the local tracking 780 // mechanism is disabled. 781 func TestTransactionQueueGlobalLimiting(t *testing.T) { 782 testTransactionQueueGlobalLimiting(t, false) 783 } 784 func TestTransactionQueueGlobalLimitingNoLocals(t *testing.T) { 785 testTransactionQueueGlobalLimiting(t, true) 786 } 787 788 func testTransactionQueueGlobalLimiting(t *testing.T, nolocals bool) { 789 t.Parallel() 790 791 // Create the pool to test the limit enforcement with 792 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 793 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 794 795 config := testTxPoolConfig 796 config.NoLocals = nolocals 797 config.GlobalQueue = config.AccountQueue*3 - 1 // reduce the queue limits to shorten test time (-1 to make it non divisible) 798 799 pool := NewTxPool(config, params.TestChainConfig, blockchain) 800 defer pool.Stop() 801 802 // Create a number of test accounts and fund them (last one will be the local) 803 keys := make([]*ecdsa.PrivateKey, 5) 804 for i := 0; i < len(keys); i++ { 805 keys[i], _ = crypto.GenerateKey() 806 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000)) 807 } 808 local := keys[len(keys)-1] 809 810 // Generate and queue a batch of transactions 811 nonces := make(map[common.Address]uint64) 812 813 txs := make(types.Transactions, 0, 3*config.GlobalQueue) 814 for len(txs) < cap(txs) { 815 key := keys[rand.Intn(len(keys)-1)] // skip adding transactions with the local account 816 addr := crypto.PubkeyToAddress(key.PublicKey) 817 818 txs = append(txs, transaction(nonces[addr]+1, 100000, key)) 819 nonces[addr]++ 820 } 821 // Import the batch and verify that limits have been enforced 822 pool.AddRemotesSync(txs) 823 824 queued := 0 825 for addr, list := range pool.queue { 826 if list.Len() > int(config.AccountQueue) { 827 t.Errorf("addr %x: queued accounts overflown allowance: %d > %d", addr, list.Len(), config.AccountQueue) 828 } 829 queued += list.Len() 830 } 831 if queued > int(config.GlobalQueue) { 832 t.Fatalf("total transactions overflow allowance: %d > %d", queued, config.GlobalQueue) 833 } 834 // Generate a batch of transactions from the local account and import them 835 txs = txs[:0] 836 for i := uint64(0); i < 3*config.GlobalQueue; i++ { 837 txs = append(txs, transaction(i+1, 100000, local)) 838 } 839 pool.AddLocals(txs) 840 841 // If locals are disabled, the previous eviction algorithm should apply here too 842 if nolocals { 843 queued := 0 844 for addr, list := range pool.queue { 845 if list.Len() > int(config.AccountQueue) { 846 t.Errorf("addr %x: queued accounts overflown allowance: %d > %d", addr, list.Len(), config.AccountQueue) 847 } 848 queued += list.Len() 849 } 850 if queued > int(config.GlobalQueue) { 851 t.Fatalf("total transactions overflow allowance: %d > %d", queued, config.GlobalQueue) 852 } 853 } else { 854 // Local exemptions are enabled, make sure the local account owned the queue 855 if len(pool.queue) != 1 { 856 t.Errorf("multiple accounts in queue: have %v, want %v", len(pool.queue), 1) 857 } 858 // Also ensure no local transactions are ever dropped, even if above global limits 859 if queued := pool.queue[crypto.PubkeyToAddress(local.PublicKey)].Len(); uint64(queued) != 3*config.GlobalQueue { 860 t.Fatalf("local account queued transaction count mismatch: have %v, want %v", queued, 3*config.GlobalQueue) 861 } 862 } 863 } 864 865 // Tests that if an account remains idle for a prolonged amount of time, any 866 // non-executable transactions queued up are dropped to prevent wasting resources 867 // on shuffling them around. 868 // 869 // This logic should not hold for local transactions, unless the local tracking 870 // mechanism is disabled. 871 func TestTransactionQueueTimeLimiting(t *testing.T) { 872 testTransactionQueueTimeLimiting(t, false) 873 } 874 func TestTransactionQueueTimeLimitingNoLocals(t *testing.T) { 875 testTransactionQueueTimeLimiting(t, true) 876 } 877 878 func testTransactionQueueTimeLimiting(t *testing.T, nolocals bool) { 879 // Reduce the eviction interval to a testable amount 880 defer func(old time.Duration) { evictionInterval = old }(evictionInterval) 881 evictionInterval = time.Millisecond * 100 882 883 // Create the pool to test the non-expiration enforcement 884 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 885 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 886 887 config := testTxPoolConfig 888 config.Lifetime = time.Second 889 config.NoLocals = nolocals 890 891 pool := NewTxPool(config, params.TestChainConfig, blockchain) 892 defer pool.Stop() 893 894 // Create two test accounts to ensure remotes expire but locals do not 895 local, _ := crypto.GenerateKey() 896 remote, _ := crypto.GenerateKey() 897 898 pool.currentState.AddBalance(crypto.PubkeyToAddress(local.PublicKey), big.NewInt(1000000000)) 899 pool.currentState.AddBalance(crypto.PubkeyToAddress(remote.PublicKey), big.NewInt(1000000000)) 900 901 // Add the two transactions and ensure they both are queued up 902 if err := pool.AddLocal(pricedTransaction(1, 100000, big.NewInt(1), local)); err != nil { 903 t.Fatalf("failed to add local transaction: %v", err) 904 } 905 if err := pool.AddRemote(pricedTransaction(1, 100000, big.NewInt(1), remote)); err != nil { 906 t.Fatalf("failed to add remote transaction: %v", err) 907 } 908 pending, queued := pool.Stats() 909 if pending != 0 { 910 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0) 911 } 912 if queued != 2 { 913 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2) 914 } 915 if err := validateTxPoolInternals(pool); err != nil { 916 t.Fatalf("pool internal state corrupted: %v", err) 917 } 918 919 // Allow the eviction interval to run 920 time.Sleep(2 * evictionInterval) 921 922 // Transactions should not be evicted from the queue yet since lifetime duration has not passed 923 pending, queued = pool.Stats() 924 if pending != 0 { 925 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0) 926 } 927 if queued != 2 { 928 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2) 929 } 930 if err := validateTxPoolInternals(pool); err != nil { 931 t.Fatalf("pool internal state corrupted: %v", err) 932 } 933 934 // Wait a bit for eviction to run and clean up any leftovers, and ensure only the local remains 935 time.Sleep(2 * config.Lifetime) 936 937 pending, queued = pool.Stats() 938 if pending != 0 { 939 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0) 940 } 941 if nolocals { 942 if queued != 0 { 943 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 944 } 945 } else { 946 if queued != 1 { 947 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1) 948 } 949 } 950 if err := validateTxPoolInternals(pool); err != nil { 951 t.Fatalf("pool internal state corrupted: %v", err) 952 } 953 954 // remove current transactions and increase nonce to prepare for a reset and cleanup 955 statedb.SetNonce(crypto.PubkeyToAddress(remote.PublicKey), 2) 956 statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 2) 957 <-pool.requestReset(nil, nil) 958 959 // make sure queue, pending are cleared 960 pending, queued = pool.Stats() 961 if pending != 0 { 962 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0) 963 } 964 if queued != 0 { 965 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 966 } 967 if err := validateTxPoolInternals(pool); err != nil { 968 t.Fatalf("pool internal state corrupted: %v", err) 969 } 970 971 // Queue gapped transactions 972 if err := pool.AddLocal(pricedTransaction(4, 100000, big.NewInt(1), local)); err != nil { 973 t.Fatalf("failed to add remote transaction: %v", err) 974 } 975 if err := pool.addRemoteSync(pricedTransaction(4, 100000, big.NewInt(1), remote)); err != nil { 976 t.Fatalf("failed to add remote transaction: %v", err) 977 } 978 time.Sleep(5 * evictionInterval) // A half lifetime pass 979 980 // Queue executable transactions, the life cycle should be restarted. 981 if err := pool.AddLocal(pricedTransaction(2, 100000, big.NewInt(1), local)); err != nil { 982 t.Fatalf("failed to add remote transaction: %v", err) 983 } 984 if err := pool.addRemoteSync(pricedTransaction(2, 100000, big.NewInt(1), remote)); err != nil { 985 t.Fatalf("failed to add remote transaction: %v", err) 986 } 987 time.Sleep(6 * evictionInterval) 988 989 // All gapped transactions shouldn't be kicked out 990 pending, queued = pool.Stats() 991 if pending != 2 { 992 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2) 993 } 994 if queued != 2 { 995 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3) 996 } 997 if err := validateTxPoolInternals(pool); err != nil { 998 t.Fatalf("pool internal state corrupted: %v", err) 999 } 1000 1001 // The whole life time pass after last promotion, kick out stale transactions 1002 time.Sleep(2 * config.Lifetime) 1003 pending, queued = pool.Stats() 1004 if pending != 2 { 1005 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2) 1006 } 1007 if nolocals { 1008 if queued != 0 { 1009 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 1010 } 1011 } else { 1012 if queued != 1 { 1013 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1) 1014 } 1015 } 1016 if err := validateTxPoolInternals(pool); err != nil { 1017 t.Fatalf("pool internal state corrupted: %v", err) 1018 } 1019 } 1020 1021 // Tests that even if the transaction count belonging to a single account goes 1022 // above some threshold, as long as the transactions are executable, they are 1023 // accepted. 1024 func TestTransactionPendingLimiting(t *testing.T) { 1025 t.Parallel() 1026 1027 // Create a test account and fund it 1028 pool, key := setupTxPool() 1029 defer pool.Stop() 1030 1031 account := crypto.PubkeyToAddress(key.PublicKey) 1032 pool.currentState.AddBalance(account, big.NewInt(1000000)) 1033 1034 // Keep track of transaction events to ensure all executables get announced 1035 events := make(chan NewTxsEvent, testTxPoolConfig.AccountQueue+5) 1036 sub := pool.txFeed.Subscribe(events) 1037 defer sub.Unsubscribe() 1038 1039 // Keep queuing up transactions and make sure all above a limit are dropped 1040 for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ { 1041 if err := pool.addRemoteSync(transaction(i, 100000, key)); err != nil { 1042 t.Fatalf("tx %d: failed to add transaction: %v", i, err) 1043 } 1044 if pool.pending[account].Len() != int(i)+1 { 1045 t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, pool.pending[account].Len(), i+1) 1046 } 1047 if len(pool.queue) != 0 { 1048 t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), 0) 1049 } 1050 } 1051 if pool.all.Count() != int(testTxPoolConfig.AccountQueue+5) { 1052 t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), testTxPoolConfig.AccountQueue+5) 1053 } 1054 if err := validateEvents(events, int(testTxPoolConfig.AccountQueue+5)); err != nil { 1055 t.Fatalf("event firing failed: %v", err) 1056 } 1057 if err := validateTxPoolInternals(pool); err != nil { 1058 t.Fatalf("pool internal state corrupted: %v", err) 1059 } 1060 } 1061 1062 // Tests that if the transaction count belonging to multiple accounts go above 1063 // some hard threshold, the higher transactions are dropped to prevent DOS 1064 // attacks. 1065 func TestTransactionPendingGlobalLimiting(t *testing.T) { 1066 t.Parallel() 1067 1068 // Create the pool to test the limit enforcement with 1069 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1070 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1071 1072 config := testTxPoolConfig 1073 config.GlobalSlots = config.AccountSlots * 10 1074 1075 pool := NewTxPool(config, params.TestChainConfig, blockchain) 1076 defer pool.Stop() 1077 1078 // Create a number of test accounts and fund them 1079 keys := make([]*ecdsa.PrivateKey, 5) 1080 for i := 0; i < len(keys); i++ { 1081 keys[i], _ = crypto.GenerateKey() 1082 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000)) 1083 } 1084 // Generate and queue a batch of transactions 1085 nonces := make(map[common.Address]uint64) 1086 1087 txs := types.Transactions{} 1088 for _, key := range keys { 1089 addr := crypto.PubkeyToAddress(key.PublicKey) 1090 for j := 0; j < int(config.GlobalSlots)/len(keys)*2; j++ { 1091 txs = append(txs, transaction(nonces[addr], 100000, key)) 1092 nonces[addr]++ 1093 } 1094 } 1095 // Import the batch and verify that limits have been enforced 1096 pool.AddRemotesSync(txs) 1097 1098 pending := 0 1099 for _, list := range pool.pending { 1100 pending += list.Len() 1101 } 1102 if pending > int(config.GlobalSlots) { 1103 t.Fatalf("total pending transactions overflow allowance: %d > %d", pending, config.GlobalSlots) 1104 } 1105 if err := validateTxPoolInternals(pool); err != nil { 1106 t.Fatalf("pool internal state corrupted: %v", err) 1107 } 1108 } 1109 1110 // Test the limit on transaction size is enforced correctly. 1111 // This test verifies every transaction having allowed size 1112 // is added to the pool, and longer transactions are rejected. 1113 func TestTransactionAllowedTxSize(t *testing.T) { 1114 t.Parallel() 1115 1116 // Create a test account and fund it 1117 pool, key := setupTxPool() 1118 defer pool.Stop() 1119 1120 account := crypto.PubkeyToAddress(key.PublicKey) 1121 pool.currentState.AddBalance(account, big.NewInt(1000000000)) 1122 1123 // Compute maximal data size for transactions (lower bound). 1124 // 1125 // It is assumed the fields in the transaction (except of the data) are: 1126 // - nonce <= 32 bytes 1127 // - gasPrice <= 32 bytes 1128 // - gasLimit <= 32 bytes 1129 // - recipient == 20 bytes 1130 // - value <= 32 bytes 1131 // - signature == 65 bytes 1132 // All those fields are summed up to at most 213 bytes. 1133 baseSize := uint64(213) 1134 dataSize := txMaxSize - baseSize 1135 1136 // Try adding a transaction with maximal allowed size 1137 tx := pricedDataTransaction(0, pool.currentMaxGas, big.NewInt(1), key, dataSize) 1138 if err := pool.addRemoteSync(tx); err != nil { 1139 t.Fatalf("failed to add transaction of size %d, close to maximal: %v", int(tx.Size()), err) 1140 } 1141 // Try adding a transaction with random allowed size 1142 if err := pool.addRemoteSync(pricedDataTransaction(1, pool.currentMaxGas, big.NewInt(1), key, uint64(rand.Intn(int(dataSize))))); err != nil { 1143 t.Fatalf("failed to add transaction of random allowed size: %v", err) 1144 } 1145 // Try adding a transaction of minimal not allowed size 1146 if err := pool.addRemoteSync(pricedDataTransaction(2, pool.currentMaxGas, big.NewInt(1), key, txMaxSize)); err == nil { 1147 t.Fatalf("expected rejection on slightly oversize transaction") 1148 } 1149 // Try adding a transaction of random not allowed size 1150 if err := pool.addRemoteSync(pricedDataTransaction(2, pool.currentMaxGas, big.NewInt(1), key, dataSize+1+uint64(rand.Intn(10*txMaxSize)))); err == nil { 1151 t.Fatalf("expected rejection on oversize transaction") 1152 } 1153 // Run some sanity checks on the pool internals 1154 pending, queued := pool.Stats() 1155 if pending != 2 { 1156 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2) 1157 } 1158 if queued != 0 { 1159 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 1160 } 1161 if err := validateTxPoolInternals(pool); err != nil { 1162 t.Fatalf("pool internal state corrupted: %v", err) 1163 } 1164 } 1165 1166 // Tests that if transactions start being capped, transactions are also removed from 'all' 1167 func TestTransactionCapClearsFromAll(t *testing.T) { 1168 t.Parallel() 1169 1170 // Create the pool to test the limit enforcement with 1171 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1172 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1173 1174 config := testTxPoolConfig 1175 config.AccountSlots = 2 1176 config.AccountQueue = 2 1177 config.GlobalSlots = 8 1178 1179 pool := NewTxPool(config, params.TestChainConfig, blockchain) 1180 defer pool.Stop() 1181 1182 // Create a number of test accounts and fund them 1183 key, _ := crypto.GenerateKey() 1184 addr := crypto.PubkeyToAddress(key.PublicKey) 1185 pool.currentState.AddBalance(addr, big.NewInt(1000000)) 1186 1187 txs := types.Transactions{} 1188 for j := 0; j < int(config.GlobalSlots)*2; j++ { 1189 txs = append(txs, transaction(uint64(j), 100000, key)) 1190 } 1191 // Import the batch and verify that limits have been enforced 1192 pool.AddRemotes(txs) 1193 if err := validateTxPoolInternals(pool); err != nil { 1194 t.Fatalf("pool internal state corrupted: %v", err) 1195 } 1196 } 1197 1198 // Tests that if the transaction count belonging to multiple accounts go above 1199 // some hard threshold, if they are under the minimum guaranteed slot count then 1200 // the transactions are still kept. 1201 func TestTransactionPendingMinimumAllowance(t *testing.T) { 1202 t.Parallel() 1203 1204 // Create the pool to test the limit enforcement with 1205 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1206 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1207 1208 config := testTxPoolConfig 1209 config.GlobalSlots = 1 1210 1211 pool := NewTxPool(config, params.TestChainConfig, blockchain) 1212 defer pool.Stop() 1213 1214 // Create a number of test accounts and fund them 1215 keys := make([]*ecdsa.PrivateKey, 5) 1216 for i := 0; i < len(keys); i++ { 1217 keys[i], _ = crypto.GenerateKey() 1218 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000)) 1219 } 1220 // Generate and queue a batch of transactions 1221 nonces := make(map[common.Address]uint64) 1222 1223 txs := types.Transactions{} 1224 for _, key := range keys { 1225 addr := crypto.PubkeyToAddress(key.PublicKey) 1226 for j := 0; j < int(config.AccountSlots)*2; j++ { 1227 txs = append(txs, transaction(nonces[addr], 100000, key)) 1228 nonces[addr]++ 1229 } 1230 } 1231 // Import the batch and verify that limits have been enforced 1232 pool.AddRemotesSync(txs) 1233 1234 for addr, list := range pool.pending { 1235 if list.Len() != int(config.AccountSlots) { 1236 t.Errorf("addr %x: total pending transactions mismatch: have %d, want %d", addr, list.Len(), config.AccountSlots) 1237 } 1238 } 1239 if err := validateTxPoolInternals(pool); err != nil { 1240 t.Fatalf("pool internal state corrupted: %v", err) 1241 } 1242 } 1243 1244 // Tests that setting the transaction pool gas price to a higher value correctly 1245 // discards everything cheaper than that and moves any gapped transactions back 1246 // from the pending pool to the queue. 1247 // 1248 // Note, local transactions are never allowed to be dropped. 1249 func TestTransactionPoolRepricing(t *testing.T) { 1250 t.Parallel() 1251 1252 // Create the pool to test the pricing enforcement with 1253 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1254 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1255 1256 pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) 1257 defer pool.Stop() 1258 1259 // Keep track of transaction events to ensure all executables get announced 1260 events := make(chan NewTxsEvent, 32) 1261 sub := pool.txFeed.Subscribe(events) 1262 defer sub.Unsubscribe() 1263 1264 // Create a number of test accounts and fund them 1265 keys := make([]*ecdsa.PrivateKey, 4) 1266 for i := 0; i < len(keys); i++ { 1267 keys[i], _ = crypto.GenerateKey() 1268 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000)) 1269 } 1270 // Generate and queue a batch of transactions, both pending and queued 1271 txs := types.Transactions{} 1272 1273 txs = append(txs, pricedTransaction(0, 100000, big.NewInt(2), keys[0])) 1274 txs = append(txs, pricedTransaction(1, 100000, big.NewInt(1), keys[0])) 1275 txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[0])) 1276 1277 txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[1])) 1278 txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[1])) 1279 txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[1])) 1280 1281 txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[2])) 1282 txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[2])) 1283 txs = append(txs, pricedTransaction(3, 100000, big.NewInt(2), keys[2])) 1284 1285 ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[3]) 1286 1287 // Import the batch and that both pending and queued transactions match up 1288 pool.AddRemotesSync(txs) 1289 pool.AddLocal(ltx) 1290 1291 pending, queued := pool.Stats() 1292 if pending != 7 { 1293 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 7) 1294 } 1295 if queued != 3 { 1296 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3) 1297 } 1298 if err := validateEvents(events, 7); err != nil { 1299 t.Fatalf("original event firing failed: %v", err) 1300 } 1301 if err := validateTxPoolInternals(pool); err != nil { 1302 t.Fatalf("pool internal state corrupted: %v", err) 1303 } 1304 // Reprice the pool and check that underpriced transactions get dropped 1305 pool.SetGasPrice(big.NewInt(2)) 1306 1307 pending, queued = pool.Stats() 1308 if pending != 2 { 1309 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2) 1310 } 1311 if queued != 5 { 1312 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 5) 1313 } 1314 if err := validateEvents(events, 0); err != nil { 1315 t.Fatalf("reprice event firing failed: %v", err) 1316 } 1317 if err := validateTxPoolInternals(pool); err != nil { 1318 t.Fatalf("pool internal state corrupted: %v", err) 1319 } 1320 // Check that we can't add the old transactions back 1321 if err := pool.AddRemote(pricedTransaction(1, 100000, big.NewInt(1), keys[0])); err != ErrUnderpriced { 1322 t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced) 1323 } 1324 if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(1), keys[1])); err != ErrUnderpriced { 1325 t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced) 1326 } 1327 if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(1), keys[2])); err != ErrUnderpriced { 1328 t.Fatalf("adding underpriced queued transaction error mismatch: have %v, want %v", err, ErrUnderpriced) 1329 } 1330 if err := validateEvents(events, 0); err != nil { 1331 t.Fatalf("post-reprice event firing failed: %v", err) 1332 } 1333 if err := validateTxPoolInternals(pool); err != nil { 1334 t.Fatalf("pool internal state corrupted: %v", err) 1335 } 1336 // However we can add local underpriced transactions 1337 tx := pricedTransaction(1, 100000, big.NewInt(1), keys[3]) 1338 if err := pool.AddLocal(tx); err != nil { 1339 t.Fatalf("failed to add underpriced local transaction: %v", err) 1340 } 1341 if pending, _ = pool.Stats(); pending != 3 { 1342 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3) 1343 } 1344 if err := validateEvents(events, 1); err != nil { 1345 t.Fatalf("post-reprice local event firing failed: %v", err) 1346 } 1347 if err := validateTxPoolInternals(pool); err != nil { 1348 t.Fatalf("pool internal state corrupted: %v", err) 1349 } 1350 // And we can fill gaps with properly priced transactions 1351 if err := pool.AddRemote(pricedTransaction(1, 100000, big.NewInt(2), keys[0])); err != nil { 1352 t.Fatalf("failed to add pending transaction: %v", err) 1353 } 1354 if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(2), keys[1])); err != nil { 1355 t.Fatalf("failed to add pending transaction: %v", err) 1356 } 1357 if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(2), keys[2])); err != nil { 1358 t.Fatalf("failed to add queued transaction: %v", err) 1359 } 1360 if err := validateEvents(events, 5); err != nil { 1361 t.Fatalf("post-reprice event firing failed: %v", err) 1362 } 1363 if err := validateTxPoolInternals(pool); err != nil { 1364 t.Fatalf("pool internal state corrupted: %v", err) 1365 } 1366 } 1367 1368 // Tests that setting the transaction pool gas price to a higher value does not 1369 // remove local transactions. 1370 func TestTransactionPoolRepricingKeepsLocals(t *testing.T) { 1371 t.Parallel() 1372 1373 // Create the pool to test the pricing enforcement with 1374 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1375 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1376 1377 pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) 1378 defer pool.Stop() 1379 1380 // Create a number of test accounts and fund them 1381 keys := make([]*ecdsa.PrivateKey, 3) 1382 for i := 0; i < len(keys); i++ { 1383 keys[i], _ = crypto.GenerateKey() 1384 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000*1000000)) 1385 } 1386 // Create transaction (both pending and queued) with a linearly growing gasprice 1387 for i := uint64(0); i < 500; i++ { 1388 // Add pending transaction. 1389 pendingTx := pricedTransaction(i, 100000, big.NewInt(int64(i)), keys[2]) 1390 if err := pool.AddLocal(pendingTx); err != nil { 1391 t.Fatal(err) 1392 } 1393 // Add queued transaction. 1394 queuedTx := pricedTransaction(i+501, 100000, big.NewInt(int64(i)), keys[2]) 1395 if err := pool.AddLocal(queuedTx); err != nil { 1396 t.Fatal(err) 1397 } 1398 } 1399 pending, queued := pool.Stats() 1400 expPending, expQueued := 500, 500 1401 validate := func() { 1402 pending, queued = pool.Stats() 1403 if pending != expPending { 1404 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, expPending) 1405 } 1406 if queued != expQueued { 1407 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, expQueued) 1408 } 1409 1410 if err := validateTxPoolInternals(pool); err != nil { 1411 t.Fatalf("pool internal state corrupted: %v", err) 1412 } 1413 } 1414 validate() 1415 1416 // Reprice the pool and check that nothing is dropped 1417 pool.SetGasPrice(big.NewInt(2)) 1418 validate() 1419 1420 pool.SetGasPrice(big.NewInt(2)) 1421 pool.SetGasPrice(big.NewInt(4)) 1422 pool.SetGasPrice(big.NewInt(8)) 1423 pool.SetGasPrice(big.NewInt(100)) 1424 validate() 1425 } 1426 1427 // Tests that when the pool reaches its global transaction limit, underpriced 1428 // transactions are gradually shifted out for more expensive ones and any gapped 1429 // pending transactions are moved into the queue. 1430 // 1431 // Note, local transactions are never allowed to be dropped. 1432 func TestTransactionPoolUnderpricing(t *testing.T) { 1433 t.Parallel() 1434 1435 // Create the pool to test the pricing enforcement with 1436 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1437 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1438 1439 config := testTxPoolConfig 1440 config.GlobalSlots = 2 1441 config.GlobalQueue = 2 1442 1443 pool := NewTxPool(config, params.TestChainConfig, blockchain) 1444 defer pool.Stop() 1445 1446 // Keep track of transaction events to ensure all executables get announced 1447 events := make(chan NewTxsEvent, 32) 1448 sub := pool.txFeed.Subscribe(events) 1449 defer sub.Unsubscribe() 1450 1451 // Create a number of test accounts and fund them 1452 keys := make([]*ecdsa.PrivateKey, 4) 1453 for i := 0; i < len(keys); i++ { 1454 keys[i], _ = crypto.GenerateKey() 1455 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000)) 1456 } 1457 // Generate and queue a batch of transactions, both pending and queued 1458 txs := types.Transactions{} 1459 1460 txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[0])) 1461 txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[0])) 1462 1463 txs = append(txs, pricedTransaction(1, 100000, big.NewInt(1), keys[1])) 1464 1465 ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[2]) 1466 1467 // Import the batch and that both pending and queued transactions match up 1468 pool.AddRemotes(txs) 1469 pool.AddLocal(ltx) 1470 1471 pending, queued := pool.Stats() 1472 if pending != 3 { 1473 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3) 1474 } 1475 if queued != 1 { 1476 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1) 1477 } 1478 if err := validateEvents(events, 3); err != nil { 1479 t.Fatalf("original event firing failed: %v", err) 1480 } 1481 if err := validateTxPoolInternals(pool); err != nil { 1482 t.Fatalf("pool internal state corrupted: %v", err) 1483 } 1484 // Ensure that adding an underpriced transaction on block limit fails 1485 if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(1), keys[1])); err != ErrUnderpriced { 1486 t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced) 1487 } 1488 // Ensure that adding high priced transactions drops cheap ones, but not own 1489 if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(3), keys[1])); err != nil { // +K1:0 => -K1:1 => Pend K0:0, K0:1, K1:0, K2:0; Que - 1490 t.Fatalf("failed to add well priced transaction: %v", err) 1491 } 1492 if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(4), keys[1])); err != nil { // +K1:2 => -K0:0 => Pend K1:0, K2:0; Que K0:1 K1:2 1493 t.Fatalf("failed to add well priced transaction: %v", err) 1494 } 1495 if err := pool.AddRemote(pricedTransaction(3, 100000, big.NewInt(5), keys[1])); err != nil { // +K1:3 => -K0:1 => Pend K1:0, K2:0; Que K1:2 K1:3 1496 t.Fatalf("failed to add well priced transaction: %v", err) 1497 } 1498 pending, queued = pool.Stats() 1499 if pending != 2 { 1500 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2) 1501 } 1502 if queued != 2 { 1503 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2) 1504 } 1505 if err := validateEvents(events, 1); err != nil { 1506 t.Fatalf("additional event firing failed: %v", err) 1507 } 1508 if err := validateTxPoolInternals(pool); err != nil { 1509 t.Fatalf("pool internal state corrupted: %v", err) 1510 } 1511 // Ensure that adding local transactions can push out even higher priced ones 1512 ltx = pricedTransaction(1, 100000, big.NewInt(0), keys[2]) 1513 if err := pool.AddLocal(ltx); err != nil { 1514 t.Fatalf("failed to append underpriced local transaction: %v", err) 1515 } 1516 ltx = pricedTransaction(0, 100000, big.NewInt(0), keys[3]) 1517 if err := pool.AddLocal(ltx); err != nil { 1518 t.Fatalf("failed to add new underpriced local transaction: %v", err) 1519 } 1520 pending, queued = pool.Stats() 1521 if pending != 3 { 1522 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3) 1523 } 1524 if queued != 1 { 1525 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1) 1526 } 1527 if err := validateEvents(events, 2); err != nil { 1528 t.Fatalf("local event firing failed: %v", err) 1529 } 1530 if err := validateTxPoolInternals(pool); err != nil { 1531 t.Fatalf("pool internal state corrupted: %v", err) 1532 } 1533 } 1534 1535 // Tests that more expensive transactions push out cheap ones from the pool, but 1536 // without producing instability by creating gaps that start jumping transactions 1537 // back and forth between queued/pending. 1538 func TestTransactionPoolStableUnderpricing(t *testing.T) { 1539 t.Parallel() 1540 1541 // Create the pool to test the pricing enforcement with 1542 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1543 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1544 1545 config := testTxPoolConfig 1546 config.GlobalSlots = 128 1547 config.GlobalQueue = 0 1548 1549 pool := NewTxPool(config, params.TestChainConfig, blockchain) 1550 defer pool.Stop() 1551 1552 // Keep track of transaction events to ensure all executables get announced 1553 events := make(chan NewTxsEvent, 32) 1554 sub := pool.txFeed.Subscribe(events) 1555 defer sub.Unsubscribe() 1556 1557 // Create a number of test accounts and fund them 1558 keys := make([]*ecdsa.PrivateKey, 2) 1559 for i := 0; i < len(keys); i++ { 1560 keys[i], _ = crypto.GenerateKey() 1561 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000)) 1562 } 1563 // Fill up the entire queue with the same transaction price points 1564 txs := types.Transactions{} 1565 for i := uint64(0); i < config.GlobalSlots; i++ { 1566 txs = append(txs, pricedTransaction(i, 100000, big.NewInt(1), keys[0])) 1567 } 1568 pool.AddRemotesSync(txs) 1569 1570 pending, queued := pool.Stats() 1571 if pending != int(config.GlobalSlots) { 1572 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, config.GlobalSlots) 1573 } 1574 if queued != 0 { 1575 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 1576 } 1577 if err := validateEvents(events, int(config.GlobalSlots)); err != nil { 1578 t.Fatalf("original event firing failed: %v", err) 1579 } 1580 if err := validateTxPoolInternals(pool); err != nil { 1581 t.Fatalf("pool internal state corrupted: %v", err) 1582 } 1583 // Ensure that adding high priced transactions drops a cheap, but doesn't produce a gap 1584 if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(3), keys[1])); err != nil { 1585 t.Fatalf("failed to add well priced transaction: %v", err) 1586 } 1587 pending, queued = pool.Stats() 1588 if pending != int(config.GlobalSlots) { 1589 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, config.GlobalSlots) 1590 } 1591 if queued != 0 { 1592 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 1593 } 1594 if err := validateEvents(events, 1); err != nil { 1595 t.Fatalf("additional event firing failed: %v", err) 1596 } 1597 if err := validateTxPoolInternals(pool); err != nil { 1598 t.Fatalf("pool internal state corrupted: %v", err) 1599 } 1600 } 1601 1602 // Tests that the pool rejects duplicate transactions. 1603 func TestTransactionDeduplication(t *testing.T) { 1604 t.Parallel() 1605 1606 // Create the pool to test the pricing enforcement with 1607 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1608 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1609 1610 pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) 1611 defer pool.Stop() 1612 1613 // Create a test account to add transactions with 1614 key, _ := crypto.GenerateKey() 1615 pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000)) 1616 1617 // Create a batch of transactions and add a few of them 1618 txs := make([]*types.Transaction, 16) 1619 for i := 0; i < len(txs); i++ { 1620 txs[i] = pricedTransaction(uint64(i), 100000, big.NewInt(1), key) 1621 } 1622 var firsts []*types.Transaction 1623 for i := 0; i < len(txs); i += 2 { 1624 firsts = append(firsts, txs[i]) 1625 } 1626 errs := pool.AddRemotesSync(firsts) 1627 if len(errs) != len(firsts) { 1628 t.Fatalf("first add mismatching result count: have %d, want %d", len(errs), len(firsts)) 1629 } 1630 for i, err := range errs { 1631 if err != nil { 1632 t.Errorf("add %d failed: %v", i, err) 1633 } 1634 } 1635 pending, queued := pool.Stats() 1636 if pending != 1 { 1637 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 1) 1638 } 1639 if queued != len(txs)/2-1 { 1640 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, len(txs)/2-1) 1641 } 1642 // Try to add all of them now and ensure previous ones error out as knowns 1643 errs = pool.AddRemotesSync(txs) 1644 if len(errs) != len(txs) { 1645 t.Fatalf("all add mismatching result count: have %d, want %d", len(errs), len(txs)) 1646 } 1647 for i, err := range errs { 1648 if i%2 == 0 && err == nil { 1649 t.Errorf("add %d succeeded, should have failed as known", i) 1650 } 1651 if i%2 == 1 && err != nil { 1652 t.Errorf("add %d failed: %v", i, err) 1653 } 1654 } 1655 pending, queued = pool.Stats() 1656 if pending != len(txs) { 1657 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, len(txs)) 1658 } 1659 if queued != 0 { 1660 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 1661 } 1662 if err := validateTxPoolInternals(pool); err != nil { 1663 t.Fatalf("pool internal state corrupted: %v", err) 1664 } 1665 } 1666 1667 // Tests that the pool rejects replacement transactions that don't meet the minimum 1668 // price bump required. 1669 func TestTransactionReplacement(t *testing.T) { 1670 t.Parallel() 1671 1672 // Create the pool to test the pricing enforcement with 1673 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1674 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1675 1676 pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) 1677 defer pool.Stop() 1678 1679 // Keep track of transaction events to ensure all executables get announced 1680 events := make(chan NewTxsEvent, 32) 1681 sub := pool.txFeed.Subscribe(events) 1682 defer sub.Unsubscribe() 1683 1684 // Create a test account to add transactions with 1685 key, _ := crypto.GenerateKey() 1686 pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000)) 1687 1688 // Add pending transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too) 1689 price := int64(100) 1690 threshold := (price * (100 + int64(testTxPoolConfig.PriceBump))) / 100 1691 1692 if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(1), key)); err != nil { 1693 t.Fatalf("failed to add original cheap pending transaction: %v", err) 1694 } 1695 if err := pool.AddRemote(pricedTransaction(0, 100001, big.NewInt(1), key)); err != ErrReplaceUnderpriced { 1696 t.Fatalf("original cheap pending transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced) 1697 } 1698 if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(2), key)); err != nil { 1699 t.Fatalf("failed to replace original cheap pending transaction: %v", err) 1700 } 1701 if err := validateEvents(events, 2); err != nil { 1702 t.Fatalf("cheap replacement event firing failed: %v", err) 1703 } 1704 1705 if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(price), key)); err != nil { 1706 t.Fatalf("failed to add original proper pending transaction: %v", err) 1707 } 1708 if err := pool.AddRemote(pricedTransaction(0, 100001, big.NewInt(threshold-1), key)); err != ErrReplaceUnderpriced { 1709 t.Fatalf("original proper pending transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced) 1710 } 1711 if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(threshold), key)); err != nil { 1712 t.Fatalf("failed to replace original proper pending transaction: %v", err) 1713 } 1714 if err := validateEvents(events, 2); err != nil { 1715 t.Fatalf("proper replacement event firing failed: %v", err) 1716 } 1717 1718 // Add queued transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too) 1719 if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(1), key)); err != nil { 1720 t.Fatalf("failed to add original cheap queued transaction: %v", err) 1721 } 1722 if err := pool.AddRemote(pricedTransaction(2, 100001, big.NewInt(1), key)); err != ErrReplaceUnderpriced { 1723 t.Fatalf("original cheap queued transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced) 1724 } 1725 if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(2), key)); err != nil { 1726 t.Fatalf("failed to replace original cheap queued transaction: %v", err) 1727 } 1728 1729 if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(price), key)); err != nil { 1730 t.Fatalf("failed to add original proper queued transaction: %v", err) 1731 } 1732 if err := pool.AddRemote(pricedTransaction(2, 100001, big.NewInt(threshold-1), key)); err != ErrReplaceUnderpriced { 1733 t.Fatalf("original proper queued transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced) 1734 } 1735 if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(threshold), key)); err != nil { 1736 t.Fatalf("failed to replace original proper queued transaction: %v", err) 1737 } 1738 1739 if err := validateEvents(events, 0); err != nil { 1740 t.Fatalf("queued replacement event firing failed: %v", err) 1741 } 1742 if err := validateTxPoolInternals(pool); err != nil { 1743 t.Fatalf("pool internal state corrupted: %v", err) 1744 } 1745 } 1746 1747 // Tests that local transactions are journaled to disk, but remote transactions 1748 // get discarded between restarts. 1749 func TestTransactionJournaling(t *testing.T) { testTransactionJournaling(t, false) } 1750 func TestTransactionJournalingNoLocals(t *testing.T) { testTransactionJournaling(t, true) } 1751 1752 func testTransactionJournaling(t *testing.T, nolocals bool) { 1753 t.Parallel() 1754 1755 // Create a temporary file for the journal 1756 file, err := ioutil.TempFile("", "") 1757 if err != nil { 1758 t.Fatalf("failed to create temporary journal: %v", err) 1759 } 1760 journal := file.Name() 1761 defer os.Remove(journal) 1762 1763 // Clean up the temporary file, we only need the path for now 1764 file.Close() 1765 os.Remove(journal) 1766 1767 // Create the original pool to inject transaction into the journal 1768 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1769 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1770 1771 config := testTxPoolConfig 1772 config.NoLocals = nolocals 1773 config.Journal = journal 1774 config.Rejournal = time.Second 1775 1776 pool := NewTxPool(config, params.TestChainConfig, blockchain) 1777 1778 // Create two test accounts to ensure remotes expire but locals do not 1779 local, _ := crypto.GenerateKey() 1780 remote, _ := crypto.GenerateKey() 1781 1782 pool.currentState.AddBalance(crypto.PubkeyToAddress(local.PublicKey), big.NewInt(1000000000)) 1783 pool.currentState.AddBalance(crypto.PubkeyToAddress(remote.PublicKey), big.NewInt(1000000000)) 1784 1785 // Add three local and a remote transactions and ensure they are queued up 1786 if err := pool.AddLocal(pricedTransaction(0, 100000, big.NewInt(1), local)); err != nil { 1787 t.Fatalf("failed to add local transaction: %v", err) 1788 } 1789 if err := pool.AddLocal(pricedTransaction(1, 100000, big.NewInt(1), local)); err != nil { 1790 t.Fatalf("failed to add local transaction: %v", err) 1791 } 1792 if err := pool.AddLocal(pricedTransaction(2, 100000, big.NewInt(1), local)); err != nil { 1793 t.Fatalf("failed to add local transaction: %v", err) 1794 } 1795 if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(1), remote)); err != nil { 1796 t.Fatalf("failed to add remote transaction: %v", err) 1797 } 1798 pending, queued := pool.Stats() 1799 if pending != 4 { 1800 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 4) 1801 } 1802 if queued != 0 { 1803 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 1804 } 1805 if err := validateTxPoolInternals(pool); err != nil { 1806 t.Fatalf("pool internal state corrupted: %v", err) 1807 } 1808 // Terminate the old pool, bump the local nonce, create a new pool and ensure relevant transaction survive 1809 pool.Stop() 1810 statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 1) 1811 blockchain = &testBlockChain{statedb, 1000000, new(event.Feed)} 1812 1813 pool = NewTxPool(config, params.TestChainConfig, blockchain) 1814 1815 pending, queued = pool.Stats() 1816 if queued != 0 { 1817 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 1818 } 1819 if nolocals { 1820 if pending != 0 { 1821 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0) 1822 } 1823 } else { 1824 if pending != 2 { 1825 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2) 1826 } 1827 } 1828 if err := validateTxPoolInternals(pool); err != nil { 1829 t.Fatalf("pool internal state corrupted: %v", err) 1830 } 1831 // Bump the nonce temporarily and ensure the newly invalidated transaction is removed 1832 statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 2) 1833 <-pool.requestReset(nil, nil) 1834 time.Sleep(2 * config.Rejournal) 1835 pool.Stop() 1836 1837 statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 1) 1838 blockchain = &testBlockChain{statedb, 1000000, new(event.Feed)} 1839 pool = NewTxPool(config, params.TestChainConfig, blockchain) 1840 1841 pending, queued = pool.Stats() 1842 if pending != 0 { 1843 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0) 1844 } 1845 if nolocals { 1846 if queued != 0 { 1847 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0) 1848 } 1849 } else { 1850 if queued != 1 { 1851 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1) 1852 } 1853 } 1854 if err := validateTxPoolInternals(pool); err != nil { 1855 t.Fatalf("pool internal state corrupted: %v", err) 1856 } 1857 pool.Stop() 1858 } 1859 1860 // TestTransactionStatusCheck tests that the pool can correctly retrieve the 1861 // pending status of individual transactions. 1862 func TestTransactionStatusCheck(t *testing.T) { 1863 t.Parallel() 1864 1865 // Create the pool to test the status retrievals with 1866 statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 1867 blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} 1868 1869 pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) 1870 defer pool.Stop() 1871 1872 // Create the test accounts to check various transaction statuses with 1873 keys := make([]*ecdsa.PrivateKey, 3) 1874 for i := 0; i < len(keys); i++ { 1875 keys[i], _ = crypto.GenerateKey() 1876 pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000)) 1877 } 1878 // Generate and queue a batch of transactions, both pending and queued 1879 txs := types.Transactions{} 1880 1881 txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[0])) // Pending only 1882 txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[1])) // Pending and queued 1883 txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[1])) 1884 txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[2])) // Queued only 1885 1886 // Import the transaction and ensure they are correctly added 1887 pool.AddRemotesSync(txs) 1888 1889 pending, queued := pool.Stats() 1890 if pending != 2 { 1891 t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2) 1892 } 1893 if queued != 2 { 1894 t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2) 1895 } 1896 if err := validateTxPoolInternals(pool); err != nil { 1897 t.Fatalf("pool internal state corrupted: %v", err) 1898 } 1899 // Retrieve the status of each transaction and validate them 1900 hashes := make([]common.Hash, len(txs)) 1901 for i, tx := range txs { 1902 hashes[i] = tx.Hash() 1903 } 1904 hashes = append(hashes, common.Hash{}) 1905 1906 statuses := pool.Status(hashes) 1907 expect := []TxStatus{TxStatusPending, TxStatusPending, TxStatusQueued, TxStatusQueued, TxStatusUnknown} 1908 1909 for i := 0; i < len(statuses); i++ { 1910 if statuses[i] != expect[i] { 1911 t.Errorf("transaction %d: status mismatch: have %v, want %v", i, statuses[i], expect[i]) 1912 } 1913 } 1914 } 1915 1916 // Test the transaction slots consumption is computed correctly 1917 func TestTransactionSlotCount(t *testing.T) { 1918 t.Parallel() 1919 1920 key, _ := crypto.GenerateKey() 1921 1922 // Check that an empty transaction consumes a single slot 1923 smallTx := pricedDataTransaction(0, 0, big.NewInt(0), key, 0) 1924 if slots := numSlots(smallTx); slots != 1 { 1925 t.Fatalf("small transactions slot count mismatch: have %d want %d", slots, 1) 1926 } 1927 // Check that a large transaction consumes the correct number of slots 1928 bigTx := pricedDataTransaction(0, 0, big.NewInt(0), key, uint64(10*txSlotSize)) 1929 if slots := numSlots(bigTx); slots != 11 { 1930 t.Fatalf("big transactions slot count mismatch: have %d want %d", slots, 11) 1931 } 1932 } 1933 1934 // Benchmarks the speed of validating the contents of the pending queue of the 1935 // transaction pool. 1936 func BenchmarkPendingDemotion100(b *testing.B) { benchmarkPendingDemotion(b, 100) } 1937 func BenchmarkPendingDemotion1000(b *testing.B) { benchmarkPendingDemotion(b, 1000) } 1938 func BenchmarkPendingDemotion10000(b *testing.B) { benchmarkPendingDemotion(b, 10000) } 1939 1940 func benchmarkPendingDemotion(b *testing.B, size int) { 1941 // Add a batch of transactions to a pool one by one 1942 pool, key := setupTxPool() 1943 defer pool.Stop() 1944 1945 account := crypto.PubkeyToAddress(key.PublicKey) 1946 pool.currentState.AddBalance(account, big.NewInt(1000000)) 1947 1948 for i := 0; i < size; i++ { 1949 tx := transaction(uint64(i), 100000, key) 1950 pool.promoteTx(account, tx.Hash(), tx) 1951 } 1952 // Benchmark the speed of pool validation 1953 b.ResetTimer() 1954 for i := 0; i < b.N; i++ { 1955 pool.demoteUnexecutables() 1956 } 1957 } 1958 1959 // Benchmarks the speed of scheduling the contents of the future queue of the 1960 // transaction pool. 1961 func BenchmarkFuturePromotion100(b *testing.B) { benchmarkFuturePromotion(b, 100) } 1962 func BenchmarkFuturePromotion1000(b *testing.B) { benchmarkFuturePromotion(b, 1000) } 1963 func BenchmarkFuturePromotion10000(b *testing.B) { benchmarkFuturePromotion(b, 10000) } 1964 1965 func benchmarkFuturePromotion(b *testing.B, size int) { 1966 // Add a batch of transactions to a pool one by one 1967 pool, key := setupTxPool() 1968 defer pool.Stop() 1969 1970 account := crypto.PubkeyToAddress(key.PublicKey) 1971 pool.currentState.AddBalance(account, big.NewInt(1000000)) 1972 1973 for i := 0; i < size; i++ { 1974 tx := transaction(uint64(1+i), 100000, key) 1975 pool.enqueueTx(tx.Hash(), tx, false, true) 1976 } 1977 // Benchmark the speed of pool validation 1978 b.ResetTimer() 1979 for i := 0; i < b.N; i++ { 1980 pool.promoteExecutables(nil) 1981 } 1982 } 1983 1984 // Benchmarks the speed of batched transaction insertion. 1985 func BenchmarkPoolBatchInsert100(b *testing.B) { benchmarkPoolBatchInsert(b, 100, false) } 1986 func BenchmarkPoolBatchInsert1000(b *testing.B) { benchmarkPoolBatchInsert(b, 1000, false) } 1987 func BenchmarkPoolBatchInsert10000(b *testing.B) { benchmarkPoolBatchInsert(b, 10000, false) } 1988 1989 func BenchmarkPoolBatchLocalInsert100(b *testing.B) { benchmarkPoolBatchInsert(b, 100, true) } 1990 func BenchmarkPoolBatchLocalInsert1000(b *testing.B) { benchmarkPoolBatchInsert(b, 1000, true) } 1991 func BenchmarkPoolBatchLocalInsert10000(b *testing.B) { benchmarkPoolBatchInsert(b, 10000, true) } 1992 1993 func benchmarkPoolBatchInsert(b *testing.B, size int, local bool) { 1994 // Generate a batch of transactions to enqueue into the pool 1995 pool, key := setupTxPool() 1996 defer pool.Stop() 1997 1998 account := crypto.PubkeyToAddress(key.PublicKey) 1999 pool.currentState.AddBalance(account, big.NewInt(1000000)) 2000 2001 batches := make([]types.Transactions, b.N) 2002 for i := 0; i < b.N; i++ { 2003 batches[i] = make(types.Transactions, size) 2004 for j := 0; j < size; j++ { 2005 batches[i][j] = transaction(uint64(size*i+j), 100000, key) 2006 } 2007 } 2008 // Benchmark importing the transactions into the queue 2009 b.ResetTimer() 2010 for _, batch := range batches { 2011 if local { 2012 pool.AddLocals(batch) 2013 } else { 2014 pool.AddRemotes(batch) 2015 } 2016 } 2017 } 2018 2019 func BenchmarkInsertRemoteWithAllLocals(b *testing.B) { 2020 // Allocate keys for testing 2021 key, _ := crypto.GenerateKey() 2022 account := crypto.PubkeyToAddress(key.PublicKey) 2023 2024 remoteKey, _ := crypto.GenerateKey() 2025 remoteAddr := crypto.PubkeyToAddress(remoteKey.PublicKey) 2026 2027 locals := make([]*types.Transaction, 4096+1024) // Occupy all slots 2028 for i := 0; i < len(locals); i++ { 2029 locals[i] = transaction(uint64(i), 100000, key) 2030 } 2031 remotes := make([]*types.Transaction, 1000) 2032 for i := 0; i < len(remotes); i++ { 2033 remotes[i] = pricedTransaction(uint64(i), 100000, big.NewInt(2), remoteKey) // Higher gasprice 2034 } 2035 // Benchmark importing the transactions into the queue 2036 b.ResetTimer() 2037 for i := 0; i < b.N; i++ { 2038 b.StopTimer() 2039 pool, _ := setupTxPool() 2040 pool.currentState.AddBalance(account, big.NewInt(100000000)) 2041 for _, local := range locals { 2042 pool.AddLocal(local) 2043 } 2044 b.StartTimer() 2045 // Assign a high enough balance for testing 2046 pool.currentState.AddBalance(remoteAddr, big.NewInt(100000000)) 2047 for i := 0; i < len(remotes); i++ { 2048 pool.AddRemotes([]*types.Transaction{remotes[i]}) 2049 } 2050 pool.Stop() 2051 } 2052 }