Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testnet deploy rao final 2/6/2025 #1252

Merged
merged 28 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c1c25e2
Merge pull request #1119 from opentensor/testnet
sam0x17 Dec 29, 2024
d4d8182
mainnet deploy 1/7/2025 (#1133)
sam0x17 Jan 7, 2025
271af2b
no owner dereg & tests
JohnReedV Jan 30, 2025
bbbfb3a
update existing tests
JohnReedV Jan 30, 2025
c7d8010
add more tests for root pending etc
Feb 5, 2025
6872057
Merge branch 'nonclaim' of https://github.com/opentensor/subtensor in…
Feb 5, 2025
102e6cc
alpha divs
Feb 6, 2025
4f900ca
fix test for root
Feb 6, 2025
2b8ed3a
new test root tao
Feb 6, 2025
fb4da1c
fix wrong parse function
open-junius Feb 6, 2025
2a57be4
fix test
Feb 6, 2025
ae25f59
add toggle off for transfers
Feb 6, 2025
811e7d5
Merge pull request #1246 from opentensor/hotfix-wrong-parse-account-id
sam0x17 Feb 6, 2025
33a842d
Merge branch 'feat/no-owner-dereg' into nonclaim
JohnReedV Feb 6, 2025
ecd265d
delete unused workflow file
sam0x17 Feb 6, 2025
95665f1
Merge pull request #1247 from opentensor/sam-fix-empty-workflow-file
sam0x17 Feb 6, 2025
841c913
Fix validation for transfer toggle
gztensor Feb 6, 2025
dafc7a2
Merge branch 'nonclaim' of github.com:opentensor/subtensor into nonclaim
gztensor Feb 6, 2025
913231b
Merge remote-tracking branch 'origin/devnet-ready' into nonclaim
sam0x17 Feb 6, 2025
a2969f8
Merge branch 'main' into nonclaim-into-main
camfairchild Feb 6, 2025
66f0097
remove duplicate tests
camfairchild Feb 6, 2025
c7a3238
fix subnet creation logic
Feb 6, 2025
a2016ee
Merge pull request #1250 from opentensor/nonclaim-into-main
sam0x17 Feb 6, 2025
3be3007
cargo fmt
sam0x17 Feb 6, 2025
c427fac
bump spec version to 228
sam0x17 Feb 6, 2025
c28cac9
Add test test_drain_alpha_childkey_parentkey
gztensor Feb 6, 2025
eae43ff
Merge pull request #1248 from opentensor/nonclaim
sam0x17 Feb 6, 2025
d70a9a4
Merge pull request #1251 from opentensor/devnet-ready
sam0x17 Feb 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
20 changes: 20 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,26 @@ pub mod pallet {
ensure_root(origin)?;
T::Grandpa::schedule_change(next_authorities, in_blocks, forced)
}

/// Enables or disables Liquid Alpha for a given subnet.
///
/// # Parameters
/// - `origin`: The origin of the call, which must be the root account or subnet owner.
/// - `netuid`: The unique identifier for the subnet.
/// - `enabled`: A boolean flag to enable or disable Liquid Alpha.
///
/// # Weight
/// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees.
#[pallet::call_index(61)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_set_toggle_transfer(
origin: OriginFor<T>,
netuid: u16,
toggle: bool,
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
pallet_subtensor::Pallet::<T>::toggle_transfer(netuid, toggle)
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,14 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::NetworkLockCostReductionIntervalSet(interval));
}
pub fn get_lock_reduction_interval() -> u64 {
NetworkLockReductionInterval::<T>::get()
let interval: I64F64 =
I64F64::saturating_from_num(NetworkLockReductionInterval::<T>::get());
let block_emission: I64F64 =
I64F64::saturating_from_num(Self::get_block_emission().unwrap_or(1_000_000_000));
let halving: I64F64 = block_emission
.checked_div(I64F64::saturating_from_num(1_000_000_000))
.unwrap_or(I64F64::saturating_from_num(0.0));
let halved_interval: I64F64 = interval.saturating_mul(halving);
halved_interval.saturating_to_num::<u64>()
}
}
35 changes: 28 additions & 7 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ impl<T: Config> Pallet<T> {
for netuid_i in subnets.iter() {
// Get alpha out.
let alpha_out_i: I96F32 = *alpha_out.get(netuid_i).unwrap_or(&asfloat!(0));
log::debug!("alpha_out_i: {:?}", alpha_out_i);
// Calculate the owner cut.
let owner_cut_i: I96F32 = alpha_out_i.saturating_mul(cut_percent);
log::debug!("owner_cut_i: {:?}", owner_cut_i);
// Save owner cut.
*owner_cuts.entry(*netuid_i).or_insert(asfloat!(0)) = owner_cut_i;
// Save new alpha_out.
Expand All @@ -155,24 +157,33 @@ impl<T: Config> Pallet<T> {
for netuid_i in subnets.iter() {
// Get remaining alpha out.
let alpha_out_i: I96F32 = *alpha_out.get(netuid_i).unwrap_or(&asfloat!(0.0));
log::debug!("alpha_out_i: {:?}", alpha_out_i);
// Get total TAO on root.
let root_tao: I96F32 = asfloat!(SubnetTAO::<T>::get(0));
log::debug!("root_tao: {:?}", root_tao);
// Get total ALPHA on subnet.
let alpha_issuance: I96F32 = asfloat!(Self::get_alpha_issuance(*netuid_i));
log::debug!("alpha_issuance: {:?}", alpha_issuance);
// Get tao_weight
let tao_weight: I96F32 = root_tao.saturating_mul(Self::get_tao_weight());
log::debug!("tao_weight: {:?}", tao_weight);
// Get root proportional dividends.
let root_proportion: I96F32 = tao_weight
.checked_div(tao_weight.saturating_add(alpha_issuance))
.unwrap_or(asfloat!(0.0));
log::debug!("root_proportion: {:?}", root_proportion);
// Get root proportion of alpha_out dividends.
let root_alpha: I96F32 = root_proportion
.saturating_mul(alpha_out_i) // Total alpha emission per block remaining.
.saturating_mul(asfloat!(0.5)); // 50% to validators.
// Remove root alpha from alpha_out.
log::debug!("root_alpha: {:?}", root_alpha);
// Get pending alpha as original alpha_out - root_alpha.
let pending_alpha: I96F32 = alpha_out_i.saturating_sub(root_alpha);
log::debug!("pending_alpha: {:?}", pending_alpha);
// Sell root emission through the pool.
let root_tao: u64 = Self::swap_alpha_for_tao(*netuid_i, tou64!(root_alpha));
log::debug!("root_tao: {:?}", root_tao);
// Accumulate alpha emission in pending.
PendingAlphaSwapped::<T>::mutate(*netuid_i, |total| {
*total = total.saturating_add(tou64!(root_alpha));
Expand Down Expand Up @@ -238,6 +249,11 @@ impl<T: Config> Pallet<T> {
BlocksSinceLastStep::<T>::mutate(netuid, |total| *total = total.saturating_add(1));
}
}

// --- 8. Apply pending childkeys of this subnet for the next epoch
for netuid in subnets.iter() {
Self::do_set_pending_children(*netuid);
}
}

pub fn drain_pending_emission(
Expand All @@ -261,6 +277,7 @@ impl<T: Config> Pallet<T> {
// Run the epoch.
let hotkey_emission: Vec<(T::AccountId, u64, u64)> =
Self::epoch(netuid, pending_alpha.saturating_add(pending_swapped));
log::debug!("hotkey_emission: {:?}", hotkey_emission);

// Accumulate emission of dividends and incentive per hotkey.
let mut incentives: BTreeMap<T::AccountId, u64> = BTreeMap::new();
Expand All @@ -282,8 +299,10 @@ impl<T: Config> Pallet<T> {
.or_insert(asfloat!(parent_div));
}
}
log::debug!("incentives: {:?}", incentives);
log::debug!("dividends: {:?}", dividends);

// Accumulate root divs and alpha_divs. For each hotkye we compute their
// Accumulate root divs and alpha_divs. For each hotkey we compute their
// local and root dividend proportion based on their alpha_stake/root_stake
let mut total_root_divs: I96F32 = asfloat!(0);
let mut root_dividends: BTreeMap<T::AccountId, I96F32> = BTreeMap::new();
Expand All @@ -300,19 +319,15 @@ impl<T: Config> Pallet<T> {
let root_alpha: I96F32 = root_stake.saturating_mul(Self::get_tao_weight());
// Get total from root and local
let total_alpha: I96F32 = alpha_stake.saturating_add(root_alpha);
// Compute alpha prop.
let alpha_prop: I96F32 = alpha_stake.checked_div(total_alpha).unwrap_or(zero);
// Copmute root prop.
let root_prop: I96F32 = root_alpha.checked_div(total_alpha).unwrap_or(zero);
// Compute alpha dividends
let alpha_divs: I96F32 = dividend.saturating_mul(alpha_prop);
// Compute root dividends
let root_divs: I96F32 = dividend.saturating_mul(root_prop);
// Record the root dividends.
alpha_dividends
.entry(hotkey.clone())
.and_modify(|e| *e = e.saturating_add(alpha_divs))
.or_insert(alpha_divs);
.and_modify(|e| *e = e.saturating_add(dividend))
.or_insert(dividend);
// Record the alpha_dividends.
root_dividends
.entry(hotkey.clone())
Expand All @@ -321,6 +336,8 @@ impl<T: Config> Pallet<T> {
// Accumulate total root divs.
total_root_divs = total_root_divs.saturating_add(root_divs);
}
log::debug!("alpha_dividends: {:?}", alpha_dividends);
log::debug!("root_dividends: {:?}", root_dividends);

// Compute root divs as TAO. Here we take
let mut tao_dividends: BTreeMap<T::AccountId, I96F32> = BTreeMap::new();
Expand All @@ -335,6 +352,7 @@ impl<T: Config> Pallet<T> {
.and_modify(|e| *e = root_tao)
.or_insert(root_tao);
}
log::debug!("tao_dividends: {:?}", tao_dividends);

// Distribute the owner cut.
if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) {
Expand Down Expand Up @@ -363,6 +381,8 @@ impl<T: Config> Pallet<T> {
// Distribute alpha divs.
let _ = AlphaDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
for (hotkey, mut alpha_divs) in alpha_dividends {
log::debug!("hotkey: {:?} alpha_divs: {:?}", hotkey, alpha_divs);

// Get take prop
let alpha_take: I96F32 =
Self::get_hotkey_take_float(&hotkey).saturating_mul(alpha_divs);
Expand All @@ -386,6 +406,7 @@ impl<T: Config> Pallet<T> {
// Distribute root tao divs.
let _ = TaoDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
for (hotkey, mut root_tao) in tao_dividends {
log::debug!("hotkey: {:?} root_tao: {:?}", hotkey, root_tao);
// Get take prop
let tao_take: I96F32 = Self::get_hotkey_take_float(&hotkey).saturating_mul(root_tao);
// Remove take prop from root_tao
Expand Down
13 changes: 13 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,9 @@ pub mod pallet {
/// ============================
/// ==== Subnet Locks =====
/// ============================
#[pallet::storage] // --- MAP ( netuid ) --> transfer_toggle
pub type TransferToggle<T: Config> =
StorageMap<_, Identity, u16, bool, ValueQuery, DefaultTrue<T>>;
#[pallet::storage] // --- MAP ( netuid ) --> total_subnet_locked
pub type SubnetLocked<T: Config> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64<T>>;
Expand Down Expand Up @@ -1645,6 +1648,7 @@ pub enum CustomTransactionError {
RateLimitExceeded,
InsufficientLiquidity,
SlippageTooHigh,
TransferDisallowed,
BadRequest,
}

Expand All @@ -1660,6 +1664,7 @@ impl From<CustomTransactionError> for u8 {
CustomTransactionError::RateLimitExceeded => 6,
CustomTransactionError::InsufficientLiquidity => 7,
CustomTransactionError::SlippageTooHigh => 8,
CustomTransactionError::TransferDisallowed => 9,
CustomTransactionError::BadRequest => 255,
}
}
Expand Down Expand Up @@ -1735,6 +1740,10 @@ where
CustomTransactionError::SlippageTooHigh.into(),
)
.into()),
Error::<T>::TransferDisallowed => Err(InvalidTransaction::Custom(
CustomTransactionError::TransferDisallowed.into(),
)
.into()),
_ => Err(
InvalidTransaction::Custom(CustomTransactionError::BadRequest.into()).into(),
),
Expand Down Expand Up @@ -1959,6 +1968,7 @@ where
*alpha_amount,
*alpha_amount,
None,
false,
))
}
Some(Call::transfer_stake {
Expand All @@ -1979,6 +1989,7 @@ where
*alpha_amount,
*alpha_amount,
None,
true,
))
}
Some(Call::swap_stake {
Expand All @@ -1998,6 +2009,7 @@ where
*alpha_amount,
*alpha_amount,
None,
false,
))
}
Some(Call::swap_stake_limit {
Expand Down Expand Up @@ -2026,6 +2038,7 @@ where
*alpha_amount,
max_amount,
Some(*allow_partial),
false,
))
}
Some(Call::register { netuid, .. } | Call::burned_register { netuid, .. }) => {
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/src/macros/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,7 @@ mod errors {
InsufficientLiquidity,
/// Slippage is too high for the transaction.
SlippageTooHigh,
/// Subnet disallows transfer.
TransferDisallowed,
}
}
6 changes: 6 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,11 @@ mod events {
/// Parameters:
/// (coldkey, hotkey, origin_netuid, destination_netuid, amount)
StakeSwapped(T::AccountId, T::AccountId, u16, u16, u64),

/// Event called when transfer is toggled on a subnet.
///
/// Parameters:
/// (netuid, bool)
TransferToggle(u16, bool),
}
}
22 changes: 13 additions & 9 deletions pallets/subtensor/src/migrations/migrate_rao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use sp_runtime::format;
use substrate_fixed::types::U64F64;

use super::*;
use crate::subnets::subnet::POOL_INITIAL_TAO;

pub fn migrate_rao<T: Config>() -> Weight {
let migration_name = b"migrate_rao".to_vec();
Expand Down Expand Up @@ -76,18 +75,23 @@ pub fn migrate_rao<T: Config>() -> Weight {

// Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha
// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
let pool_initial_tao = POOL_INITIAL_TAO.min(lock.max(1));
let pool_initial_tao = Pallet::<T>::get_network_min_lock();
if lock < pool_initial_tao {
let difference: u64 = pool_initial_tao.saturating_sub(lock);
TotalIssuance::<T>::mutate(|total| {
*total = total.saturating_add(difference);
});
}

let remaining_lock = lock.saturating_sub(pool_initial_tao);
// Refund the owner for the remaining lock.
Pallet::<T>::add_balance_to_coldkey_account(&owner, remaining_lock);
SubnetTAO::<T>::insert(netuid, pool_initial_tao); // Set TAO to the lock.

SubnetAlphaIn::<T>::insert(
netuid,
pool_initial_tao.saturating_mul(netuids.len() as u64),
); // Set AlphaIn to the initial alpha distribution.

SubnetLocked::<T>::insert(netuid, 0); // Clear lock amount.
SubnetTAO::<T>::insert(netuid, pool_initial_tao);
TotalStake::<T>::mutate(|total| {
*total = total.saturating_add(pool_initial_tao);
}); // Increase total stake.
SubnetAlphaIn::<T>::insert(netuid, pool_initial_tao); // Set initial alpha to pool initial tao.
SubnetAlphaOut::<T>::insert(netuid, 0); // Set zero subnet alpha out.
SubnetMechanism::<T>::insert(netuid, 1); // Convert to dynamic immediately with initialization.
Tempo::<T>::insert(netuid, DefaultTempo::<T>::get());
Expand Down
16 changes: 16 additions & 0 deletions pallets/subtensor/src/staking/move_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl<T: Config> Pallet<T> {
alpha_amount,
None,
None,
false,
)?;

// Log the event.
Expand Down Expand Up @@ -96,6 +97,16 @@ impl<T: Config> Pallet<T> {
///
/// # Events
/// Emits a `StakeTransferred` event upon successful completion of the transfer.
pub fn toggle_transfer(netuid: u16, toggle: bool) -> dispatch::DispatchResult {
TransferToggle::<T>::insert(netuid, toggle);
log::debug!(
"TransferToggle( netuid: {:?}, toggle: {:?} ) ",
netuid,
toggle
);
Self::deposit_event(Event::TransferToggle(netuid, toggle));
Ok(())
}
pub fn do_transfer_stake(
origin: T::RuntimeOrigin,
destination_coldkey: T::AccountId,
Expand All @@ -118,6 +129,7 @@ impl<T: Config> Pallet<T> {
alpha_amount,
None,
None,
true,
)?;

// 9. Emit an event for logging/monitoring.
Expand Down Expand Up @@ -187,6 +199,7 @@ impl<T: Config> Pallet<T> {
alpha_amount,
None,
None,
false,
)?;

// Emit an event for logging.
Expand Down Expand Up @@ -258,6 +271,7 @@ impl<T: Config> Pallet<T> {
alpha_amount,
Some(limit_price),
Some(allow_partial),
false,
)?;

// Emit an event for logging.
Expand Down Expand Up @@ -293,6 +307,7 @@ impl<T: Config> Pallet<T> {
alpha_amount: u64,
maybe_limit_price: Option<u64>,
maybe_allow_partial: Option<bool>,
check_transfer_toggle: bool,
) -> Result<u64, Error<T>> {
// Calculate the maximum amount that can be executed
let max_amount = if let Some(limit_price) = maybe_limit_price {
Expand All @@ -312,6 +327,7 @@ impl<T: Config> Pallet<T> {
alpha_amount,
max_amount,
maybe_allow_partial,
check_transfer_toggle,
)?;

// Unstake from the origin subnet, returning TAO (or a 1:1 equivalent).
Expand Down
13 changes: 13 additions & 0 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ impl<T: Config> Pallet<T> {
alpha_amount: u64,
max_amount: u64,
maybe_allow_partial: Option<bool>,
check_transfer_toggle: bool,
) -> Result<(), Error<T>> {
// Ensure that both subnets exist.
ensure!(
Expand Down Expand Up @@ -899,6 +900,18 @@ impl<T: Config> Pallet<T> {
}
}

if check_transfer_toggle {
// Ensure transfer is toggled.
ensure!(
TransferToggle::<T>::get(origin_netuid),
Error::<T>::TransferDisallowed
);
ensure!(
TransferToggle::<T>::get(destination_netuid),
Error::<T>::TransferDisallowed
);
}

Ok(())
}
}
Expand Down
Loading
Loading