Skip to content

Commit 35dc836

Browse files
authored
Merge pull request #369 from opentensor/ban-direct-indexing
Ban direct indexing
2 parents 002633d + f61f92e commit 35dc836

File tree

21 files changed

+600
-724
lines changed

21 files changed

+600
-724
lines changed

node/src/chain_spec.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Allowed since it's actually better to panic during chain setup when there is an error
2+
#![allow(clippy::unwrap_used)]
3+
14
use node_subtensor_runtime::{
25
AccountId, AuraConfig, BalancesConfig, GrandpaConfig, RuntimeGenesisConfig,
36
SenateMembersConfig, Signature, SubtensorModuleConfig, SudoConfig, SystemConfig,
@@ -90,14 +93,16 @@ pub fn finney_mainnet_config() -> Result<ChainSpec, String> {
9093
Vec<(sp_runtime::AccountId32, (u64, u16))>,
9194
)> = Vec::new();
9295
for (coldkey_str, hotkeys) in old_state.stakes.iter() {
93-
let coldkey = <sr25519::Public as Ss58Codec>::from_ss58check(coldkey_str).unwrap();
96+
let coldkey = <sr25519::Public as Ss58Codec>::from_ss58check(coldkey_str)
97+
.map_err(|e| e.to_string())?;
9498
let coldkey_account = sp_runtime::AccountId32::from(coldkey);
9599

96100
let mut processed_hotkeys: Vec<(sp_runtime::AccountId32, (u64, u16))> = Vec::new();
97101

98102
for (hotkey_str, amount_uid) in hotkeys.iter() {
99103
let (amount, uid) = amount_uid;
100-
let hotkey = <sr25519::Public as Ss58Codec>::from_ss58check(hotkey_str).unwrap();
104+
let hotkey = <sr25519::Public as Ss58Codec>::from_ss58check(hotkey_str)
105+
.map_err(|e| e.to_string())?;
101106
let hotkey_account = sp_runtime::AccountId32::from(hotkey);
102107

103108
processed_hotkeys.push((hotkey_account, (*amount, *uid)));
@@ -109,7 +114,8 @@ pub fn finney_mainnet_config() -> Result<ChainSpec, String> {
109114
let mut balances_issuance: u64 = 0;
110115
let mut processed_balances: Vec<(sp_runtime::AccountId32, u64)> = Vec::new();
111116
for (key_str, amount) in old_state.balances.iter() {
112-
let key = <sr25519::Public as Ss58Codec>::from_ss58check(key_str).unwrap();
117+
let key =
118+
<sr25519::Public as Ss58Codec>::from_ss58check(key_str).map_err(|e| e.to_string())?;
113119
let key_account = sp_runtime::AccountId32::from(key);
114120

115121
processed_balances.push((key_account, *amount));
@@ -266,14 +272,16 @@ pub fn finney_testnet_config() -> Result<ChainSpec, String> {
266272
Vec<(sp_runtime::AccountId32, (u64, u16))>,
267273
)> = Vec::new();
268274
for (coldkey_str, hotkeys) in old_state.stakes.iter() {
269-
let coldkey = <sr25519::Public as Ss58Codec>::from_ss58check(coldkey_str).unwrap();
275+
let coldkey = <sr25519::Public as Ss58Codec>::from_ss58check(coldkey_str)
276+
.map_err(|e| e.to_string())?;
270277
let coldkey_account = sp_runtime::AccountId32::from(coldkey);
271278

272279
let mut processed_hotkeys: Vec<(sp_runtime::AccountId32, (u64, u16))> = Vec::new();
273280

274281
for (hotkey_str, amount_uid) in hotkeys.iter() {
275282
let (amount, uid) = amount_uid;
276-
let hotkey = <sr25519::Public as Ss58Codec>::from_ss58check(hotkey_str).unwrap();
283+
let hotkey = <sr25519::Public as Ss58Codec>::from_ss58check(hotkey_str)
284+
.map_err(|e| e.to_string())?;
277285
let hotkey_account = sp_runtime::AccountId32::from(hotkey);
278286

279287
processed_hotkeys.push((hotkey_account, (*amount, *uid)));
@@ -285,7 +293,8 @@ pub fn finney_testnet_config() -> Result<ChainSpec, String> {
285293
let mut balances_issuance: u64 = 0;
286294
let mut processed_balances: Vec<(sp_runtime::AccountId32, u64)> = Vec::new();
287295
for (key_str, amount) in old_state.balances.iter() {
288-
let key = <sr25519::Public as Ss58Codec>::from_ss58check(key_str).unwrap();
296+
let key =
297+
<sr25519::Public as Ss58Codec>::from_ss58check(key_str).map_err(|e| e.to_string())?;
289298
let key_account = sp_runtime::AccountId32::from(key);
290299

291300
processed_balances.push((key_account, *amount));

pallets/commitments/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ impl Encode for Data {
8787
Data::None => vec![0u8; 1],
8888
Data::Raw(ref x) => {
8989
let l = x.len().min(128);
90-
let mut r = vec![l as u8 + 1; l + 1];
91-
r[1..].copy_from_slice(&x[..l]);
90+
let mut r = vec![l as u8 + 1];
91+
r.extend_from_slice(&x[..]);
9292
r
9393
}
9494
Data::BlakeTwo256(ref h) => once(130).chain(h.iter().cloned()).collect(),

pallets/registry/src/benchmarking.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::*;
66
use crate::Pallet as Registry;
77
use frame_benchmarking::v1::account;
88
use frame_benchmarking::v2::*;
9+
use frame_support::traits::tokens::fungible::Mutate;
910
use frame_system::RawOrigin;
1011

1112
use sp_runtime::traits::Bounded;
@@ -40,7 +41,7 @@ mod benchmarks {
4041
fn set_identity() {
4142
// The target user
4243
let caller: T::AccountId = whitelisted_caller();
43-
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
44+
let _ = T::Currency::set_balance(&caller, BalanceOf::<T>::max_value());
4445

4546
#[extrinsic_call]
4647
_(
@@ -56,7 +57,7 @@ mod benchmarks {
5657
fn clear_identity() {
5758
// The target user
5859
let caller: T::AccountId = whitelisted_caller();
59-
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
60+
let _ = T::Currency::set_balance(&caller, BalanceOf::<T>::max_value());
6061

6162
let vali_account = account::<T::AccountId>("account", 0, 0u32);
6263

pallets/registry/src/lib.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ pub use pallet::*;
1212
pub use types::*;
1313
pub use weights::WeightInfo;
1414

15-
use frame_support::traits::Currency;
15+
use frame_support::traits::tokens::{
16+
fungible::{self, MutateHold as _},
17+
Precision,
18+
};
1619
use sp_runtime::traits::Zero;
1720
use sp_std::boxed::Box;
1821

1922
type BalanceOf<T> =
20-
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
23+
<<T as Config>::Currency as fungible::Inspect<<T as frame_system::Config>::AccountId>>::Balance;
2124

2225
#[frame_support::pallet]
2326
pub mod pallet {
2427
use super::*;
25-
use frame_support::{pallet_prelude::*, traits::ReservableCurrency};
28+
use frame_support::{pallet_prelude::*, traits::tokens::fungible};
2629
use frame_system::pallet_prelude::*;
2730

2831
#[pallet::pallet]
@@ -36,7 +39,8 @@ pub mod pallet {
3639
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
3740

3841
// Currency type that will be used to place deposits on neurons
39-
type Currency: ReservableCurrency<Self::AccountId> + Send + Sync;
42+
type Currency: fungible::Mutate<Self::AccountId>
43+
+ fungible::MutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>;
4044

4145
// Weight information for extrinsics in this pallet.
4246
type WeightInfo: WeightInfo;
@@ -56,6 +60,9 @@ pub mod pallet {
5660
/// The amount held on deposit per additional field for a registered identity.
5761
#[pallet::constant]
5862
type FieldDeposit: Get<BalanceOf<Self>>;
63+
64+
/// Reasons for putting funds on hold.
65+
type RuntimeHoldReason: From<HoldReason>;
5966
}
6067

6168
#[pallet::event]
@@ -75,6 +82,11 @@ pub mod pallet {
7582
NotRegistered,
7683
}
7784

85+
#[pallet::composite_enum]
86+
pub enum HoldReason {
87+
RegistryIdentity,
88+
}
89+
7890
/// Identity data by account
7991
#[pallet::storage]
8092
#[pallet::getter(fn identity_of)]
@@ -125,11 +137,21 @@ pub mod pallet {
125137
let old_deposit = id.deposit;
126138
id.deposit = T::InitialDeposit::get() + fd;
127139
if id.deposit > old_deposit {
128-
T::Currency::reserve(&who, id.deposit - old_deposit)?;
140+
T::Currency::hold(
141+
&HoldReason::RegistryIdentity.into(),
142+
&who,
143+
id.deposit - old_deposit,
144+
)?;
129145
}
130146
if old_deposit > id.deposit {
131-
let err_amount = T::Currency::unreserve(&who, old_deposit - id.deposit);
132-
debug_assert!(err_amount.is_zero());
147+
let release_res = T::Currency::release(
148+
&HoldReason::RegistryIdentity.into(),
149+
&who,
150+
old_deposit - id.deposit,
151+
Precision::BestEffort,
152+
);
153+
debug_assert!(release_res
154+
.is_ok_and(|released_amount| released_amount == (old_deposit - id.deposit)));
133155
}
134156

135157
<IdentityOf<T>>::insert(&identified, id);
@@ -153,8 +175,13 @@ pub mod pallet {
153175
let id = <IdentityOf<T>>::take(&identified).ok_or(Error::<T>::NotRegistered)?;
154176
let deposit = id.total_deposit();
155177

156-
let err_amount = T::Currency::unreserve(&who, deposit);
157-
debug_assert!(err_amount.is_zero());
178+
let release_res = T::Currency::release(
179+
&HoldReason::RegistryIdentity.into(),
180+
&who,
181+
deposit,
182+
Precision::BestEffort,
183+
);
184+
debug_assert!(release_res.is_ok_and(|released_amount| released_amount == deposit));
158185

159186
Self::deposit_event(Event::IdentityDissolved { who: identified });
160187

pallets/registry/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ impl Encode for Data {
8888
Data::None => vec![0u8; 1],
8989
Data::Raw(ref x) => {
9090
let l = x.len().min(64);
91-
let mut r = vec![l as u8 + 1; l + 1];
92-
r[1..].copy_from_slice(&x[..l]);
91+
let mut r = vec![l as u8 + 1];
92+
r.extend_from_slice(&x[..]);
9393
r
9494
}
9595
Data::BlakeTwo256(ref h) => once(66u8).chain(h.iter().cloned()).collect(),

pallets/subtensor/src/block_step.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,8 @@ impl<T: Config> Pallet<T> {
7474
}
7575
}
7676

77-
pub fn has_loaded_emission_tuples(netuid: u16) -> bool {
78-
LoadedEmission::<T>::contains_key(netuid)
79-
}
80-
pub fn get_loaded_emission_tuples(netuid: u16) -> Vec<(T::AccountId, u64, u64)> {
81-
LoadedEmission::<T>::get(netuid).unwrap()
77+
pub fn get_loaded_emission_tuples(netuid: u16) -> Option<Vec<(T::AccountId, u64, u64)>> {
78+
LoadedEmission::<T>::get(netuid)
8279
}
8380

8481
// Reads from the loaded emission storage which contains lists of pending emission tuples ( hotkey, amount )
@@ -87,11 +84,10 @@ impl<T: Config> Pallet<T> {
8784
pub fn drain_emission(_: u64) {
8885
// --- 1. We iterate across each network.
8986
for (netuid, _) in <Tempo<T> as IterableStorageMap<u16, u16>>::iter() {
90-
if !Self::has_loaded_emission_tuples(netuid) {
87+
let Some(tuples_to_drain) = Self::get_loaded_emission_tuples(netuid) else {
88+
// There are no tuples to emit.
9189
continue;
92-
} // There are no tuples to emit.
93-
let tuples_to_drain: Vec<(T::AccountId, u64, u64)> =
94-
Self::get_loaded_emission_tuples(netuid);
90+
};
9591
let mut total_emitted: u64 = 0;
9692
for (hotkey, server_amount, validator_amount) in tuples_to_drain.iter() {
9793
Self::emit_inflation_through_hotkey_account(
@@ -189,10 +185,8 @@ impl<T: Config> Pallet<T> {
189185
// --- 10. Sink the emission tuples onto the already loaded.
190186
let mut concat_emission_tuples: Vec<(T::AccountId, u64, u64)> =
191187
emission_tuples_this_block.clone();
192-
if Self::has_loaded_emission_tuples(netuid) {
188+
if let Some(mut current_emission_tuples) = Self::get_loaded_emission_tuples(netuid) {
193189
// 10.a We already have loaded emission tuples, so we concat the new ones.
194-
let mut current_emission_tuples: Vec<(T::AccountId, u64, u64)> =
195-
Self::get_loaded_emission_tuples(netuid);
196190
concat_emission_tuples.append(&mut current_emission_tuples);
197191
}
198192
LoadedEmission::<T>::insert(netuid, concat_emission_tuples);

pallets/subtensor/src/delegate_info.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<T: Config> Pallet<T> {
8787
}
8888

8989
let delegate: AccountIdOf<T> =
90-
T::AccountId::decode(&mut delegate_account_vec.as_bytes_ref()).unwrap();
90+
T::AccountId::decode(&mut delegate_account_vec.as_bytes_ref()).ok()?;
9191
// Check delegate exists
9292
if !<Delegates<T>>::contains_key(delegate.clone()) {
9393
return None;
@@ -108,12 +108,9 @@ impl<T: Config> Pallet<T> {
108108
}
109109

110110
pub fn get_delegated(delegatee_account_vec: Vec<u8>) -> Vec<(DelegateInfo<T>, Compact<u64>)> {
111-
if delegatee_account_vec.len() != 32 {
111+
let Ok(delegatee) = T::AccountId::decode(&mut delegatee_account_vec.as_bytes_ref()) else {
112112
return Vec::new(); // No delegates for invalid account
113-
}
114-
115-
let delegatee: AccountIdOf<T> =
116-
T::AccountId::decode(&mut delegatee_account_vec.as_bytes_ref()).unwrap();
113+
};
117114

118115
let mut delegates: Vec<(DelegateInfo<T>, Compact<u64>)> = Vec::new();
119116
for delegate in <Delegates<T> as IterableStorageMap<T::AccountId, u16>>::iter_keys() {

0 commit comments

Comments
 (0)