diff --git a/domain/miningmanager/mempool/mempool_utxo_set.go b/domain/miningmanager/mempool/mempool_utxo_set.go index 390fe7afe..7ea1be8ff 100644 --- a/domain/miningmanager/mempool/mempool_utxo_set.go +++ b/domain/miningmanager/mempool/mempool_utxo_set.go @@ -2,7 +2,9 @@ package mempool import ( "github.com/kaspanet/kaspad/domain/consensus/model/externalapi" + "github.com/kaspanet/kaspad/domain/consensus/utils/utxo" "github.com/kaspanet/kaspad/domain/miningmanager/mempool/model" + "github.com/pkg/errors" ) type mempoolUTXOSet struct { @@ -37,7 +39,34 @@ func (mpus *mempoolUTXOSet) getParentsInPool(transaction *model.MempoolTransacti } func (mpus *mempoolUTXOSet) addTransaction(transaction *model.MempoolTransaction) error { - panic("mempoolUTXOSet.addTransaction not implemented") // TODO (Mike) + outpoint := &externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID()} + + for i, input := range transaction.Transaction.Inputs { + outpoint.Index = uint32(i) + + // Sanity check for consistency + if existingTx, exists := mpus.transactionsByPreviousOutpoint[input.PreviousOutpoint]; exists { + return errors.Errorf("outpoint %s is already used by %s", + input.PreviousOutpoint, existingTx.TransactionID()) + } + + delete(mpus.poolUnspentOutputs, *outpoint) + mpus.transactionsByPreviousOutpoint[input.PreviousOutpoint] = transaction + } + + for i, output := range transaction.Transaction.Outputs { + outpoint := externalapi.DomainOutpoint{TransactionID: *transaction.TransactionID(), Index: uint32(i)} + + // Sanity check for consistency + if _, exists := mpus.poolUnspentOutputs[outpoint]; exists { + return errors.Errorf("outpoint %s already exists", outpoint) + } + + mpus.poolUnspentOutputs[outpoint] = + utxo.NewUTXOEntry(output.Value, output.ScriptPublicKey, false, model.UnacceptedDAAScore) + } + + return nil } func (mpus *mempoolUTXOSet) removeTransaction(transactionID *externalapi.DomainTransactionID) error { diff --git a/domain/miningmanager/mempool/model/constants.go b/domain/miningmanager/mempool/model/constants.go new file mode 100644 index 000000000..d2d743bdc --- /dev/null +++ b/domain/miningmanager/mempool/model/constants.go @@ -0,0 +1,6 @@ +package model + +import "math" + +// UnacceptedDAAScore is used to for UTXOEntries that were created by transactions in the mempool. +const UnacceptedDAAScore = math.MaxUint64