Skip to content

Re-enable balancer#2505

Open
gztensor wants to merge 16 commits into
devnet-readyfrom
feat/balancer_swap
Open

Re-enable balancer#2505
gztensor wants to merge 16 commits into
devnet-readyfrom
feat/balancer_swap

Conversation

@gztensor
Copy link
Copy Markdown
Contributor

Description

Reintroduce balancer swap. The main PR:

#2290

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Other (please describe):

Breaking Change

See #2290

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have run ./scripts/fix_rust.sh to ensure my code is formatted and linted correctly
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@gztensor gztensor added the skip-cargo-audit This PR fails cargo audit but needs to be merged anyway label Mar 17, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 25, 2026

🛡️ AI Review — Skeptic (security review)

VERDICT: VULNERABLE

BASELINE scrutiny: author has repo write permission and substantial prior subtensor activity; no Gittensor allowlist hit found; branch feat/balancer_swap -> devnet-ready.

I found no .github/ai-review/* or .github/copilot-instructions.md modifications in this PR.

Findings

Sev File Finding
HIGH pallets/subtensor/src/macros/hooks.rs::on_runtime_upgrade Cleanup migration for provided liquidity is added but never wired into runtime upgrade (off-diff)
HIGH Cargo.toml:324 Do not globally patch zstd to author-controlled Git forks inline
HIGH pallets/swap/src/pallet/migrations/migrate_swapv3_to_balancer.rs:55 Do not replace failed price initialization with default weights before deleting old price state inline
MEDIUM Cargo.toml:68 New runtime math dependency is pulled from Git instead of a vetted release inline

Other findings

  • [HIGH] Cleanup migration for provided liquidity is added but never wired into runtime upgrade (pallets/subtensor/src/macros/hooks.rs::on_runtime_upgrade) — The PR adds migrate_cleanup_swap_v3, which moves SubnetTaoProvided and SubnetAlphaInProvided into the active reserves and removes the old maps, but it is not called from pallets/subtensor/src/macros/hooks.rs::on_runtime_upgrade. The rest of the PR removes normal reads/writes of those provided-liquidity maps, so any live values remain orphaned and excluded from reserve/pricing calculations after upgrade. Wire this migration into the Subtensor runtime-upgrade chain before removing use of the old maps.

Prior-comment reconciliation

  • b4b1fd62: not addressedmigrate_cleanup_swap_v3 is still added/exported, but no call was added to the Subtensor on_runtime_upgrade chain.
  • afc4fe7b: not addressed — The [patch.crates-io] entries for zstd-sys and zstd-safe still point to github.com/gztensor/*.
  • f2ba865f: not addressed — Partially changed, but the migration still swallows initialization failure by inserting a default balancer and then clearing the old Swap V3 price state.
  • 52b90a8c: not addressedsafe-bigmath remains a Git dependency used by the runtime swap math.

Conclusion

I found no evidence of intentional backdoor, but this PR is not safe to merge while runtime upgrade state can still be lost/orphaned and build/runtime dependencies are sourced from unvetted Git repositories.


📜 Previous run (superseded)
Sev File Finding Status
HIGH pallets/subtensor/src/macros/hooks.rs on_runtime_upgrade Cleanup migration for provided liquidity is added but never wired into runtime upgrade ➡️ Carried forward to current findings
migrate_cleanup_swap_v3 is still added/exported, but no call was added to the Subtensor on_runtime_upgrade chain.
HIGH Cargo.toml:324 Do not globally patch zstd to author-controlled git forks ❔ Status unknown in current run
HIGH pallets/swap/src/pallet/hooks.rs:20 Migration drops old swap price state even when balancer initialization fails ❔ Status unknown in current run
MEDIUM Cargo.toml:68 New runtime math dependency is pulled from Git instead of a vetted release ➡️ Carried forward to current findings
safe-bigmath remains a Git dependency used by the runtime swap math.

# 🔍 AI Review — Auditor (domain review) has not yet run on this PR.

@github-actions
Copy link
Copy Markdown
Contributor

🔄 AI review updated — Skeptic: VULNERABLE

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review — see the sticky summary comment for the verdict and the inline comments below for specific findings.

Comment thread Cargo.toml
Comment on lines +323 to +324
zstd-sys = { git = "https://github.com/gztensor/zstd-sys" }
zstd-safe = { git = "https://github.com/gztensor/zstd-safe" } No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] Do not globally patch zstd to author-controlled git forks

This globally replaces crates.io zstd-sys and zstd-safe with Git forks under the PR author's account. zstd-sys includes build-time native code, so once CI/builds are approved this is a supply-chain execution path that bypasses normal crates.io provenance. Remove the patch or replace it with a vetted upstream release or an organization-controlled, pinned fork with a documented reason.

Comment on lines +18 to +20
// Cleanup uniswap v3 and migrate to balancer
.saturating_add(
migrations::migrate_swapv3_to_balancer::migrate_swapv3_to_balancer::<T>(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] Migration drops old swap price state even when balancer initialization fails

This newly wires migrate_swapv3_to_balancer, but that migration calls maybe_initialize_palswap(...).unwrap_or_default() and then clears AlphaSqrtPrice and the rest of the V3 maps. If balancer initialization rejects an imbalanced subnet, the error is swallowed and the old price/liquidity state is still deleted, leaving that subnet migrated as if it succeeded. Make the migration fail-safe: only clear old state after successful initialization for every subnet, or preserve/record failures and do not mark the migration complete.

Comment thread Cargo.toml
pallet-subtensor-swap-runtime-api = { path = "pallets/swap/runtime-api", default-features = false }
pallet-subtensor-swap-rpc = { path = "pallets/swap/rpc", default-features = false }
procedural-fork = { path = "support/procedural-fork", default-features = false }
safe-bigmath = { package = "safe-bigmath", default-features = false, git = "https://github.com/sam0x17/safe-bigmath" }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MEDIUM] New runtime math dependency is pulled from Git instead of a vetted release

safe-bigmath is introduced as a Git dependency and is used in runtime swap math. Cargo.lock currently pins a commit, but the dependency lacks normal crates.io provenance and review signals in the manifest. For runtime economic math, use a vetted release or vendor/audit the exact source under organization control before merging.

@github-actions
Copy link
Copy Markdown
Contributor

🔄 AI review updated — Skeptic: VULNERABLE

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review — see the sticky summary comment for the verdict and the inline comments below for specific findings.

Comment thread Cargo.toml
Comment on lines +323 to +324
zstd-sys = { git = "https://github.com/gztensor/zstd-sys" }
zstd-safe = { git = "https://github.com/gztensor/zstd-safe" } No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] Do not globally patch zstd to author-controlled Git forks

This workspace-wide [patch.crates-io] overrides every zstd-sys and zstd-safe consumer to build from github.com/gztensor/*. Those crates run native build logic during CI and developer builds, so this is a supply-chain execution path controlled by the PR author rather than by crates.io checksums or an opentensor-owned fork. Remove the patch or move any required zstd fix to a vetted release/opentensor-controlled source before merging.

Comment on lines +47 to +55
if let Err(error) = crate::Pallet::<T>::maybe_initialize_palswap(netuid, Some(price)) {
log::warn!(
"Migration '{}' failed to initialize balancer with V3 price for netuid {}: {:?}. Falling back to default balancer.",
String::from_utf8_lossy(&migration_name),
netuid,
error,
);
SwapBalancer::<T>::insert(netuid, Balancer::default());
PalSwapInitialized::<T>::insert(netuid, true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] Do not replace failed price initialization with default weights before deleting old price state

If maybe_initialize_palswap(netuid, Some(price)) rejects the V3 price/reserve combination, this fallback marks the subnet initialized with Balancer::default() and the migration then removes AlphaSqrtPrice below. That converts an initialization failure into a permanent price reset to default weights while deleting the only old price state that could be retried or diagnosed. Runtime migration should preserve the old state or fail/skip cleanup for that netuid instead of silently initializing a materially different pool.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is expected behavior: If the state of v3 pool is not representable with balancer, it will be set to default balancer. Additionally, this case does not appear neither on mainnet nor testnet currently and it is nearly impossible that liquidity balance shifts so much between now and next release.

Comment thread Cargo.toml
pallet-subtensor-swap-runtime-api = { path = "pallets/swap/runtime-api", default-features = false }
pallet-subtensor-swap-rpc = { path = "pallets/swap/rpc", default-features = false }
procedural-fork = { path = "support/procedural-fork", default-features = false }
safe-bigmath = { package = "safe-bigmath", default-features = false, git = "https://github.com/sam0x17/safe-bigmath" }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MEDIUM] New runtime math dependency is pulled from Git instead of a vetted release

safe-bigmath is used in runtime swap math, but this adds it directly from Git. Even though Cargo.lock pins the current revision, this bypasses normal crates.io provenance and makes future lock refreshes depend on a Git source for consensus-critical arithmetic. Vendor it, use a reviewed crates.io release, or move the needed code into an audited internal primitive before enabling it in runtime code.

@github-actions
Copy link
Copy Markdown
Contributor

🔄 AI review updated — Skeptic: VULNERABLE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-cargo-audit This PR fails cargo audit but needs to be merged anyway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant