Skip to content

Commit 369a319

Browse files
authored
Merge pull request #2353 from opentensor/feat/re-enable-alpha-fees
Reenable alpha fees
2 parents f4b4a21 + e687d43 commit 369a319

File tree

42 files changed

+2952
-678
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2952
-678
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ hex = { version = "0.4", default-features = false }
8787
hex-literal = "0.4.1"
8888
jsonrpsee = { version = "0.24.9", default-features = false }
8989
libsecp256k1 = { version = "0.7.2", default-features = false }
90+
lencode = "0.1.6"
9091
log = { version = "0.4.21", default-features = false }
9192
memmap2 = "0.9.8"
9293
ndarray = { version = "0.16.1", default-features = false }
@@ -315,3 +316,4 @@ pow-faucet = []
315316

316317
[patch.crates-io]
317318
w3f-bls = { git = "https://github.com/opentensor/bls", branch = "fix-no-std" }
319+

chain-extensions/src/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ impl pallet_subtensor_swap::Config for Test {
435435
type SubnetInfo = SubtensorModule;
436436
type BalanceOps = SubtensorModule;
437437
type ProtocolId = SwapProtocolId;
438-
type TaoReserve = TaoCurrencyReserve<Self>;
439-
type AlphaReserve = AlphaCurrencyReserve<Self>;
438+
type TaoReserve = TaoBalanceReserve<Self>;
439+
type AlphaReserve = AlphaBalanceReserve<Self>;
440440
type MaxFeeRate = SwapMaxFeeRate;
441441
type MaxPositions = SwapMaxPositions;
442442
type MinimumLiquidity = SwapMinimumLiquidity;

common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub trait BalanceOps<AccountId> {
259259
hotkey: &AccountId,
260260
netuid: NetUid,
261261
alpha: AlphaBalance,
262-
) -> Result<AlphaBalance, DispatchError>;
262+
) -> Result<(), DispatchError>;
263263
}
264264

265265
/// Allows to query the current block author

docs/rust-setup.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@ Open the Terminal application and execute the following commands:
6767

6868
# Make sure Homebrew is up-to-date, install protobuf and openssl
6969
brew update
70-
brew install protobuf openssl
70+
brew install protobuf openssl llvm@16
71+
```
72+
73+
Also, add the following lines at the end of your ~/.zshrc:
74+
75+
```
76+
# LLVM 16 from Homebrew
77+
export PATH="/opt/homebrew/opt/llvm@16/bin:$PATH"
78+
79+
export CC="/opt/homebrew/opt/llvm@16/bin/clang"
80+
export CXX="/opt/homebrew/opt/llvm@16/bin/clang++"
81+
export LIBCLANG_PATH="/opt/homebrew/opt/llvm@16/lib/libclang.dylib"
82+
83+
export LDFLAGS="-L/opt/homebrew/opt/llvm@16/lib"
84+
export CPPFLAGS="-I/opt/homebrew/opt/llvm@16/include"
7185
```
7286

7387
### Windows

pallets/admin-utils/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ pub mod pallet {
11491149
/// The extrinsic will call the Subtensor pallet to set the minimum stake required for nominators.
11501150
#[pallet::call_index(43)]
11511151
#[pallet::weight(Weight::from_parts(28_050_000, 6792)
1152-
.saturating_add(T::DbWeight::get().reads(4_u64))
1152+
.saturating_add(T::DbWeight::get().reads(5_u64))
11531153
.saturating_add(T::DbWeight::get().writes(1_u64)))]
11541154
pub fn sudo_set_nominator_min_required_stake(
11551155
origin: OriginFor<T>,
@@ -1595,7 +1595,7 @@ pub mod pallet {
15951595
/// # Weight
15961596
/// Weight is handled by the `#[pallet::weight]` attribute.
15971597
#[pallet::call_index(65)]
1598-
#[pallet::weight(Weight::from_parts(6_201_000, 0)
1598+
#[pallet::weight(Weight::from_parts(3_415_000, 0)
15991599
.saturating_add(T::DbWeight::get().reads(0_u64))
16001600
.saturating_add(T::DbWeight::get().writes(1_u64)))]
16011601
pub fn sudo_set_ema_price_halving_period(

pallets/admin-utils/src/tests/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ impl pallet_subtensor_swap::Config for Test {
340340
type SubnetInfo = SubtensorModule;
341341
type BalanceOps = SubtensorModule;
342342
type ProtocolId = SwapProtocolId;
343-
type TaoReserve = pallet_subtensor::TaoCurrencyReserve<Self>;
344-
type AlphaReserve = pallet_subtensor::AlphaCurrencyReserve<Self>;
343+
type TaoReserve = pallet_subtensor::TaoBalanceReserve<Self>;
344+
type AlphaReserve = pallet_subtensor::AlphaBalanceReserve<Self>;
345345
type MaxFeeRate = SwapMaxFeeRate;
346346
type MaxPositions = SwapMaxPositions;
347347
type MinimumLiquidity = SwapMinimumLiquidity;

pallets/subtensor/src/coinbase/block_step.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
3333
Self::run_auto_claim_root_divs(last_block_hash);
3434
// --- 9. Populate root coldkey maps.
3535
Self::populate_root_coldkey_staking_maps();
36+
Self::populate_root_coldkey_staking_maps_v2();
3637

3738
// Return ok.
3839
Ok(())

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,15 +515,15 @@ impl<T: Config> Pallet<T> {
515515
log::debug!(
516516
"owner_hotkey: {owner_hotkey:?} owner_coldkey: {owner_coldkey:?}, owner_cut: {owner_cut:?}"
517517
);
518-
let real_owner_cut = Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
518+
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
519519
&owner_hotkey,
520520
&owner_coldkey,
521521
netuid,
522522
owner_cut,
523523
);
524524
// If the subnet is leased, notify the lease logic that owner cut has been distributed.
525525
if let Some(lease_id) = SubnetUidToLeaseId::<T>::get(netuid) {
526-
Self::distribute_leased_network_dividends(lease_id, real_owner_cut);
526+
Self::distribute_leased_network_dividends(lease_id, owner_cut);
527527
}
528528
}
529529

@@ -617,7 +617,7 @@ impl<T: Config> Pallet<T> {
617617
root_alpha = root_alpha.saturating_sub(alpha_take);
618618
// Give the validator their take.
619619
log::debug!("hotkey: {hotkey:?} alpha_take: {alpha_take:?}");
620-
let _validator_stake = Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
620+
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
621621
&hotkey,
622622
&Owner::<T>::get(hotkey.clone()),
623623
netuid,

pallets/subtensor/src/lib.rs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub mod pallet {
9292
use frame_system::pallet_prelude::*;
9393
use pallet_drand::types::RoundNumber;
9494
use runtime_common::prod_or_fast;
95+
use share_pool::SafeFloat;
9596
use sp_core::{ConstU32, H160, H256};
9697
use sp_runtime::traits::{Dispatchable, TrailingZeroInput};
9798
use sp_std::collections::btree_map::BTreeMap;
@@ -1433,11 +1434,41 @@ pub mod pallet {
14331434
ValueQuery,
14341435
>;
14351436

1437+
/// DMAP ( hot, netuid ) --> total_alpha_shares | Returns the number of alpha shares for a hotkey on a subnet, stores SafeFloat.
1438+
#[pallet::storage]
1439+
pub type TotalHotkeySharesV2<T: Config> = StorageDoubleMap<
1440+
_,
1441+
Blake2_128Concat,
1442+
T::AccountId, // hot
1443+
Identity,
1444+
NetUid, // subnet
1445+
SafeFloat, // Hotkey shares in unlimited precision
1446+
ValueQuery,
1447+
>;
1448+
1449+
/// --- NMAP ( hot, cold, netuid ) --> alpha | Returns the alpha shares for a hotkey, coldkey, netuid triplet, stores SafeFloat.
1450+
#[pallet::storage]
1451+
pub type AlphaV2<T: Config> = StorageNMap<
1452+
_,
1453+
(
1454+
NMapKey<Blake2_128Concat, T::AccountId>, // hot
1455+
NMapKey<Blake2_128Concat, T::AccountId>, // cold
1456+
NMapKey<Identity, NetUid>, // subnet
1457+
),
1458+
SafeFloat, // Shares in unlimited precision
1459+
ValueQuery,
1460+
>;
1461+
14361462
/// Contains last Alpha storage map key to iterate (check first)
14371463
#[pallet::storage]
14381464
pub type AlphaMapLastKey<T: Config> =
14391465
StorageValue<_, Option<Vec<u8>>, ValueQuery, DefaultAlphaIterationLastKey<T>>;
14401466

1467+
/// Contains last AlphaV2 storage map key to iterate (check first)
1468+
#[pallet::storage]
1469+
pub type AlphaV2MapLastKey<T: Config> =
1470+
StorageValue<_, Option<Vec<u8>>, ValueQuery, DefaultAlphaIterationLastKey<T>>;
1471+
14411472
/// --- MAP ( netuid ) --> token_symbol | Returns the token symbol for a subnet.
14421473
#[pallet::storage]
14431474
pub type TokenSymbol<T: Config> =
@@ -2493,9 +2524,9 @@ use sp_std::vec::Vec;
24932524
use subtensor_macros::freeze_struct;
24942525

24952526
#[derive(Clone)]
2496-
pub struct TaoCurrencyReserve<T: Config>(PhantomData<T>);
2527+
pub struct TaoBalanceReserve<T: Config>(PhantomData<T>);
24972528

2498-
impl<T: Config> TokenReserve<TaoBalance> for TaoCurrencyReserve<T> {
2529+
impl<T: Config> TokenReserve<TaoBalance> for TaoBalanceReserve<T> {
24992530
#![deny(clippy::expect_used)]
25002531
fn reserve(netuid: NetUid) -> TaoBalance {
25012532
SubnetTAO::<T>::get(netuid).saturating_add(SubnetTaoProvided::<T>::get(netuid))
@@ -2511,9 +2542,9 @@ impl<T: Config> TokenReserve<TaoBalance> for TaoCurrencyReserve<T> {
25112542
}
25122543

25132544
#[derive(Clone)]
2514-
pub struct AlphaCurrencyReserve<T: Config>(PhantomData<T>);
2545+
pub struct AlphaBalanceReserve<T: Config>(PhantomData<T>);
25152546

2516-
impl<T: Config> TokenReserve<AlphaBalance> for AlphaCurrencyReserve<T> {
2547+
impl<T: Config> TokenReserve<AlphaBalance> for AlphaBalanceReserve<T> {
25172548
#![deny(clippy::expect_used)]
25182549
fn reserve(netuid: NetUid) -> AlphaBalance {
25192550
SubnetAlphaIn::<T>::get(netuid).saturating_add(SubnetAlphaInProvided::<T>::get(netuid))
@@ -2529,9 +2560,9 @@ impl<T: Config> TokenReserve<AlphaBalance> for AlphaCurrencyReserve<T> {
25292560
}
25302561

25312562
pub type GetAlphaForTao<T> =
2532-
subtensor_swap_interface::GetAlphaForTao<TaoCurrencyReserve<T>, AlphaCurrencyReserve<T>>;
2563+
subtensor_swap_interface::GetAlphaForTao<TaoBalanceReserve<T>, AlphaBalanceReserve<T>>;
25332564
pub type GetTaoForAlpha<T> =
2534-
subtensor_swap_interface::GetTaoForAlpha<AlphaCurrencyReserve<T>, TaoCurrencyReserve<T>>;
2565+
subtensor_swap_interface::GetTaoForAlpha<AlphaBalanceReserve<T>, TaoBalanceReserve<T>>;
25352566

25362567
impl<T: Config + pallet_balances::Config<Balance = TaoBalance>>
25372568
subtensor_runtime_common::SubnetInfo<T::AccountId> for Pallet<T>
@@ -2619,20 +2650,25 @@ impl<T: Config + pallet_balances::Config<Balance = TaoBalance>>
26192650
hotkey: &T::AccountId,
26202651
netuid: NetUid,
26212652
alpha: AlphaBalance,
2622-
) -> Result<AlphaBalance, DispatchError> {
2653+
) -> Result<(), DispatchError> {
26232654
ensure!(
26242655
Self::hotkey_account_exists(hotkey),
26252656
Error::<T>::HotKeyAccountNotExists
26262657
);
26272658

2659+
ensure!(
2660+
Self::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid) >= alpha,
2661+
Error::<T>::InsufficientBalance
2662+
);
2663+
26282664
// Decrese alpha out counter
26292665
SubnetAlphaOut::<T>::mutate(netuid, |total| {
26302666
*total = total.saturating_sub(alpha);
26312667
});
26322668

2633-
Ok(Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
2634-
hotkey, coldkey, netuid, alpha,
2635-
))
2669+
Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid, alpha);
2670+
2671+
Ok(())
26362672
}
26372673
}
26382674

0 commit comments

Comments
 (0)