diff --git a/domain/miningmanager/mempool/mempool_utxo_set.go b/domain/miningmanager/mempool/mempool_utxo_set.go index 79d793a9a..be8233bec 100644 --- a/domain/miningmanager/mempool/mempool_utxo_set.go +++ b/domain/miningmanager/mempool/mempool_utxo_set.go @@ -3,8 +3,6 @@ package mempool import ( "fmt" - "github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing" - "github.com/kaspanet/kaspad/domain/consensus/model/externalapi" "github.com/kaspanet/kaspad/domain/consensus/utils/utxo" "github.com/kaspanet/kaspad/domain/miningmanager/mempool/model" @@ -24,23 +22,6 @@ func newMempoolUTXOSet(mp *mempool) *mempoolUTXOSet { } } -func (mpus *mempoolUTXOSet) getParentsInPool(transaction *externalapi.DomainTransaction) model.ParentUTXOsInPool { - parentsInPool := model.ParentUTXOsInPool{} - - outpoint := &externalapi.DomainOutpoint{ - TransactionID: *consensushashing.TransactionID(transaction), - } - for i := range transaction.Inputs { - outpoint.Index = uint32(i) - utxo, ok := mpus.poolUnspentOutputs[*outpoint] - if ok { - parentsInPool.Set(i, utxo) - } - } - - return parentsInPool -} - func (mpus *mempoolUTXOSet) addTransaction(transaction *model.MempoolTransaction) { outpoint := &externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID()} @@ -65,7 +46,7 @@ func (mpus *mempoolUTXOSet) removeTransaction(transaction *model.MempoolTransact delete(mpus.transactionsByPreviousOutpoint, input.PreviousOutpoint) } - outpoint := &externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID()} + outpoint := externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID()} for i := range transaction.Transaction.Outputs { outpoint.Index = uint32(i) diff --git a/domain/miningmanager/mempool/model/mempool_transaction.go b/domain/miningmanager/mempool/model/mempool_transaction.go index 9ec2a364e..e89fda409 100644 --- a/domain/miningmanager/mempool/model/mempool_transaction.go +++ b/domain/miningmanager/mempool/model/mempool_transaction.go @@ -7,10 +7,10 @@ import ( // MempoolTransaction represents a transaction inside the main TransactionPool type MempoolTransaction struct { - Transaction *externalapi.DomainTransaction - ParentsInPool ParentUTXOsInPool - IsHighPriority bool - AddedAtDAAScore uint64 + Transaction *externalapi.DomainTransaction + ParentTransactionsInPool OutpointToTransaction + IsHighPriority bool + AddedAtDAAScore uint64 } // TransactionID returns the ID of this MempoolTransaction diff --git a/domain/miningmanager/mempool/model/parent_utxos_in_pool.go b/domain/miningmanager/mempool/model/parent_utxos_in_pool.go deleted file mode 100644 index c17f149ce..000000000 --- a/domain/miningmanager/mempool/model/parent_utxos_in_pool.go +++ /dev/null @@ -1,16 +0,0 @@ -package model - -import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi" - -// ParentUTXOsInPool represent the utxos a transaction spends out of the mempool. -// The utxos are indexed by transaction output index, for convenient access. -type ParentUTXOsInPool map[int]externalapi.UTXOEntry - -func (pip ParentUTXOsInPool) Get(index int) (externalapi.UTXOEntry, bool) { - utxoEntry, ok := pip[index] - return utxoEntry, ok -} - -func (pip ParentUTXOsInPool) Set(index int, utxoEntry externalapi.UTXOEntry) { - pip[index] = utxoEntry -} diff --git a/domain/miningmanager/mempool/orphan_pool.go b/domain/miningmanager/mempool/orphan_pool.go index 02491984a..ae8f77f0d 100644 --- a/domain/miningmanager/mempool/orphan_pool.go +++ b/domain/miningmanager/mempool/orphan_pool.go @@ -146,10 +146,10 @@ func (op *orphansPool) unorphanTransaction(orphanTransaction *model.MempoolTrans return nil, err } mempoolTransaction := &model.MempoolTransaction{ - Transaction: orphanTransaction.Transaction, - ParentsInPool: op.mempool.mempoolUTXOSet.getParentsInPool(orphanTransaction.Transaction), - IsHighPriority: false, - AddedAtDAAScore: virtualDAAScore, + Transaction: orphanTransaction.Transaction, + ParentTransactionsInPool: op.mempool.transactionsPool.getParentTransactionsInPool(orphanTransaction.Transaction), + IsHighPriority: false, + AddedAtDAAScore: virtualDAAScore, } err = op.mempool.transactionsPool.addMempoolTransaction(mempoolTransaction) if err != nil { @@ -165,7 +165,7 @@ func (op *orphansPool) removeOrphan(orphanTransactionID *externalapi.DomainTrans return nil } - delete(op.allOrphans, orphanTransactionID) + delete(op.allOrphans, *orphanTransactionID) for i, input := range orphanTransaction.Transaction.Inputs { orphans, ok := op.orphansByPreviousOutpoint[input.PreviousOutpoint] diff --git a/domain/miningmanager/mempool/transactions_pool.go b/domain/miningmanager/mempool/transactions_pool.go index 5f1a239ba..3aca75769 100644 --- a/domain/miningmanager/mempool/transactions_pool.go +++ b/domain/miningmanager/mempool/transactions_pool.go @@ -25,12 +25,43 @@ func newTransactionsPool(mp *mempool) *transactionsPool { } } -func (tp *transactionsPool) addTransaction(transaction *externalapi.DomainTransaction, parentsInPool []*model.MempoolTransaction) error { - panic("transactionsPool.addTransaction not implemented") // TODO (Mike) +func (tp *transactionsPool) addTransaction(transaction *externalapi.DomainTransaction, + parentTransactionsInPool model.OutpointToTransaction, isHighPriority bool) error { + + virtualDAAScore, err := tp.mempool.virtualDAAScore() + if err != nil { + return err + } + + mempoolTransaction := &model.MempoolTransaction{ + Transaction: transaction, + ParentTransactionsInPool: parentTransactionsInPool, + IsHighPriority: isHighPriority, + AddedAtDAAScore: virtualDAAScore, + } + + return tp.addMempoolTransaction(mempoolTransaction) } func (tp *transactionsPool) addMempoolTransaction(transaction *model.MempoolTransaction) error { - panic("transactionsPool.addMempoolTransaction not implemented") // TODO (Mike) + tp.allTransactions[*transaction.TransactionID()] = transaction + + for outpoint, parentTransactionInPool := range transaction.ParentTransactionsInPool { + tp.chainedTransactionsByPreviousOutpoint[outpoint] = parentTransactionInPool + } + + tp.mempool.mempoolUTXOSet.addTransaction(transaction) + + err := tp.transactionsByFeeRate.Push(transaction) + if err != nil { + return err + } + + if transaction.IsHighPriority { + tp.highPriorityTransactions[*transaction.TransactionID()] = transaction + } + + return nil } func (tp *transactionsPool) expireOldTransactions() error { @@ -66,10 +97,23 @@ func (tp *transactionsPool) allReadyTransactions() []*externalapi.DomainTransact result := []*externalapi.DomainTransaction{} for _, mempoolTransaction := range tp.allTransactions { - if len(mempoolTransaction.ParentsInPool) == 0 { + if len(mempoolTransaction.ParentTransactionsInPool) == 0 { result = append(result, mempoolTransaction.Transaction) } } return result } + +func (tp *transactionsPool) getParentTransactionsInPool( + transaction *externalapi.DomainTransaction) model.OutpointToTransaction { + + parentsTransactionsInPool := model.OutpointToTransaction{} + + for _, input := range transaction.Inputs { + transaction := tp.allTransactions[input.PreviousOutpoint.TransactionID] + parentsTransactionsInPool[input.PreviousOutpoint] = transaction + } + + return parentsTransactionsInPool +}