Skip to content

Commit 169dced

Browse files
author
MarcoFalke
committed
Merge #15408: Remove unused TransactionError constants
fa9b60c Remove unused TransactionError constants (MarcoFalke) Pull request description: Fixup to #14978, which introduced a bunch of unused enum values, such as `UNKNOWN_ERROR`, `ERROR_COUNT` and `TRANSACTION_ERR_LAST`. None of those have a meaning in the context of an `enum class`, where the compiler can infer if all cases have been covered in a switch-case. Also, move the global `::maxTxFee` back to the rpc caller, so it can be set on a per call basis (in the future). Tree-SHA512: 7f1e2d795f1c1278ecd54ddab2b92c2a862f3c637b482d1d008208925befa1c9dd4b3c4bb1bfcbc5ca4b66a41004aaf01ea96ea95236f944250b8a6cf99ff173
2 parents 77fcf25 + fa9b60c commit 169dced

File tree

9 files changed

+45
-70
lines changed

9 files changed

+45
-70
lines changed

src/node/transaction.cpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <future>
1414

15-
const char* TransactionErrorString(const TransactionError err)
15+
std::string TransactionErrorString(const TransactionError err)
1616
{
1717
switch (err) {
1818
case TransactionError::OK:
@@ -33,22 +33,16 @@ const char* TransactionErrorString(const TransactionError err)
3333
return "PSBTs not compatible (different transactions)";
3434
case TransactionError::SIGHASH_MISMATCH:
3535
return "Specified sighash value does not match existing value";
36-
37-
case TransactionError::UNKNOWN_ERROR:
38-
default: break;
36+
// no default case, so the compiler can warn about missing cases
3937
}
40-
return "Unknown error";
38+
assert(false);
4139
}
4240

43-
bool BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, TransactionError& error, std::string& err_string, const bool allowhighfees)
41+
TransactionError BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, std::string& err_string, const CAmount& highfee)
4442
{
4543
std::promise<void> promise;
4644
hashTx = tx->GetHash();
4745

48-
CAmount nMaxRawTxFee = maxTxFee;
49-
if (allowhighfees)
50-
nMaxRawTxFee = 0;
51-
5246
{ // cs_main scope
5347
LOCK(cs_main);
5448
CCoinsViewCache &view = *pcoinsTip;
@@ -63,19 +57,16 @@ bool BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, Transaction
6357
CValidationState state;
6458
bool fMissingInputs;
6559
if (!AcceptToMemoryPool(mempool, state, std::move(tx), &fMissingInputs,
66-
nullptr /* plTxnReplaced */, false /* bypass_limits */, nMaxRawTxFee)) {
60+
nullptr /* plTxnReplaced */, false /* bypass_limits */, highfee)) {
6761
if (state.IsInvalid()) {
6862
err_string = FormatStateMessage(state);
69-
error = TransactionError::MEMPOOL_REJECTED;
70-
return false;
63+
return TransactionError::MEMPOOL_REJECTED;
7164
} else {
7265
if (fMissingInputs) {
73-
error = TransactionError::MISSING_INPUTS;
74-
return false;
66+
return TransactionError::MISSING_INPUTS;
7567
}
7668
err_string = FormatStateMessage(state);
77-
error = TransactionError::MEMPOOL_ERROR;
78-
return false;
69+
return TransactionError::MEMPOOL_ERROR;
7970
}
8071
} else {
8172
// If wallet is enabled, ensure that the wallet has been made aware
@@ -88,8 +79,7 @@ bool BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, Transaction
8879
});
8980
}
9081
} else if (fHaveChain) {
91-
error = TransactionError::ALREADY_IN_CHAIN;
92-
return false;
82+
return TransactionError::ALREADY_IN_CHAIN;
9383
} else {
9484
// Make sure we don't block forever if re-sending
9585
// a transaction already in mempool.
@@ -100,16 +90,14 @@ bool BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, Transaction
10090

10191
promise.get_future().wait();
10292

103-
if(!g_connman) {
104-
error = TransactionError::P2P_DISABLED;
105-
return false;
93+
if (!g_connman) {
94+
return TransactionError::P2P_DISABLED;
10695
}
10796

10897
CInv inv(MSG_TX, hashTx);
109-
g_connman->ForEachNode([&inv](CNode* pnode)
110-
{
98+
g_connman->ForEachNode([&inv](CNode* pnode) {
11199
pnode->PushInventory(inv);
112100
});
113101

114-
return true;
115-
}
102+
return TransactionError::OK;
103+
}

src/node/transaction.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
// Copyright (c) 2017-2018 The Bitcoin Core developers
1+
// Copyright (c) 2017-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#ifndef BITCOIN_NODE_TRANSACTION_H
66
#define BITCOIN_NODE_TRANSACTION_H
77

8+
#include <attributes.h>
89
#include <primitives/transaction.h>
910
#include <uint256.h>
1011

1112
enum class TransactionError {
12-
OK = 0,
13-
UNKNOWN_ERROR,
14-
13+
OK, //!< No error
1514
MISSING_INPUTS,
1615
ALREADY_IN_CHAIN,
1716
P2P_DISABLED,
@@ -20,24 +19,19 @@ enum class TransactionError {
2019
INVALID_PSBT,
2120
PSBT_MISMATCH,
2221
SIGHASH_MISMATCH,
23-
24-
ERROR_COUNT
2522
};
2623

27-
#define TRANSACTION_ERR_LAST TransactionError::ERROR_COUNT
28-
29-
const char* TransactionErrorString(const TransactionError error);
24+
std::string TransactionErrorString(const TransactionError error);
3025

3126
/**
3227
* Broadcast a transaction
3328
*
3429
* @param[in] tx the transaction to broadcast
3530
* @param[out] &txid the txid of the transaction, if successfully broadcast
36-
* @param[out] &error reference to UniValue to fill with error info on failure
3731
* @param[out] &err_string reference to std::string to fill with error string if available
38-
* @param[in] allowhighfees whether to allow fees exceeding maxTxFee
39-
* return true on success, false on error (and fills in `error`)
32+
* @param[in] highfee Reject txs with fees higher than this (if 0, accept any fee)
33+
* return error
4034
*/
41-
bool BroadcastTransaction(CTransactionRef tx, uint256& txid, TransactionError& error, std::string& err_string, bool allowhighfees = false);
35+
NODISCARD TransactionError BroadcastTransaction(CTransactionRef tx, uint256& txid, std::string& err_string, const CAmount& highfee);
4236

4337
#endif // BITCOIN_NODE_TRANSACTION_H

src/psbt.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,21 +309,19 @@ bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransacti
309309
return true;
310310
}
311311

312-
bool CombinePSBTs(PartiallySignedTransaction& out, TransactionError& error, const std::vector<PartiallySignedTransaction>& psbtxs)
312+
TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs)
313313
{
314314
out = psbtxs[0]; // Copy the first one
315315

316316
// Merge
317317
for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
318318
if (!out.Merge(*it)) {
319-
error = TransactionError::PSBT_MISMATCH;
320-
return false;
319+
return TransactionError::PSBT_MISMATCH;
321320
}
322321
}
323322
if (!out.IsSane()) {
324-
error = TransactionError::INVALID_PSBT;
325-
return false;
323+
return TransactionError::INVALID_PSBT;
326324
}
327325

328-
return true;
326+
return TransactionError::OK;
329327
}

src/psbt.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2009-2018 The Bitcoin Core developers
1+
// Copyright (c) 2009-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -575,10 +575,9 @@ bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransacti
575575
* Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial signatures from each input.
576576
*
577577
* @param[out] &out the combined PSBT, if successful
578-
* @param[out] &error reference to TransactionError to fill with error info on failure
579578
* @param[in] psbtxs the PSBTs to combine
580-
* @return True if we successfully combined the transactions, false if they were not compatible
579+
* @return error (OK if we successfully combined the transactions, other error if they were not compatible)
581580
*/
582-
bool CombinePSBTs(PartiallySignedTransaction& out, TransactionError& error, const std::vector<PartiallySignedTransaction>& psbtxs);
581+
NODISCARD TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs);
583582

584583
#endif // BITCOIN_PSBT_H

src/rpc/rawtransaction.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,11 @@ static UniValue sendrawtransaction(const JSONRPCRequest& request)
10651065

10661066
bool allowhighfees = false;
10671067
if (!request.params[1].isNull()) allowhighfees = request.params[1].get_bool();
1068+
const CAmount highfee{allowhighfees ? 0 : ::maxTxFee};
10681069
uint256 txid;
1069-
TransactionError err;
10701070
std::string err_string;
1071-
if (!BroadcastTransaction(tx, txid, err, err_string, allowhighfees)) {
1071+
const TransactionError err = BroadcastTransaction(tx, txid, err_string, highfee);
1072+
if (TransactionError::OK != err) {
10721073
throw JSONRPCTransactionError(err, err_string);
10731074
}
10741075

@@ -1493,8 +1494,8 @@ UniValue combinepsbt(const JSONRPCRequest& request)
14931494
}
14941495

14951496
PartiallySignedTransaction merged_psbt;
1496-
TransactionError error;
1497-
if (!CombinePSBTs(merged_psbt, error, psbtxs)) {
1497+
const TransactionError error = CombinePSBTs(merged_psbt, psbtxs);
1498+
if (error != TransactionError::OK) {
14981499
throw JSONRPCTransactionError(error);
14991500
}
15001501

src/wallet/psbtwallet.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <wallet/psbtwallet.h>
66

7-
bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, TransactionError& error, bool& complete, int sighash_type, bool sign, bool bip32derivs)
7+
TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, bool& complete, int sighash_type, bool sign, bool bip32derivs)
88
{
99
LOCK(pwallet->cs_wallet);
1010
// Get all of the previous transactions
@@ -19,8 +19,7 @@ bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, Transac
1919

2020
// Verify input looks sane. This will check that we have at most one uxto, witness or non-witness.
2121
if (!input.IsSane()) {
22-
error = TransactionError::INVALID_PSBT;
23-
return false;
22+
return TransactionError::INVALID_PSBT;
2423
}
2524

2625
// If we have no utxo, grab it from the wallet.
@@ -37,8 +36,7 @@ bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, Transac
3736

3837
// Get the Sighash type
3938
if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
40-
error = TransactionError::SIGHASH_MISMATCH;
41-
return false;
39+
return TransactionError::SIGHASH_MISMATCH;
4240
}
4341

4442
complete &= SignPSBTInput(HidingSigningProvider(pwallet, !sign, !bip32derivs), psbtx, i, sighash_type);
@@ -58,5 +56,5 @@ bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, Transac
5856
psbt_out.FromSignatureData(sigdata);
5957
}
6058

61-
return true;
59+
return TransactionError::OK;
6260
}

src/wallet/psbtwallet.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2009-2018 The Bitcoin Core developers
1+
// Copyright (c) 2009-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -18,16 +18,14 @@
1818
*
1919
* @param[in] pwallet pointer to a wallet
2020
* @param[in] &psbtx reference to PartiallySignedTransaction to fill in
21-
* @param[out] &error reference to UniValue to fill with error info on failure
2221
* @param[out] &complete indicates whether the PSBT is now complete
2322
* @param[in] sighash_type the sighash type to use when signing (if PSBT does not specify)
2423
* @param[in] sign whether to sign or not
2524
* @param[in] bip32derivs whether to fill in bip32 derivation information if available
26-
* return true on success, false on error (and fills in `error`)
25+
* return error
2726
*/
28-
bool FillPSBT(const CWallet* pwallet,
27+
NODISCARD TransactionError FillPSBT(const CWallet* pwallet,
2928
PartiallySignedTransaction& psbtx,
30-
TransactionError& error,
3129
bool& complete,
3230
int sighash_type = 1 /* SIGHASH_ALL */,
3331
bool sign = true,

src/wallet/rpcwallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4007,8 +4007,8 @@ UniValue walletprocesspsbt(const JSONRPCRequest& request)
40074007
bool sign = request.params[1].isNull() ? true : request.params[1].get_bool();
40084008
bool bip32derivs = request.params[3].isNull() ? false : request.params[3].get_bool();
40094009
bool complete = true;
4010-
TransactionError err;
4011-
if (!FillPSBT(pwallet, psbtx, err, complete, nHashType, sign, bip32derivs)) {
4010+
const TransactionError err = FillPSBT(pwallet, psbtx, complete, nHashType, sign, bip32derivs);
4011+
if (err != TransactionError::OK) {
40124012
throw JSONRPCTransactionError(err);
40134013
}
40144014

@@ -4125,8 +4125,8 @@ UniValue walletcreatefundedpsbt(const JSONRPCRequest& request)
41254125
// Fill transaction with out data but don't sign
41264126
bool bip32derivs = request.params[4].isNull() ? false : request.params[4].get_bool();
41274127
bool complete = true;
4128-
TransactionError err;
4129-
if (!FillPSBT(pwallet, psbtx, err, complete, 1, false, bip32derivs)) {
4128+
const TransactionError err = FillPSBT(pwallet, psbtx, complete, 1, false, bip32derivs);
4129+
if (err != TransactionError::OK) {
41304130
throw JSONRPCTransactionError(err);
41314131
}
41324132

src/wallet/test/psbt_wallet_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ BOOST_AUTO_TEST_CASE(psbt_updater_test)
6262
ssData >> psbtx;
6363

6464
// Fill transaction with our data
65-
TransactionError err;
6665
bool complete = true;
67-
FillPSBT(&m_wallet, psbtx, err, complete, SIGHASH_ALL, false, true);
66+
BOOST_REQUIRE_EQUAL(TransactionError::OK, FillPSBT(&m_wallet, psbtx, complete, SIGHASH_ALL, false, true));
6867

6968
// Get the final tx
7069
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);

0 commit comments

Comments
 (0)