kaspad/domain/miningmanager/mempool/validate_and_insert_transaction.go
D-Stacks eb693c4a86
Mempool: Retrive stable state of the mempool. optimze get mempool entries by addresses (#2111)
* fix mempool accessing, rewrite get_mempool_entries_by_addresses

* fix counter, add verbose

* fmt

* addresses as string

* Define error in case utxoEntry is missing.

* fix error variable to string

* stop tests from failing (see in code comment)

* access both pools in the same state via parameters

* get rid of todo message

* fmt - very important!

* perf: scriptpublickey in mempool, no txscript.

* address reveiw

* fmt fix

* mixed up isorphan bool, pass tests now

* do map preallocation in mempoolbyaddresses

* no proallocation for orphanpool sending.

Co-authored-by: Ori Newman <orinewman1@gmail.com>
2022-07-26 23:06:08 +03:00

66 lines
1.9 KiB
Go

package mempool
import (
"fmt"
"github.com/kaspanet/kaspad/infrastructure/logger"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
)
func (mp *mempool) validateAndInsertTransaction(transaction *externalapi.DomainTransaction, isHighPriority bool,
allowOrphan bool) (acceptedTransactions []*externalapi.DomainTransaction, err error) {
onEnd := logger.LogAndMeasureExecutionTime(log,
fmt.Sprintf("validateAndInsertTransaction %s", consensushashing.TransactionID(transaction)))
defer onEnd()
// Populate mass in the beginning, it will be used in multiple places throughout the validation and insertion.
mp.consensusReference.Consensus().PopulateMass(transaction)
err = mp.validateTransactionPreUTXOEntry(transaction)
if err != nil {
return nil, err
}
parentsInPool, missingOutpoints, err := mp.fillInputsAndGetMissingParents(transaction)
if err != nil {
return nil, err
}
if len(missingOutpoints) > 0 {
if !allowOrphan {
str := fmt.Sprintf("Transaction %s is an orphan, where allowOrphan = false",
consensushashing.TransactionID(transaction))
return nil, transactionRuleError(RejectBadOrphan, str)
}
return nil, mp.orphansPool.maybeAddOrphan(transaction, isHighPriority)
}
err = mp.validateTransactionInContext(transaction)
if err != nil {
return nil, err
}
mempoolTransaction, err := mp.transactionsPool.addTransaction(transaction, parentsInPool, isHighPriority)
if err != nil {
return nil, err
}
acceptedOrphans, err := mp.orphansPool.processOrphansAfterAcceptedTransaction(mempoolTransaction.Transaction())
if err != nil {
return nil, err
}
acceptedTransactions = append([]*externalapi.DomainTransaction{transaction.Clone()}, acceptedOrphans...) //these pointer leave the mempool, hence we clone.
err = mp.transactionsPool.limitTransactionCount()
if err != nil {
return nil, err
}
return acceptedTransactions, nil
}