Skip to content

Commit 974b16b

Browse files
committed
refactor: Avoid copying util::Result values
Copying util::Result values is less efficient than moving them because they allocate memory and contain strings. Also this is needed to avoid compile errors in the next commit which adds a std::unique_ptr member to util::Result which implicity disables copying.
1 parent cdf6329 commit 974b16b

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/wallet/spend.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ util::Result<SelectionResult> AutomaticCoinSelection(const CWallet& wallet, Coin
818818
// Coin Selection attempts to select inputs from a pool of eligible UTXOs to fund the
819819
// transaction at a target feerate. If an attempt fails, more attempts may be made using a more
820820
// permissive CoinEligibilityFilter.
821-
util::Result<SelectionResult> res = [&] {
821+
{
822822
// Place coins eligibility filters on a scope increasing order.
823823
std::vector<SelectionFilter> ordered_filters{
824824
// If possible, fund the transaction with confirmed UTXOs only. Prefer at least six
@@ -866,7 +866,7 @@ util::Result<SelectionResult> AutomaticCoinSelection(const CWallet& wallet, Coin
866866
if (CAmount total_amount = available_coins.GetTotalAmount() - total_discarded < value_to_select) {
867867
// Special case, too-long-mempool cluster.
868868
if (total_amount + total_unconf_long_chain > value_to_select) {
869-
return util::Result<SelectionResult>({_("Unconfirmed UTXOs are available, but spending them creates a chain of transactions that will be rejected by the mempool")});
869+
return util::Error{_("Unconfirmed UTXOs are available, but spending them creates a chain of transactions that will be rejected by the mempool")};
870870
}
871871
return util::Result<SelectionResult>(util::Error()); // General "Insufficient Funds"
872872
}
@@ -885,19 +885,17 @@ util::Result<SelectionResult> AutomaticCoinSelection(const CWallet& wallet, Coin
885885
// If any specific error message appears here, then something particularly wrong might have happened.
886886
// Save the error and continue the selection process. So if no solutions gets found, we can return
887887
// the detailed error to the upper layers.
888-
if (HasErrorMsg(res)) res_detailed_errors.emplace_back(res);
888+
if (HasErrorMsg(res)) res_detailed_errors.emplace_back(std::move(res));
889889
}
890890
}
891891

892892
// Return right away if we have a detailed error
893-
if (!res_detailed_errors.empty()) return res_detailed_errors.front();
893+
if (!res_detailed_errors.empty()) return std::move(res_detailed_errors.front());
894894

895895

896896
// General "Insufficient Funds"
897897
return util::Result<SelectionResult>(util::Error());
898-
}();
899-
900-
return res;
898+
}
901899
}
902900

903901
static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, const uint256& block_hash)

src/wallet/test/fuzz/coinselection.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ FUZZ_TARGET(coinselection)
291291
}
292292

293293
std::vector<COutput> utxos;
294-
std::vector<util::Result<SelectionResult>> results{result_srd, result_knapsack, result_bnb};
294+
std::vector<util::Result<SelectionResult>> results;
295+
results.emplace_back(std::move(result_srd));
296+
results.emplace_back(std::move(result_knapsack));
297+
results.emplace_back(std::move(result_bnb));
295298
CAmount new_total_balance{CreateCoins(fuzzed_data_provider, utxos, coin_params, next_locktime)};
296299
if (new_total_balance > 0) {
297300
std::set<std::shared_ptr<COutput>> new_utxo_pool;

0 commit comments

Comments
 (0)