transaction_pool_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 }), 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 != ErrImpossibleNonce {
  49. t.Error("expected", ErrImpossibleNonce)
  50. }
  51. }