feat(routing): fund waterfall tiers in descending score order - #836
Open
woahwhattheheck wants to merge 3 commits into
Open
feat(routing): fund waterfall tiers in descending score order#836woahwhattheheck wants to merge 3 commits into
woahwhattheheck wants to merge 3 commits into
Conversation
Tiers were always funded in declaration order, so a lower-value tier declared first would take the funds ahead of a preferred one. Tiers are now ordered by score before execution, with declaration order preserved among equal scores. Ordering is done on a copy via an index tiebreak, so the caller's array is untouched and ties keep FIFO order independently of engine sort stability. A non-finite score is rejected rather than silently producing an arbitrary order through a NaN comparator.
4 tasks
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.
Closes #777.
WaterfallRouterfunded tiers strictly in declaration order, so alower-value tier listed first took the funds ahead of a preferred one, with
no way to express preference short of reordering the array.
Changes
src/types/routing.ts—WaterfallTiergainsscore?: number(higher isfunded first, default
0).src/routing/WaterfallRouter.ts— tiers are ordered by score descendingbefore the waterfall runs; equal scores keep declaration order.
A config with no scores behaves exactly as before, so existing callers are
unaffected.
Ordering is total, not merely stable
The sort decorates each tier with its original index and breaks ties on that
index:
Array.prototype.sorthas been stable since ES2019, but an explicit tiebreakmakes the ordering total, so FIFO among equal scores holds regardless of the
engine or compile target rather than resting on that guarantee. The tiers are
sorted on a copy, so the caller's array is never reordered — there is a test
for that.
A non-finite score is rejected
NaNin a comparator makes it returnNaN, which is neither<0,0, nor>0. The sort then produces an arbitrary order with no error — the fundingsequence silently becomes undefined. So
scoreis validated alongside theexisting
minimumAmountcheck and throwsValidationError, consistent withhow this file already reports bad tier input.
Infinityis rejected on thesame grounds.
Validation
npx vitest run test/waterfallRouter.test.ts— 18/18 passing: the 9pre-existing cases plus 9 new ones covering unscored configs (unchanged
order), descending order, unscored-treated-as-0, negative scores, FIFO
across 8 equal-scored tiers, FIFO within each score group when groups
interleave, a high score winning limited funds, caller-array immutability,
and each non-finite score.
npx tsc --noEmitreports 210 errors on this branch and 210 on unmodifiedmain— identical, with none in either changed file. That baseline ispre-existing and unrelated to this change.