transaction_pool_test.go 2.0 KB

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