Skip to content

Commit

Permalink
Track count of attempts to prevent regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
murchandamus committed Jan 5, 2024
1 parent 05448cf commit 3f5213c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c

if (curr_try >= TOTAL_TRIES) {
// Solution is not guaranteed to be optimal if `curr_try` hit TOTAL_TRIES
result.SetSelectionsEvaluated(curr_try);
result.SetAlgoCompleted(false);
break;
}
Expand All @@ -356,6 +357,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
if (curr_selection.empty()) {
// Exhausted search space before running into attempt limit
is_done = true;
result.SetSelectionsEvaluated(curr_try);
result.SetAlgoCompleted(true);
break;
}
Expand Down Expand Up @@ -724,10 +726,20 @@ void SelectionResult::SetAlgoCompleted(bool algo_completed) {
m_algo_completed = algo_completed;
}

bool SelectionResult::GetAlgoCompleted() {
bool SelectionResult::GetAlgoCompleted() const
{
return m_algo_completed;
}

void SelectionResult::SetSelectionsEvaluated(size_t attempts) {
m_selections_evaluated = attempts;
}

size_t SelectionResult::GetSelectionsEvaluated() const
{
return m_selections_evaluated;
}

CAmount SelectionResult::GetWaste() const
{
return *Assert(m_waste);
Expand Down
10 changes: 9 additions & 1 deletion src/wallet/coinselection.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ struct SelectionResult
std::optional<CAmount> m_waste;
/** False if algorithm was cut short by hitting limit of attempts and solution is non-optimal */
bool m_algo_completed{true};
/** The count of selections that were evaluated by this coin selection attempt */
size_t m_selections_evaluated;
/** Total weight of the selected inputs */
int m_weight{0};
/** How much individual inputs overestimated the bump fees for the shared ancestry */
Expand Down Expand Up @@ -393,7 +395,13 @@ struct SelectionResult
void SetAlgoCompleted(bool algo_completed);

/** Get m_algo_completed */
bool GetAlgoCompleted();
bool GetAlgoCompleted() const;

/** Record the number of selections that were evaluated */
void SetSelectionsEvaluated(size_t attempts);

/** Get selections_evaluated */
size_t GetSelectionsEvaluated() const ;

/**
* Combines the @param[in] other selection result into 'this' selection result.
Expand Down
4 changes: 4 additions & 0 deletions src/wallet/test/coinselector_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,8 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
return available_coins;
});
BOOST_CHECK(res);
// If this takes more attempts, the algorithm has regressed
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == 184, "Expected 184 attempts, but got " + std::to_string(res->GetSelectionsEvaluated());
}

{
Expand All @@ -1195,6 +1197,8 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
add_coin(1 * COIN, 1, expected_result);
add_coin(1 * COIN, 2, expected_result);
BOOST_CHECK(EquivalentResult(expected_result, *res));
// If it takes more attempts, the algorithm has regressed
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == 3, "Expected 3 attempts, but got " + std::to_string(res->GetSelectionsEvaluated());
}
}

Expand Down

0 comments on commit 3f5213c

Please sign in to comment.