Another suggestion (but it's also good as-is):
<details>
<summary>Diff</summary>
--- a/src/rpc/rawtransaction.cpp
+++ b/src/rpc/rawtransaction.cpp
@@ -923,34 +923,36 @@ static UniValue testmempoolaccept(const JSONRPCRequest& request)
result_0.pushKV("txid", tx_hash.GetHex());
TxValidationState state;
- bool test_accept_res;
+ bool allowed;
CAmount fee{0}; // To return transaction fee from AcceptToMemoryPool
{
LOCK(cs_main);
- test_accept_res = AcceptToMemoryPool(mempool, state, std::move(tx),
+ allowed = AcceptToMemoryPool(mempool, state, std::move(tx),
nullptr /* plTxnReplaced */, false /* bypass_limits */, /* test_accept */ true, &fee);
}
+ std::string reject_reason;
// Check that fee does not exceed maxfee
- if (test_accept_res && max_raw_tx_fee && fee > max_raw_tx_fee) {
- result_0.pushKV("allowed", false);
- result_0.pushKV("reject-reason", "max-fee-exceeded");
- result.push_back(std::move(result_0));
- return result;
+ if (allowed) {
+ if (max_raw_tx_fee && fee > max_raw_tx_fee) {
+ allowed = false;
+ reject_reason = "max-fee-exceeded";
}
- result_0.pushKV("allowed", test_accept_res);
- if (!test_accept_res) {
+ } else {
if (state.IsInvalid()) {
if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) {
- result_0.pushKV("reject-reason", "missing-inputs");
+ reject_reason = "missing-inputs";
} else {
- result_0.pushKV("reject-reason", strprintf("%s", state.GetRejectReason()));
+ reject_reason = strprintf("%s", state.GetRejectReason());
}
} else {
- result_0.pushKV("reject-reason", state.GetRejectReason());
+ reject_reason = state.GetRejectReason();
}
}
-
+ result_0.pushKV("allowed", allowed);
+ if (!reject_reason.empty()) {
+ result_0.pushKV("reject-reason", reject_reason);
+ }
result.push_back(std::move(result_0));
return result;
}
</details>
<details>
<summary>Code</summary>
bool allowed;
CAmount fee{0}; // To return transaction fee from AcceptToMemoryPool
{
LOCK(cs_main);
allowed = AcceptToMemoryPool(mempool, state, std::move(tx),
nullptr /* plTxnReplaced */, false /* bypass_limits */, /* test_accept */ true, &fee);
}
std::string reject_reason;
// Check that fee does not exceed maxfee
if (allowed) {
if (max_raw_tx_fee && fee > max_raw_tx_fee) {
allowed = false;
reject_reason = "max-fee-exceeded";
}
} else {
if (state.IsInvalid()) {
if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) {
reject_reason = "missing-inputs";
} else {
reject_reason = strprintf("%s", state.GetRejectReason());
}
} else {
reject_reason = state.GetRejectReason();
}
}
result_0.pushKV("allowed", allowed);
if (!reject_reason.empty()) {
result_0.pushKV("reject-reason", reject_reason);
}
result.push_back(std::move(result_0));
return result;
<details>