Skip to content

Commit 3282fad

Browse files
S3RKfurszy
authored andcommitted
wallet: add assert to SelectionResult::Merge for safety
1 parent c4e3b7d commit 3282fad

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/wallet/coinselection.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,16 @@ void SelectionResult::AddInputs(const std::set<COutput>& inputs, bool subtract_f
452452

453453
void SelectionResult::Merge(const SelectionResult& other)
454454
{
455+
// Obtain the expected selected inputs count after the merge (for now, duplicates are not allowed)
456+
const size_t expected_count = m_selected_inputs.size() + other.m_selected_inputs.size();
457+
455458
m_target += other.m_target;
456459
m_use_effective |= other.m_use_effective;
457460
if (m_algo == SelectionAlgorithm::MANUAL) {
458461
m_algo = other.m_algo;
459462
}
460463
util::insert(m_selected_inputs, other.m_selected_inputs);
464+
assert(m_selected_inputs.size() == expected_count);
461465
}
462466

463467
const std::set<COutput>& SelectionResult::GetInputSet() const

src/wallet/coinselection.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ struct SelectionResult
316316
void ComputeAndSetWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee);
317317
[[nodiscard]] CAmount GetWaste() const;
318318

319+
/**
320+
* Combines the @param[in] other selection result into 'this' selection result.
321+
*
322+
* Important note:
323+
* There must be no shared 'COutput' among the two selection results being combined.
324+
*/
319325
void Merge(const SelectionResult& other);
320326

321327
/** Get m_selected_inputs */

0 commit comments

Comments
 (0)