Tests that the pool rejects replacement transactions that don't meet the minimum price bump required.
(t *testing.T)
| 1494 | // Tests that the pool rejects replacement transactions that don't meet the minimum |
| 1495 | // price bump required. |
| 1496 | func TestTransactionReplacement(t *testing.T) { |
| 1497 | t.Parallel() |
| 1498 | |
| 1499 | // Create the pool to test the pricing enforcement with |
| 1500 | statedb, _ := state.New(common.Hash{}, state.NewDatabase(database.NewMemDatabase())) |
| 1501 | blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)} |
| 1502 | |
| 1503 | pool := NewTxPool(testTxPoolConfig, configs.TestChainConfig, blockchain) |
| 1504 | defer pool.Stop() |
| 1505 | |
| 1506 | // Keep track of transaction events to ensure all executables get announced |
| 1507 | events := make(chan NewTxsEvent, 32) |
| 1508 | sub := pool.txFeed.Subscribe(events) |
| 1509 | defer sub.Unsubscribe() |
| 1510 | |
| 1511 | // Create a test account to add transactions with |
| 1512 | key, _ := crypto.GenerateKey() |
| 1513 | pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000)) |
| 1514 | |
| 1515 | // Add pending transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too) |
| 1516 | price := int64(100) |
| 1517 | threshold := (price * (100 + int64(testTxPoolConfig.PriceBump))) / 100 |
| 1518 | |
| 1519 | if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(1), key)); err != nil { |
| 1520 | t.Fatalf("failed to add original cheap pending transaction: %v", err) |
| 1521 | } |
| 1522 | if err := pool.AddRemote(pricedTransaction(0, 100001, big.NewInt(1), key)); err != ErrReplaceUnderpriced { |
| 1523 | t.Fatalf("original cheap pending transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced) |
| 1524 | } |
| 1525 | if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(2), key)); err != nil { |
| 1526 | t.Fatalf("failed to replace original cheap pending transaction: %v", err) |
| 1527 | } |
| 1528 | if err := validateEvents(events, 2); err != nil { |
| 1529 | t.Fatalf("cheap replacement event firing failed: %v", err) |
| 1530 | } |
| 1531 | |
| 1532 | if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(price), key)); err != nil { |
| 1533 | t.Fatalf("failed to add original proper pending transaction: %v", err) |
| 1534 | } |
| 1535 | if err := pool.AddRemote(pricedTransaction(0, 100001, big.NewInt(threshold-1), key)); err != ErrReplaceUnderpriced { |
| 1536 | t.Fatalf("original proper pending transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced) |
| 1537 | } |
| 1538 | if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(threshold), key)); err != nil { |
| 1539 | t.Fatalf("failed to replace original proper pending transaction: %v", err) |
| 1540 | } |
| 1541 | if err := validateEvents(events, 2); err != nil { |
| 1542 | t.Fatalf("proper replacement event firing failed: %v", err) |
| 1543 | } |
| 1544 | // Add queued transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too) |
| 1545 | if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(1), key)); err != nil { |
| 1546 | t.Fatalf("failed to add original cheap queued transaction: %v", err) |
| 1547 | } |
| 1548 | if err := pool.AddRemote(pricedTransaction(2, 100001, big.NewInt(1), key)); err != ErrReplaceUnderpriced { |
| 1549 | t.Fatalf("original cheap queued transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced) |
| 1550 | } |
| 1551 | if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(2), key)); err != nil { |
| 1552 | t.Fatalf("failed to replace original cheap queued transaction: %v", err) |
| 1553 | } |
nothing calls this directly
no test coverage detected