fix(bids): enforce deterministic ranking, winner selection, and cursored pagination - #2553
Open
Madukanino wants to merge 2 commits into
Open
Conversation
|
@Madukanino Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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 #2446
fix(bids): deterministic bid ranking, winner selection, and cursored pagination semantics
Description
This PR provides production-grade guarantees for bid acceptance, ranking, expiry, winner selection, and cursored query pagination. It guarantees deterministic evaluation and mathematical total ordering across all validators while eliminating stale or adversarial inputs across normal, invalid, repeated, concurrent, and failure conditions.
Technical Highlights & Invariants
Deterministic Multi-Key Ranking & Tiebreaking (
BidStorage::compare_bids):expected_return.saturating_sub(bid_amount).max(0)(descending)expected_return(descending)bid_amount(descending)timestamp(descending, newer bids first)bid_id.to_array()(lexicographical tiebreaker)get_best_bid(...) == rank_bids(...).get(0).Stale & Adversarial Input Elimination:
load_accept_bid_contextandaccept_bid_implvalidate bid status and expiration, returningQuickLendXError::BidStalefor non-Placedor expired bids.rank_bids,get_best_bid,rank_bids_paged) filter expired bids in real time prior to lazy compaction.Safe Bounded Cursored Pagination (
pagination.rs):MAX_QUERY_LIMIT = 50, safe slice bounding, and string-encoded cursors ("{generation}_{offset}").require_stable_cursordetects and halts iteration withQuickLendXError::UnstableCursorupon mid-pagination state mutation.get_ranked_bids_paged(invoice_id, offset, limit)->PaginatedBidsget_bid_history_paged(invoice_id, status_filter, offset, limit)->PaginatedBidsget_investor_bids_paged(investor, status_filter, offset, limit)->PaginatedBidsResource Footprint & Storage Isolation:
get_active_bid_countto use indexed counts directly without redundant O(N) deserializations.Validation Performed
cargo test --lib(98 passed, 0 failed, 3 ignored legacy specs).quicklendx-contracts/src/test_bid_ranking_determinism.rscovering mathematical ordering, tiebreakers, empty slices, pagination boundaries, and cursor stability.quicklendx-contracts/src/test_bid_concurrency.rsvalidating concurrent placement, race safety, retry after conflict, and capacity limits.cargo check --target wasm32v1-nonecompleted with 0 errors.cargo fmt --all --checkpassed cleanly.