refactor!: pass Target to BnbMetric methods instead of storing it#52
Merged
evanlinjin merged 1 commit intoJul 2, 2026
Merged
Conversation
72f46ec to
fc8bac3
Compare
`BnbMetric::score`/`bound`/`drain` now take `target: Target` as a parameter, and `CoinSelector::run_bnb`/`bnb_solutions` gain a leading `target` argument. `LowestFee` and `Changeless` no longer store a `target` field. Target is the problem being solved (a constraint), not part of the metric's objective config, and it's already passed as a parameter everywhere else in `CoinSelector` (`excess`, `is_target_met`, `drain`). Threading it through the metric methods aligns them with that style and, as a bonus, removes the `target` that `Changeless<M>` previously had to keep in sync with its inner metric — a target mismatch across composed metrics is now unrepresentable. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
fc8bac3 to
304ac2d
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
BnbMetricimplementations currently each store their ownTarget(LowestFee { target, .. },Changeless { target, inner }). That has two problems:Changeless<M>duplicates the target and must keep its copy in sync with the inner metric's — a silent-mismatch footgun (both fields are public and set independently).targetis always a parameter:cs.excess(target, drain),cs.is_target_met(target),cs.drain(target, policy). The metric structs are the only place target is baked into state.What this does
Make
targeta parameter of the metric methods (and the BnB entry points):Consequently:
LowestFee→{ long_term_feerate, dust_relay_feerate, drain_weights }(notarget).Changeless<M>→Changeless<M>(pub M)— withtargetgone it collapses to a single field, so it becomes a plain newtype:Changeless(LowestFee { .. }). The duplicate target is gone by construction, so the "must match inner's target" invariant is deleted rather than documented.BnbIterholds the singletargetand threads it to the metric.Conceptually this reflects the right split:
targetis the problem/constraints; the metric is the objective. The feerates/drain-weights stay in the metric (objective config); target — universal and problem-level — becomes a shared input, so a mismatch across composed metrics is unrepresentable.Argument order is
cs, thentarget(then the rest), keeping the samecs-before-targetrelative order the library already uses (wherecsis usually the receiver).Notes
targetfield and its plumbing simply disappear, so the net diff is small despite the wide surface.cargo fmt/clippy/doc/ full test suite + doctests /no_stdbuild are all green.🤖 Generated with Claude Code