[refactor] return MempoolAcceptResult

This creates a cleaner interface with ATMP, allows us to make results const,
and makes accessing values that don't make sense (e.g. fee when tx is
invalid) an error.
This commit is contained in:
gzhao408
2021-01-19 05:29:40 -08:00
parent 9db10a5506
commit f82baf0762
9 changed files with 106 additions and 92 deletions

View File

@ -12,6 +12,7 @@
#include <amount.h>
#include <coins.h>
#include <consensus/validation.h>
#include <crypto/common.h> // for ReadLE64
#include <fs.h>
#include <optional.h>
@ -23,6 +24,7 @@
#include <txdb.h>
#include <versionbits.h>
#include <serialize.h>
#include <util/check.h>
#include <util/hasher.h>
#include <atomic>
@ -46,7 +48,6 @@ class CConnman;
class CScriptCheck;
class CTxMemPool;
class ChainstateManager;
class TxValidationState;
struct ChainTxData;
struct DisconnectedBlockTransactions;
@ -181,12 +182,46 @@ void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
/** Prune block files up to a given height */
void PruneBlockFilesManual(int nManualPruneHeight);
/** (try to) add transaction to memory pool
* plTxnReplaced will be appended to with all transactions replaced from mempool
* @param[out] fee_out optional argument to return tx fee to the caller **/
bool AcceptToMemoryPool(CTxMemPool& pool, TxValidationState &state, const CTransactionRef &tx,
std::list<CTransactionRef>* plTxnReplaced,
bool bypass_limits, bool test_accept=false, CAmount* fee_out=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/**
* Validation result for a single transaction mempool acceptance.
*/
struct MempoolAcceptResult {
/** Used to indicate the results of mempool validation,
* including the possibility of unfinished validation.
*/
enum class ResultType {
VALID, //!> Fully validated, valid.
INVALID, //!> Invalid.
};
ResultType m_result_type;
TxValidationState m_state;
// The following fields are only present when m_result_type = ResultType::VALID
/** Mempool transactions replaced by the tx per BIP 125 rules. */
std::optional<std::list<CTransactionRef>> m_replaced_transactions;
/** Raw base fees. */
std::optional<CAmount> m_base_fees;
/** Constructor for failure case */
explicit MempoolAcceptResult(TxValidationState state)
: m_result_type(ResultType::INVALID),
m_state(state), m_replaced_transactions(nullopt), m_base_fees(nullopt) {
Assume(!state.IsValid()); // Can be invalid or error
}
/** Constructor for success case */
explicit MempoolAcceptResult(std::list<CTransactionRef>&& replaced_txns, CAmount fees)
: m_result_type(ResultType::VALID), m_state(TxValidationState{}),
m_replaced_transactions(std::move(replaced_txns)), m_base_fees(fees) {}
};
/**
* (Try to) add a transaction to the memory pool.
* @param[in] bypass_limits When true, don't enforce mempool fee limits.
* @param[in] test_accept When true, run validation checks but don't submit to mempool.
*/
MempoolAcceptResult AcceptToMemoryPool(CTxMemPool& pool, const CTransactionRef& tx,
bool bypass_limits, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** Get the BIP9 state for a given deployment at the current tip. */
ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos);