Implemented removeOrphan

This commit is contained in:
Mike Zak 2021-06-08 14:00:17 +03:00
parent 65da2462b8
commit 9a1d548e7f
3 changed files with 47 additions and 14 deletions

View File

@ -38,8 +38,7 @@ func (tobf *TransactionsOrderedByFeeRate) Remove(transaction *MempoolTransaction
return errors.Errorf("Couldn't find %s in mp.orderedTransactionsByFeeRate", txID)
}
tobf.RemoveAtIndex(index)
return nil
return tobf.RemoveAtIndex(index)
}
// RemoveAtIndex removes the transaction at the given index.

View File

@ -7,12 +7,12 @@ import (
// OrphanTransaction represents a transaction in the OrphanPool
type OrphanTransaction struct {
transaction *externalapi.DomainTransaction
Transaction *externalapi.DomainTransaction
IsHighPriority bool
AddedAtDAAScore uint64
}
// TransactionID returns the ID of this OrphanTransaction
func (ot *OrphanTransaction) TransactionID() *externalapi.DomainTransactionID {
return consensushashing.TransactionID(ot.transaction)
return consensushashing.TransactionID(ot.Transaction)
}

View File

@ -3,24 +3,25 @@ package mempool
import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/miningmanager/mempool/model"
"github.com/pkg/errors"
)
type idToOrphan map[externalapi.DomainTransactionID]*model.OrphanTransaction
type previousOutpointToOrphans map[externalapi.DomainOutpoint]idToOrphan
type orphansPool struct {
mempool *mempool
allOrphans idToOrphan
orphanByPreviousOutpoint previousOutpointToOrphans
lastExpireScan uint64
mempool *mempool
allOrphans idToOrphan
orphansByPreviousOutpoint previousOutpointToOrphans
lastExpireScan uint64
}
func newOrphansPool(mp *mempool) *orphansPool {
return &orphansPool{
mempool: mp,
allOrphans: idToOrphan{},
orphanByPreviousOutpoint: previousOutpointToOrphans{},
lastExpireScan: 0,
mempool: mp,
allOrphans: idToOrphan{},
orphansByPreviousOutpoint: previousOutpointToOrphans{},
lastExpireScan: 0,
}
}
@ -40,8 +41,41 @@ func (op *orphansPool) unorphanTransaction(orphanTransactionID *externalapi.Doma
panic("orphansPool.unorphanTransaction not implemented") // TODO (Mike)
}
func (op *orphansPool) removeOrphan(orphanTransactionID *externalapi.DomainTransactionID) error {
panic("orphansPool.removeOrphan not implemented") // TODO (Mike)
func (op *orphansPool) removeOrphan(orphanTransactionID *externalapi.DomainTransactionID, removeRedeemers bool) error {
orphanTransaction, ok := op.allOrphans[*orphanTransactionID]
if !ok {
return nil
}
delete(op.allOrphans, orphanTransactionID)
for i, input := range orphanTransaction.Transaction.Inputs {
orphans, ok := op.orphansByPreviousOutpoint[input.PreviousOutpoint]
if !ok {
return errors.Errorf("Input No. %d of %s (%s) doesn't exist in orphansByPreviousOutpoint",
i, orphanTransactionID, input.PreviousOutpoint)
}
delete(orphans, *orphanTransactionID)
if len(orphans) == 0 {
delete(op.orphansByPreviousOutpoint, input.PreviousOutpoint)
}
}
if removeRedeemers {
outpoint := externalapi.DomainOutpoint{TransactionID: *orphanTransactionID}
for i := range orphanTransaction.Transaction.Outputs {
outpoint.Index = uint32(i)
for _, orphan := range op.orphansByPreviousOutpoint[outpoint] {
// Recursive call is bound by size of orphan pool (which is very small)
err := op.removeOrphan(orphan.TransactionID(), true)
if err != nil {
return err
}
}
}
}
return nil
}
func (op *orphansPool) expireOrphanTransactions() error {