فهرست منبع

core: remove txs from queue in addition to removal of pending

obscuren 10 سال پیش
والد
کامیت
88292f35db
2فایلهای تغییر یافته به همراه49 افزوده شده و 1 حذف شده
  1. 22 1
      core/transaction_pool.go
  2. 27 0
      core/transaction_pool_test.go

+ 22 - 1
core/transaction_pool.go

@@ -306,6 +306,27 @@ func (pool *TxPool) checkQueue() {
 	}
 }
 
+func (pool *TxPool) removeTx(hash common.Hash) {
+	// delete from pending pool
+	delete(pool.txs, hash)
+
+	// delete from queue
+out:
+	for address, txs := range pool.queue {
+		for i, tx := range txs {
+			if tx.Hash() == hash {
+				if len(txs) == 1 {
+					// if only one tx, remove entire address entry
+					delete(pool.queue, address)
+				} else {
+					pool.queue[address][len(txs)-1], pool.queue[address] = nil, append(txs[:i], txs[i+1:]...)
+				}
+				break out
+			}
+		}
+	}
+}
+
 func (pool *TxPool) validatePool() {
 	pool.mu.Lock()
 	defer pool.mu.Unlock()
@@ -316,7 +337,7 @@ func (pool *TxPool) validatePool() {
 				glog.Infof("removed tx (%x) from pool: %v\n", hash[:4], err)
 			}
 
-			delete(pool.txs, hash)
+			pool.removeTx(hash)
 		}
 	}
 }

+ 27 - 0
core/transaction_pool_test.go

@@ -111,3 +111,30 @@ func TestTransactionQueue(t *testing.T) {
 		t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
 	}
 }
+
+func TestRemoveTx(t *testing.T) {
+	pool, key := setupTxPool()
+	tx := transaction()
+	tx.SignECDSA(key)
+	from, _ := tx.From()
+	pool.currentState().AddBalance(from, big.NewInt(1))
+	pool.queueTx(tx)
+	pool.addTx(tx)
+	if len(pool.queue) != 1 {
+		t.Error("expected queue to be 1, got", len(pool.queue))
+	}
+
+	if len(pool.txs) != 1 {
+		t.Error("expected txs to be 1, got", len(pool.txs))
+	}
+
+	pool.removeTx(tx.Hash())
+
+	if len(pool.queue) > 0 {
+		t.Error("expected queue to be 0, got", len(pool.queue))
+	}
+
+	if len(pool.txs) > 0 {
+		t.Error("expected txs to be 0, got", len(pool.txs))
+	}
+}