MCPcopy Create free account
hub / github.com/CPChain/chain / TestTransactionReplacement

Function TestTransactionReplacement

core/tx_pool_test.go:1496–1571  ·  view source on GitHub ↗

Tests that the pool rejects replacement transactions that don't meet the minimum price bump required.

(t *testing.T)

Source from the content-addressed store, hash-verified

1494// Tests that the pool rejects replacement transactions that don't meet the minimum
1495// price bump required.
1496func 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 }

Callers

nothing calls this directly

Calls 9

StopMethod · 0.95
AddRemoteMethod · 0.95
NewTxPoolFunction · 0.85
pricedTransactionFunction · 0.85
validateEventsFunction · 0.85
validateTxPoolInternalsFunction · 0.85
SubscribeMethod · 0.65
UnsubscribeMethod · 0.65
AddBalanceMethod · 0.65

Tested by

no test coverage detected