Skip to content

Commit 1576fdd

Browse files
committed
fix conflict
2 parents 3fccecf + e58b165 commit 1576fdd

File tree

11 files changed

+100
-88
lines changed

11 files changed

+100
-88
lines changed

Cargo.lock

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "p
274274
w3f-bls = { git = "https://github.com/opentensor/bls", branch = "fix-no-std", default-features = false }
275275
ark-crypto-primitives = { version = "0.4.0", default-features = false }
276276
ark-scale = { version = "0.0.11", default-features = false }
277-
sp-ark-bls12-381 = { git = "https://github.com/paritytech/arkworks-substrate", package = "sp-ark-bls12-381", default-features = false }
278277
ark-bls12-381 = { version = "0.4.0", default-features = false }
279278
ark-serialize = { version = "0.4.0", default-features = false }
280279
ark-ff = { version = "0.4.0", default-features = false }

node/src/mev_shield/author.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ pub async fn submit_announce_extrinsic<B, C, Pool>(
316316
) -> anyhow::Result<()>
317317
where
318318
B: sp_runtime::traits::Block,
319-
C: sc_client_api::HeaderBackend<B> + Send + Sync + 'static,
319+
C: sc_client_api::HeaderBackend<B> + sp_api::ProvideRuntimeApi<B> + Send + Sync + 'static,
320+
C::Api: sp_api::Core<B>,
320321
Pool: sc_transaction_pool_api::TransactionPool<Block = B> + Send + Sync + 'static,
321322
B::Extrinsic: From<sp_runtime::OpaqueExtrinsic>,
322323
B::Hash: AsRef<[u8]>,
@@ -325,12 +326,13 @@ where
325326
use runtime::{RuntimeCall, SignedPayload, UncheckedExtrinsic};
326327

327328
use sc_transaction_pool_api::TransactionSource;
329+
use sp_api::Core as _;
328330
use sp_core::H256;
329331
use sp_runtime::codec::Encode;
330332
use sp_runtime::{
331333
BoundedVec, MultiSignature,
332334
generic::Era,
333-
traits::{ConstU32, TransactionExtension},
335+
traits::{ConstU32, SaturatedConversion, TransactionExtension},
334336
};
335337

336338
fn to_h256<H: AsRef<[u8]>>(h: H) -> H256 {
@@ -365,13 +367,23 @@ where
365367

366368
// 2) Build the transaction extensions exactly like the runtime.
367369
type Extra = runtime::TransactionExtensions;
370+
371+
let info = client.info();
372+
let at_hash = info.best_hash;
373+
let at_hash_h256: H256 = to_h256(at_hash);
374+
let genesis_h256: H256 = to_h256(info.genesis_hash);
375+
376+
const ERA_PERIOD: u64 = 12;
377+
let current_block: u64 = info.best_number.saturated_into();
378+
let era = Era::mortal(ERA_PERIOD, current_block);
379+
368380
let extra: Extra =
369381
(
370382
frame_system::CheckNonZeroSender::<runtime::Runtime>::new(),
371383
frame_system::CheckSpecVersion::<runtime::Runtime>::new(),
372384
frame_system::CheckTxVersion::<runtime::Runtime>::new(),
373385
frame_system::CheckGenesis::<runtime::Runtime>::new(),
374-
frame_system::CheckEra::<runtime::Runtime>::from(Era::Immortal),
386+
frame_system::CheckEra::<runtime::Runtime>::from(era),
375387
node_subtensor_runtime::check_nonce::CheckNonce::<runtime::Runtime>::from(nonce).into(),
376388
frame_system::CheckWeight::<runtime::Runtime>::new(),
377389
node_subtensor_runtime::transaction_payment_wrapper::ChargeTransactionPaymentWrapper::<
@@ -389,21 +401,35 @@ where
389401
// 3) Manually construct the `Implicit` tuple that the runtime will also derive.
390402
type Implicit = <Extra as TransactionExtension<RuntimeCall>>::Implicit;
391403

392-
let info = client.info();
393-
let genesis_h256: H256 = to_h256(info.genesis_hash);
404+
// Try to get the *current* runtime version from on-chain WASM; if that fails,
405+
// fall back to the compiled runtime::VERSION.
406+
let (spec_version, tx_version) = match client.runtime_api().version(at_hash) {
407+
Ok(v) => (v.spec_version, v.transaction_version),
408+
Err(e) => {
409+
log::debug!(
410+
target: "mev-shield",
411+
"runtime_api::version failed at_hash={at_hash:?}: {e:?}; \
412+
falling back to compiled runtime::VERSION",
413+
);
414+
(
415+
runtime::VERSION.spec_version,
416+
runtime::VERSION.transaction_version,
417+
)
418+
}
419+
};
394420

395421
let implicit: Implicit = (
396-
(), // CheckNonZeroSender
397-
runtime::VERSION.spec_version, // CheckSpecVersion::Implicit = u32
398-
runtime::VERSION.transaction_version, // CheckTxVersion::Implicit = u32
399-
genesis_h256, // CheckGenesis::Implicit = Hash
400-
genesis_h256, // CheckEra::Implicit (Immortal => genesis hash)
401-
(), // CheckNonce::Implicit = ()
402-
(), // CheckWeight::Implicit = ()
403-
(), // ChargeTransactionPaymentWrapper::Implicit = ()
404-
(), // SubtensorTransactionExtension::Implicit = ()
405-
(), // DrandPriority::Implicit = ()
406-
None, // CheckMetadataHash::Implicit = Option<[u8; 32]>
422+
(), // CheckNonZeroSender
423+
spec_version, // dynamic or fallback spec_version
424+
tx_version, // dynamic or fallback transaction_version
425+
genesis_h256, // CheckGenesis::Implicit = Hash
426+
at_hash_h256, // CheckEra::Implicit = hash of the block the tx is created at
427+
(), // CheckNonce::Implicit = ()
428+
(), // CheckWeight::Implicit = ()
429+
(), // ChargeTransactionPaymentWrapper::Implicit = ()
430+
(), // SubtensorTransactionExtension::Implicit = ()
431+
(), // DrandPriority::Implicit = ()
432+
None, // CheckMetadataHash::Implicit = Option<[u8; 32]>
407433
);
408434

409435
// 4) Build the exact signable payload from call + extra + implicit.
@@ -433,12 +459,13 @@ where
433459
let opaque: sp_runtime::OpaqueExtrinsic = uxt.into();
434460
let xt: <B as sp_runtime::traits::Block>::Extrinsic = opaque.into();
435461

436-
pool.submit_one(info.best_hash, TransactionSource::Local, xt)
462+
pool.submit_one(at_hash, TransactionSource::Local, xt)
437463
.await?;
438464

439465
log::debug!(
440466
target: "mev-shield",
441-
"announce_next_key submitted: xt=0x{xt_hash_hex}, nonce={nonce:?}",
467+
"announce_next_key submitted: xt=0x{xt_hash_hex}, nonce={nonce:?}, \
468+
spec_version={spec_version}, tx_version={tx_version}, era={era:?}",
442469
);
443470

444471
Ok(())

pallets/drand/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ scale-info = { workspace = true, features = ["derive"] }
1717
serde = { workspace = true, features = ["derive"] }
1818
serde_json.workspace = true
1919
log.workspace = true
20-
hex = { workspace = true, features = ["serde"] }
20+
hex = { workspace = true, features = ["serde", "alloc"] }
2121
sha2.workspace = true
2222
anyhow.workspace = true
2323
# frame deps
@@ -29,7 +29,7 @@ sp-io.workspace = true
2929
sp-runtime.workspace = true
3030
sp-std.workspace = true
3131
# arkworks dependencies
32-
sp-ark-bls12-381.workspace = true
32+
sp-crypto-ec-utils = { workspace = true, features = ["bls12-381"] }
3333
ark-bls12-381 = { workspace = true, features = ["curve"] }
3434
ark-serialize = { workspace = true, features = ["derive"] }
3535
ark-ff.workspace = true
@@ -64,7 +64,7 @@ std = [
6464
"serde/std",
6565
"serde_json/std",
6666
"hex/std",
67-
"sp-ark-bls12-381/std",
67+
"sp-crypto-ec-utils/std",
6868
"ark-bls12-381/std",
6969
"ark-serialize/std",
7070
"ark-ff/std",

pallets/drand/src/bls12_381.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use ark_ec::pairing::Pairing;
1818
use ark_std::{Zero, ops::Neg};
19-
use sp_ark_bls12_381::{
19+
use sp_crypto_ec_utils::bls12_381::{
2020
Bls12_381 as Bls12_381Opt, G1Affine as G1AffineOpt, G2Affine as G2AffineOpt,
2121
};
2222

pallets/drand/src/verifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use ark_ec::{AffineRepr, hashing::HashToCurve};
2727
use ark_serialize::CanonicalSerialize;
2828
use codec::Decode;
2929
use sha2::{Digest, Sha256};
30-
use sp_ark_bls12_381::{G1Affine as G1AffineOpt, G2Affine as G2AffineOpt};
30+
use sp_crypto_ec_utils::bls12_381::{G1Affine as G1AffineOpt, G2Affine as G2AffineOpt};
3131
use tle::curves::drand::TinyBLS381;
3232
use w3f_bls::engine::EngineBLS;
3333

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -484,31 +484,37 @@ impl<T: Config> Pallet<T> {
484484
let mut stakers: Vec<(T::AccountId, T::AccountId, u128)> = Vec::new();
485485
let mut total_alpha_value_u128: u128 = 0;
486486

487-
for ((hot, cold, this_netuid), share_u64f64) in Alpha::<T>::iter() {
488-
if this_netuid != netuid {
489-
continue;
490-
}
491-
492-
keys_to_remove.push((hot.clone(), cold.clone()));
493-
if !hotkeys_seen.contains(&hot) {
494-
hotkeys_seen.push(hot.clone());
495-
}
487+
let hotkeys_in_subnet: Vec<T::AccountId> = TotalHotkeyAlpha::<T>::iter()
488+
.filter(|(_, this_netuid, _)| *this_netuid == netuid)
489+
.map(|(hot, _, _)| hot.clone())
490+
.collect::<Vec<_>>();
491+
492+
for hot in hotkeys_in_subnet.iter() {
493+
for ((cold, this_netuid), share_u64f64) in Alpha::<T>::iter_prefix((hot,)) {
494+
if this_netuid != netuid {
495+
continue;
496+
}
497+
keys_to_remove.push((hot.clone(), cold.clone()));
498+
if !hotkeys_seen.contains(hot) {
499+
hotkeys_seen.push(hot.clone());
500+
}
496501

497-
// Primary: actual α value via share pool.
498-
let pool = Self::get_alpha_share_pool(hot.clone(), netuid);
499-
let actual_val_u64 = pool.try_get_value(&cold).unwrap_or(0);
502+
// Primary: actual α value via share pool.
503+
let pool = Self::get_alpha_share_pool(hot.clone(), netuid);
504+
let actual_val_u64 = pool.try_get_value(&cold).unwrap_or(0);
500505

501-
// Fallback: if pool uninitialized, treat raw Alpha share as value.
502-
let val_u64 = if actual_val_u64 == 0 {
503-
share_u64f64.saturating_to_num::<u64>()
504-
} else {
505-
actual_val_u64
506-
};
506+
// Fallback: if pool uninitialized, treat raw Alpha share as value.
507+
let val_u64 = if actual_val_u64 == 0 {
508+
share_u64f64.saturating_to_num::<u64>()
509+
} else {
510+
actual_val_u64
511+
};
507512

508-
if val_u64 > 0 {
509-
let val_u128 = val_u64 as u128;
510-
total_alpha_value_u128 = total_alpha_value_u128.saturating_add(val_u128);
511-
stakers.push((hot, cold, val_u128));
513+
if val_u64 > 0 {
514+
let val_u128 = val_u64 as u128;
515+
total_alpha_value_u128 = total_alpha_value_u128.saturating_add(val_u128);
516+
stakers.push((hot.clone(), cold, val_u128));
517+
}
512518
}
513519
}
514520

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,6 @@ impl<T: Config> Pallet<T> {
694694
price_limit: TaoCurrency,
695695
drop_fees: bool,
696696
) -> Result<TaoCurrency, DispatchError> {
697-
// Record the protocol TAO before the swap.
698-
let protocol_tao = Self::get_protocol_tao(netuid);
699-
700697
// Decrease alpha on subnet
701698
let actual_alpha_decrease =
702699
Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid, alpha);
@@ -705,13 +702,6 @@ impl<T: Config> Pallet<T> {
705702
let swap_result =
706703
Self::swap_alpha_for_tao(netuid, actual_alpha_decrease, price_limit, drop_fees)?;
707704

708-
// Record the protocol TAO after the swap.
709-
let protocol_tao_after = Self::get_protocol_tao(netuid);
710-
// This should decrease as we are removing TAO from the protocol.
711-
let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after);
712-
// Use max to overstate the TAO flow from the protocol.
713-
let tao_flow = protocol_tao_delta.max(swap_result.amount_paid_out.into());
714-
715705
// Refund the unused alpha (in case if limit price is hit)
716706
let refund = actual_alpha_decrease.saturating_sub(
717707
swap_result
@@ -738,7 +728,7 @@ impl<T: Config> Pallet<T> {
738728
// }
739729

740730
// Record TAO outflow
741-
Self::record_tao_outflow(netuid, tao_flow);
731+
Self::record_tao_outflow(netuid, swap_result.amount_paid_out.into());
742732

743733
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
744734

@@ -777,20 +767,9 @@ impl<T: Config> Pallet<T> {
777767
set_limit: bool,
778768
drop_fees: bool,
779769
) -> Result<AlphaCurrency, DispatchError> {
780-
// Record the protocol TAO before the swap.
781-
let protocol_tao = Self::get_protocol_tao(netuid);
782-
783770
// Swap the tao to alpha.
784771
let swap_result = Self::swap_tao_for_alpha(netuid, tao, price_limit, drop_fees)?;
785772

786-
// Record the protocol TAO after the swap.
787-
let protocol_tao_after = Self::get_protocol_tao(netuid);
788-
789-
// This should increase as we are adding TAO to the protocol.
790-
let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao);
791-
// Use min to understate the TAO flow into the protocol.
792-
let tao_flow = protocol_tao_delta.min(tao);
793-
794773
ensure!(
795774
!swap_result.amount_paid_out.is_zero(),
796775
Error::<T>::AmountTooLow
@@ -826,7 +805,7 @@ impl<T: Config> Pallet<T> {
826805
}
827806

828807
// Record TAO inflow
829-
Self::record_tao_inflow(netuid, tao_flow);
808+
Self::record_tao_inflow(netuid, swap_result.amount_paid_in.into());
830809

831810
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
832811

pallets/subtensor/src/tests/networks.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ fn dissolve_single_alpha_out_staker_gets_all_tao() {
106106
let pot: u64 = 99_999;
107107
SubnetTAO::<Test>::insert(net, TaoCurrency::from(pot));
108108
SubtensorModule::set_subnet_locked_balance(net, 0.into());
109+
TotalHotkeyAlpha::<Test>::insert(s_hot, net, AlphaCurrency::from(5_000u64));
109110

110111
// Cold-key balance before
111112
let before = SubtensorModule::get_coldkey_balance(&s_cold);
@@ -142,6 +143,9 @@ fn dissolve_two_stakers_pro_rata_distribution() {
142143
Alpha::<Test>::insert((s1_hot, s1_cold, net), U64F64::from_num(a1));
143144
Alpha::<Test>::insert((s2_hot, s2_cold, net), U64F64::from_num(a2));
144145

146+
TotalHotkeyAlpha::<Test>::insert(s1_hot, net, AlphaCurrency::from(a1 as u64));
147+
TotalHotkeyAlpha::<Test>::insert(s2_hot, net, AlphaCurrency::from(a2 as u64));
148+
145149
let pot: u64 = 10_000;
146150
SubnetTAO::<Test>::insert(net, TaoCurrency::from(pot));
147151
SubtensorModule::set_subnet_locked_balance(net, 5_000.into()); // owner refund path present; emission = 0
@@ -627,6 +631,7 @@ fn dissolve_alpha_out_but_zero_tao_no_rewards() {
627631
SubnetTAO::<Test>::insert(net, TaoCurrency::from(0)); // zero TAO
628632
SubtensorModule::set_subnet_locked_balance(net, TaoCurrency::from(0));
629633
Emission::<Test>::insert(net, Vec::<AlphaCurrency>::new());
634+
TotalHotkeyAlpha::<Test>::insert(sh, net, AlphaCurrency::from(1_000u64));
630635

631636
let before = SubtensorModule::get_coldkey_balance(&sc);
632637
assert_ok!(SubtensorModule::do_dissolve_network(net));
@@ -672,6 +677,9 @@ fn dissolve_rounding_remainder_distribution() {
672677
SubnetTAO::<Test>::insert(net, TaoCurrency::from(1)); // TAO pot = 1
673678
SubtensorModule::set_subnet_locked_balance(net, TaoCurrency::from(0));
674679

680+
TotalHotkeyAlpha::<Test>::insert(s1h, net, AlphaCurrency::from(3u64));
681+
TotalHotkeyAlpha::<Test>::insert(s2h, net, AlphaCurrency::from(2u64));
682+
675683
// Cold-key balances before
676684
let c1_before = SubtensorModule::get_coldkey_balance(&s1c);
677685
let c2_before = SubtensorModule::get_coldkey_balance(&s2c);

pallets/swap/src/pallet/impls.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -880,10 +880,12 @@ impl<T: Config> Pallet<T> {
880880
// ---------------- USER: refund τ and convert α → stake ----------------
881881

882882
// 1) Refund τ principal directly.
883-
if rm.tao > TaoCurrency::ZERO {
884-
T::BalanceOps::increase_balance(&owner, rm.tao);
885-
user_refunded_tao = user_refunded_tao.saturating_add(rm.tao);
886-
T::TaoReserve::decrease_provided(netuid, rm.tao);
883+
let tao_total_from_pool: TaoCurrency = rm.tao.saturating_add(rm.fee_tao);
884+
if tao_total_from_pool > TaoCurrency::ZERO {
885+
T::BalanceOps::increase_balance(&owner, tao_total_from_pool);
886+
user_refunded_tao =
887+
user_refunded_tao.saturating_add(tao_total_from_pool);
888+
T::TaoReserve::decrease_provided(netuid, tao_total_from_pool);
887889
}
888890

889891
// 2) Stake ALL withdrawn α (principal + fees) to the best permitted validator.
@@ -967,17 +969,17 @@ impl<T: Config> Pallet<T> {
967969
Ok(rm) => {
968970
let alpha_total_from_pool: AlphaCurrency =
969971
rm.alpha.saturating_add(rm.fee_alpha);
970-
let tao = rm.tao;
972+
let tao_total_from_pool: TaoCurrency = rm.tao.saturating_add(rm.fee_tao);
971973

972-
if tao > TaoCurrency::ZERO {
973-
burned_tao = burned_tao.saturating_add(tao);
974+
if tao_total_from_pool > TaoCurrency::ZERO {
975+
burned_tao = burned_tao.saturating_add(tao_total_from_pool);
974976
}
975977
if alpha_total_from_pool > AlphaCurrency::ZERO {
976978
burned_alpha = burned_alpha.saturating_add(alpha_total_from_pool);
977979
}
978980

979981
log::debug!(
980-
"clear_protocol_liquidity: burned protocol pos: netuid={netuid:?}, pos_id={pos_id:?}, τ={tao:?}, α_total={alpha_total_from_pool:?}"
982+
"clear_protocol_liquidity: burned protocol pos: netuid={netuid:?}, pos_id={pos_id:?}, τ={tao_total_from_pool:?}, α_total={alpha_total_from_pool:?}"
981983
);
982984
}
983985
Err(e) => {

0 commit comments

Comments
 (0)