Merge #15408: Remove unused TransactionError constants

fa9b60c842 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
This commit is contained in:
MarcoFalke 2019-02-22 11:09:44 -05:00
commit 169dced9a4
No known key found for this signature in database
GPG key ID: D2EA4850E7528B25
9 changed files with 45 additions and 70 deletions

View file

@ -12,7 +12,7 @@
#include <future> #include <future>
const char* TransactionErrorString(const TransactionError err) std::string TransactionErrorString(const TransactionError err)
{ {
switch (err) { switch (err) {
case TransactionError::OK: case TransactionError::OK:
@ -33,22 +33,16 @@ const char* TransactionErrorString(const TransactionError err)
return "PSBTs not compatible (different transactions)"; return "PSBTs not compatible (different transactions)";
case TransactionError::SIGHASH_MISMATCH: case TransactionError::SIGHASH_MISMATCH:
return "Specified sighash value does not match existing value"; return "Specified sighash value does not match existing value";
// no default case, so the compiler can warn about missing cases
case TransactionError::UNKNOWN_ERROR:
default: break;
} }
return "Unknown error"; assert(false);
} }
bool BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, TransactionError& error, std::string& err_string, const bool allowhighfees) TransactionError BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, std::string& err_string, const CAmount& highfee)
{ {
std::promise<void> promise; std::promise<void> promise;
hashTx = tx->GetHash(); hashTx = tx->GetHash();
CAmount nMaxRawTxFee = maxTxFee;
if (allowhighfees)
nMaxRawTxFee = 0;
{ // cs_main scope { // cs_main scope
LOCK(cs_main); LOCK(cs_main);
CCoinsViewCache &view = *pcoinsTip; CCoinsViewCache &view = *pcoinsTip;
@ -63,19 +57,16 @@ bool BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, Transaction
CValidationState state; CValidationState state;
bool fMissingInputs; bool fMissingInputs;
if (!AcceptToMemoryPool(mempool, state, std::move(tx), &fMissingInputs, if (!AcceptToMemoryPool(mempool, state, std::move(tx), &fMissingInputs,
nullptr /* plTxnReplaced */, false /* bypass_limits */, nMaxRawTxFee)) { nullptr /* plTxnReplaced */, false /* bypass_limits */, highfee)) {
if (state.IsInvalid()) { if (state.IsInvalid()) {
err_string = FormatStateMessage(state); err_string = FormatStateMessage(state);
error = TransactionError::MEMPOOL_REJECTED; return TransactionError::MEMPOOL_REJECTED;
return false;
} else { } else {
if (fMissingInputs) { if (fMissingInputs) {
error = TransactionError::MISSING_INPUTS; return TransactionError::MISSING_INPUTS;
return false;
} }
err_string = FormatStateMessage(state); err_string = FormatStateMessage(state);
error = TransactionError::MEMPOOL_ERROR; return TransactionError::MEMPOOL_ERROR;
return false;
} }
} else { } else {
// If wallet is enabled, ensure that the wallet has been made aware // If wallet is enabled, ensure that the wallet has been made aware
@ -88,8 +79,7 @@ bool BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, Transaction
}); });
} }
} else if (fHaveChain) { } else if (fHaveChain) {
error = TransactionError::ALREADY_IN_CHAIN; return TransactionError::ALREADY_IN_CHAIN;
return false;
} else { } else {
// Make sure we don't block forever if re-sending // Make sure we don't block forever if re-sending
// a transaction already in mempool. // a transaction already in mempool.
@ -100,16 +90,14 @@ bool BroadcastTransaction(const CTransactionRef tx, uint256& hashTx, Transaction
promise.get_future().wait(); promise.get_future().wait();
if(!g_connman) { if (!g_connman) {
error = TransactionError::P2P_DISABLED; return TransactionError::P2P_DISABLED;
return false;
} }
CInv inv(MSG_TX, hashTx); CInv inv(MSG_TX, hashTx);
g_connman->ForEachNode([&inv](CNode* pnode) g_connman->ForEachNode([&inv](CNode* pnode) {
{
pnode->PushInventory(inv); pnode->PushInventory(inv);
}); });
return true; return TransactionError::OK;
} }

View file

@ -1,17 +1,16 @@
// Copyright (c) 2017-2018 The Bitcoin Core developers // Copyright (c) 2017-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_NODE_TRANSACTION_H #ifndef BITCOIN_NODE_TRANSACTION_H
#define BITCOIN_NODE_TRANSACTION_H #define BITCOIN_NODE_TRANSACTION_H
#include <attributes.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <uint256.h> #include <uint256.h>
enum class TransactionError { enum class TransactionError {
OK = 0, OK, //!< No error
UNKNOWN_ERROR,
MISSING_INPUTS, MISSING_INPUTS,
ALREADY_IN_CHAIN, ALREADY_IN_CHAIN,
P2P_DISABLED, P2P_DISABLED,
@ -20,24 +19,19 @@ enum class TransactionError {
INVALID_PSBT, INVALID_PSBT,
PSBT_MISMATCH, PSBT_MISMATCH,
SIGHASH_MISMATCH, SIGHASH_MISMATCH,
ERROR_COUNT
}; };
#define TRANSACTION_ERR_LAST TransactionError::ERROR_COUNT std::string TransactionErrorString(const TransactionError error);
const char* TransactionErrorString(const TransactionError error);
/** /**
* Broadcast a transaction * Broadcast a transaction
* *
* @param[in] tx the transaction to broadcast * @param[in] tx the transaction to broadcast
* @param[out] &txid the txid of the transaction, if successfully broadcast * @param[out] &txid the txid of the transaction, if successfully broadcast
* @param[out] &error reference to UniValue to fill with error info on failure
* @param[out] &err_string reference to std::string to fill with error string if available * @param[out] &err_string reference to std::string to fill with error string if available
* @param[in] allowhighfees whether to allow fees exceeding maxTxFee * @param[in] highfee Reject txs with fees higher than this (if 0, accept any fee)
* return true on success, false on error (and fills in `error`) * return error
*/ */
bool BroadcastTransaction(CTransactionRef tx, uint256& txid, TransactionError& error, std::string& err_string, bool allowhighfees = false); NODISCARD TransactionError BroadcastTransaction(CTransactionRef tx, uint256& txid, std::string& err_string, const CAmount& highfee);
#endif // BITCOIN_NODE_TRANSACTION_H #endif // BITCOIN_NODE_TRANSACTION_H

View file

@ -309,21 +309,19 @@ bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransacti
return true; return true;
} }
bool CombinePSBTs(PartiallySignedTransaction& out, TransactionError& error, const std::vector<PartiallySignedTransaction>& psbtxs) TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs)
{ {
out = psbtxs[0]; // Copy the first one out = psbtxs[0]; // Copy the first one
// Merge // Merge
for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) { for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
if (!out.Merge(*it)) { if (!out.Merge(*it)) {
error = TransactionError::PSBT_MISMATCH; return TransactionError::PSBT_MISMATCH;
return false;
} }
} }
if (!out.IsSane()) { if (!out.IsSane()) {
error = TransactionError::INVALID_PSBT; return TransactionError::INVALID_PSBT;
return false;
} }
return true; return TransactionError::OK;
} }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2009-2018 The Bitcoin Core developers // Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -575,10 +575,9 @@ bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransacti
* Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial signatures from each input. * Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial signatures from each input.
* *
* @param[out] &out the combined PSBT, if successful * @param[out] &out the combined PSBT, if successful
* @param[out] &error reference to TransactionError to fill with error info on failure
* @param[in] psbtxs the PSBTs to combine * @param[in] psbtxs the PSBTs to combine
* @return True if we successfully combined the transactions, false if they were not compatible * @return error (OK if we successfully combined the transactions, other error if they were not compatible)
*/ */
bool CombinePSBTs(PartiallySignedTransaction& out, TransactionError& error, const std::vector<PartiallySignedTransaction>& psbtxs); NODISCARD TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs);
#endif // BITCOIN_PSBT_H #endif // BITCOIN_PSBT_H

View file

@ -1065,10 +1065,11 @@ static UniValue sendrawtransaction(const JSONRPCRequest& request)
bool allowhighfees = false; bool allowhighfees = false;
if (!request.params[1].isNull()) allowhighfees = request.params[1].get_bool(); if (!request.params[1].isNull()) allowhighfees = request.params[1].get_bool();
const CAmount highfee{allowhighfees ? 0 : ::maxTxFee};
uint256 txid; uint256 txid;
TransactionError err;
std::string err_string; std::string err_string;
if (!BroadcastTransaction(tx, txid, err, err_string, allowhighfees)) { const TransactionError err = BroadcastTransaction(tx, txid, err_string, highfee);
if (TransactionError::OK != err) {
throw JSONRPCTransactionError(err, err_string); throw JSONRPCTransactionError(err, err_string);
} }
@ -1493,8 +1494,8 @@ UniValue combinepsbt(const JSONRPCRequest& request)
} }
PartiallySignedTransaction merged_psbt; PartiallySignedTransaction merged_psbt;
TransactionError error; const TransactionError error = CombinePSBTs(merged_psbt, psbtxs);
if (!CombinePSBTs(merged_psbt, error, psbtxs)) { if (error != TransactionError::OK) {
throw JSONRPCTransactionError(error); throw JSONRPCTransactionError(error);
} }

View file

@ -4,7 +4,7 @@
#include <wallet/psbtwallet.h> #include <wallet/psbtwallet.h>
bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, TransactionError& error, bool& complete, int sighash_type, bool sign, bool bip32derivs) TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, bool& complete, int sighash_type, bool sign, bool bip32derivs)
{ {
LOCK(pwallet->cs_wallet); LOCK(pwallet->cs_wallet);
// Get all of the previous transactions // Get all of the previous transactions
@ -19,8 +19,7 @@ bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, Transac
// Verify input looks sane. This will check that we have at most one uxto, witness or non-witness. // Verify input looks sane. This will check that we have at most one uxto, witness or non-witness.
if (!input.IsSane()) { if (!input.IsSane()) {
error = TransactionError::INVALID_PSBT; return TransactionError::INVALID_PSBT;
return false;
} }
// If we have no utxo, grab it from the wallet. // If we have no utxo, grab it from the wallet.
@ -37,8 +36,7 @@ bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, Transac
// Get the Sighash type // Get the Sighash type
if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) { if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
error = TransactionError::SIGHASH_MISMATCH; return TransactionError::SIGHASH_MISMATCH;
return false;
} }
complete &= SignPSBTInput(HidingSigningProvider(pwallet, !sign, !bip32derivs), psbtx, i, sighash_type); complete &= SignPSBTInput(HidingSigningProvider(pwallet, !sign, !bip32derivs), psbtx, i, sighash_type);
@ -58,5 +56,5 @@ bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, Transac
psbt_out.FromSignatureData(sigdata); psbt_out.FromSignatureData(sigdata);
} }
return true; return TransactionError::OK;
} }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2009-2018 The Bitcoin Core developers // Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -18,16 +18,14 @@
* *
* @param[in] pwallet pointer to a wallet * @param[in] pwallet pointer to a wallet
* @param[in] &psbtx reference to PartiallySignedTransaction to fill in * @param[in] &psbtx reference to PartiallySignedTransaction to fill in
* @param[out] &error reference to UniValue to fill with error info on failure
* @param[out] &complete indicates whether the PSBT is now complete * @param[out] &complete indicates whether the PSBT is now complete
* @param[in] sighash_type the sighash type to use when signing (if PSBT does not specify) * @param[in] sighash_type the sighash type to use when signing (if PSBT does not specify)
* @param[in] sign whether to sign or not * @param[in] sign whether to sign or not
* @param[in] bip32derivs whether to fill in bip32 derivation information if available * @param[in] bip32derivs whether to fill in bip32 derivation information if available
* return true on success, false on error (and fills in `error`) * return error
*/ */
bool FillPSBT(const CWallet* pwallet, NODISCARD TransactionError FillPSBT(const CWallet* pwallet,
PartiallySignedTransaction& psbtx, PartiallySignedTransaction& psbtx,
TransactionError& error,
bool& complete, bool& complete,
int sighash_type = 1 /* SIGHASH_ALL */, int sighash_type = 1 /* SIGHASH_ALL */,
bool sign = true, bool sign = true,

View file

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

View file

@ -62,9 +62,8 @@ BOOST_AUTO_TEST_CASE(psbt_updater_test)
ssData >> psbtx; ssData >> psbtx;
// Fill transaction with our data // Fill transaction with our data
TransactionError err;
bool complete = true; bool complete = true;
FillPSBT(&m_wallet, psbtx, err, complete, SIGHASH_ALL, false, true); BOOST_REQUIRE_EQUAL(TransactionError::OK, FillPSBT(&m_wallet, psbtx, complete, SIGHASH_ALL, false, true));
// Get the final tx // Get the final tx
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);