aboutsummaryrefslogtreecommitdiffstats
path: root/core/transaction_pool_test.go
blob: 418cb041547ad633ebbf0a965973069cc76b4d1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package core

import (
    "crypto/ecdsa"
    "testing"

    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/ethdb"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/event"
    "github.com/ethereum/go-ethereum/state"
)

// State query interface
type stateQuery struct{ db common.Database }

func SQ() stateQuery {
    db, _ := ethdb.NewMemDatabase()
    return stateQuery{db: db}
}

func (self stateQuery) GetAccount(addr []byte) *state.StateObject {
    return state.NewStateObject(addr, self.db)
}

func transaction() *types.Transaction {
    return types.NewTransactionMessage(make([]byte, 20), 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()
    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) {
    pool, key := setup()
    tx1 := transaction()
    pool.addTx(tx1)
    pool.RemoveInvalid(SQ())
    if pool.Size() > 0 {
        t.Error("expected pool size to be 0")
    }

    tx1.SetNonce(1)
    tx1.SignECDSA(key)
    pool.addTx(tx1)
    pool.RemoveInvalid(SQ())
    if pool.Size() != 1 {
        t.Error("expected pool size to be 1, is", pool.Size())
    }
}

func TestInvalidSender(t *testing.T) {
    pool, _ := setup()
    tx := new(types.Transaction)
    tx.V = 28
    err := pool.ValidateTransaction(tx)
    if err != ErrInvalidSender {
        t.Error("expected %v, got %v", ErrInvalidSender, err)
    }
}