transaction_pool_test.go 1.6 KB

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