transaction_pool_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package core
  2. import (
  3. "crypto/ecdsa"
  4. "math/big"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/core/state"
  8. "github.com/ethereum/go-ethereum/core/types"
  9. "github.com/ethereum/go-ethereum/crypto"
  10. "github.com/ethereum/go-ethereum/ethdb"
  11. "github.com/ethereum/go-ethereum/event"
  12. )
  13. func transaction() *types.Transaction {
  14. return types.NewTransactionMessage(common.Address{}, big.NewInt(100), big.NewInt(100), big.NewInt(100), nil)
  15. }
  16. func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
  17. db, _ := ethdb.NewMemDatabase()
  18. statedb := state.New(common.Hash{}, db)
  19. var m event.TypeMux
  20. key, _ := crypto.GenerateKey()
  21. return NewTxPool(&m, func() *state.StateDB { return statedb }, func() *big.Int { return big.NewInt(1000000) }), key
  22. }
  23. func TestInvalidTransactions(t *testing.T) {
  24. pool, key := setupTxPool()
  25. tx := transaction()
  26. tx.SignECDSA(key)
  27. err := pool.Add(tx)
  28. if err != ErrNonExistentAccount {
  29. t.Error("expected", ErrNonExistentAccount)
  30. }
  31. from, _ := tx.From()
  32. pool.currentState().AddBalance(from, big.NewInt(1))
  33. err = pool.Add(tx)
  34. if err != ErrInsufficientFunds {
  35. t.Error("expected", ErrInsufficientFunds)
  36. }
  37. balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
  38. pool.currentState().AddBalance(from, balance)
  39. err = pool.Add(tx)
  40. if err != ErrIntrinsicGas {
  41. t.Error("expected", ErrIntrinsicGas, "got", err)
  42. }
  43. pool.currentState().SetNonce(from, 1)
  44. pool.currentState().AddBalance(from, big.NewInt(0xffffffffffffff))
  45. tx.GasLimit = big.NewInt(100000)
  46. tx.Price = big.NewInt(1)
  47. tx.SignECDSA(key)
  48. err = pool.Add(tx)
  49. if err != ErrNonce {
  50. t.Error("expected", ErrNonce)
  51. }
  52. }
  53. func TestTransactionQueue(t *testing.T) {
  54. pool, key := setupTxPool()
  55. tx := transaction()
  56. tx.SignECDSA(key)
  57. from, _ := tx.From()
  58. pool.currentState().AddBalance(from, big.NewInt(1))
  59. pool.queueTx(tx)
  60. pool.checkQueue()
  61. if len(pool.txs) != 1 {
  62. t.Error("expected valid txs to be 1 is", len(pool.txs))
  63. }
  64. tx = transaction()
  65. tx.SignECDSA(key)
  66. from, _ = tx.From()
  67. pool.currentState().SetNonce(from, 10)
  68. tx.SetNonce(1)
  69. pool.queueTx(tx)
  70. pool.checkQueue()
  71. if _, ok := pool.txs[tx.Hash()]; ok {
  72. t.Error("expected transaction to be in tx pool")
  73. }
  74. if len(pool.queue[from]) != 0 {
  75. t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
  76. }
  77. pool, key = setupTxPool()
  78. tx1, tx2, tx3 := transaction(), transaction(), transaction()
  79. tx2.SetNonce(10)
  80. tx3.SetNonce(11)
  81. tx1.SignECDSA(key)
  82. tx2.SignECDSA(key)
  83. tx3.SignECDSA(key)
  84. pool.queueTx(tx1)
  85. pool.queueTx(tx2)
  86. pool.queueTx(tx3)
  87. from, _ = tx1.From()
  88. pool.checkQueue()
  89. if len(pool.txs) != 1 {
  90. t.Error("expected tx pool to be 1 =")
  91. }
  92. if len(pool.queue[from]) != 3 {
  93. t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
  94. }
  95. }