transaction_pool_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // State query interface
  14. type stateQuery struct{ db common.Database }
  15. func SQ() stateQuery {
  16. db, _ := ethdb.NewMemDatabase()
  17. return stateQuery{db: db}
  18. }
  19. func (self stateQuery) GetAccount(addr []byte) *state.StateObject {
  20. return state.NewStateObject(common.BytesToAddress(addr), self.db)
  21. }
  22. func transaction() *types.Transaction {
  23. return types.NewTransactionMessage(common.Address{}, common.Big0, common.Big0, common.Big0, nil)
  24. }
  25. func setup() (*TxPool, *ecdsa.PrivateKey) {
  26. var m event.TypeMux
  27. key, _ := crypto.GenerateKey()
  28. return NewTxPool(&m), key
  29. }
  30. func TestTxAdding(t *testing.T) {
  31. pool, key := setup()
  32. tx1 := transaction()
  33. tx1.SignECDSA(key)
  34. err := pool.Add(tx1)
  35. if err != nil {
  36. t.Error(err)
  37. }
  38. err = pool.Add(tx1)
  39. if err == nil {
  40. t.Error("added tx twice")
  41. }
  42. }
  43. func TestAddInvalidTx(t *testing.T) {
  44. pool, _ := setup()
  45. tx1 := transaction()
  46. err := pool.Add(tx1)
  47. if err == nil {
  48. t.Error("expected error")
  49. }
  50. }
  51. func TestRemoveSet(t *testing.T) {
  52. pool, _ := setup()
  53. tx1 := transaction()
  54. pool.addTx(tx1)
  55. pool.RemoveSet(types.Transactions{tx1})
  56. if pool.Size() > 0 {
  57. t.Error("expected pool size to be 0")
  58. }
  59. }
  60. func TestRemoveInvalid(t *testing.T) {
  61. pool, key := setup()
  62. tx1 := transaction()
  63. pool.addTx(tx1)
  64. pool.RemoveInvalid(SQ())
  65. if pool.Size() > 0 {
  66. t.Error("expected pool size to be 0")
  67. }
  68. tx1.SetNonce(1)
  69. tx1.SignECDSA(key)
  70. pool.addTx(tx1)
  71. pool.RemoveInvalid(SQ())
  72. if pool.Size() != 1 {
  73. t.Error("expected pool size to be 1, is", pool.Size())
  74. }
  75. }
  76. func TestInvalidSender(t *testing.T) {
  77. pool, _ := setup()
  78. tx := new(types.Transaction)
  79. tx.R = new(big.Int)
  80. tx.S = new(big.Int)
  81. err := pool.ValidateTransaction(tx)
  82. if err != ErrInvalidSender {
  83. t.Errorf("expected %v, got %v", ErrInvalidSender, err)
  84. }
  85. }