plugeth/core/transaction_pool_test.go

100 lines
2.0 KiB
Go
Raw Normal View History

package core
import (
"crypto/ecdsa"
2015-04-04 19:41:24 +00:00
"math/big"
"testing"
2015-03-18 12:38:47 +00:00
"github.com/ethereum/go-ethereum/common"
2015-04-04 19:41:24 +00:00
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
2015-01-07 12:17:48 +00:00
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
)
// State query interface
2015-03-16 10:27:38 +00:00
type stateQuery struct{ db common.Database }
2015-01-07 12:17:48 +00:00
func SQ() stateQuery {
db, _ := ethdb.NewMemDatabase()
return stateQuery{db: db}
}
func (self stateQuery) GetAccount(addr []byte) *state.StateObject {
2015-03-18 12:38:47 +00:00
return state.NewStateObject(common.BytesToAddress(addr), self.db)
}
func transaction() *types.Transaction {
2015-03-18 12:38:47 +00:00
return types.NewTransactionMessage(common.Address{}, common.Big0, common.Big0, common.Big0, nil)
}
func setup() (*TxPool, *ecdsa.PrivateKey) {
var m event.TypeMux
key, _ := crypto.GenerateKey()
return NewTxPool(&m), key
}
func TestTxAdding(t *testing.T) {
pool, key := setup()
tx1 := transaction()
tx1.SignECDSA(key)
err := pool.Add(tx1)
if err != nil {
t.Error(err)
}
err = pool.Add(tx1)
if err == nil {
t.Error("added tx twice")
}
}
func TestAddInvalidTx(t *testing.T) {
pool, _ := setup()
tx1 := transaction()
err := pool.Add(tx1)
if err == nil {
t.Error("expected error")
}
}
func TestRemoveSet(t *testing.T) {
pool, _ := setup()
tx1 := transaction()
2015-01-06 12:18:09 +00:00
pool.addTx(tx1)
pool.RemoveSet(types.Transactions{tx1})
if pool.Size() > 0 {
t.Error("expected pool size to be 0")
}
}
func TestRemoveInvalid(t *testing.T) {
2015-01-02 11:18:23 +00:00
pool, key := setup()
tx1 := transaction()
2015-01-06 12:18:09 +00:00
pool.addTx(tx1)
2015-01-07 12:17:48 +00:00
pool.RemoveInvalid(SQ())
if pool.Size() > 0 {
t.Error("expected pool size to be 0")
}
2015-01-02 11:18:23 +00:00
tx1.SetNonce(1)
tx1.SignECDSA(key)
2015-01-06 12:18:09 +00:00
pool.addTx(tx1)
2015-01-07 12:17:48 +00:00
pool.RemoveInvalid(SQ())
2015-01-02 11:18:23 +00:00
if pool.Size() != 1 {
t.Error("expected pool size to be 1, is", pool.Size())
}
}
func TestInvalidSender(t *testing.T) {
pool, _ := setup()
2015-04-04 19:41:24 +00:00
tx := new(types.Transaction)
tx.R = new(big.Int)
tx.S = new(big.Int)
err := pool.ValidateTransaction(tx)
if err != ErrInvalidSender {
2015-03-18 12:38:47 +00:00
t.Errorf("expected %v, got %v", ErrInvalidSender, err)
}
}