fix(stableswap)!: fix peg ordering by accepting and co-sorting asset-peg pairs#1424
fix(stableswap)!: fix peg ordering by accepting and co-sorting asset-peg pairs#1424khuzama98 wants to merge 9 commits into
Conversation
|
Crate versions that have been updated:
Runtime version has been increased. |
There was a problem hiding this comment.
Pull request overview
Fixes a correctness bug in the Stableswap pallet where assets were internally sorted during pool creation but peg sources were not co-sorted, causing silent peg/asset misalignment and incorrect pricing.
Changes:
- Breaking API change:
create_pool_with_pegsnow takes explicit(asset_id, peg_source)pairs and co-sorts them byasset_idbefore storage. - Moves/clarifies sorting responsibility:
create_poolsorts assets at dispatch;do_create_poolno longer sorts internally and expects pre-sorted assets. - Adds/updates extensive unit + integration regression coverage for peg ordering, and bumps versions accordingly.
Reviewed changes
Copilot reviewed 22 out of 23 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| runtime/hydradx/src/lib.rs | Bumps runtime spec_version to reflect the breaking change. |
| runtime/hydradx/Cargo.toml | Bumps runtime crate version. |
| pallets/stableswap/src/lib.rs | Changes peg-pool creation API to asset/peg pairs, co-sorts pairs, removes internal sorting in do_create_pool, and adds a sorted-assets debug assertion for peg resolution. |
| pallets/stableswap/src/benchmarks.rs | Updates benchmarks to call create_pool_with_pegs using (asset, peg) pairs. |
| pallets/stableswap/README.md | Updates documentation to describe (asset_id, peg_source) pairing and sorting semantics. |
| pallets/stableswap/Cargo.toml | Major version bump for the breaking pallet API. |
| pallets/stableswap/src/tests/mod.rs | Registers new peg_ordering test module. |
| pallets/stableswap/src/tests/mock.rs | Updates test ExtBuilder to construct (asset, peg) pairs when creating peg pools. |
| pallets/stableswap/src/tests/peg.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/peg_one.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/peg_ordering.rs | Adds dedicated regression tests ensuring peg sources are co-sorted with assets and pricing behavior matches sorted-reference pools. |
| pallets/stableswap/src/tests/pegs_with_different_decimals.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/remove_liquidity.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/update_peg_source.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/update_max_peg_update.rs | Updates tests to the new peg-pool creation signature. |
| pallets/hsm/src/tests/mock.rs | Updates HSM test ExtBuilder to call create_pool_with_pegs using (asset, peg) pairs. |
| pallets/hsm/src/benchmarks.rs | Updates HSM benchmarks to call create_pool_with_pegs using (asset, peg) pairs. |
| pallets/hsm/Cargo.toml | Patch bump reflecting dependent breaking change adaptation. |
| integration-tests/src/stableswap.rs | Updates existing peg-pool creation usage and adds an integration regression test for reversed-order asset inputs. |
| integration-tests/src/omnipool_liquidity_mining.rs | Updates peg-pool creation usage to the new signature. |
| integration-tests/src/hsm.rs | Updates peg-pool creation usage to the new signature. |
| integration-tests/Cargo.toml | Patch bump for integration tests crate. |
| Cargo.lock | Updates lockfile versions for the bumped crates. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| peg_sources.len(), | ||
| "Pool assets and peg sources must have the same length" | ||
| ); | ||
| debug_assert!( |
There was a problem hiding this comment.
next to debug assert, shall we make it prod runtime failure too?
just like we did recently for trades #1429
| @@ -0,0 +1,626 @@ | |||
| // This file is part of HydraDX. | |||
There was a problem hiding this comment.
the name of this is off, it must be with underscore so pallet_stableswap.rs
so this should not be a new file, rather a modification of our existing weight files.
We possibly have a bug in the benchmark workflow file, could you please check it out that too and fix? thanks
| /// - `ShareAssetInPoolAssets`: If the share asset is among the pool assets. | ||
| /// - `AssetNotRegistered`: If one or more assets are not registered in the AssetRegistry. | ||
| /// - `InvalidAmplification`: If the amplification parameter is invalid. | ||
| /// - `IncorrectInitialPegs`: If the initial pegs are incorrect. |
There was a problem hiding this comment.
what is hte reason this was removed? i still see IncorrectInitialPegs error returned when creating pool
Description
Fixes a peg-ordering bug in
create_pool_with_pegswhere passing assets and pegsources in different orders caused silent misalignment after internal sorting.
Root cause:
do_create_poolsorted assets internally, but the correspondingpeg_sourcearray was not co-sorted — sopeg_source[i]could end up paired withthe wrong asset after sort.
Changes:
create_pool_with_pegsnow acceptsBoundedVec<(AssetId, PegSource), _>instead of separate
assetsandpeg_sourceparameters. Pairing is now explicit atthe call site, making misalignment impossible.
do_create_poolno longer sorts internally (callers are now responsible for providing sorted assets).
create_pool(non-peg variant) sorts assets at dispatch.debug_assert!inget_target_pegsverifying assets arrive pre-sorted.Related Issue
Fixes: #1164
Motivation and Context
A pool created via
create_pool_with_pegswith assets provided in non-ascending orderwould store a peg source at an index that no longer matched its intended asset after the
internal sort. This caused incorrect peg values to be applied to assets, a correctness
issue with direct impact on pool pricing.
How Has This Been Tested?
(
pallets/stableswap/src/tests/peg_ordering.rs)peg.rs,peg_one.rs,peg_ordering.rs,pegs_with_different_decimals.rs,update_peg_source.rs,update_max_peg_update.rs,remove_liquidity.rsintegration-tests/src/stableswap.rsthatcreates a pool with out-of-order assets and asserts correct peg assignment post-sort
Checklist: