Cache CL8Y fee-discount tier on pair with 5-minute TTL (reduce per-swap registry query) #251
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#251
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
Add a short-lived on-pair cache of the trader’s CL8Y fee-discount tier so
execute_swap(and optionally limit placement) does not call the fee-discount registry on every transaction. Cache entries expire after 5 minutes (wall-clock block time).Current codebase
effective_fee_bps_with_deregister_msgs→lookup_effective_fee_bps(contract.rs~46–75, ~824–832).DISCOUNT_REGISTRYis set, each swap performsquery_wasm_smartto registryGetDiscount { trader, sender }— cross-contract query gas on every hybrid swap.discount_registry == None.deregister_msgswhen tier is single-use (deregister_msgs_for_discount).Why this is needed
Discount tier changes are infrequent relative to swap frequency. A 5-minute TTL removes a fixed cross-contract query from the hot path, reducing hybrid swap gas for CL8Y tier holders and all users on pairs with registry configured.
Constraints / guardrails
(trader_addr, sender_addr)or trader-only if sender irrelevant for tier — match currentGetDiscountsemantics exactly.Mapon pair, e.g.DISCOUNT_CACHE: Map<(Addr, Addr), DiscountCacheEntry { effective_fee_bps, discount_meta, cached_at }>— bound entry size; no unbounded growth (TTL + overwrite per key).execute_place_limit_orders_batch— document in PR.may_loaddefaults.HybridSimulationwithtradershould use same cache logic for parity with execute.Relevant files
smartcontracts/contracts/pair/src/contract.rslookup_effective_fee_bps, swap + sim pathssmartcontracts/contracts/pair/src/state.rssmartcontracts/contracts/fee-discount/smartcontracts/packages/dex-common/src/pair.rssmartcontracts/tests/docs/reference/fee-discount-tiers.mdfrontend-dapp/Recommended solution direction
DISCOUNT_TTL_SECONDS: u64 = 300and cache map instate.rs.lookup_effective_fee_bps→lookup_effective_fee_bps_cached(deps, block_time, ...):block_time - cached_at < TTLand tier not consumable (or policy defined): return cached bps.effective_fee_bps_for_simfor query parity.Acceptance criteria
HybridSimulationwith trader matches execute fee within cache window.Test plan — functional paths
effective_fee_bps.cached_at + 299→ cache hit.cached_at + 301→ cache miss, re-query.Test plan — attack / abuse vectors
Verification criteria
smartcontracts/tests/for cache hit/miss/TTL.docs/reference/fee-discount-tiers.mdupdated with TTL semantics.mentioned in commit
d2be3b8d6cImplementation (merged to
main—d2be3b8)Added a 5-minute (300s) on-pair cache for CL8Y fee-discount registry lookups so
execute_swapand limit placement avoid aGetDiscountcross-contract query on every tx when the same(trader, sender)repeats within TTL.What changed
smartcontracts/contracts/pair/src/discount_cache.rs— cache read/write/invalidate; TTL fromdex_common::pair::DISCOUNT_CACHE_TTL_SECONDSDISCOUNT_CACHEmap in pairstate.rs; pair contract version 1.7.0needs_deregistertiers are never cached; cache entry removed when deregister submessages are emittedHybridSimulation/ reverse sim read cache (no write) for parity with execute within TTLdiscount_cache_hit_within_ttl_after_registry_upgrade,discount_cache_ttl_boundary,discount_cache_hybrid_sim_matches_executedocs/reference/fee-discount-tiers.mdAGENTS_FEE_DISCOUNT_TIERS.md,AGENTS_TERRACLASSIC_GAS.md,AGENTS_HYBRID_QUOTING.mdVerification checklist
effective_fee_bpsevent; lowergas_usedvs first swap (LocalTerra / columbus benchmark)cached_at + 301s: registry re-queried; fee reflects current tierneeds_deregisterpath: deregister still fires once; no double-apply via cacheHybridSimulationwithtradermatches execute fee when cache warmdiscount_registry: no cache storage overheadcargo test -p cl8y-dex-tests fee_discount_testsandhybrid_simulation_matches_execute_with_fee_discountpass on CIFollow-ups
DISCOUNT_CACHEmap by default).@qa-agent-team — please verify the checklist above on LocalTerra (or staging) after pair wasm upgrade; leave this issue open until sign-off.
mentioned in issue #252
Verified the CL8Y fee-discount cache against the source on
6b22feb, plus observed its gas effect live in the #252 benchmark.Works as designed (source-confirmed in discount_cache.rs + contract.rs):
Gas effect (observed live in #252): the first fee-bearing op per (trader, sender) key per window pays ~145k extra for the cross-contract registry query + the cache write; warm ops within 300s skip both. That's the cold/warm split in the #252 table (cold M=1 625,896 vs warm 481,066). Cache key is (trader, sender), and on a swap trader defaults to sender when the hook omits it — so each distinct trader pays its own cold cost once per window.
Layer, being explicit: all five behavior points are source-verified; the cold/warm gas delta was observed live in the benchmark. The invalidation and staleness paths are source-confirmed but not separately exercised on-chain in a dedicated test here.
One tradeoff to confirm: a registry discount change is not reflected for up to 300s (until TTL expiry), except revokes — those invalidate immediately via the deregister path. So the effective fee can be up to 5 min stale on a discount increase/decrease that isn't a revoke.
@PlasticDigits — confirm 300s is the intended TTL and that up-to-5-min fee staleness (non-revoke changes) is acceptable. If yes, I'm good signing off the cache behavior here; the staleness window is the only thing I'd want your explicit OK on before this closes.
We accept the 5 min staleness on fees, closing. Note that future observation of traders to identify exploitation of the 5 min staleness should be done, but is not critical enough to require anything other than casual observation (no metrics needed)
mentioned in issue #258
mentioned in issue #275
mentioned in merge request !747