mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 22:26:47 +00:00
Change removeTransactionAndItsChainedTransactions to be non-recursive (#1696)
* Change removeTransactionAndItsChainedTransactions to be non-recursive * Split the variables assigning. * Change names of function and variables. * Append the correct queue. Co-authored-by: tal <tal@daglabs.com> Co-authored-by: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com>
This commit is contained in:
parent
28a8e96e65
commit
19718ac102
@ -406,34 +406,49 @@ func (mp *mempool) removeTransactionsFromPool(txs []*consensusexternalapi.Domain
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type transactionAndOutpoint struct {
|
||||||
|
transaction *consensusexternalapi.DomainTransaction
|
||||||
|
outpoint *consensusexternalapi.DomainOutpoint
|
||||||
|
}
|
||||||
|
|
||||||
// removeTransactionAndItsChainedTransactions removes a transaction and all of its chained transaction from the mempool.
|
// removeTransactionAndItsChainedTransactions removes a transaction and all of its chained transaction from the mempool.
|
||||||
//
|
|
||||||
// This function MUST be called with the mempool lock held (for writes).
|
// This function MUST be called with the mempool lock held (for writes).
|
||||||
func (mp *mempool) removeTransactionAndItsChainedTransactions(tx *consensusexternalapi.DomainTransaction) error {
|
func (mp *mempool) removeTransactionAndItsChainedTransactions(transaction *consensusexternalapi.DomainTransaction) error {
|
||||||
txID := consensushashing.TransactionID(tx)
|
transactionAndOutpointQueue := make([]*transactionAndOutpoint, 0)
|
||||||
|
transactionAndOutpointQueue = appendTransactionToTransactionAndOutpointQueue(transactionAndOutpointQueue, transaction)
|
||||||
// Remove any transactions which rely on this one.
|
// Remove any transactions which rely on this one.
|
||||||
for i := uint32(0); i < uint32(len(tx.Outputs)); i++ {
|
for len(transactionAndOutpointQueue) > 0 {
|
||||||
prevOut := consensusexternalapi.DomainOutpoint{TransactionID: *txID, Index: i}
|
txAndOutpoint := transactionAndOutpointQueue[0]
|
||||||
if txRedeemer, exists := mp.mempoolUTXOSet.poolTransactionBySpendingOutpoint(prevOut); exists {
|
transactionAndOutpointQueue = transactionAndOutpointQueue[1:]
|
||||||
err := mp.removeTransactionAndItsChainedTransactions(txRedeemer)
|
if txRedeemer, exists := mp.mempoolUTXOSet.poolTransactionBySpendingOutpoint(*txAndOutpoint.outpoint); exists {
|
||||||
if err != nil {
|
transactionAndOutpointQueue = appendTransactionToTransactionAndOutpointQueue(transactionAndOutpointQueue, txRedeemer)
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
|
transactionID := txAndOutpoint.outpoint.TransactionID
|
||||||
|
if _, exists := mp.chainedTransactions[transactionID]; exists {
|
||||||
|
mp.removeChainTransaction(txAndOutpoint.transaction)
|
||||||
|
}
|
||||||
|
err := mp.cleanTransactionFromSets(txAndOutpoint.transaction)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, exists := mp.chainedTransactions[*txID]; exists {
|
|
||||||
mp.removeChainTransaction(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := mp.cleanTransactionFromSets(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appendTransactionToTransactionAndOutpointQueue(queue []*transactionAndOutpoint,
|
||||||
|
transaction *consensusexternalapi.DomainTransaction) []*transactionAndOutpoint {
|
||||||
|
|
||||||
|
transactionID := consensushashing.TransactionID(transaction)
|
||||||
|
queueWithAddedTransactionAndOutpoint := queue
|
||||||
|
for i := uint32(0); i < uint32(len(transaction.Outputs)); i++ {
|
||||||
|
previousOutpoint := consensusexternalapi.DomainOutpoint{TransactionID: *transactionID, Index: i}
|
||||||
|
queueWithAddedTransactionAndOutpoint = append(queueWithAddedTransactionAndOutpoint,
|
||||||
|
&transactionAndOutpoint{transaction: transaction, outpoint: &previousOutpoint})
|
||||||
|
}
|
||||||
|
return queueWithAddedTransactionAndOutpoint
|
||||||
|
}
|
||||||
|
|
||||||
// cleanTransactionFromSets removes the transaction from all mempool related transaction sets.
|
// cleanTransactionFromSets removes the transaction from all mempool related transaction sets.
|
||||||
// It assumes that any chained transaction is already cleaned from the mempool.
|
// It assumes that any chained transaction is already cleaned from the mempool.
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user