|
@@ -13,8 +13,9 @@ import (
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-func transaction() *types.Transaction {
|
|
|
|
|
- return types.NewTransactionMessage(common.Address{}, big.NewInt(100), big.NewInt(100), big.NewInt(100), nil)
|
|
|
|
|
|
|
+func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
|
|
|
|
|
+ tx, _ := types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, big.NewInt(1), nil).SignECDSA(key)
|
|
|
|
|
+ return tx
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
|
|
func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
|
|
@@ -29,43 +30,34 @@ func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
|
|
|
func TestInvalidTransactions(t *testing.T) {
|
|
func TestInvalidTransactions(t *testing.T) {
|
|
|
pool, key := setupTxPool()
|
|
pool, key := setupTxPool()
|
|
|
|
|
|
|
|
- tx := transaction()
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
- err := pool.Add(tx)
|
|
|
|
|
- if err != ErrNonExistentAccount {
|
|
|
|
|
|
|
+ tx := transaction(0, big.NewInt(100), key)
|
|
|
|
|
+ if err := pool.Add(tx); err != ErrNonExistentAccount {
|
|
|
t.Error("expected", ErrNonExistentAccount)
|
|
t.Error("expected", ErrNonExistentAccount)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
from, _ := tx.From()
|
|
from, _ := tx.From()
|
|
|
pool.currentState().AddBalance(from, big.NewInt(1))
|
|
pool.currentState().AddBalance(from, big.NewInt(1))
|
|
|
- err = pool.Add(tx)
|
|
|
|
|
- if err != ErrInsufficientFunds {
|
|
|
|
|
|
|
+ if err := pool.Add(tx); err != ErrInsufficientFunds {
|
|
|
t.Error("expected", ErrInsufficientFunds)
|
|
t.Error("expected", ErrInsufficientFunds)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
|
|
balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
|
|
|
pool.currentState().AddBalance(from, balance)
|
|
pool.currentState().AddBalance(from, balance)
|
|
|
- err = pool.Add(tx)
|
|
|
|
|
- if err != ErrIntrinsicGas {
|
|
|
|
|
|
|
+ if err := pool.Add(tx); err != ErrIntrinsicGas {
|
|
|
t.Error("expected", ErrIntrinsicGas, "got", err)
|
|
t.Error("expected", ErrIntrinsicGas, "got", err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pool.currentState().SetNonce(from, 1)
|
|
pool.currentState().SetNonce(from, 1)
|
|
|
pool.currentState().AddBalance(from, big.NewInt(0xffffffffffffff))
|
|
pool.currentState().AddBalance(from, big.NewInt(0xffffffffffffff))
|
|
|
- tx.GasLimit = big.NewInt(100000)
|
|
|
|
|
- tx.Price = big.NewInt(1)
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
-
|
|
|
|
|
- err = pool.Add(tx)
|
|
|
|
|
- if err != ErrNonce {
|
|
|
|
|
|
|
+ tx = transaction(0, big.NewInt(100000), key)
|
|
|
|
|
+ if err := pool.Add(tx); err != ErrNonce {
|
|
|
t.Error("expected", ErrNonce)
|
|
t.Error("expected", ErrNonce)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionQueue(t *testing.T) {
|
|
func TestTransactionQueue(t *testing.T) {
|
|
|
pool, key := setupTxPool()
|
|
pool, key := setupTxPool()
|
|
|
- tx := transaction()
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
|
|
+ tx := transaction(0, big.NewInt(100), key)
|
|
|
from, _ := tx.From()
|
|
from, _ := tx.From()
|
|
|
pool.currentState().AddBalance(from, big.NewInt(1))
|
|
pool.currentState().AddBalance(from, big.NewInt(1))
|
|
|
pool.queueTx(tx.Hash(), tx)
|
|
pool.queueTx(tx.Hash(), tx)
|
|
@@ -75,9 +67,7 @@ func TestTransactionQueue(t *testing.T) {
|
|
|
t.Error("expected valid txs to be 1 is", len(pool.pending))
|
|
t.Error("expected valid txs to be 1 is", len(pool.pending))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- tx = transaction()
|
|
|
|
|
- tx.SetNonce(1)
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
|
|
+ tx = transaction(1, big.NewInt(100), key)
|
|
|
from, _ = tx.From()
|
|
from, _ = tx.From()
|
|
|
pool.currentState().SetNonce(from, 2)
|
|
pool.currentState().SetNonce(from, 2)
|
|
|
pool.queueTx(tx.Hash(), tx)
|
|
pool.queueTx(tx.Hash(), tx)
|
|
@@ -91,12 +81,9 @@ func TestTransactionQueue(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pool, key = setupTxPool()
|
|
pool, key = setupTxPool()
|
|
|
- tx1, tx2, tx3 := transaction(), transaction(), transaction()
|
|
|
|
|
- tx2.SetNonce(10)
|
|
|
|
|
- tx3.SetNonce(11)
|
|
|
|
|
- tx1.SignECDSA(key)
|
|
|
|
|
- tx2.SignECDSA(key)
|
|
|
|
|
- tx3.SignECDSA(key)
|
|
|
|
|
|
|
+ tx1 := transaction(0, big.NewInt(100), key)
|
|
|
|
|
+ tx2 := transaction(10, big.NewInt(100), key)
|
|
|
|
|
+ tx3 := transaction(11, big.NewInt(100), key)
|
|
|
pool.queueTx(tx1.Hash(), tx1)
|
|
pool.queueTx(tx1.Hash(), tx1)
|
|
|
pool.queueTx(tx2.Hash(), tx2)
|
|
pool.queueTx(tx2.Hash(), tx2)
|
|
|
pool.queueTx(tx3.Hash(), tx3)
|
|
pool.queueTx(tx3.Hash(), tx3)
|
|
@@ -114,8 +101,7 @@ func TestTransactionQueue(t *testing.T) {
|
|
|
|
|
|
|
|
func TestRemoveTx(t *testing.T) {
|
|
func TestRemoveTx(t *testing.T) {
|
|
|
pool, key := setupTxPool()
|
|
pool, key := setupTxPool()
|
|
|
- tx := transaction()
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
|
|
+ tx := transaction(0, big.NewInt(100), key)
|
|
|
from, _ := tx.From()
|
|
from, _ := tx.From()
|
|
|
pool.currentState().AddBalance(from, big.NewInt(1))
|
|
pool.currentState().AddBalance(from, big.NewInt(1))
|
|
|
pool.queueTx(tx.Hash(), tx)
|
|
pool.queueTx(tx.Hash(), tx)
|
|
@@ -142,13 +128,10 @@ func TestRemoveTx(t *testing.T) {
|
|
|
func TestNegativeValue(t *testing.T) {
|
|
func TestNegativeValue(t *testing.T) {
|
|
|
pool, key := setupTxPool()
|
|
pool, key := setupTxPool()
|
|
|
|
|
|
|
|
- tx := transaction()
|
|
|
|
|
- tx.Value().Set(big.NewInt(-1))
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
|
|
+ tx, _ := types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil).SignECDSA(key)
|
|
|
from, _ := tx.From()
|
|
from, _ := tx.From()
|
|
|
pool.currentState().AddBalance(from, big.NewInt(1))
|
|
pool.currentState().AddBalance(from, big.NewInt(1))
|
|
|
- err := pool.Add(tx)
|
|
|
|
|
- if err != ErrNegativeValue {
|
|
|
|
|
|
|
+ if err := pool.Add(tx); err != ErrNegativeValue {
|
|
|
t.Error("expected", ErrNegativeValue, "got", err)
|
|
t.Error("expected", ErrNegativeValue, "got", err)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -165,20 +148,15 @@ func TestTransactionChainFork(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
resetState()
|
|
resetState()
|
|
|
|
|
|
|
|
- tx := transaction()
|
|
|
|
|
- tx.GasLimit = big.NewInt(100000)
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
-
|
|
|
|
|
- err := pool.add(tx)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ tx := transaction(0, big.NewInt(100000), key)
|
|
|
|
|
+ if err := pool.add(tx); err != nil {
|
|
|
t.Error("didn't expect error", err)
|
|
t.Error("didn't expect error", err)
|
|
|
}
|
|
}
|
|
|
pool.RemoveTransactions([]*types.Transaction{tx})
|
|
pool.RemoveTransactions([]*types.Transaction{tx})
|
|
|
|
|
|
|
|
// reset the pool's internal state
|
|
// reset the pool's internal state
|
|
|
resetState()
|
|
resetState()
|
|
|
- err = pool.add(tx)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err := pool.add(tx); err != nil {
|
|
|
t.Error("didn't expect error", err)
|
|
t.Error("didn't expect error", err)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -195,24 +173,14 @@ func TestTransactionDoubleNonce(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
resetState()
|
|
resetState()
|
|
|
|
|
|
|
|
- tx := transaction()
|
|
|
|
|
- tx.GasLimit = big.NewInt(100000)
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
-
|
|
|
|
|
- err := pool.add(tx)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ tx := transaction(0, big.NewInt(100000), key)
|
|
|
|
|
+ tx2 := transaction(0, big.NewInt(1000000), key)
|
|
|
|
|
+ if err := pool.add(tx); err != nil {
|
|
|
t.Error("didn't expect error", err)
|
|
t.Error("didn't expect error", err)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- tx2 := transaction()
|
|
|
|
|
- tx2.GasLimit = big.NewInt(1000000)
|
|
|
|
|
- tx2.SignECDSA(key)
|
|
|
|
|
-
|
|
|
|
|
- err = pool.add(tx2)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ if err := pool.add(tx2); err != nil {
|
|
|
t.Error("didn't expect error", err)
|
|
t.Error("didn't expect error", err)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
if len(pool.pending) != 2 {
|
|
if len(pool.pending) != 2 {
|
|
|
t.Error("expected 2 pending txs. Got", len(pool.pending))
|
|
t.Error("expected 2 pending txs. Got", len(pool.pending))
|
|
|
}
|
|
}
|
|
@@ -222,20 +190,13 @@ func TestMissingNonce(t *testing.T) {
|
|
|
pool, key := setupTxPool()
|
|
pool, key := setupTxPool()
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
|
|
pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
|
|
|
- tx := transaction()
|
|
|
|
|
- tx.AccountNonce = 1
|
|
|
|
|
- tx.GasLimit = big.NewInt(100000)
|
|
|
|
|
- tx.SignECDSA(key)
|
|
|
|
|
-
|
|
|
|
|
- err := pool.add(tx)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ tx := transaction(1, big.NewInt(100000), key)
|
|
|
|
|
+ if err := pool.add(tx); err != nil {
|
|
|
t.Error("didn't expect error", err)
|
|
t.Error("didn't expect error", err)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
if len(pool.pending) != 0 {
|
|
if len(pool.pending) != 0 {
|
|
|
t.Error("expected 0 pending transactions, got", len(pool.pending))
|
|
t.Error("expected 0 pending transactions, got", len(pool.pending))
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
if len(pool.queue[addr]) != 1 {
|
|
if len(pool.queue[addr]) != 1 {
|
|
|
t.Error("expected 1 queued transaction, got", len(pool.queue[addr]))
|
|
t.Error("expected 1 queued transaction, got", len(pool.queue[addr]))
|
|
|
}
|
|
}
|