Feat/recycle burn chain exts#2560
Conversation
|
Thanks for the contribution. Can you add the ticket for the requirement? I am wondering the extension like AddStakeRecycleV1, who really want to do it. just confirm all is from the real requirements. |
Just got it ticketed up here - #2564 Particular snippet from the issue to elaborate on the AddStakeBurn + AddStakeRecycle funcs and why those 2 are included:
|
|
Looks good to me, please fix the 'Cargo fmt' |
👍 |
e527c74 to
1c4f180
Compare
|
it also needed |
|
Addressed the review feedback:
@evgeny-s / @open-junius if you could give this a re-look when you have a chance, thanks Commit of changes: 65357d3 |
|
|
||
| match call_result { | ||
| Ok(_) => { | ||
| env.write_output(&amount.encode()) |
There was a problem hiding this comment.
I can see you remove the real_amount, and put the amount into result directly. but it could be wrong if the amount is more than you staked. So I suggest we can update the recycle_alpha function with the real recycled alpha as result, or doesn't return any value in RecycleAlphaV1. and the same for burn function.
There was a problem hiding this comment.
Good call, I got it updated w this commit - 60dffb3
Trying to keep the diff concise with what i add/change. Reran unit tests and passing
|
|
||
| let caller = env.caller(); | ||
|
|
||
| let result = transactional::with_transaction(|| { |
There was a problem hiding this comment.
There is do_add_stake_burn function, can we use it instead of call both do_add_stake and do_burn_alpha
There was a problem hiding this comment.
Looked into this but I don't think do_add_stake_burn is usable from a chain ext
- It's
pub(crate), so it's not accessible from thechain-extensionscrate. - Also it calls
ensure_subnet_owneron the origin which we won't want
Because of this, I kept it as the do_add_stake + do_burn_alpha composition
| <<T as pallet_subtensor::Config>::WeightInfo as SubtensorWeightInfo>::recycle_alpha(), | ||
| ); | ||
|
|
||
| env.charge_weight(weight)?; |
There was a problem hiding this comment.
we can charge the add_stake part first. If do_add_stake is Ok, then continue with recycle_alpha weight
There was a problem hiding this comment.
Okay, I updated this.. AddStakeRecycleV1 now charges add_stake() upfront and only charges recycle_alpha() after do_add_stake returns Ok. Applied the same split to AddStakeBurnV1 for consistency. Both ops still run inside with_transaction, & tests updated and passing.
Strip development log::info!/log::error! calls from dispatch entry and AddStakeRecycleV1 handler. Normalize AddStakeRecycleV1 to use the same concise ? pattern as all other handlers.
Replaces hardcoded Weight::from_parts values with calls to pallet_subtensor::weights::WeightInfo, matching the pattern junius introduced in PR 2550. Weights now auto-track benchmark updates. AddStakeRecycleV1 sums add_stake() + recycle_alpha() since the pallet has no add_stake_recycle weight; AddStakeBurnV1 uses add_stake_burn() directly (1:1 with pallet's do_add_stake_burn).
For AddStakeRecycleV1 and AddStakeBurnV1, charge add_stake() upfront and only charge the second-stage weight (recycle_alpha()/burn_alpha()) after do_add_stake returns Ok. Atomicity is preserved by keeping both ops inside with_transaction and tracking attempt state via a stack flag.
- cargo fmt wraps the add_stake() call in AddStakeRecycleV1/AddStakeBurnV1 - mock charge_weight replaces saturating_add with checked_add().unwrap() to satisfy the ForbidSaturatingMath custom lint
Swap the decode tuple in RecycleAlphaV1 and BurnAlphaV1 from (hotkey, amount, netuid) to (hotkey, netuid, amount) so the position of netuid matches AddStakeRecycleV1 and AddStakeBurnV1. Addresses evgeny-s's consistency feedback on opentensor#2560.
Adds pub fn do_add_stake_recycle and do_add_stake_burn_permissionless
to pallet_subtensor; each wraps do_add_stake + do_{recycle,burn}_alpha
in a transactional::with_transaction so atomicity lives in the pallet.
No subnet-owner guard or rate limit — those stay on do_add_stake_burn
for the owner-priority path (per shamil-gadelshin).
Chain-extension handlers AddStakeRecycleV1 and AddStakeBurnV1 collapse
to a single pallet call, matching the thin-handler pattern of the rest
of the extension. Weight is charged once upfront as add_stake + the
matching tail weight. Addresses evgeny-s's architectural feedback on
opentensor#2560.
0179ac5 to
46989aa
Compare
Applies the bench-patch artifact from validate-benchmarks CI (run 24733556355). Drift exceeded the 40% weight-compare threshold for pallet_subtensor, pallet_admin_utils, and pallet_subtensor_proxy. Autogenerated weights only - no logic change.
Unblocks the Devnet Deploy Check — devnet chain has caught up to spec_version 397, so local must be strictly greater.
This reverts commit 78ad491.
Resolves conflicts from upstream precompile test port (opentensor#2616), remove_stake benchmark fix (opentensor#2602), TransactionFeePaidWithAlpha netuid event, and spec_version bumps. - pallets/proxy/src/weights.rs: accept upstream regenerated weights - pallets/subtensor/src/weights.rs: accept upstream regenerated weights - runtime/src/lib.rs: bump spec_version 401 -> 403 (was 402 upstream) - pallets/subtensor/src/macros/dispatches.rs: auto-merged
Resolves conflicts from upstream chore/imbalances merge (opentensor#2524) and small follow-on PRs (opentensor#2621, opentensor#2623). - chain-extensions/src/types.rs: add upstream's CannotUseSystemAccount=20, shift our CannotBurnOrRecycleOnRootSubnet to 21 and SubtokenDisabled to 22 - chain-extensions/src/tests.rs: switch our recycle/burn test calls from pallet_subtensor::Pallet::<mock::Test>::add_balance_to_coldkey_account to the mock free function (pallet method removed in opentensor#2524) - docs/wasm-contracts.md: align Output table with new discriminants 21/22 - pallets/admin-utils/src/weights.rs: accept upstream regenerated sudo_set_total_issuance weights

Description
Add 4 new chain extension functions (IDs 16-19) enabling WASM smart contracts to recycle and burn alpha stake.
RecycleAlphaV1(16): Recycle alpha stakeBurnAlphaV1(17): Burn alpha stakeAddStakeRecycleV1(18): Atomically add TAO stake then recycleAddStakeBurnV1(19): Atomically add TAO stake then burnAll functions return the actual alpha amount via the output buffer.
Related Issue(s)
Closes #2564
Type of Change
Breaking Change
No breaking changes. Existing chain extension functions (IDs 0-15) are unaffected. New function IDs 16-19 are additive.
Checklist
./scripts/fix_rust.shto ensure my code is formatted and linted correctlyAdditional Notes
7 unit tests cover: success paths for all 4 functions, root subnet rejection, nonexistent subnet, and insufficient balance. E2E validated on localnet with an ink! contract calling
add_stake_recyclevia chain extension.I also ran some local E2E testing with a local running subtensor chain (with the new extensions), deployed contract, etc.
Screenshot 1 shows:
Screenshot 2 shows:
Unit tests successfully passing:
