Skip to content

devnet deploy 4/2/2025 #1499

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

Merged
merged 14 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ futures = "0.3.30"
hex = { version = "0.4", default-features = false }
hex-literal = "0.4.1"
jsonrpsee = { version = "0.24.4", default-features = false }
libsecp256k1 = { version = "0.7.2", default-features = false }
log = { version = "0.4.21", default-features = false }
memmap2 = "0.9.4"
ndarray = { version = "0.15.6", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ serde_bytes = { workspace = true, features = ["alloc"] }
serde_with = { workspace = true, features = ["macros"] }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
libsecp256k1 = { workspace = true }
log = { workspace = true }
substrate-fixed = { workspace = true }
pallet-transaction-payment = { workspace = true }
Expand Down Expand Up @@ -91,6 +92,7 @@ std = [
"sp-tracing/std",
"sp-version/std",
"hex/std",
"libsecp256k1/std",
"log/std",
"ndarray/std",
"serde/std",
Expand Down
10 changes: 9 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub mod pallet {
};
use frame_system::pallet_prelude::*;
use pallet_drand::types::RoundNumber;
use sp_core::{ConstU32, H256};
use sp_core::{ConstU32, H160, H256};
use sp_runtime::traits::{Dispatchable, TrailingZeroInput};
use sp_std::collections::vec_deque::VecDeque;
use sp_std::vec;
Expand Down Expand Up @@ -1561,6 +1561,14 @@ pub mod pallet {
OptionQuery,
>;

/// =============================
/// ==== EVM related storage ====
/// =============================
#[pallet::storage]
/// --- DMAP (netuid, uid) --> (H160, last_block_where_ownership_was_proven)
pub type AssociatedEvmAddress<T: Config> =
StorageDoubleMap<_, Twox64Concat, u16, Twox64Concat, u16, (H160, u64), OptionQuery>;

/// ==================
/// ==== Genesis =====
/// ==================
Expand Down
45 changes: 45 additions & 0 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod dispatches {
use frame_support::traits::schedule::DispatchTime;
use frame_support::traits::schedule::v3::Anon as ScheduleAnon;
use frame_system::pallet_prelude::BlockNumberFor;
use sp_core::ecdsa::Signature;
use sp_runtime::traits::Saturating;

use crate::MAX_CRV3_COMMIT_SIZE_BYTES;
Expand Down Expand Up @@ -1929,6 +1930,50 @@ mod dispatches {
Ok(())
}

/// Attempts to associate a hotkey with an EVM key.
///
/// The signature will be checked to see if the recovered public key matches the `evm_key` provided.
///
/// The EVM key is expected to sign the message according to this formula to produce the signature:
/// ```text
/// keccak_256(hotkey ++ keccak_256(block_number))
/// ```
///
/// # Arguments
/// * `origin` - The origin of the transaction, which must be signed by the coldkey that owns the `hotkey`.
/// * `netuid` - The netuid that the `hotkey` belongs to.
/// * `hotkey` - The hotkey associated with the `origin`.
/// * `evm_key` - The EVM key to associate with the `hotkey`.
/// * `block_number` - The block number used in the `signature`.
/// * `signature` - A signed message by the `evm_key` containing the `hotkey` and the hashed `block_number`.
///
/// # Errors
/// Returns an error if:
/// * The transaction is not signed.
/// * The hotkey is not owned by the origin coldkey.
/// * The hotkey does not belong to the subnet identified by the netuid.
/// * The EVM key cannot be recovered from the signature.
/// * The EVM key recovered from the signature does not match the given EVM key.
///
/// # Events
/// May emit a `EvmKeyAssociated` event on success
#[pallet::call_index(93)]
#[pallet::weight((
Weight::from_parts(3_000_000, 0).saturating_add(T::DbWeight::get().reads_writes(2, 1)),
DispatchClass::Operational,
Pays::Yes
))]
pub fn associate_evm_key(
origin: T::RuntimeOrigin,
netuid: u16,
hotkey: T::AccountId,
evm_key: H160,
block_number: u64,
signature: Signature,
) -> DispatchResult {
Self::do_associate_evm_key(origin, netuid, hotkey, evm_key, block_number, signature)
}

/// Recycles alpha from a cold/hot key pair, reducing AlphaOut on a subnet
///
/// # Arguments
Expand Down
4 changes: 4 additions & 0 deletions pallets/subtensor/src/macros/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,9 @@ mod errors {
NotEnoughAlphaOutToRecycle,
/// Cannot burn or recycle TAO from root subnet
CannotBurnOrRecycleOnRootSubnet,
/// Public key cannot be recovered.
UnableToRecoverPublicKey,
/// Recovered public key is invalid.
InvalidRecoveredPublicKey,
}
}
12 changes: 12 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@ mod events {
/// (coldkey, hotkey, amount, subnet_id)
AlphaBurned(T::AccountId, T::AccountId, u64, u16),

/// An EVM key has been associated with a hotkey.
EvmKeyAssociated {
/// The subnet that the hotkey belongs to.
netuid: u16,
/// The hotkey associated with the EVM key.
hotkey: T::AccountId,
/// The EVM key being associated with the hotkey.
evm_key: H160,
/// The block where the association happened.
block_associated: u64,
},

/// CRV3 Weights have been successfully revealed.
///
/// - **netuid**: The network identifier.
Expand Down
30 changes: 30 additions & 0 deletions pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ impl<T: Config> Pallet<T> {
// Ensure that the hotkey has enough stake to withdraw.
let alpha_unstaked =
Self::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);

if Self::validate_remove_stake(
&coldkey,
&hotkey,
netuid,
alpha_unstaked,
alpha_unstaked,
false,
)
.is_err()
{
// Don't unstake from this netuid
continue;
}

let fee = Self::calculate_staking_fee(
Some((&hotkey, netuid)),
&coldkey,
Expand Down Expand Up @@ -211,6 +226,21 @@ impl<T: Config> Pallet<T> {
// Ensure that the hotkey has enough stake to withdraw.
let alpha_unstaked =
Self::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);

if Self::validate_remove_stake(
&coldkey,
&hotkey,
netuid,
alpha_unstaked,
alpha_unstaked,
false,
)
.is_err()
{
// Don't unstake from this netuid
continue;
}

let fee = Self::calculate_staking_fee(
Some((&hotkey, netuid)),
&coldkey,
Expand Down
16 changes: 11 additions & 5 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,24 @@ impl<T: Config> Pallet<T> {
}
}
pub fn update_moving_price(netuid: u16) {
let blocks_since_registration = U96F32::saturating_from_num(
Self::get_current_block_as_u64().saturating_sub(NetworkRegisteredAt::<T>::get(netuid)),
);
let blocks_since_start_call = U96F32::saturating_from_num({
// We expect FirstEmissionBlockNumber to be set earlier, and we take the block when
// `start_call` was called (first block before FirstEmissionBlockNumber).
let start_call_block = FirstEmissionBlockNumber::<T>::get(netuid)
.unwrap_or_default()
.saturating_sub(1);

Self::get_current_block_as_u64().saturating_sub(start_call_block)
});

// Use halving time hyperparameter. The meaning of this parameter can be best explained under
// the assumption of a constant price and SubnetMovingAlpha == 0.5: It is how many blocks it
// will take in order for the distance between current EMA of price and current price to shorten
// by half.
let halving_time = EMAPriceHalvingBlocks::<T>::get(netuid);
let current_ma_unsigned = U96F32::saturating_from_num(SubnetMovingAlpha::<T>::get());
let alpha: U96F32 = current_ma_unsigned.saturating_mul(blocks_since_registration.safe_div(
blocks_since_registration.saturating_add(U96F32::saturating_from_num(halving_time)),
let alpha: U96F32 = current_ma_unsigned.saturating_mul(blocks_since_start_call.safe_div(
blocks_since_start_call.saturating_add(U96F32::saturating_from_num(halving_time)),
));
// Because alpha = b / (b + h), where b and h > 0, alpha < 1, so 1 - alpha > 0.
// We can use unsigned type here: U96F32
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn test_coinbase_moving_prices() {
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
SubnetMechanism::<Test>::insert(netuid, 1);
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(1));
NetworkRegisteredAt::<Test>::insert(netuid, 1);
FirstEmissionBlockNumber::<Test>::insert(netuid, 1);

// Updating the moving price keeps it the same.
assert_eq!(
Expand Down Expand Up @@ -250,7 +250,7 @@ fn test_update_moving_price_initial() {

// Registered recently
System::set_block_number(510);
NetworkRegisteredAt::<Test>::insert(netuid, 500);
FirstEmissionBlockNumber::<Test>::insert(netuid, 500);

SubtensorModule::update_moving_price(netuid);

Expand All @@ -275,7 +275,7 @@ fn test_update_moving_price_after_time() {

// Registered long time ago
System::set_block_number(144_000_500);
NetworkRegisteredAt::<Test>::insert(netuid, 500);
FirstEmissionBlockNumber::<Test>::insert(netuid, 500);

SubtensorModule::update_moving_price(netuid);

Expand Down
Loading
Loading