Skip to content

Commit 1502231

Browse files
committed
coinselection: Track whether CG completed
CoinGrinder may not be able to exhaustively search all potentially interesting combinations for large UTXO pools, so we keep track of whether the search was terminated by the iteration limit.
1 parent 7488acc commit 1502231

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/wallet/coinselection.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
426426

427427
if (curr_try >= TOTAL_TRIES) {
428428
// Solution is not guaranteed to be optimal if `curr_try` hit TOTAL_TRIES
429+
result.SetAlgoCompleted(false);
429430
break;
430431
}
431432

@@ -447,6 +448,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
447448
// Set `next_utxo` to one after last selected, then deselect last selected UTXO
448449
if (curr_selection.empty()) {
449450
// Exhausted search space before running into attempt limit
451+
result.SetAlgoCompleted(true);
450452
break;
451453
}
452454
next_utxo = curr_selection.back() + 1;
@@ -794,6 +796,16 @@ void SelectionResult::ComputeAndSetWaste(const CAmount min_viable_change, const
794796
}
795797
}
796798

799+
void SelectionResult::SetAlgoCompleted(bool algo_completed)
800+
{
801+
m_algo_completed = algo_completed;
802+
}
803+
804+
bool SelectionResult::GetAlgoCompleted() const
805+
{
806+
return m_algo_completed;
807+
}
808+
797809
void SelectionResult::SetSelectionsEvaluated(size_t attempts)
798810
{
799811
m_selections_evaluated = attempts;

src/wallet/coinselection.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ struct SelectionResult
330330
bool m_use_effective{false};
331331
/** The computed waste */
332332
std::optional<CAmount> m_waste;
333+
/** False if algorithm was cut short by hitting limit of attempts and solution is non-optimal */
334+
bool m_algo_completed{true};
333335
/** The count of selections that were evaluated by this coin selection attempt */
334336
size_t m_selections_evaluated;
335337
/** Total weight of the selected inputs */
@@ -389,6 +391,12 @@ struct SelectionResult
389391
void ComputeAndSetWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee);
390392
[[nodiscard]] CAmount GetWaste() const;
391393

394+
/** Tracks that algorithm was able to exhaustively search the entire combination space before hitting limit of tries */
395+
void SetAlgoCompleted(bool algo_completed);
396+
397+
/** Get m_algo_completed */
398+
bool GetAlgoCompleted() const;
399+
392400
/** Record the number of selections that were evaluated */
393401
void SetSelectionsEvaluated(size_t attempts);
394402

0 commit comments

Comments
 (0)