Skip to content

Commit 09147a5

Browse files
committed
Improved tx pool to ignore invalid transactions
Transaction pool will attempt to ignore invalid transactions it had previously encountered.
1 parent d09d2b9 commit 09147a5

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎core/transaction_pool.go‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/ethereum/go-ethereum/core/types"
1010
"github.com/ethereum/go-ethereum/event"
1111
"github.com/ethereum/go-ethereum/logger"
12+
"gopkg.in/fatih/set.v0"
1213
)
1314

1415
var (
@@ -42,7 +43,8 @@ type TxPool struct {
4243
quit chan bool
4344
// The actual pool
4445
//pool *list.List
45-
txs map[common.Hash]*types.Transaction
46+
txs map[common.Hash]*types.Transaction
47+
invalidHashes *set.Set
4648

4749
SecondaryProcessor TxProcessor
4850

@@ -53,10 +55,11 @@ type TxPool struct {
5355

5456
func NewTxPool(eventMux *event.TypeMux) *TxPool {
5557
return &TxPool{
56-
txs: make(map[common.Hash]*types.Transaction),
57-
queueChan: make(chan *types.Transaction, txPoolQueueSize),
58-
quit: make(chan bool),
59-
eventMux: eventMux,
58+
txs: make(map[common.Hash]*types.Transaction),
59+
queueChan: make(chan *types.Transaction, txPoolQueueSize),
60+
quit: make(chan bool),
61+
eventMux: eventMux,
62+
invalidHashes: set.New(),
6063
}
6164
}
6265

@@ -87,20 +90,24 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
8790
}
8891

8992
func (self *TxPool) addTx(tx *types.Transaction) {
90-
self.txs[tx.Hash()] = tx
9193
}
9294

9395
func (self *TxPool) add(tx *types.Transaction) error {
9496
hash := tx.Hash()
97+
98+
if self.invalidHashes.Has(hash) {
99+
return fmt.Errorf("Invalid transaction (%x)", hash[:4])
100+
}
101+
95102
if self.txs[hash] != nil {
96-
return fmt.Errorf("Known transaction (%x)", hash[0:4])
103+
return fmt.Errorf("Known transaction (%x)", hash[:4])
97104
}
98105
err := self.ValidateTransaction(tx)
99106
if err != nil {
100107
return err
101108
}
102109

103-
self.addTx(tx)
110+
self.txs[hash] = tx
104111

105112
var toname string
106113
if to := tx.To(); to != nil {
@@ -115,7 +122,9 @@ func (self *TxPool) add(tx *types.Transaction) error {
115122
txplogger.Debugf("(t) %x => %s (%v) %x\n", from, toname, tx.Value, tx.Hash())
116123

117124
// Notify the subscribers
125+
//println("post")
118126
go self.eventMux.Post(TxPreEvent{tx})
127+
//println("done post")
119128

120129
return nil
121130
}
@@ -127,6 +136,7 @@ func (self *TxPool) Size() int {
127136
func (self *TxPool) Add(tx *types.Transaction) error {
128137
self.mu.Lock()
129138
defer self.mu.Unlock()
139+
130140
return self.add(tx)
131141
}
132142

@@ -183,6 +193,17 @@ func (self *TxPool) RemoveSet(txs types.Transactions) {
183193
}
184194
}
185195

196+
func (self *TxPool) InvalidateSet(hashes *set.Set) {
197+
self.mu.Lock()
198+
defer self.mu.Unlock()
199+
200+
hashes.Each(func(v interface{}) bool {
201+
delete(self.txs, v.(common.Hash))
202+
return true
203+
})
204+
self.invalidHashes.Merge(hashes)
205+
}
206+
186207
func (pool *TxPool) Flush() {
187208
pool.txs = make(map[common.Hash]*types.Transaction)
188209
}

‎event/filter/eth_filter.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"sync"
77

88
"github.com/ethereum/go-ethereum/core"
9-
"github.com/ethereum/go-ethereum/event"
109
"github.com/ethereum/go-ethereum/core/state"
10+
"github.com/ethereum/go-ethereum/event"
1111
)
1212

1313
type FilterManager struct {

0 commit comments

Comments
 (0)