Cap expired limit order parking during hybrid match walks (max 5 per swap) #250
Labels
No labels
agent:fix_bugfix
agent:fix_conflicts
agent:fix_security
agent:gap_analysis
agent:implement
agent:implement
agent:implement
agent:open_issues
agent:ready
agent:research
agent:security_audit
agent:verify
architecture
backend
blocker:hybrid
blocker:launch
blocker:limit-orders
blocker:v2
block:log_only
block:security
bug
ci
contracts
correctness
deploy
dev
devops
docs
documentation
duplicate
e2e
enhancement
epic
feature
frontend
functional-completion
gas
good first issue
governance
help wanted
high-risk
hooks
hybrid
indexer
infra
infrastructure
integrators
invalid
launch-blocker
limit-orders
localnet
localterra
low priority
missing-implementation
needs-design
ops
performance
priority
high
priority
medium
product
qa
QA
question
ready
ready
research
scripts
security
security-hardening
smartcontracts
tech-debt
testing
ux
UX
v2
verification
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
code/cl8y-dex-terraclassic#250
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Stop takers from unbounded gas spend cleaning up expired resting orders during
match_bids/match_asks. Cappark_expired_limit_order_for_claimcalls to 5 per swap; beyond the cap, skip expired orders without parking (leave for maker claim via separate path or future keeper).Current codebase
orderbook.rs~632–645 bids, ~811–824 asks): whenorder.expires_at <= now, callspark_expired_limit_order_for_claim:unlink_order(DLL repair + storage remove)EXPIRED_LIMIT_CLAIMS.save(new map row)PENDING_ESCROW_*unchanged until maker claimslimit_order_expired_parkedeventmax_maker_fillscaps profitable fills but does not cap expiry parks (expired orders do not incrementmakers_used).Why this is needed
Expired orders are the maker’s responsibility to cancel/claim. Requiring every taker to subsidize unbounded expiry cleanup is a gas griefing surface: attacker (or inactive MM) leaves many expired limits at best prices → every hybrid swap pays linear park cost before matching real liquidity.
Constraints / guardrails
match_bids/match_asksinvocation (constant index-commonororderbook.rs, e.g.MAX_EXPIRED_PARKS_PER_SWAP = 5).cur = next_ptrwithout storage write for additional expired orders (they remain on book until another tx parks them or maker cancels). Document this explicitly — trade-off vs leaving stale head.max_maker_fillsstill limits profitable matches only.CancelLimitOrderor get parked on a later swap.simulate_match_bids/simulate_match_asksto mirror skip logic (today sim skips expired without parking already — align execute with sim where possible).Relevant files
smartcontracts/contracts/pair/src/orderbook.rsmatch_bids,match_asks,park_expired_limit_order_for_claimsmartcontracts/packages/dex-common/src/pair.rssmartcontracts/contracts/pair/src/error.rssmartcontracts/tests/src/limit_order_tests.rsindexer/src/indexer/parser.rsdocs/limit-orders.md,docs/contracts-security-audit.mdRecommended solution direction
MAX_EXPIRED_PARKS_PER_SWAP: u32 = 5index-common.match_bids/match_asks, trackexpired_parks: u32; on expired order:expired_parks < MAX: park as today, increment counter.cur = next_ptr; continue(no storage mutation).expired_parks_capped=true,expired_parks_skipped=N(optional, for indexers).Acceptance criteria
EXPIRED_LIMIT_CLAIMSwrite (per skip design).PENDING_ESCROW_*).Test plan — functional paths
Test plan — attack / abuse vectors
CancelLimitOrderon skipped expired order on book.Verification criteria
make test-contractswith new expiry-cap tests green.docs/limit-orders.md§ Expiry updated with cap + skip semantics.Implementation summary (merged to
main@ff7a680)Capped expired limit order parking during hybrid
match_bids/match_askswalks to bound taker gas when many expired limits stack at the book head.Contract changes
MAX_EXPIRED_PARKS_PER_SWAP = 5index-common.match_bids/match_askstrackexpired_parks/expired_parks_skipped:park_expired_limit_order_for_claim(unchanged semantics).cur = next_ptr; continue).expired_parks_used,expired_parks_capped=true,expired_parks_skipped(when applicable).max_maker_fillsunchanged; expired parks do not incrementmakers_used.PENDING_ESCROW_*unchanged for skipped orders; makers can stillCancelLimitOrderon skipped expired rows.Docs / invariants
docs/limit-orders.md§ Expiry + tx attrs table.docs/contracts-security-audit.md.skills/AGENTS_TERRACLASSIC_GAS.mdandskills/AGENTS_FRONTEND_LIMIT_PARKED_EXPIRED.md.Tests added
orderbook::expired_park_cap_tests::*hybrid_walk_parks_at_most_five_expired_bids_per_swap,hybrid_walk_three_expired_bids_all_parked_then_fills_live_bid,hybrid_walk_ten_expired_asks_parks_five_skips_five,skipped_expired_bid_cancelable_by_maker,hybrid_simulation_matches_execute_with_expired_park_capVerification checklist
make test-contractsgreen (orcargo test -p cl8y-dex-pair expired_park_cap+cargo test -p cl8y-dex-tests hybrid_walk skipped_expired hybrid_simulation_matches_execute_with_expired)limit_order_expired_parkedevents per book side walkedExpiredLimitRefundrow until a later park/claim pathCancelLimitOrdersucceedsHybridSimulationreturn amount matches execute when fillable liquidity exists behind expired stackexpired_parks_used/expired_parks_skippedpresent when cap bitesFollow-ups (optional)
HYBRID_SWAP_*constants is desired.@qa-agent-team — please verify on LocalTerra or CI: run the checklist above against
mainafter pair wasm rebuild/deploy. Leave this issue open until QA signs off.mentioned in issue #252
mentioned in issue #254
mentioned in issue #259
mentioned in issue #262
mentioned in issue #263
marked as related to #263
mentioned in commit
ff7a680b9e