Implement transactionsPool.addTransaction

This commit is contained in:
Mike Zak 2021-06-08 16:30:53 +03:00
parent 4e46be67a4
commit 25cc6184d6
5 changed files with 58 additions and 49 deletions

View File

@ -3,8 +3,6 @@ package mempool
import ( import (
"fmt" "fmt"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi" "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/utxo" "github.com/kaspanet/kaspad/domain/consensus/utils/utxo"
"github.com/kaspanet/kaspad/domain/miningmanager/mempool/model" "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) { func (mpus *mempoolUTXOSet) addTransaction(transaction *model.MempoolTransaction) {
outpoint := &externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID()} outpoint := &externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID()}
@ -65,7 +46,7 @@ func (mpus *mempoolUTXOSet) removeTransaction(transaction *model.MempoolTransact
delete(mpus.transactionsByPreviousOutpoint, input.PreviousOutpoint) delete(mpus.transactionsByPreviousOutpoint, input.PreviousOutpoint)
} }
outpoint := &externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID()} outpoint := externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID()}
for i := range transaction.Transaction.Outputs { for i := range transaction.Transaction.Outputs {
outpoint.Index = uint32(i) outpoint.Index = uint32(i)

View File

@ -8,7 +8,7 @@ import (
// MempoolTransaction represents a transaction inside the main TransactionPool // MempoolTransaction represents a transaction inside the main TransactionPool
type MempoolTransaction struct { type MempoolTransaction struct {
Transaction *externalapi.DomainTransaction Transaction *externalapi.DomainTransaction
ParentsInPool ParentUTXOsInPool ParentTransactionsInPool OutpointToTransaction
IsHighPriority bool IsHighPriority bool
AddedAtDAAScore uint64 AddedAtDAAScore uint64
} }

View File

@ -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
}

View File

@ -147,7 +147,7 @@ func (op *orphansPool) unorphanTransaction(orphanTransaction *model.MempoolTrans
} }
mempoolTransaction := &model.MempoolTransaction{ mempoolTransaction := &model.MempoolTransaction{
Transaction: orphanTransaction.Transaction, Transaction: orphanTransaction.Transaction,
ParentsInPool: op.mempool.mempoolUTXOSet.getParentsInPool(orphanTransaction.Transaction), ParentTransactionsInPool: op.mempool.transactionsPool.getParentTransactionsInPool(orphanTransaction.Transaction),
IsHighPriority: false, IsHighPriority: false,
AddedAtDAAScore: virtualDAAScore, AddedAtDAAScore: virtualDAAScore,
} }
@ -165,7 +165,7 @@ func (op *orphansPool) removeOrphan(orphanTransactionID *externalapi.DomainTrans
return nil return nil
} }
delete(op.allOrphans, orphanTransactionID) delete(op.allOrphans, *orphanTransactionID)
for i, input := range orphanTransaction.Transaction.Inputs { for i, input := range orphanTransaction.Transaction.Inputs {
orphans, ok := op.orphansByPreviousOutpoint[input.PreviousOutpoint] orphans, ok := op.orphansByPreviousOutpoint[input.PreviousOutpoint]

View File

@ -25,12 +25,43 @@ func newTransactionsPool(mp *mempool) *transactionsPool {
} }
} }
func (tp *transactionsPool) addTransaction(transaction *externalapi.DomainTransaction, parentsInPool []*model.MempoolTransaction) error { func (tp *transactionsPool) addTransaction(transaction *externalapi.DomainTransaction,
panic("transactionsPool.addTransaction not implemented") // TODO (Mike) 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 { 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 { func (tp *transactionsPool) expireOldTransactions() error {
@ -66,10 +97,23 @@ func (tp *transactionsPool) allReadyTransactions() []*externalapi.DomainTransact
result := []*externalapi.DomainTransaction{} result := []*externalapi.DomainTransaction{}
for _, mempoolTransaction := range tp.allTransactions { for _, mempoolTransaction := range tp.allTransactions {
if len(mempoolTransaction.ParentsInPool) == 0 { if len(mempoolTransaction.ParentTransactionsInPool) == 0 {
result = append(result, mempoolTransaction.Transaction) result = append(result, mempoolTransaction.Transaction)
} }
} }
return result 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
}