From f900b26be3c40e282eeb8792f31d4ccf9624956f Mon Sep 17 00:00:00 2001 From: Shamil Gadelshin Date: Fri, 27 Jun 2025 15:55:37 +0400 Subject: [PATCH 1/3] Refactor reveal_crv3_commits() --- pallets/subtensor/src/coinbase/mod.rs | 1 + .../subtensor/src/coinbase/reveal_commits.rs | 154 ++++++++++++++++++ .../subtensor/src/coinbase/run_coinbase.rs | 149 ----------------- pallets/subtensor/src/tests/weights.rs | 2 +- 4 files changed, 156 insertions(+), 150 deletions(-) create mode 100644 pallets/subtensor/src/coinbase/reveal_commits.rs diff --git a/pallets/subtensor/src/coinbase/mod.rs b/pallets/subtensor/src/coinbase/mod.rs index dc153b1aed..cd95dbb7a7 100644 --- a/pallets/subtensor/src/coinbase/mod.rs +++ b/pallets/subtensor/src/coinbase/mod.rs @@ -1,5 +1,6 @@ use super::*; pub mod block_emission; pub mod block_step; +pub mod reveal_commits; pub mod root; pub mod run_coinbase; diff --git a/pallets/subtensor/src/coinbase/reveal_commits.rs b/pallets/subtensor/src/coinbase/reveal_commits.rs new file mode 100644 index 0000000000..f9738cdd79 --- /dev/null +++ b/pallets/subtensor/src/coinbase/reveal_commits.rs @@ -0,0 +1,154 @@ +use super::*; +use ark_serialize::CanonicalDeserialize; +use codec::Decode; +use frame_support::dispatch; +use frame_support::traits::OriginTrait; +use subtensor_runtime_common::NetUid; +use tle::curves::drand::TinyBLS381; +use tle::stream_ciphers::AESGCMStreamCipherProvider; +use tle::tlock::TLECiphertext; +use tle::tlock::tld; +use w3f_bls::EngineBLS; + +/// Contains all necesarry information to set weights. +/// +/// In the context of commit-reveal v3, this is the payload which should be +/// encrypted, compressed, serialized, and submitted to the `commit_crv3_weights` +/// extrinsic. +#[derive(Encode, Decode)] +#[freeze_struct("46e75a8326ba3665")] +pub struct WeightsTlockPayload { + pub uids: Vec, + pub values: Vec, + pub version_key: u64, +} + +impl Pallet { + /// The `reveal_crv3_commits` function is run at the very beginning of epoch `n`, + pub fn reveal_crv3_commits(netuid: NetUid) -> dispatch::DispatchResult { + let cur_block = Self::get_current_block_as_u64(); + let cur_epoch = Self::get_epoch_index(netuid, cur_block); + + // Weights revealed must have been committed during epoch `cur_epoch - reveal_period`. + let reveal_epoch = + cur_epoch.saturating_sub(Self::get_reveal_period(netuid).saturating_sub(1)); + + // Clean expired commits + for (epoch, _) in CRV3WeightCommits::::iter_prefix(netuid) { + if epoch < reveal_epoch { + CRV3WeightCommits::::remove(netuid, epoch); + } + } + + // No commits to reveal until at least epoch 2. + if cur_epoch < 2 { + log::warn!("Failed to reveal commit for subnet {} Too early", netuid); + return Ok(()); + } + + let mut entries = CRV3WeightCommits::::take(netuid, reveal_epoch); + + // Keep popping item off the end of the queue until we sucessfully reveal a commit. + while let Some((who, serialized_compresssed_commit, round_number)) = entries.pop_front() { + let reader = &mut &serialized_compresssed_commit[..]; + let commit = match TLECiphertext::::deserialize_compressed(reader) { + Ok(c) => c, + Err(e) => { + log::warn!( + "Failed to reveal commit for subnet {} submitted by {:?} due to error deserializing the commit: {:?}", + netuid, + who, + e + ); + continue; + } + }; + + // Try to get the round number from pallet_drand. + let pulse = match pallet_drand::Pulses::::get(round_number) { + Some(p) => p, + None => { + // Round number used was not found on the chain. Skip this commit. + log::warn!( + "Failed to reveal commit for subnet {} submitted by {:?} due to missing round number {} at time of reveal.", + netuid, + who, + round_number + ); + continue; + } + }; + + let signature_bytes = pulse + .signature + .strip_prefix(b"0x") + .unwrap_or(&pulse.signature); + + let sig_reader = &mut &signature_bytes[..]; + let sig = match ::SignatureGroup::deserialize_compressed( + sig_reader, + ) { + Ok(s) => s, + Err(e) => { + log::error!( + "Failed to reveal commit for subnet {} submitted by {:?} due to error deserializing signature from drand pallet: {:?}", + netuid, + who, + e + ); + continue; + } + }; + + let decrypted_bytes: Vec = match tld::( + commit, sig, + ) { + Ok(d) => d, + Err(e) => { + log::warn!( + "Failed to reveal commit for subnet {} submitted by {:?} due to error decrypting the commit: {:?}", + netuid, + who, + e + ); + continue; + } + }; + + // Decrypt the bytes into WeightsPayload + let mut reader = &decrypted_bytes[..]; + let payload: WeightsTlockPayload = match Decode::decode(&mut reader) { + Ok(w) => w, + Err(e) => { + log::warn!( + "Failed to reveal commit for subnet {} submitted by {:?} due to error deserializing WeightsPayload: {:?}", + netuid, + who, + e + ); + continue; + } + }; + + if let Err(e) = Self::do_set_weights( + T::RuntimeOrigin::signed(who.clone()), + netuid, + payload.uids, + payload.values, + payload.version_key, + ) { + log::warn!( + "Failed to `do_set_weights` for subnet {} submitted by {:?}: {:?}", + netuid, + who, + e + ); + continue; + } else { + Self::deposit_event(Event::CRV3WeightsRevealed(netuid, who)); + }; + } + + Ok(()) + } +} diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index 2e88e9678c..cfc07b17dc 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -4,21 +4,6 @@ use safe_math::*; use substrate_fixed::types::U96F32; use subtensor_runtime_common::NetUid; use subtensor_swap_interface::SwapHandler; -use tle::stream_ciphers::AESGCMStreamCipherProvider; -use tle::tlock::tld; - -/// Contains all necesarry information to set weights. -/// -/// In the context of commit-reveal v3, this is the payload which should be -/// encrypted, compressed, serialized, and submitted to the `commit_crv3_weights` -/// extrinsic. -#[derive(Encode, Decode)] -#[freeze_struct("46e75a8326ba3665")] -pub struct WeightsTlockPayload { - pub uids: Vec, - pub values: Vec, - pub version_key: u64, -} // Distribute dividends to each hotkey macro_rules! asfloat { @@ -849,138 +834,4 @@ impl Pallet { let remainder = adjusted_block.checked_rem(tempo_plus_one).unwrap_or(0); (tempo as u64).saturating_sub(remainder) } - - /// The `reveal_crv3_commits` function is run at the very beginning of epoch `n`, - pub fn reveal_crv3_commits(netuid: NetUid) -> dispatch::DispatchResult { - use ark_serialize::CanonicalDeserialize; - use frame_support::traits::OriginTrait; - use tle::curves::drand::TinyBLS381; - use tle::tlock::TLECiphertext; - use w3f_bls::EngineBLS; - - let cur_block = Self::get_current_block_as_u64(); - let cur_epoch = Self::get_epoch_index(netuid, cur_block); - - // Weights revealed must have been committed during epoch `cur_epoch - reveal_period`. - let reveal_epoch = - cur_epoch.saturating_sub(Self::get_reveal_period(netuid).saturating_sub(1)); - - // Clean expired commits - for (epoch, _) in CRV3WeightCommits::::iter_prefix(netuid) { - if epoch < reveal_epoch { - CRV3WeightCommits::::remove(netuid, epoch); - } - } - - // No commits to reveal until at least epoch 2. - if cur_epoch < 2 { - log::warn!("Failed to reveal commit for subnet {} Too early", netuid); - return Ok(()); - } - - let mut entries = CRV3WeightCommits::::take(netuid, reveal_epoch); - - // Keep popping item off the end of the queue until we sucessfully reveal a commit. - while let Some((who, serialized_compresssed_commit, round_number)) = entries.pop_front() { - let reader = &mut &serialized_compresssed_commit[..]; - let commit = match TLECiphertext::::deserialize_compressed(reader) { - Ok(c) => c, - Err(e) => { - log::warn!( - "Failed to reveal commit for subnet {} submitted by {:?} due to error deserializing the commit: {:?}", - netuid, - who, - e - ); - continue; - } - }; - - // Try to get the round number from pallet_drand. - let pulse = match pallet_drand::Pulses::::get(round_number) { - Some(p) => p, - None => { - // Round number used was not found on the chain. Skip this commit. - log::warn!( - "Failed to reveal commit for subnet {} submitted by {:?} due to missing round number {} at time of reveal.", - netuid, - who, - round_number - ); - continue; - } - }; - - let signature_bytes = pulse - .signature - .strip_prefix(b"0x") - .unwrap_or(&pulse.signature); - - let sig_reader = &mut &signature_bytes[..]; - let sig = match ::SignatureGroup::deserialize_compressed( - sig_reader, - ) { - Ok(s) => s, - Err(e) => { - log::error!( - "Failed to reveal commit for subnet {} submitted by {:?} due to error deserializing signature from drand pallet: {:?}", - netuid, - who, - e - ); - continue; - } - }; - - let decrypted_bytes: Vec = match tld::( - commit, sig, - ) { - Ok(d) => d, - Err(e) => { - log::warn!( - "Failed to reveal commit for subnet {} submitted by {:?} due to error decrypting the commit: {:?}", - netuid, - who, - e - ); - continue; - } - }; - - // Decrypt the bytes into WeightsPayload - let mut reader = &decrypted_bytes[..]; - let payload: WeightsTlockPayload = match Decode::decode(&mut reader) { - Ok(w) => w, - Err(e) => { - log::warn!( - "Failed to reveal commit for subnet {} submitted by {:?} due to error deserializing WeightsPayload: {:?}", - netuid, - who, - e - ); - continue; - } - }; - - if let Err(e) = Self::do_set_weights( - T::RuntimeOrigin::signed(who.clone()), - netuid, - payload.uids, - payload.values, - payload.version_key, - ) { - log::warn!( - "Failed to `do_set_weights` for subnet {} submitted by {:?}: {:?}", - netuid, - who, - e - ); - continue; - } else { - Self::deposit_event(Event::CRV3WeightsRevealed(netuid, who)); - }; - } - - Ok(()) - } } diff --git a/pallets/subtensor/src/tests/weights.rs b/pallets/subtensor/src/tests/weights.rs index 3e1c2f7557..f978b17596 100644 --- a/pallets/subtensor/src/tests/weights.rs +++ b/pallets/subtensor/src/tests/weights.rs @@ -29,7 +29,7 @@ use w3f_bls::EngineBLS; use super::mock; use super::mock::*; -use crate::coinbase::run_coinbase::WeightsTlockPayload; +use crate::coinbase::reveal_commits::WeightsTlockPayload; use crate::*; /*************************** From 81fac059db1c26475319cb63817229deb6264be3 Mon Sep 17 00:00:00 2001 From: shamil-gadelshin Date: Fri, 27 Jun 2025 16:23:29 +0400 Subject: [PATCH 2/3] Update pallets/subtensor/src/coinbase/reveal_commits.rs Co-authored-by: Loris Moulin <45130584+l0r1s@users.noreply.github.com> --- pallets/subtensor/src/coinbase/reveal_commits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/coinbase/reveal_commits.rs b/pallets/subtensor/src/coinbase/reveal_commits.rs index f9738cdd79..1fc9f49493 100644 --- a/pallets/subtensor/src/coinbase/reveal_commits.rs +++ b/pallets/subtensor/src/coinbase/reveal_commits.rs @@ -10,7 +10,7 @@ use tle::tlock::TLECiphertext; use tle::tlock::tld; use w3f_bls::EngineBLS; -/// Contains all necesarry information to set weights. +/// Contains all necessary information to set weights. /// /// In the context of commit-reveal v3, this is the payload which should be /// encrypted, compressed, serialized, and submitted to the `commit_crv3_weights` From 0bf356ebad92c1f8529a01f117e3a6fd129d4c8a Mon Sep 17 00:00:00 2001 From: Shamil Gadelshin Date: Fri, 27 Jun 2025 17:24:09 +0400 Subject: [PATCH 3/3] Bump spec version to 284 --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index f92276325d..17a313ce7c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -217,7 +217,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 283, + spec_version: 284, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1,