diff --git a/Cargo.lock b/Cargo.lock index a5f4583519..4b32cbe31a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9590,8 +9590,8 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.9", - "sp-ark-bls12-381", "sp-core", + "sp-crypto-ec-utils", "sp-io", "sp-keyring", "sp-keystore", @@ -16801,15 +16801,6 @@ dependencies = [ "static_assertions", ] -[[package]] -name = "sp-ark-bls12-381" -version = "0.4.2" -source = "git+https://github.com/paritytech/arkworks-substrate#f08093a5f7c32778eae1295430ec064dccd062a6" -dependencies = [ - "ark-bls12-381-ext", - "sp-crypto-ec-utils", -] - [[package]] name = "sp-authority-discovery" version = "37.0.0" diff --git a/Cargo.toml b/Cargo.toml index 05cea1d929..1d65a3cd5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -274,7 +274,6 @@ sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "p w3f-bls = { git = "https://github.com/opentensor/bls", branch = "fix-no-std", default-features = false } ark-crypto-primitives = { version = "0.4.0", default-features = false } ark-scale = { version = "0.0.11", default-features = false } -sp-ark-bls12-381 = { git = "https://github.com/paritytech/arkworks-substrate", package = "sp-ark-bls12-381", default-features = false } ark-bls12-381 = { version = "0.4.0", default-features = false } ark-serialize = { version = "0.4.0", default-features = false } ark-ff = { version = "0.4.0", default-features = false } diff --git a/pallets/drand/Cargo.toml b/pallets/drand/Cargo.toml index 269e993d02..6e3ad982f7 100644 --- a/pallets/drand/Cargo.toml +++ b/pallets/drand/Cargo.toml @@ -17,7 +17,7 @@ scale-info = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] } serde_json.workspace = true log.workspace = true -hex = { workspace = true, features = ["serde"] } +hex = { workspace = true, features = ["serde", "alloc"] } sha2.workspace = true anyhow.workspace = true # frame deps @@ -29,7 +29,7 @@ sp-io.workspace = true sp-runtime.workspace = true sp-std.workspace = true # arkworks dependencies -sp-ark-bls12-381.workspace = true +sp-crypto-ec-utils = { workspace = true, features = ["bls12-381"] } ark-bls12-381 = { workspace = true, features = ["curve"] } ark-serialize = { workspace = true, features = ["derive"] } ark-ff.workspace = true @@ -64,7 +64,7 @@ std = [ "serde/std", "serde_json/std", "hex/std", - "sp-ark-bls12-381/std", + "sp-crypto-ec-utils/std", "ark-bls12-381/std", "ark-serialize/std", "ark-ff/std", diff --git a/pallets/drand/src/bls12_381.rs b/pallets/drand/src/bls12_381.rs index 1415613ef6..e31118a3e4 100644 --- a/pallets/drand/src/bls12_381.rs +++ b/pallets/drand/src/bls12_381.rs @@ -16,7 +16,7 @@ use ark_ec::pairing::Pairing; use ark_std::{Zero, ops::Neg}; -use sp_ark_bls12_381::{ +use sp_crypto_ec_utils::bls12_381::{ Bls12_381 as Bls12_381Opt, G1Affine as G1AffineOpt, G2Affine as G2AffineOpt, }; diff --git a/pallets/drand/src/verifier.rs b/pallets/drand/src/verifier.rs index e24b82003a..36c212181a 100644 --- a/pallets/drand/src/verifier.rs +++ b/pallets/drand/src/verifier.rs @@ -27,7 +27,7 @@ use ark_ec::{AffineRepr, hashing::HashToCurve}; use ark_serialize::CanonicalSerialize; use codec::Decode; use sha2::{Digest, Sha256}; -use sp_ark_bls12_381::{G1Affine as G1AffineOpt, G2Affine as G2AffineOpt}; +use sp_crypto_ec_utils::bls12_381::{G1Affine as G1AffineOpt, G2Affine as G2AffineOpt}; use tle::curves::drand::TinyBLS381; use w3f_bls::engine::EngineBLS; diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index e367fce1dd..d0f78551c1 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -694,9 +694,6 @@ impl Pallet { price_limit: TaoCurrency, drop_fees: bool, ) -> Result { - // Record the protocol TAO before the swap. - let protocol_tao = Self::get_protocol_tao(netuid); - // Decrease alpha on subnet let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid, alpha); @@ -705,13 +702,6 @@ impl Pallet { let swap_result = Self::swap_alpha_for_tao(netuid, actual_alpha_decrease, price_limit, drop_fees)?; - // Record the protocol TAO after the swap. - let protocol_tao_after = Self::get_protocol_tao(netuid); - // This should decrease as we are removing TAO from the protocol. - let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after); - // Use max to overstate the TAO flow from the protocol. - let tao_flow = protocol_tao_delta.max(swap_result.amount_paid_out.into()); - // Refund the unused alpha (in case if limit price is hit) let refund = actual_alpha_decrease.saturating_sub( swap_result @@ -738,7 +728,7 @@ impl Pallet { // } // Record TAO outflow - Self::record_tao_outflow(netuid, tao_flow); + Self::record_tao_outflow(netuid, swap_result.amount_paid_out.into()); LastColdkeyHotkeyStakeBlock::::insert(coldkey, hotkey, Self::get_current_block_as_u64()); @@ -777,20 +767,9 @@ impl Pallet { set_limit: bool, drop_fees: bool, ) -> Result { - // Record the protocol TAO before the swap. - let protocol_tao = Self::get_protocol_tao(netuid); - // Swap the tao to alpha. let swap_result = Self::swap_tao_for_alpha(netuid, tao, price_limit, drop_fees)?; - // Record the protocol TAO after the swap. - let protocol_tao_after = Self::get_protocol_tao(netuid); - - // This should increase as we are adding TAO to the protocol. - let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao); - // Use min to understate the TAO flow into the protocol. - let tao_flow = protocol_tao_delta.min(tao); - ensure!( !swap_result.amount_paid_out.is_zero(), Error::::AmountTooLow @@ -826,7 +805,7 @@ impl Pallet { } // Record TAO inflow - Self::record_tao_inflow(netuid, tao_flow); + Self::record_tao_inflow(netuid, swap_result.amount_paid_in.into()); LastColdkeyHotkeyStakeBlock::::insert(coldkey, hotkey, Self::get_current_block_as_u64()); diff --git a/pallets/swap/src/pallet/impls.rs b/pallets/swap/src/pallet/impls.rs index eb7d27d1a6..9c27b82a02 100644 --- a/pallets/swap/src/pallet/impls.rs +++ b/pallets/swap/src/pallet/impls.rs @@ -880,10 +880,12 @@ impl Pallet { // ---------------- USER: refund τ and convert α → stake ---------------- // 1) Refund τ principal directly. - if rm.tao > TaoCurrency::ZERO { - T::BalanceOps::increase_balance(&owner, rm.tao); - user_refunded_tao = user_refunded_tao.saturating_add(rm.tao); - T::TaoReserve::decrease_provided(netuid, rm.tao); + let tao_total_from_pool: TaoCurrency = rm.tao.saturating_add(rm.fee_tao); + if tao_total_from_pool > TaoCurrency::ZERO { + T::BalanceOps::increase_balance(&owner, tao_total_from_pool); + user_refunded_tao = + user_refunded_tao.saturating_add(tao_total_from_pool); + T::TaoReserve::decrease_provided(netuid, tao_total_from_pool); } // 2) Stake ALL withdrawn α (principal + fees) to the best permitted validator. @@ -967,17 +969,17 @@ impl Pallet { Ok(rm) => { let alpha_total_from_pool: AlphaCurrency = rm.alpha.saturating_add(rm.fee_alpha); - let tao = rm.tao; + let tao_total_from_pool: TaoCurrency = rm.tao.saturating_add(rm.fee_tao); - if tao > TaoCurrency::ZERO { - burned_tao = burned_tao.saturating_add(tao); + if tao_total_from_pool > TaoCurrency::ZERO { + burned_tao = burned_tao.saturating_add(tao_total_from_pool); } if alpha_total_from_pool > AlphaCurrency::ZERO { burned_alpha = burned_alpha.saturating_add(alpha_total_from_pool); } log::debug!( - "clear_protocol_liquidity: burned protocol pos: netuid={netuid:?}, pos_id={pos_id:?}, τ={tao:?}, α_total={alpha_total_from_pool:?}" + "clear_protocol_liquidity: burned protocol pos: netuid={netuid:?}, pos_id={pos_id:?}, τ={tao_total_from_pool:?}, α_total={alpha_total_from_pool:?}" ); } Err(e) => { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 9d502c925d..35272d9817 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -237,7 +237,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 360, + spec_version: 361, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1,