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

Commit-Reveal Weights #396

Merged
merged 43 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a7fc5e8
start commit-reveal WIP
JohnReedV May 5, 2024
e323eb6
do commit/reveal & add tests
JohnReedV May 6, 2024
172e31a
fix
JohnReedV May 6, 2024
c044947
fix checks
JohnReedV May 7, 2024
81a8aad
fix tests
JohnReedV May 7, 2024
51b6189
Merge branch 'development' into commit-reveal-weights
JohnReedV May 7, 2024
001918a
re add errors
JohnReedV May 7, 2024
1b22f33
revert set_weights removal
JohnReedV May 8, 2024
dcd0b0f
revert set_weights removal
JohnReedV May 8, 2024
2686bef
cargo clippy
JohnReedV May 8, 2024
9ba4e48
expand test_commit_reveal_interval
JohnReedV May 8, 2024
024c744
feat: make faucet feeless
May 8, 2024
2b8d13e
revert make faucet feeless
JohnReedV May 9, 2024
60e8fd0
remove nonce from hash
JohnReedV May 9, 2024
2a2ba23
cargo clippy
JohnReedV May 9, 2024
7d70267
cargo fmt
JohnReedV May 9, 2024
0ae2d5e
start commit-reveal WIP
JohnReedV May 5, 2024
9bf95fb
do commit/reveal & add tests
JohnReedV May 6, 2024
2d26b09
fix
JohnReedV May 6, 2024
bbc9383
fix checks
JohnReedV May 7, 2024
93b3e6c
fix tests
JohnReedV May 7, 2024
45989cb
re add errors
JohnReedV May 7, 2024
69899f0
revert set_weights removal
JohnReedV May 8, 2024
3466bf6
revert set_weights removal
JohnReedV May 8, 2024
3f5cc37
cargo clippy
JohnReedV May 8, 2024
677a241
expand test_commit_reveal_interval
JohnReedV May 8, 2024
c8b5d2e
feat: make faucet feeless
May 8, 2024
098efb2
revert make faucet feeless
JohnReedV May 9, 2024
e6daa91
remove nonce from hash
JohnReedV May 9, 2024
7f88478
cargo clippy
JohnReedV May 9, 2024
a65a43b
cargo fmt
JohnReedV May 10, 2024
4b5175b
Merge branch 'commit-reveal-weights' of github.com:opentensor/subtens…
JohnReedV May 10, 2024
50a305b
Merge branch 'development' into commit-reveal-weights
JohnReedV May 10, 2024
3fa337e
fix cargo.lock
JohnReedV May 10, 2024
9c8cab6
Merge branch 'commit-reveal-weights' of github.com:opentensor/subtens…
JohnReedV May 10, 2024
3178bfa
add logs
JohnReedV May 10, 2024
841db8a
add doc comments for commit/reveal
JohnReedV May 12, 2024
4a3156e
finish doc comments
JohnReedV May 12, 2024
c4246dc
prevent division by zero
JohnReedV May 12, 2024
869eabb
add benchmarks for commit/reveal
JohnReedV May 12, 2024
da4d9b9
fix warning
JohnReedV May 12, 2024
5e557b2
comments -> rust docs
JohnReedV May 13, 2024
22a9a66
update weight values
JohnReedV May 13, 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
8 changes: 8 additions & 0 deletions pallets/subtensor/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,13 @@ mod errors {
NomStakeBelowMinimumThreshold,
/// delegate take is being set out of bounds
InvalidTake,
/// Not allowed to commit weights
CommitNotAllowed,
/// No commit found for provided hotkey+netuid when attempting to reveal weights
NoCommitFound,
/// Not the correct block/range to reveal weights
InvalidRevealTempo,
/// Committed hash does not equal the hashed reveal data
InvalidReveal,
}
}
86 changes: 83 additions & 3 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub mod pallet {
traits::{tokens::fungible, UnfilteredDispatchable},
};
use frame_system::pallet_prelude::*;
use sp_core::H256;
use sp_runtime::traits::TrailingZeroInput;
use sp_std::vec;
use sp_std::vec::Vec;
Expand Down Expand Up @@ -786,6 +787,27 @@ pub mod pallet {
pub type AdjustmentAlpha<T: Config> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultAdjustmentAlpha<T>>;

// --- MAP (netuid, who) --> (hash, weight) | Returns the hash and weight committed by an account for a given netuid.
#[pallet::storage]
pub type WeightCommits<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
u16,
Twox64Concat,
T::AccountId,
(H256, u64),
OptionQuery,
>;

#[pallet::type_value]
pub fn DefaultWeightCommitRevealInterval<T: Config>() -> u64 {
1000
}

#[pallet::storage]
pub type WeightCommitRevealInterval<T> =
StorageValue<_, u64, ValueQuery, DefaultWeightCommitRevealInterval<T>>;

// =======================================
// ==== Subnetwork Consensus Storage ====
// =======================================
Expand Down Expand Up @@ -1184,8 +1206,8 @@ pub mod pallet {
// - Attempting to set weights with max value exceeding limit.
#[pallet::call_index(0)]
#[pallet::weight((Weight::from_parts(10_151_000_000, 0)
.saturating_add(T::DbWeight::get().reads(4104))
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
.saturating_add(T::DbWeight::get().reads(4104))
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
pub fn set_weights(
origin: OriginFor<T>,
netuid: u16,
Expand All @@ -1196,6 +1218,32 @@ pub mod pallet {
Self::do_set_weights(origin, netuid, dests, weights, version_key)
}

#[pallet::call_index(96)]
#[pallet::weight((Weight::from_parts(10_151_000_000, 0)
JohnReedV marked this conversation as resolved.
Show resolved Hide resolved
.saturating_add(T::DbWeight::get().reads(4104))
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
pub fn commit_weights(
origin: T::RuntimeOrigin,
netuid: u16,
commit_hash: H256,
) -> DispatchResult {
Self::do_commit_weights(origin, netuid, commit_hash)
}

#[pallet::call_index(97)]
#[pallet::weight((Weight::from_parts(10_151_000_000, 0)
.saturating_add(T::DbWeight::get().reads(4104))
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
pub fn reveal_weights(
origin: T::RuntimeOrigin,
netuid: u16,
uids: Vec<u16>,
values: Vec<u16>,
version_key: u64,
) -> DispatchResult {
Self::do_reveal_weights(origin, netuid, uids, values, version_key)
}

// # Args:
// * `origin`: (<T as frame_system::Config>Origin):
// - The caller, a hotkey who wishes to set their weights.
Expand Down Expand Up @@ -1756,7 +1804,7 @@ pub mod pallet {

#[pallet::call_index(60)]
#[pallet::weight((Weight::from_parts(91_000_000, 0)
.saturating_add(T::DbWeight::get().reads(27))
.saturating_add(T::DbWeight::get().reads(27))
.saturating_add(T::DbWeight::get().writes(22)), DispatchClass::Normal, Pays::No))]
pub fn faucet(
origin: OriginFor<T>,
Expand Down Expand Up @@ -1908,6 +1956,30 @@ where
_len: usize,
) -> TransactionValidity {
match call.is_sub_type() {
Some(Call::commit_weights { netuid, .. }) => {
if Self::check_weights_min_stake(who) {
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
Ok(ValidTransaction {
priority,
longevity: 1,
..Default::default()
})
} else {
Err(InvalidTransaction::Call.into())
}
}
Some(Call::reveal_weights { netuid, .. }) => {
if Self::check_weights_min_stake(who) {
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
Ok(ValidTransaction {
priority,
longevity: 1,
..Default::default()
})
} else {
Err(InvalidTransaction::Call.into())
}
}
Some(Call::set_weights { netuid, .. }) => {
if Self::check_weights_min_stake(who) {
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
Expand Down Expand Up @@ -1986,6 +2058,14 @@ where
let transaction_fee = 0;
Ok((CallType::SetWeights, transaction_fee, who.clone()))
}
Some(Call::commit_weights { .. }) => {
let transaction_fee = 0;
Ok((CallType::SetWeights, transaction_fee, who.clone()))
}
Some(Call::reveal_weights { .. }) => {
let transaction_fee = 0;
Ok((CallType::SetWeights, transaction_fee, who.clone()))
}
Some(Call::register { .. }) => {
let transaction_fee = 0;
Ok((CallType::Register, transaction_fee, who.clone()))
Expand Down
98 changes: 98 additions & 0 deletions pallets/subtensor/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
use super::*;
use crate::math::*;
use sp_core::H256;
use sp_runtime::traits::{BlakeTwo256, Hash};
use sp_std::vec;

impl<T: Config> Pallet<T> {
pub fn do_commit_weights(
JohnReedV marked this conversation as resolved.
Show resolved Hide resolved
origin: T::RuntimeOrigin,
netuid: u16,
commit_hash: H256,
) -> DispatchResult {
let who = ensure_signed(origin)?;

log::info!("do_commit_weights( hotkey:{:?} netuid:{:?})", who, netuid);

ensure!(Self::can_commit(netuid, &who), Error::<T>::CommitNotAllowed);

WeightCommits::<T>::insert(
netuid,
&who,
(commit_hash, Self::get_current_block_as_u64()),
);
Ok(())
}

pub fn do_reveal_weights(
JohnReedV marked this conversation as resolved.
Show resolved Hide resolved
origin: T::RuntimeOrigin,
netuid: u16,
uids: Vec<u16>,
values: Vec<u16>,
version_key: u64,
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

log::info!("do_reveal_weights( hotkey:{:?} netuid:{:?})", who, netuid);

WeightCommits::<T>::try_mutate_exists(netuid, &who, |maybe_commit| -> DispatchResult {
let (commit_hash, commit_block) =
maybe_commit.take().ok_or(Error::<T>::NoCommitFound)?;

ensure!(
Self::is_reveal_block_range(commit_block),
Error::<T>::InvalidRevealTempo
);

let provided_hash: H256 = BlakeTwo256::hash_of(&(
who.clone(),
netuid,
uids.clone(),
values.clone(),
version_key,
));
ensure!(provided_hash == commit_hash, Error::<T>::InvalidReveal);

Self::do_set_weights(origin, netuid, uids, values, version_key)
})
}

// ---- The implementation for the extrinsic set_weights.
//
// # Args:
Expand Down Expand Up @@ -324,4 +378,48 @@ impl<T: Config> Pallet<T> {
// we should expect at most subnetwork_n uids.
uids.len() <= subnetwork_n as usize
}

pub fn can_commit(netuid: u16, who: &T::AccountId) -> bool {
if let Some((_hash, commit_block)) = WeightCommits::<T>::get(netuid, who) {
let interval: u64 = Self::get_weight_commit_interval();
let current_block: u64 = Self::get_current_block_as_u64();
let interval_start: u64 = current_block - (current_block % interval);
let last_commit_interval_start: u64 = commit_block - (commit_block % interval);

// Allow commit if we're within the interval bounds
if current_block <= interval_start + interval
&& interval_start > last_commit_interval_start
{
return true;
}

false
} else {
true
}
}

pub fn is_reveal_block_range(commit_block: u64) -> bool {
let interval: u64 = Self::get_weight_commit_interval();
let commit_interval_start: u64 = commit_block - (commit_block % interval); // Find the start of the interval in which the commit occurred
let reveal_interval_start: u64 = commit_interval_start + interval; // Start of the next interval after the commit interval
let current_block: u64 = Self::get_current_block_as_u64();

// Allow reveal if the current block is within the interval following the commit's interval
if current_block >= reveal_interval_start
&& current_block < reveal_interval_start + interval
{
return true;
}

false
}

pub fn get_weight_commit_interval() -> u64 {
WeightCommitRevealInterval::<T>::get()
}

pub fn set_weight_commit_interval(interval: u64) {
WeightCommitRevealInterval::<T>::set(interval)
}
}
Loading
Loading