transaction_pool_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. pool.currentState().AddBalance(from, big.NewInt(100*100))
  38. err = pool.Add(tx)
  39. if err != ErrIntrinsicGas {
  40. t.Error("expected", ErrIntrinsicGas)
  41. }
  42. pool.currentState().SetNonce(from, 1)
  43. pool.currentState().AddBalance(from, big.NewInt(0xffffffffffffff))
  44. tx.GasLimit = big.NewInt(100000)
  45. tx.Price = big.NewInt(1)
  46. tx.SignECDSA(key)
  47. err = pool.Add(tx)
  48. if err != ErrNonce {
  49. t.Error("expected", ErrNonce)
  50. }
  51. }
  52. func TestTransactionQueue(t *testing.T) {
  53. pool, key := setupTxPool()
  54. tx := transaction()
  55. tx.SignECDSA(key)
  56. from, _ := tx.From()
  57. pool.currentState().AddBalance(from, big.NewInt(1))
  58. pool.queueTx(tx)
  59. pool.checkQueue()
  60. if len(pool.txs) != 1 {
  61. t.Error("expected valid txs to be 1 is", len(pool.txs))
  62. }
  63. tx = transaction()
  64. tx.SignECDSA(key)
  65. from, _ = tx.From()
  66. pool.currentState().SetNonce(from, 10)
  67. tx.SetNonce(1)
  68. pool.queueTx(tx)
  69. pool.checkQueue()
  70. if _, ok := pool.txs[tx.Hash()]; ok {
  71. t.Error("expected transaction to be in tx pool")
  72. }
  73. if len(pool.queue[from]) != 0 {
  74. t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
  75. }
  76. pool, key = setupTxPool()
  77. tx1, tx2, tx3 := transaction(), transaction(), transaction()
  78. tx2.SetNonce(10)
  79. tx3.SetNonce(11)
  80. tx1.SignECDSA(key)
  81. tx2.SignECDSA(key)
  82. tx3.SignECDSA(key)
  83. pool.queueTx(tx1)
  84. pool.queueTx(tx2)
  85. pool.queueTx(tx3)
  86. from, _ = tx1.From()
  87. pool.checkQueue()
  88. if len(pool.txs) != 1 {
  89. t.Error("expected tx pool to be 1 =")
  90. }
  91. if len(pool.queue[from]) != 3 {
  92. t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
  93. }
  94. }