Skip to content

Commit bb00c95

Browse files
committed
Consistently use FormatStateMessage in RPC error output
This will include the error code and debug output as well as the reason string. See #11955 for the motivation.
1 parent 8b8a1c4 commit bb00c95

File tree

9 files changed

+15
-16
lines changed

9 files changed

+15
-16
lines changed

src/rpc/blockchain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ UniValue preciousblock(const JSONRPCRequest& request)
14341434
PreciousBlock(state, Params(), pblockindex);
14351435

14361436
if (!state.IsValid()) {
1437-
throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason());
1437+
throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state));
14381438
}
14391439

14401440
return NullUniValue;
@@ -1472,7 +1472,7 @@ UniValue invalidateblock(const JSONRPCRequest& request)
14721472
}
14731473

14741474
if (!state.IsValid()) {
1475-
throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason());
1475+
throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state));
14761476
}
14771477

14781478
return NullUniValue;
@@ -1509,7 +1509,7 @@ UniValue reconsiderblock(const JSONRPCRequest& request)
15091509
ActivateBestChain(state, Params());
15101510

15111511
if (!state.IsValid()) {
1512-
throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason());
1512+
throw JSONRPCError(RPC_DATABASE_ERROR, FormatStateMessage(state));
15131513
}
15141514

15151515
return NullUniValue;
@@ -1563,7 +1563,7 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
15631563
pindex = chainActive.Tip();
15641564
}
15651565
}
1566-
1566+
15671567
assert(pindex != nullptr);
15681568

15691569
if (request.params[0].isNull()) {

src/rpc/mining.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ static UniValue BIP22ValidationResult(const CValidationState& state)
264264
if (state.IsValid())
265265
return NullUniValue;
266266

267-
std::string strRejectReason = state.GetRejectReason();
268267
if (state.IsError())
269-
throw JSONRPCError(RPC_VERIFY_ERROR, strRejectReason);
268+
throw JSONRPCError(RPC_VERIFY_ERROR, FormatStateMessage(state));
270269
if (state.IsInvalid())
271270
{
271+
std::string strRejectReason = state.GetRejectReason();
272272
if (strRejectReason.empty())
273273
return "rejected";
274274
return strRejectReason;

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,12 +981,12 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
981981
if (!AcceptToMemoryPool(mempool, state, std::move(tx), &fMissingInputs,
982982
nullptr /* plTxnReplaced */, false /* bypass_limits */, nMaxRawTxFee)) {
983983
if (state.IsInvalid()) {
984-
throw JSONRPCError(RPC_TRANSACTION_REJECTED, strprintf("%i: %s", state.GetRejectCode(), state.GetRejectReason()));
984+
throw JSONRPCError(RPC_TRANSACTION_REJECTED, FormatStateMessage(state));
985985
} else {
986986
if (fMissingInputs) {
987987
throw JSONRPCError(RPC_TRANSACTION_ERROR, "Missing inputs");
988988
}
989-
throw JSONRPCError(RPC_TRANSACTION_ERROR, state.GetRejectReason());
989+
throw JSONRPCError(RPC_TRANSACTION_ERROR, FormatStateMessage(state));
990990
}
991991
} else {
992992
// If wallet is enabled, ensure that the wallet has been made aware

src/wallet/feebumper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Result CommitTransaction(CWallet* wallet, const uint256& txid, CMutableTransacti
267267
CValidationState state;
268268
if (!wallet->CommitTransaction(wtxBumped, reservekey, g_connman.get(), state)) {
269269
// NOTE: CommitTransaction never returns false, so this should never happen.
270-
errors.push_back(strprintf("The transaction was rejected: %s", state.GetRejectReason()));
270+
errors.push_back(strprintf("The transaction was rejected: %s", FormatStateMessage(state)));
271271
return Result::WALLET_ERROR;
272272
}
273273

@@ -290,4 +290,3 @@ Result CommitTransaction(CWallet* wallet, const uint256& txid, CMutableTransacti
290290
}
291291

292292
} // namespace feebumper
293-

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ static void SendMoney(CWallet * const pwallet, const CTxDestination &address, CA
435435
}
436436
CValidationState state;
437437
if (!pwallet->CommitTransaction(wtxNew, reservekey, g_connman.get(), state)) {
438-
strError = strprintf("Error: The transaction was rejected! Reason given: %s", state.GetRejectReason());
438+
strError = strprintf("Error: The transaction was rejected! Reason given: %s", FormatStateMessage(state));
439439
throw JSONRPCError(RPC_WALLET_ERROR, strError);
440440
}
441441
}
@@ -1155,7 +1155,7 @@ UniValue sendmany(const JSONRPCRequest& request)
11551155
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
11561156
CValidationState state;
11571157
if (!pwallet->CommitTransaction(wtx, keyChange, g_connman.get(), state)) {
1158-
strFailReason = strprintf("Transaction commit failed:: %s", state.GetRejectReason());
1158+
strFailReason = strprintf("Transaction commit failed:: %s", FormatStateMessage(state));
11591159
throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
11601160
}
11611161

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3094,7 +3094,7 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, CCon
30943094
{
30953095
// Broadcast
30963096
if (!wtx.AcceptToMemoryPool(maxTxFee, state)) {
3097-
LogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", state.GetRejectReason());
3097+
LogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", FormatStateMessage(state));
30983098
// TODO: if we expect the failure to be long term or permanent, instead delete wtx from the wallet and return failure.
30993099
} else {
31003100
wtx.RelayWalletTransaction(connman);

test/functional/feature_nulldummy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from io import BytesIO
2222
import time
2323

24-
NULLDUMMY_ERROR = "64: non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero)"
24+
NULLDUMMY_ERROR = "non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero) (code 64)"
2525

2626
def trueDummy(tx):
2727
scriptSig = CScript(tx.vin[0].scriptSig)

test/functional/mempool_limit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def run_test(self):
5858
# specifically fund this tx with a fee < mempoolminfee, >= than minrelaytxfee
5959
txF = self.nodes[0].fundrawtransaction(tx, {'feeRate': relayfee})
6060
txFS = self.nodes[0].signrawtransaction(txF['hex'])
61-
assert_raises_rpc_error(-26, "66: mempool min fee not met", self.nodes[0].sendrawtransaction, txFS['hex'])
61+
assert_raises_rpc_error(-26, "mempool min fee not met, 166 < 411 (code 66)", self.nodes[0].sendrawtransaction, txFS['hex'])
6262

6363
if __name__ == '__main__':
6464
MempoolLimitTest().main()

test/functional/mining_prioritisetransaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def run_test(self):
120120
tx_id = self.nodes[0].decoderawtransaction(tx_hex)["txid"]
121121

122122
# This will raise an exception due to min relay fee not being met
123-
assert_raises_rpc_error(-26, "66: min relay fee not met", self.nodes[0].sendrawtransaction, tx_hex)
123+
assert_raises_rpc_error(-26, "min relay fee not met (code 66)", self.nodes[0].sendrawtransaction, tx_hex)
124124
assert(tx_id not in self.nodes[0].getrawmempool())
125125

126126
# This is a less than 1000-byte transaction, so just set the fee

0 commit comments

Comments
 (0)