-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdummy_tx_pool.go
More file actions
177 lines (151 loc) · 3.85 KB
/
Copy pathdummy_tx_pool.go
File metadata and controls
177 lines (151 loc) · 3.85 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gossip
import (
"math/big"
"math/rand"
"sort"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
notify "github.com/ethereum/go-ethereum/event"
"github.com/Fantom-foundation/go-opera/evmcore"
)
// dummyTxPool is a fake, helper transaction pool for testing purposes
type dummyTxPool struct {
txFeed notify.Feed
pool []*types.Transaction // Collection of all transactions
added chan<- []*types.Transaction // Notification channel for new transactions
signer types.Signer
lock sync.RWMutex // Protects the transaction pool
}
// AddRemotes appends a batch of transactions to the pool, and notifies any
// listeners if the addition channel is non nil
func (p *dummyTxPool) AddRemotes(txs []*types.Transaction) []error {
p.lock.Lock()
defer p.lock.Unlock()
p.pool = append(p.pool, txs...)
if p.added != nil {
p.added <- txs
}
return make([]error, len(txs))
}
func (p *dummyTxPool) AddLocals(txs []*types.Transaction) []error {
return p.AddRemotes(txs)
}
func (p *dummyTxPool) AddLocal(tx *types.Transaction) error {
return p.AddLocals([]*types.Transaction{tx})[0]
}
func (p *dummyTxPool) Nonce(addr common.Address) uint64 {
return 0
}
func (p *dummyTxPool) Stats() (int, int) {
return p.Count(), 0
}
func (p *dummyTxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
return nil, nil
}
func (p *dummyTxPool) ContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
return nil, nil
}
// Pending returns all the transactions known to the pool
func (p *dummyTxPool) Pending(enforceTips bool) (map[common.Address]types.Transactions, error) {
p.lock.RLock()
defer p.lock.RUnlock()
batches := make(map[common.Address]types.Transactions)
for _, tx := range p.pool {
from, _ := types.Sender(p.signer, tx)
batches[from] = append(batches[from], tx)
}
for _, batch := range batches {
sort.Sort(types.TxByNonce(batch))
}
return batches, nil
}
func (p *dummyTxPool) GasPrice() *big.Int {
return big.NewInt(0)
}
func (p *dummyTxPool) SubscribeNewTxsNotify(ch chan<- evmcore.NewTxsNotify) notify.Subscription {
return p.txFeed.Subscribe(ch)
}
func (p *dummyTxPool) Map() map[common.Hash]*types.Transaction {
p.lock.RLock()
defer p.lock.RUnlock()
res := make(map[common.Hash]*types.Transaction, len(p.pool))
for _, tx := range p.pool {
res[tx.Hash()] = tx
}
return nil
}
func (p *dummyTxPool) Get(txid common.Hash) *types.Transaction {
p.lock.RLock()
defer p.lock.RUnlock()
for _, tx := range p.pool {
if tx.Hash() == txid {
return tx
}
}
return nil
}
func (p *dummyTxPool) Has(txid common.Hash) bool {
p.lock.RLock()
defer p.lock.RUnlock()
for _, tx := range p.pool {
if tx.Hash() == txid {
return true
}
}
return false
}
func (p *dummyTxPool) OnlyNotExisting(txids []common.Hash) []common.Hash {
m := p.Map()
notExisting := make([]common.Hash, 0, len(txids))
for _, txid := range txids {
if m[txid] == nil {
notExisting = append(notExisting, txid)
}
}
return notExisting
}
func (p *dummyTxPool) SampleHashes(max int) []common.Hash {
p.lock.RLock()
defer p.lock.RUnlock()
res := make([]common.Hash, 0, max)
skip := 0
if len(p.pool) > max {
skip = rand.Intn(len(p.pool) - max)
}
for _, tx := range p.pool {
if len(res) >= max {
break
}
if skip > 0 {
skip--
continue
}
res = append(res, tx.Hash())
}
return res
}
func (p *dummyTxPool) Count() int {
return len(p.pool)
}
func (p *dummyTxPool) Clear() {
p.lock.Lock()
defer p.lock.Unlock()
if len(p.pool) != 0 {
p.pool = p.pool[:0]
}
}
func (p *dummyTxPool) Delete(needle common.Hash) {
p.lock.Lock()
defer p.lock.Unlock()
if len(p.pool) == 0 {
return
}
notErased := make([]*types.Transaction, 0, len(p.pool)-1)
for _, tx := range p.pool {
if tx.Hash() != needle {
notErased = append(notErased, tx)
}
}
p.pool = notErased
}