Skip to content

Commit 7138404

Browse files
committed
core: only post event once per tx & fixed test
1 parent 4feb5f6 commit 7138404

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

‎core/transaction_pool.go‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
125125
return nil
126126
}
127127

128-
func (self *TxPool) addTx(tx *types.Transaction) {
129-
from, _ := tx.From()
130-
self.queue[from] = append(self.queue[from], tx)
131-
}
132-
133128
func (self *TxPool) add(tx *types.Transaction) error {
134129
hash := tx.Hash()
135130

@@ -147,7 +142,7 @@ func (self *TxPool) add(tx *types.Transaction) error {
147142
return err
148143
}
149144

150-
self.addTx(tx)
145+
self.queueTx(tx)
151146

152147
var toname string
153148
if to := tx.To(); to != nil {
@@ -226,6 +221,19 @@ func (pool *TxPool) Stop() {
226221
glog.V(logger.Info).Infoln("TX Pool stopped")
227222
}
228223

224+
func (self *TxPool) queueTx(tx *types.Transaction) {
225+
from, _ := tx.From()
226+
self.queue[from] = append(self.queue[from], tx)
227+
}
228+
229+
func (pool *TxPool) addTx(tx *types.Transaction) {
230+
if _, ok := pool.txs[tx.Hash()]; !ok {
231+
pool.txs[tx.Hash()] = tx
232+
// Notify the subscribers
233+
pool.eventMux.Post(TxPreEvent{tx})
234+
}
235+
}
236+
229237
// check queue will attempt to insert
230238
func (pool *TxPool) checkQueue() {
231239
pool.mu.Lock()
@@ -257,9 +265,7 @@ func (pool *TxPool) checkQueue() {
257265
}
258266
enonce++
259267

260-
pool.txs[tx.Hash()] = tx
261-
// Notify the subscribers
262-
go pool.eventMux.Post(TxPreEvent{tx})
268+
pool.addTx(tx)
263269
}
264270
//pool.queue[address] = txs[i:]
265271
// delete the entire queue entry if it's empty. There's no need to keep it

‎core/transaction_pool_test.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestTransactionQueue(t *testing.T) {
6767
tx.SignECDSA(key)
6868
from, _ := tx.From()
6969
pool.currentState().AddBalance(from, big.NewInt(1))
70-
pool.addTx(tx)
70+
pool.queueTx(tx)
7171

7272
pool.checkQueue()
7373
if len(pool.txs) != 1 {
@@ -79,7 +79,7 @@ func TestTransactionQueue(t *testing.T) {
7979
from, _ = tx.From()
8080
pool.currentState().SetNonce(from, 10)
8181
tx.SetNonce(1)
82-
pool.addTx(tx)
82+
pool.queueTx(tx)
8383
pool.checkQueue()
8484
if _, ok := pool.txs[tx.Hash()]; ok {
8585
t.Error("expected transaction to be in tx pool")
@@ -96,17 +96,17 @@ func TestTransactionQueue(t *testing.T) {
9696
tx1.SignECDSA(key)
9797
tx2.SignECDSA(key)
9898
tx3.SignECDSA(key)
99-
pool.addTx(tx1)
100-
pool.addTx(tx2)
101-
pool.addTx(tx3)
99+
pool.queueTx(tx1)
100+
pool.queueTx(tx2)
101+
pool.queueTx(tx3)
102102
from, _ = tx1.From()
103103
pool.checkQueue()
104104

105105
if len(pool.txs) != 1 {
106106
t.Error("expected tx pool to be 1 =")
107107
}
108108

109-
if len(pool.queue[from]) != 2 {
109+
if len(pool.queue[from]) != 3 {
110110
t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
111111
}
112112
}

0 commit comments

Comments
 (0)