mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-29 08:28:50 +00:00
Implement transactionsPool.addTransaction
This commit is contained in:
parent
4e46be67a4
commit
25cc6184d6
@ -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)
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
@ -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]
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user