Skip to content
Open
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
3 changes: 2 additions & 1 deletion node/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub fn create_benchmark_extrinsic(
pallet_shield::CheckShieldedTxValidity::<runtime::Runtime>::new(),
pallet_subtensor::SubtensorTransactionExtension::<runtime::Runtime>::new(),
pallet_drand::drand_priority::DrandPriority::<runtime::Runtime>::new(),
pallet_commitments::CommitmentsTransactionExtension::<runtime::Runtime>::new(),
),
frame_metadata_hash_extension::CheckMetadataHash::<runtime::Runtime>::new(true),
);
Expand All @@ -158,7 +159,7 @@ pub fn create_benchmark_extrinsic(
(),
(),
),
((), (), (), (), ()),
((), (), (), (), (), ()),
None,
),
);
Expand Down
131 changes: 126 additions & 5 deletions pallets/commitments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ pub mod weights;

use ark_serialize::CanonicalDeserialize;
use codec::Encode;
use frame_support::IterableStorageDoubleMap;
use frame_support::{
BoundedVec,
traits::{Currency, Get},
BoundedVec, IterableStorageDoubleMap,
dispatch::{DispatchErrorWithPostInfo, DispatchExtension, DispatchInfo, PostDispatchInfo},
pallet_prelude::{
Decode, DecodeWithMemTracking, PhantomData, ValidTransaction, ValidateResult,
},
traits::{Currency, Get, IsSubType, OriginTrait},
};
use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;
use scale_info::prelude::collections::BTreeSet;
use sp_runtime::SaturatedConversion;
use sp_runtime::{Saturating, Weight, traits::Zero};
use sp_runtime::{
SaturatedConversion, Saturating, Weight,
traits::Zero,
traits::{
AsSystemOriginSigner, DispatchInfoOf, Dispatchable, Implication, TransactionExtension,
},
transaction_validity::{InvalidTransaction, TransactionSource, TransactionValidityError},
};
use sp_std::{boxed::Box, vec::Vec};
use subtensor_macros::freeze_struct;
use subtensor_runtime_common::NetUid;
use tle::{
curves::drand::TinyBLS381,
Expand Down Expand Up @@ -576,6 +586,117 @@ impl<T: Config> Pallet<T> {
}
}

type CallOf<T> = <T as frame_system::Config>::RuntimeCall;
type OriginOf<T> = <CallOf<T> as Dispatchable>::RuntimeOrigin;

#[derive(Default, Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)]
#[scale_info(skip_type_params(T))]
#[freeze_struct("7f03f99666ee2c4f")]
pub struct CommitmentsTransactionExtension<T>(PhantomData<T>);

impl<T> sp_std::fmt::Debug for CommitmentsTransactionExtension<T> {
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
write!(f, "CommitmentsTransactionExtension")
}
}

impl<T> CommitmentsTransactionExtension<T>
where
T: Config + Send + Sync + TypeInfo,
CallOf<T>: Dispatchable + IsSubType<pallet::Call<T>>,
{
pub fn new() -> Self {
Self(Default::default())
}
}

impl<T> TransactionExtension<CallOf<T>> for CommitmentsTransactionExtension<T>
where
T: Config + Send + Sync + TypeInfo,
CallOf<T>: Dispatchable + IsSubType<pallet::Call<T>>,
OriginOf<T>: AsSystemOriginSigner<T::AccountId> + Clone,
{
const IDENTIFIER: &'static str = "CommitmentsTransactionExtension";

type Implicit = ();
type Val = ();
type Pre = ();

fn weight(&self, _call: &CallOf<T>) -> Weight {
Weight::from_parts(0, 0)
}

fn validate(
&self,
origin: OriginOf<T>,
call: &CallOf<T>,
_info: &DispatchInfoOf<CallOf<T>>,
_len: usize,
_self_implicit: Self::Implicit,
_inherited_implication: &impl Implication,
_source: TransactionSource,
) -> ValidateResult<Self::Val, CallOf<T>> {
let Some(who) = origin.as_system_origin_signer() else {
return Ok((ValidTransaction::default(), (), origin));
};

match call.is_sub_type() {
Some(pallet::Call::set_commitment { netuid, .. }) => {
if !T::CanCommit::can_commit(*netuid, who) {
return Err(InvalidTransaction::BadSigner.into());
}

Ok((ValidTransaction::default(), (), origin))
}
_ => Ok((ValidTransaction::default(), (), origin)),
}
}

fn prepare(
self,
_val: Self::Val,
_origin: &OriginOf<T>,
_call: &CallOf<T>,
_info: &DispatchInfoOf<CallOf<T>>,
_len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
Ok(())
}
}

pub struct CommitmentsDispatchExtension<T: Config>(PhantomData<T>);

impl<T> DispatchExtension<CallOf<T>> for CommitmentsDispatchExtension<T>
where
T: Config,
CallOf<T>:
Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo> + IsSubType<pallet::Call<T>>,
OriginOf<T>: OriginTrait<AccountId = T::AccountId>,
{
type Pre = ();

fn weight(_call: &CallOf<T>) -> Weight {
T::DbWeight::get().reads(1)
}

fn pre_dispatch(
origin: &OriginOf<T>,
call: &CallOf<T>,
) -> Result<Self::Pre, DispatchErrorWithPostInfo> {
let Some(who) = origin.as_signer() else {
return Ok(());
};

if let Some(pallet::Call::set_commitment { netuid, .. }) = call.is_sub_type() {
if !T::CanCommit::can_commit(*netuid, who) {
return Err(Error::<T>::AccountNotAllowedCommit.into());
}
}

Ok(())
}
}

pub trait GetCommitments<AccountId> {
fn get_commitments(netuid: NetUid) -> Vec<(AccountId, Vec<u8>)>;
}
Expand Down
52 changes: 49 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ use codec::{Compact, Decode, Encode};
use ethereum::AuthorizationList;
use frame_support::{
PalletId,
dispatch::DispatchResult,
dispatch::{
DispatchErrorWithPostInfo, DispatchExtension, DispatchInfo, DispatchResult,
PostDispatchInfo,
},
genesis_builder_helper::{build_state, get_preset},
pallet_prelude::Get,
traits::{Contains, InsideBoth, LinearStoragePrice, fungible::HoldConsideration},
traits::{Contains, InsideBoth, LinearStoragePrice, OriginTrait, fungible::HoldConsideration},
};
use frame_system::{EnsureRoot, EnsureRootWithSuccess, EnsureSigned};
use pallet_commitments::{CanCommit, OnMetadataCommitment};
Expand Down Expand Up @@ -204,6 +207,7 @@ impl frame_system::offchain::CreateSignedTransaction<pallet_drand::Call<Runtime>
pallet_shield::CheckShieldedTxValidity::<Runtime>::new(),
pallet_subtensor::SubtensorTransactionExtension::<Runtime>::new(),
pallet_drand::drand_priority::DrandPriority::<Runtime>::new(),
pallet_commitments::CommitmentsTransactionExtension::<Runtime>::new(),
),
frame_metadata_hash_extension::CheckMetadataHash::<Runtime>::new(true),
);
Expand Down Expand Up @@ -381,7 +385,7 @@ impl frame_system::Config for Runtime {
type PostInherents = ();
type PostTransactions = ();
type ExtensionsWeightInfo = frame_system::SubstrateExtensionsWeight<Runtime>;
type DispatchExtension = pallet_subtensor::CheckColdkeySwap<Runtime>;
type DispatchExtension = RuntimeDispatchExtension;
}

impl pallet_insecure_randomness_collective_flip::Config for Runtime {}
Expand Down Expand Up @@ -1681,6 +1685,7 @@ pub type CustomTxExtension = (
pallet_shield::CheckShieldedTxValidity<Runtime>,
pallet_subtensor::SubtensorTransactionExtension<Runtime>,
pallet_drand::drand_priority::DrandPriority<Runtime>,
pallet_commitments::CommitmentsTransactionExtension<Runtime>,
);
pub type TxExtension = (
SystemTxExtension,
Expand Down Expand Up @@ -1719,6 +1724,47 @@ pub type Executive = frame_executive::Executive<
Migrations,
>;

type RuntimeDispatchableOrigin = <RuntimeCall as Dispatchable>::RuntimeOrigin;
type ColdkeySwapDispatchPre =
<pallet_subtensor::CheckColdkeySwap<Runtime> as DispatchExtension<RuntimeCall>>::Pre;
type CommitmentsDispatchPre =
<pallet_commitments::CommitmentsDispatchExtension<Runtime> as DispatchExtension<RuntimeCall>>::Pre;

pub struct RuntimeDispatchExtension;

impl DispatchExtension<RuntimeCall> for RuntimeDispatchExtension
where
RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
RuntimeDispatchableOrigin: OriginTrait<AccountId = AccountId>,
{
type Pre = (ColdkeySwapDispatchPre, CommitmentsDispatchPre);

fn weight(call: &RuntimeCall) -> Weight {
<pallet_subtensor::CheckColdkeySwap<Runtime> as DispatchExtension<RuntimeCall>>::weight(
call,
)
.saturating_add(
<pallet_commitments::CommitmentsDispatchExtension<Runtime> as DispatchExtension<
RuntimeCall,
>>::weight(call),
)
}

fn pre_dispatch(
origin: &RuntimeDispatchableOrigin,
call: &RuntimeCall,
) -> Result<Self::Pre, DispatchErrorWithPostInfo> {
let coldkey_swap_pre =
<pallet_subtensor::CheckColdkeySwap<Runtime> as DispatchExtension<RuntimeCall>>::pre_dispatch(origin, call)?;
let commitments_pre =
<pallet_commitments::CommitmentsDispatchExtension<Runtime> as DispatchExtension<
RuntimeCall,
>>::pre_dispatch(origin, call)?;

Ok((coldkey_swap_pre, commitments_pre))
}
}

#[cfg(feature = "runtime-benchmarks")]
#[macro_use]
extern crate frame_benchmarking;
Expand Down
Loading