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 11-1-2024 #903

Merged
merged 37 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8810f4b
store more blocks & batch reveals out of order
JohnReedV Oct 25, 2024
6886a6d
update test
JohnReedV Oct 28, 2024
59426cd
add test_get_reveal_blocks & fmt
JohnReedV Oct 29, 2024
25a26e8
update doc
JohnReedV Oct 29, 2024
d81cbaa
Make devnet-ready mergable with main
gztensor Oct 29, 2024
653ba75
fix cargo audit label
sam0x17 Oct 29, 2024
f94b4be
fix
sam0x17 Oct 29, 2024
c64caf1
bump spec version
sam0x17 Oct 29, 2024
65f656e
Merge pull request #884 from opentensor/fix/stake-deltas-conflicts-de…
unconst Oct 29, 2024
05de4a5
fix toolchain
sam0x17 Oct 29, 2024
beca39b
bump CI
sam0x17 Oct 29, 2024
50f8a9d
fix
sam0x17 Oct 29, 2024
91fe42b
Merge pull request #886 from opentensor/sam-hotfix
unconst Oct 29, 2024
f37dd7a
hotfix
sam0x17 Oct 29, 2024
a389bcd
bump CI
sam0x17 Oct 29, 2024
69b7ac8
fix again
sam0x17 Oct 29, 2024
a82a1f8
disable set weights rate limit for commit-reveal
JohnReedV Oct 30, 2024
2b4610e
Merge pull request #885 from opentensor/devnet-ready
unconst Oct 30, 2024
f5df0ba
doc comments and lints
JohnReedV Oct 30, 2024
b07c1ff
add events
JohnReedV Oct 30, 2024
96911e6
revert hyperparam name change
JohnReedV Oct 30, 2024
498d30a
add set weights rate limit to commit-reveal
JohnReedV Oct 30, 2024
f95fb5b
clippy
JohnReedV Oct 30, 2024
8a97a12
use new index
JohnReedV Oct 30, 2024
ba7e34c
update comments
JohnReedV Oct 31, 2024
93c3f9c
expand test
JohnReedV Oct 31, 2024
08e62d0
bump migration version
JohnReedV Oct 31, 2024
bc08dc8
update migration test
JohnReedV Oct 31, 2024
6133648
reduce key swap cost to 0.1 TAO
camfairchild Oct 31, 2024
e8cd841
Merge pull request #895 from opentensor/commit-reveal-2-1
unconst Oct 31, 2024
e8d926a
Merge pull request #897 from opentensor/feat/lower-key-swap-cost
unconst Oct 31, 2024
8e7e140
bump spec version to 204
sam0x17 Oct 31, 2024
d5b0703
Merge pull request #900 from opentensor/bump-spec-version-204
unconst Oct 31, 2024
362d0b8
alice default port from 9946 to 9944
roman-opentensor Oct 31, 2024
8b1ce51
Merge pull request #902 from opentensor/fix/roman/alice-port-9944-loc…
unconst Oct 31, 2024
04766ec
bump spec version to 205
sam0x17 Oct 31, 2024
d70b8e2
Merge pull request #905 from opentensor/bump-spec-version-205
unconst Oct 31, 2024
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
17 changes: 4 additions & 13 deletions .github/workflows/check-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,7 @@ jobs:
cargo-audit:
name: cargo audit
runs-on: SubtensorCI
strategy:
matrix:
rust-branch:
- stable
rust-target:
- x86_64-unknown-linux-gnu
# - x86_64-apple-darwin
os:
- ubuntu-latest
# - macos-latest
if: ${{ github.event_name != 'push' && !contains(github.event.pull_request.labels.*.name, 'skip-cargo-audit') }}
steps:
- name: Check-out repositoroy under $GITHUB_WORKSPACE
uses: actions/checkout@v4
Expand All @@ -231,17 +222,17 @@ jobs:
sudo apt-get update &&
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler

- name: Install Rust ${{ matrix.rust-branch }}
- name: Install Rust Stable
uses: actions-rs/[email protected]
with:
toolchain: ${{ matrix.rust-branch }}
toolchain: stable
components: rustfmt, clippy
profile: minimal

- name: Utilize Shared Rust Cache
uses: Swatinem/[email protected]
with:
key: ${{ matrix.os }}-${{ env.RUST_BIN_DIR }}
key: ubuntu-latest-${{ env.RUST_BIN_DIR }}

- name: Install cargo-audit
run: cargo install cargo-audit
Expand Down
4 changes: 2 additions & 2 deletions pallets/admin-utils/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ mod benchmarks {
}

#[benchmark]
fn sudo_set_commit_reveal_weights_periods() {
fn sudo_set_commit_reveal_weights_interval() {
pallet_subtensor::Pallet::<T>::init_new_network(
1u16, /*netuid*/
1u16, /*sudo_tempo*/
);

#[extrinsic_call]
_(RawOrigin::Root, 1u16/*netuid*/, 3u64/*interval*/)/*set_commit_reveal_weights_periods()*/;
_(RawOrigin::Root, 1u16/*netuid*/, 3u64/*interval*/)/*sudo_set_commit_reveal_weights_interval()*/;
}

#[benchmark]
Expand Down
14 changes: 7 additions & 7 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,12 +1187,12 @@ pub mod pallet {
///
/// # Weight
/// Weight is handled by the `#[pallet::weight]` attribute.
#[pallet::call_index(56)]
#[pallet::weight(T::WeightInfo::sudo_set_commit_reveal_weights_periods())]
pub fn sudo_set_commit_reveal_weights_periods(
#[pallet::call_index(57)]
#[pallet::weight(T::WeightInfo::sudo_set_commit_reveal_weights_interval())]
pub fn sudo_set_commit_reveal_weights_interval(
origin: OriginFor<T>,
netuid: u16,
periods: u64,
interval: u64,
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;

Expand All @@ -1201,11 +1201,11 @@ pub mod pallet {
Error::<T>::SubnetDoesNotExist
);

pallet_subtensor::Pallet::<T>::set_reveal_period(netuid, periods);
pallet_subtensor::Pallet::<T>::set_reveal_period(netuid, interval);
log::debug!(
"SetWeightCommitPeriods( netuid: {:?}, periods: {:?} ) ",
"SetWeightCommitInterval( netuid: {:?}, interval: {:?} ) ",
netuid,
periods
interval
);
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions pallets/admin-utils/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait WeightInfo {
fn sudo_set_min_burn() -> Weight;
fn sudo_set_network_registration_allowed() -> Weight;
fn sudo_set_tempo() -> Weight;
fn sudo_set_commit_reveal_weights_periods() -> Weight;
fn sudo_set_commit_reveal_weights_interval() -> Weight;
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
}

Expand Down Expand Up @@ -413,7 +413,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
fn sudo_set_commit_reveal_weights_periods() -> Weight {
fn sudo_set_commit_reveal_weights_interval() -> Weight {
// Proof Size summary in bytes:
// Measured: `456`
// Estimated: `3921`
Expand Down Expand Up @@ -781,7 +781,7 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
fn sudo_set_commit_reveal_weights_periods() -> Weight {
fn sudo_set_commit_reveal_weights_interval() -> Weight {
// -- Extrinsic Time --
// Model:
// Time ~= 19.38
Expand Down
4 changes: 2 additions & 2 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,15 +1414,15 @@ fn test_sudo_set_dissolve_network_schedule_duration() {
}

#[test]
fn sudo_set_commit_reveal_weights_periods() {
fn sudo_set_commit_reveal_weights_interval() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 10);

let to_be_set = 55;
let init_value = SubtensorModule::get_reveal_period(netuid);

assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_periods(
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
Expand Down
64 changes: 37 additions & 27 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,69 +265,65 @@ impl<T: Config> Pallet<T> {
// --- 1.0 Drain the hotkey emission.
PendingdHotkeyEmission::<T>::insert(hotkey, 0);

// --- 2 Retrieve the last time this hotkey's emissions were drained.
let last_emission_drain: u64 = LastHotkeyEmissionDrain::<T>::get(hotkey);

// --- 3 Update the block value to the current block number.
// --- 2 Update the block value to the current block number.
LastHotkeyEmissionDrain::<T>::insert(hotkey, block_number);

// --- 4 Retrieve the total stake for the hotkey from all nominations.
// --- 3 Retrieve the total stake for the hotkey from all nominations.
let total_hotkey_stake: u64 = Self::get_total_stake_for_hotkey(hotkey);

// --- 5 Calculate the emission take for the hotkey.
// --- 4 Calculate the emission take for the hotkey.
let take_proportion: I64F64 = I64F64::from_num(Delegates::<T>::get(hotkey))
.saturating_div(I64F64::from_num(u16::MAX));
let hotkey_take: u64 =
(take_proportion.saturating_mul(I64F64::from_num(emission))).to_num::<u64>();

// --- 6 Compute the remaining emission after deducting the hotkey's take.
// --- 5 Compute the remaining emission after deducting the hotkey's take.
let emission_minus_take: u64 = emission.saturating_sub(hotkey_take);

// --- 7 Calculate the remaining emission after the hotkey's take.
// --- 6 Calculate the remaining emission after the hotkey's take.
let mut remainder: u64 = emission_minus_take;

// --- 8 Iterate over each nominator and get all viable stake.
// --- 7 Iterate over each nominator and get all viable stake.
let mut total_viable_nominator_stake: u64 = total_hotkey_stake;
for (nominator, nominator_stake) in Stake::<T>::iter_prefix(hotkey) {
if LastAddStakeIncrease::<T>::get(hotkey, nominator) > last_emission_drain {
total_viable_nominator_stake =
total_viable_nominator_stake.saturating_sub(nominator_stake);
}
for (nominator, _) in Stake::<T>::iter_prefix(hotkey) {
let nonviable_nomintaor_stake = Self::get_nonviable_stake(hotkey, &nominator);

total_viable_nominator_stake =
total_viable_nominator_stake.saturating_sub(nonviable_nomintaor_stake);
}

// --- 9 Iterate over each nominator.
// --- 8 Iterate over each nominator.
if total_viable_nominator_stake != 0 {
for (nominator, nominator_stake) in Stake::<T>::iter_prefix(hotkey) {
// --- 10 Check if the stake was manually increased by the user since the last emission drain for this hotkey.
// --- 9 Check if the stake was manually increased by the user since the last emission drain for this hotkey.
// If it was, skip this nominator as they will not receive their proportion of the emission.
if LastAddStakeIncrease::<T>::get(hotkey, nominator.clone()) > last_emission_drain {
continue;
}
let viable_nominator_stake =
nominator_stake.saturating_sub(Self::get_nonviable_stake(hotkey, &nominator));

// --- 11 Calculate this nominator's share of the emission.
let nominator_emission: I64F64 = I64F64::from_num(emission_minus_take)
.saturating_mul(I64F64::from_num(nominator_stake))
// --- 10 Calculate this nominator's share of the emission.
let nominator_emission: I64F64 = I64F64::from_num(viable_nominator_stake)
.checked_div(I64F64::from_num(total_viable_nominator_stake))
.unwrap_or(I64F64::from_num(0));
.unwrap_or(I64F64::from_num(0))
.saturating_mul(I64F64::from_num(emission_minus_take));

// --- 12 Increase the stake for the nominator.
// --- 11 Increase the stake for the nominator.
Self::increase_stake_on_coldkey_hotkey_account(
&nominator,
hotkey,
nominator_emission.to_num::<u64>(),
);

// --- 13* Record event and Subtract the nominator's emission from the remainder.
// --- 12* Record event and Subtract the nominator's emission from the remainder.
total_new_tao = total_new_tao.saturating_add(nominator_emission.to_num::<u64>());
remainder = remainder.saturating_sub(nominator_emission.to_num::<u64>());
}
}

// --- 14 Finally, add the stake to the hotkey itself, including its take and the remaining emission.
// --- 13 Finally, add the stake to the hotkey itself, including its take and the remaining emission.
let hotkey_new_tao: u64 = hotkey_take.saturating_add(remainder);
Self::increase_stake_on_hotkey_account(hotkey, hotkey_new_tao);

// --- 15 Record new tao creation event and return the amount created.
// --- 14 Record new tao creation event and return the amount created.
total_new_tao = total_new_tao.saturating_add(hotkey_new_tao);
total_new_tao
}
Expand Down Expand Up @@ -382,4 +378,18 @@ impl<T: Config> Pallet<T> {
let remainder = block_plus_netuid.rem_euclid(tempo_plus_one);
(tempo as u64).saturating_sub(remainder)
}

/// Calculates the nonviable stake for a nominator.
/// The nonviable stake is the stake that was added by the nominator since the last emission drain.
/// This stake will not receive emission until the next emission drain.
/// Note: if the stake delta is below zero, we return zero. We don't allow more stake than the nominator has.
pub fn get_nonviable_stake(hotkey: &T::AccountId, nominator: &T::AccountId) -> u64 {
let stake_delta = StakeDeltaSinceLastEmissionDrain::<T>::get(hotkey, nominator);
if stake_delta.is_negative() {
0
} else {
// Should never fail the into, but we handle it anyway.
stake_delta.try_into().unwrap_or(u64::MAX)
}
}
}
19 changes: 12 additions & 7 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ pub mod pallet {
0
}
#[pallet::type_value]
/// Default stake delta.
pub fn DefaultStakeDelta<T: Config>() -> i128 {
0
}
#[pallet::type_value]
/// Default stakes per interval.
pub fn DefaultStakesPerInterval<T: Config>() -> (u64, u64) {
(0, 0)
Expand Down Expand Up @@ -569,7 +574,7 @@ pub mod pallet {
0
}
#[pallet::type_value]
/// Default minimum stake for weights.
/// Default Reveal Period Epochs
pub fn DefaultRevealPeriodEpochs<T: Config>() -> u64 {
1
}
Expand Down Expand Up @@ -791,16 +796,16 @@ pub mod pallet {
DefaultAccumulatedEmission<T>,
>;
#[pallet::storage]
/// Map ( hot, cold ) --> block_number | Last add stake increase.
pub type LastAddStakeIncrease<T: Config> = StorageDoubleMap<
/// Map ( hot, cold ) --> stake: i128 | Stake added/removed since last emission drain.
pub type StakeDeltaSinceLastEmissionDrain<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Identity,
T::AccountId,
u64,
i128,
ValueQuery,
DefaultAccountTake<T>,
DefaultStakeDelta<T>,
>;
#[pallet::storage]
/// DMAP ( parent, netuid ) --> Vec<(proportion,child)>
Expand Down Expand Up @@ -1251,14 +1256,14 @@ pub mod pallet {
/// ITEM( weights_min_stake )
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
#[pallet::storage]
/// --- MAP (netuid, who) --> VecDeque<(hash, commit_block)> | Stores a queue of commits for an account on a given netuid.
/// --- MAP (netuid, who) --> VecDeque<(hash, commit_block, first_reveal_block, last_reveal_block)> | Stores a queue of commits for an account on a given netuid.
pub type WeightCommits<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
u16,
Twox64Concat,
T::AccountId,
VecDeque<(H256, u64)>,
VecDeque<(H256, u64, u64, u64)>,
OptionQuery,
>;
#[pallet::storage]
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 @@ -188,5 +188,7 @@ mod errors {
RevealTooEarly,
/// Attempted to batch reveal weights with mismatched vector input lenghts.
InputLengthsUnequal,
/// A transactor exceeded the rate limit for setting weights.
CommittingWeightsTooFast,
}
}
20 changes: 20 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,25 @@ mod events {
ColdkeySwapScheduleDurationSet(BlockNumberFor<T>),
/// The duration of dissolve network has been set
DissolveNetworkScheduleDurationSet(BlockNumberFor<T>),
/// Weights have been successfully committed.
///
/// - **who**: The account ID of the user committing the weights.
/// - **netuid**: The network identifier.
/// - **commit_hash**: The hash representing the committed weights.
WeightsCommitted(T::AccountId, u16, H256),

/// Weights have been successfully revealed.
///
/// - **who**: The account ID of the user revealing the weights.
/// - **netuid**: The network identifier.
/// - **commit_hash**: The hash of the revealed weights.
WeightsRevealed(T::AccountId, u16, H256),

/// Weights have been successfully batch revealed.
///
/// - **who**: The account ID of the user revealing the weights.
/// - **netuid**: The network identifier.
/// - **revealed_hashes**: A vector of hashes representing each revealed weight set.
WeightsBatchRevealed(T::AccountId, u16, Vec<H256>),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use scale_info::prelude::string::String;
use sp_io::{hashing::twox_128, storage::clear_prefix, KillStorageResult};

pub fn migrate_commit_reveal_2<T: Config>() -> Weight {
let migration_name = b"migrate_commit_reveal_2".to_vec();
let migration_name = b"migrate_commit_reveal_2_v2".to_vec();
let mut weight = T::DbWeight::get().reads(1);

if HasMigrationRun::<T>::get(&migration_name) {
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/src/rpc_info/subnet_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct SubnetInfov2<T: Config> {
identity: Option<SubnetIdentity>,
}

#[freeze_struct("4ceb81dfe8a8f96d")]
#[freeze_struct("55b472510f10e76a")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct SubnetHyperparams {
rho: Compact<u16>,
Expand All @@ -76,7 +76,7 @@ pub struct SubnetHyperparams {
max_validators: Compact<u16>,
adjustment_alpha: Compact<u64>,
difficulty: Compact<u64>,
commit_reveal_periods: Compact<u64>,
commit_reveal_weights_interval: Compact<u64>,
commit_reveal_weights_enabled: bool,
alpha_high: Compact<u16>,
alpha_low: Compact<u16>,
Expand Down Expand Up @@ -280,7 +280,7 @@ impl<T: Config> Pallet<T> {
max_validators: max_validators.into(),
adjustment_alpha: adjustment_alpha.into(),
difficulty: difficulty.into(),
commit_reveal_periods: commit_reveal_periods.into(),
commit_reveal_weights_interval: commit_reveal_periods.into(),
commit_reveal_weights_enabled,
alpha_high: alpha_high.into(),
alpha_low: alpha_low.into(),
Expand Down
6 changes: 4 additions & 2 deletions pallets/subtensor/src/staking/add_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ impl<T: Config> Pallet<T> {
Error::<T>::StakeRateLimitExceeded
);

// Set the last time the stake increased for nominator drain protection.
LastAddStakeIncrease::<T>::insert(&hotkey, &coldkey, Self::get_current_block_as_u64());
// Track this addition in the stake delta.
StakeDeltaSinceLastEmissionDrain::<T>::mutate(&hotkey, &coldkey, |stake_delta| {
*stake_delta = stake_delta.saturating_add_unsigned(stake_to_be_added as u128);
});

// If coldkey is not owner of the hotkey, it's a nomination stake.
if !Self::coldkey_owns_hotkey(&coldkey, &hotkey) {
Expand Down
Loading
Loading