feat: add CoinSelector::select_srd (Single Random Draw)#50
Open
evanlinjin wants to merge 1 commit into
Open
Conversation
932e9f0 to
a28b8ff
Compare
c44425b to
f07df30
Compare
evanlinjin
commented
Jul 2, 2026
1381fd4 to
99af2c6
Compare
Port of Bitcoin Core's `SelectCoinsSRD`: add candidates in random order until the change reaches a fixed `change_lower`, yielding a healthy-sized (privacy-friendly) change output rather than minimizing fees. Like Core, the threshold is fixed and the change amount comes out random from the draw itself. The stop condition reuses `excess(target, drain_weights)`, so no new fee math. RNG is a dependency-free `impl FnMut() -> u64`. Adds the `CHANGE_LOWER` constant (Core's 50k). Maximum-selection-weight handling is left as a TODO pending the max-weight PR. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
99af2c6 to
7ab6377
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Adds Single Random Draw coin selection — a port of Bitcoin Core's
SelectCoinsSRD. Unlikerun_bnbwithLowestFee(which minimizes fees), SRD deliberately aims for a healthy-sized, privacy-friendly change output, avoiding tiny "toxic" change. It's the natural complement to the fee-minimizing metric from #49.What it does
CoinSelector::select_srdadds candidates in random order until the change reacheschange_lower, then returns theDrainto attach (orInsufficientFunds).change_lower; the change amount comes out random on its own, because candidates are added in random order and the final change is wherever the last random input pushed it. Core uses the same fixed-threshold approach (and its comment notes SRD "will result in a random change amount anyway"), so there's no need to randomize the target.excess(target, drain_weights), so there's no new fee math — it inherits the existing rate/absolute/replacement handling.impl FnMut() -> u64(the crate is deliberately dependency-free), used to shuffle the candidates.CHANGE_LOWER(Core's 50k) is added as the sensible default forchange_lower.max_selection_weighthandling (evicting the least-valuable inputs when over a weight cap) is left as a documented// TODOpending the max-weight PR.Tests
tests/srd.rscovers: change ≥change_lowerand target met across 300 seeds; a customchange_loweris honored; insufficient funds; per-seed determinism; preselected inputs preserved. Full suite +no_stdbuild + clippy are green.🤖 Generated with Claude Code