Skip to content

Commit 578148d

Browse files
committed
[validation] explicit Success/Failure ctors for MempoolAcceptResult
Makes code more clear and prevents accidentally calling the wrong ctor.
1 parent b88d77a commit 578148d

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/validation.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,28 +1040,28 @@ MempoolAcceptResult MemPoolAccept::AcceptSingleTransaction(const CTransactionRef
10401040

10411041
Workspace ws(ptx);
10421042

1043-
if (!PreChecks(args, ws)) return MempoolAcceptResult(ws.m_state);
1043+
if (!PreChecks(args, ws)) return MempoolAcceptResult::Failure(ws.m_state);
10441044

10451045
// Only compute the precomputed transaction data if we need to verify
10461046
// scripts (ie, other policy checks pass). We perform the inexpensive
10471047
// checks first and avoid hashing and signature verification unless those
10481048
// checks pass, to mitigate CPU exhaustion denial-of-service attacks.
10491049
PrecomputedTransactionData txdata;
10501050

1051-
if (!PolicyScriptChecks(args, ws, txdata)) return MempoolAcceptResult(ws.m_state);
1051+
if (!PolicyScriptChecks(args, ws, txdata)) return MempoolAcceptResult::Failure(ws.m_state);
10521052

1053-
if (!ConsensusScriptChecks(args, ws, txdata)) return MempoolAcceptResult(ws.m_state);
1053+
if (!ConsensusScriptChecks(args, ws, txdata)) return MempoolAcceptResult::Failure(ws.m_state);
10541054

10551055
// Tx was accepted, but not added
10561056
if (args.m_test_accept) {
1057-
return MempoolAcceptResult(std::move(ws.m_replaced_transactions), ws.m_base_fees);
1057+
return MempoolAcceptResult::Success(std::move(ws.m_replaced_transactions), ws.m_base_fees);
10581058
}
10591059

1060-
if (!Finalize(args, ws)) return MempoolAcceptResult(ws.m_state);
1060+
if (!Finalize(args, ws)) return MempoolAcceptResult::Failure(ws.m_state);
10611061

10621062
GetMainSignals().TransactionAddedToMempool(ptx, m_pool.GetAndIncrementSequence());
10631063

1064-
return MempoolAcceptResult(std::move(ws.m_replaced_transactions), ws.m_base_fees);
1064+
return MempoolAcceptResult::Success(std::move(ws.m_replaced_transactions), ws.m_base_fees);
10651065
}
10661066

10671067
} // anon namespace

src/validation.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ void PruneBlockFilesManual(CChainState& active_chainstate, int nManualPruneHeigh
169169
* Validation result for a single transaction mempool acceptance.
170170
*/
171171
struct MempoolAcceptResult {
172-
/** Used to indicate the results of mempool validation,
173-
* including the possibility of unfinished validation.
174-
*/
172+
/** Used to indicate the results of mempool validation. */
175173
enum class ResultType {
176174
VALID, //!> Fully validated, valid.
177175
INVALID, //!> Invalid.
@@ -184,7 +182,16 @@ struct MempoolAcceptResult {
184182
const std::optional<std::list<CTransactionRef>> m_replaced_transactions;
185183
/** Raw base fees in satoshis. */
186184
const std::optional<CAmount> m_base_fees;
185+
static MempoolAcceptResult Failure(TxValidationState state) {
186+
return MempoolAcceptResult(state);
187+
}
188+
189+
static MempoolAcceptResult Success(std::list<CTransactionRef>&& replaced_txns, CAmount fees) {
190+
return MempoolAcceptResult(std::move(replaced_txns), fees);
191+
}
187192

193+
// Private constructors. Use static methods MempoolAcceptResult::Success, etc. to construct.
194+
private:
188195
/** Constructor for failure case */
189196
explicit MempoolAcceptResult(TxValidationState state)
190197
: m_result_type(ResultType::INVALID), m_state(state) {

0 commit comments

Comments
 (0)