diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index c99487992..45267722a 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -1,6 +1,6 @@ use node_subspace_runtime::{ - AccountId, AuraConfig, BalancesConfig, GrandpaConfig, Signature, - SubspaceModuleConfig, SudoConfig, SystemConfig, WASM_BINARY, Precompiles, RuntimeGenesisConfig + AccountId, AuraConfig, BalancesConfig, GrandpaConfig, Precompiles, RuntimeGenesisConfig, + Signature, SubspaceModuleConfig, SudoConfig, SystemConfig, WASM_BINARY, }; use sc_service::ChainType; use sp_consensus_aura::sr25519::AuthorityId as AuraId; @@ -230,7 +230,7 @@ fn network_genesis( block: u32, ) -> RuntimeGenesisConfig { use node_subspace_runtime::{EVMChainIdConfig, EVMConfig}; - + RuntimeGenesisConfig { system: SystemConfig { // Add Wasm runtime to storage. diff --git a/node/src/command.rs b/node/src/command.rs index 8c4cd551b..45cfdba3e 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -107,7 +107,7 @@ pub fn run() -> sc_cli::Result<()> { runner.sync_run(|config| { use sp_io::SubstrateHostFunctions; - + // This switch needs to be in the client, since the client decides // which sub-commands it wants to support. match cmd { diff --git a/node/src/rpc.rs b/node/src/rpc.rs index 67b1dbf96..ddc5f25dd 100644 --- a/node/src/rpc.rs +++ b/node/src/rpc.rs @@ -41,9 +41,9 @@ where P: TransactionPool + 'static, { use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; + use subspace_rpc::{SubspaceApiServer, SubspacePallet}; use substrate_frame_rpc_system::{System, SystemApiServer}; - use subspace_rpc::{SubspacePallet, SubspaceApiServer}; - + let mut module = RpcModule::new(()); let FullDeps { client, pool, deny_unsafe } = deps; diff --git a/pallets/subspace/rpc/runtime-api/src/lib.rs b/pallets/subspace/rpc/runtime-api/src/lib.rs index a49daa698..84d935dd0 100644 --- a/pallets/subspace/rpc/runtime-api/src/lib.rs +++ b/pallets/subspace/rpc/runtime-api/src/lib.rs @@ -1,21 +1,25 @@ #![cfg_attr(not(feature = "std"), no_std)] -use sp_runtime::{DispatchError, MultiSignature, traits::{Verify, IdentifyAccount}}; -use sp_runtime::{sp_std::prelude::Vec, ArithmeticError}; use parity_scale_codec::{Decode, Encode, EncodeLike, MaxEncodedLen}; use scale_info::TypeInfo; use serde::{Deserialize, Serialize}; use sp_arithmetic::per_things::Percent; +use sp_runtime::{ + sp_std::prelude::Vec, + traits::{IdentifyAccount, Verify}, + ArithmeticError, DispatchError, MultiSignature, +}; type Result = core::result::Result; type Signature = MultiSignature; pub type AccountId = <::Signer as IdentifyAccount>::AccountId; #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo, Serialize, Deserialize)] -pub struct ModuleStats{ +pub struct ModuleStats { pub last_update: u64, pub registration_block: u64, - pub stake_from: Vec<(AccountId, u64)>, /* map of key to stake on this module/key * (includes delegations) */ + pub stake_from: Vec<(AccountId, u64)>, /* map of key to stake on this module/key * (includes + * delegations) */ pub emission: u64, pub incentive: u16, pub dividends: u16, @@ -36,7 +40,6 @@ pub struct ModuleInfo { pub stats: ModuleStats, } - // sp_api::decl_runtime_apis! { // pub trait SubspaceRuntimeApi where // AccountId: <::Signer as IdentifyAccount>::AccountId @@ -52,4 +55,4 @@ sp_api::decl_runtime_apis! { fn get_module_info(key: AccountId, netuid: u16) -> ModuleInfo; } -} \ No newline at end of file +} diff --git a/pallets/subspace/rpc/src/lib.rs b/pallets/subspace/rpc/src/lib.rs index c3977fd65..c2efdd8c2 100644 --- a/pallets/subspace/rpc/src/lib.rs +++ b/pallets/subspace/rpc/src/lib.rs @@ -1,5 +1,3 @@ -use subspace_runtime_api::ModuleInfo; -pub use subspace_runtime_api::SubspaceRuntimeApi; use jsonrpsee::{ core::{Error as JsonRpseeError, RpcResult}, proc_macros::rpc, @@ -7,14 +5,18 @@ use jsonrpsee::{ }; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; -use sp_runtime::{generic::BlockId, traits::Block as BlockT}; +use sp_runtime::{ + generic::BlockId, + traits::{Block as BlockT, IdentifyAccount, Verify}, + DispatchError, MultiSignature, +}; use std::sync::Arc; -use sp_runtime::{DispatchError, MultiSignature, traits::{Verify, IdentifyAccount}}; +use subspace_runtime_api::ModuleInfo; +pub use subspace_runtime_api::SubspaceRuntimeApi; type Signature = MultiSignature; type AccountId = <::Signer as IdentifyAccount>::AccountId; - #[derive(serde::Deserialize, serde::Serialize)] pub struct Custom { code: u32, @@ -22,15 +24,17 @@ pub struct Custom { } #[rpc(client, server)] -pub trait SubspaceApi -{ +pub trait SubspaceApi { #[method(name = "subspace_getBurnRate")] fn get_burn_rate(&self, at: Option) -> RpcResult; #[method(name = "subspace_getModuleInfo")] - fn get_module_info(&self, key: AccountId, netuid: u16, at: Option) -> RpcResult; - - + fn get_module_info( + &self, + key: AccountId, + netuid: u16, + at: Option, + ) -> RpcResult; } pub struct SubspacePallet { @@ -50,19 +54,24 @@ where C: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend, C::Api: SubspaceRuntimeApi, { - fn get_burn_rate(&self, at: Option<::Hash>) -> RpcResult { + fn get_burn_rate(&self, at: Option<::Hash>) -> RpcResult { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); let value = api.get_burn_rate(at).map_err(runtime_error_into_rpc_err); - Ok(Custom{ code: 200, burn_rate: value.unwrap()}) + Ok(Custom { code: 200, burn_rate: value.unwrap() }) } - fn get_module_info(&self, key: AccountId, netuid: u16, at: Option<::Hash>) -> RpcResult { + fn get_module_info( + &self, + key: AccountId, + netuid: u16, + at: Option<::Hash>, + ) -> RpcResult { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - let value = api.get_module_info(at, key, netuid, ).map_err(runtime_error_into_rpc_err); + let value = api.get_module_info(at, key, netuid).map_err(runtime_error_into_rpc_err); Ok(value.unwrap()) } } @@ -77,4 +86,4 @@ fn runtime_error_into_rpc_err(err: impl std::fmt::Debug) -> JsonRpseeError { Some(format!("{:?}", err)), )) .into() -} \ No newline at end of file +} diff --git a/pallets/subspace/src/benchmarking.rs b/pallets/subspace/src/benchmarking.rs index 9f3ed70e2..8bc5b1231 100644 --- a/pallets/subspace/src/benchmarking.rs +++ b/pallets/subspace/src/benchmarking.rs @@ -1,4 +1,3 @@ - #![cfg(feature = "runtime-benchmarks")] use super::*; @@ -14,355 +13,300 @@ const BALANCE: u64 = 1_000_000_000_000_000_000; const MIN_STAKE: u64 = 1_00_000_000_000; fn set_user_balance(user: &T::AccountId) { - T::Currency::deposit_creating(user, >::u64_to_balance(BALANCE).unwrap()); + T::Currency::deposit_creating(user, >::u64_to_balance(BALANCE).unwrap()); } -fn default_register_helper() -> ( - Vec, - Vec, - Vec, - T::AccountId, - u16 -) { - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let module_key: T::AccountId = account("key", 0, SEED); - - let netuid = register_helper::( - network.clone(), - name.clone(), - address.clone(), - module_key.clone() - ); - - (network, name, address, module_key, netuid) +fn default_register_helper() -> (Vec, Vec, Vec, T::AccountId, u16) { + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let module_key: T::AccountId = account("key", 0, SEED); + + let netuid = + register_helper::(network.clone(), name.clone(), address.clone(), module_key.clone()); + + (network, name, address, module_key, netuid) } fn register_helper( - network: Vec, - name: Vec, - address: Vec, - module_key: T::AccountId, + network: Vec, + name: Vec, + address: Vec, + module_key: T::AccountId, ) -> u16 { - set_user_balance::(&module_key); - - >::register( - RawOrigin::Signed(module_key.clone()).into(), - network.clone(), - name.clone(), - address.clone(), - MIN_STAKE, - module_key.clone(), - ); - - let netuid = >::get_netuid_for_name(network.clone()); - - netuid + set_user_balance::(&module_key); + + >::register( + RawOrigin::Signed(module_key.clone()).into(), + network.clone(), + name.clone(), + address.clone(), + MIN_STAKE, + module_key.clone(), + ); + + let netuid = >::get_netuid_for_name(network.clone()); + + netuid } fn add_stake_helper( - network: Vec, - name: Vec, - address: Vec, - module_key: T::AccountId, - amount: u64 + network: Vec, + name: Vec, + address: Vec, + module_key: T::AccountId, + amount: u64, ) -> u16 { - let netuid = register_helper::( - network, - name.clone(), - address.clone(), - module_key.clone() - ); - - >::add_stake( - RawOrigin::Signed(module_key.clone()).into(), - netuid, - module_key, - amount, - ); - - netuid + let netuid = register_helper::(network, name.clone(), address.clone(), module_key.clone()); + + >::add_stake( + RawOrigin::Signed(module_key.clone()).into(), + netuid, + module_key, + amount, + ); + + netuid } fn add_stake_multiple_helper( - caller: T::AccountId, -) -> ( - u16, - Vec, - Vec -) { - let network: Vec = b"network".to_vec(); - let address: Vec = b"address".to_vec(); - - let module_keys: Vec = (0..10).map(|i| account("key", i, SEED)).collect(); - let amounts: Vec = (0..10).map(|i| i + MIN_STAKE).collect(); - - let mut netuid: u16 = 0; - - for (index, module_key) in module_keys.iter().enumerate() { - let mut name: Vec = b"name".to_vec(); - name.extend(vec![index as u8]); - - netuid = register_helper::( - network.clone(), - name, - address.clone(), - module_key.clone() - ); - } - - set_user_balance::(&caller); - - >::add_stake_multiple( - RawOrigin::Signed(caller).into(), - netuid, - module_keys.clone(), - amounts.clone(), - ); - - (netuid, module_keys, amounts) -} + caller: T::AccountId, +) -> (u16, Vec, Vec) { + let network: Vec = b"network".to_vec(); + let address: Vec = b"address".to_vec(); + + let module_keys: Vec = (0..10).map(|i| account("key", i, SEED)).collect(); + let amounts: Vec = (0..10).map(|i| i + MIN_STAKE).collect(); + + let mut netuid: u16 = 0; + + for (index, module_key) in module_keys.iter().enumerate() { + let mut name: Vec = b"name".to_vec(); + name.extend(vec![index as u8]); + + netuid = register_helper::(network.clone(), name, address.clone(), module_key.clone()); + } + set_user_balance::(&caller); + + >::add_stake_multiple( + RawOrigin::Signed(caller).into(), + netuid, + module_keys.clone(), + amounts.clone(), + ); + + (netuid, module_keys, amounts) +} #[benchmarks] mod benchmarks { - use super::*; + use super::*; - #[benchmark] - fn register() -> Result<(), BenchmarkError> { - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let module_key: T::AccountId = account("key", 0, SEED); + #[benchmark] + fn register() -> Result<(), BenchmarkError> { + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let module_key: T::AccountId = account("key", 0, SEED); - set_user_balance::(&module_key); + set_user_balance::(&module_key); - #[extrinsic_call] + #[extrinsic_call] register( - RawOrigin::Signed(module_key.clone()), + RawOrigin::Signed(module_key.clone()), network.clone(), - name, - address, - MIN_STAKE, + name, + address, + MIN_STAKE, module_key.clone(), - ); + ); - let netuid = >::get_netuid_for_name(network); + let netuid = >::get_netuid_for_name(network); - assert!( - >::is_registered(netuid, &module_key), - "Register failed" - ); + assert!(>::is_registered(netuid, &module_key), "Register failed"); - Ok(()) - } + Ok(()) + } - #[benchmark] - fn set_weights() -> Result<(), BenchmarkError> { - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let caller: T::AccountId = account("key", 0, SEED); + #[benchmark] + fn set_weights() -> Result<(), BenchmarkError> { + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let caller: T::AccountId = account("key", 0, SEED); - add_stake_helper::( - network.clone(), - name.clone(), - address.clone(), - caller.clone(), - MIN_STAKE - ); + add_stake_helper::( + network.clone(), + name.clone(), + address.clone(), + caller.clone(), + MIN_STAKE, + ); - let (netuid, _, _) = add_stake_multiple_helper::(caller.clone()); + let (netuid, _, _) = add_stake_multiple_helper::(caller.clone()); - let uids = >::get_uids(netuid); + let uids = >::get_uids(netuid); - #[extrinsic_call] + #[extrinsic_call] set_weights( - RawOrigin::Signed(caller.clone()), - netuid, - uids.clone(), - vec![1u16;uids.len()] - ); - - Ok(()) - } - - #[benchmark] - fn add_stake() -> Result<(), BenchmarkError> { - let (network, name, address, module_key, netuid) = default_register_helper::(); - - #[extrinsic_call] - add_stake( - RawOrigin::Signed(module_key.clone()), + RawOrigin::Signed(caller.clone()), netuid, - module_key.clone(), - MIN_STAKE, - ); - - Ok(()) - } - - #[benchmark] - fn add_stake_multiple() -> Result<(), BenchmarkError> { - let caller: T::AccountId = account("caller", 0, SEED); - - let network: Vec = b"network".to_vec(); - let address: Vec = b"address".to_vec(); - - let module_keys: Vec = (0..10).map(|i| account("key", i, SEED)).collect(); - let amounts: Vec = (0..10).map(|i| i + MIN_STAKE).collect(); - - let mut netuid: u16 = 0; - - for (index, module_key) in module_keys.iter().enumerate() { - let mut name: Vec = b"name".to_vec(); - name.extend(vec![index as u8]); - - netuid = register_helper::( - network.clone(), - name, - address.clone(), - module_key.clone() - ); - } - - set_user_balance::(&caller); - - #[extrinsic_call] - add_stake_multiple( - RawOrigin::Signed(caller), - netuid, - module_keys, - amounts, - ); - - Ok(()) - } - - #[benchmark] - fn transfer_stake() -> Result<(), BenchmarkError> { - let (_, _, _, new_module_key, _) = default_register_helper::(); - - let network: Vec = b"network".to_vec(); - let old_name: Vec = b"old_name".to_vec(); - let old_address: Vec = b"old_address".to_vec(); - let old_module_key: T::AccountId = account("old_key", 0, SEED); - - let netuid = add_stake_helper::( - network.clone(), - old_name.clone(), - old_address.clone(), - old_module_key.clone(), - MIN_STAKE - ); - - #[extrinsic_call] + uids.clone(), + vec![1u16; uids.len()], + ); + + Ok(()) + } + + #[benchmark] + fn add_stake() -> Result<(), BenchmarkError> { + let (network, name, address, module_key, netuid) = default_register_helper::(); + + #[extrinsic_call] + add_stake(RawOrigin::Signed(module_key.clone()), netuid, module_key.clone(), MIN_STAKE); + + Ok(()) + } + + #[benchmark] + fn add_stake_multiple() -> Result<(), BenchmarkError> { + let caller: T::AccountId = account("caller", 0, SEED); + + let network: Vec = b"network".to_vec(); + let address: Vec = b"address".to_vec(); + + let module_keys: Vec = (0..10).map(|i| account("key", i, SEED)).collect(); + let amounts: Vec = (0..10).map(|i| i + MIN_STAKE).collect(); + + let mut netuid: u16 = 0; + + for (index, module_key) in module_keys.iter().enumerate() { + let mut name: Vec = b"name".to_vec(); + name.extend(vec![index as u8]); + + netuid = + register_helper::(network.clone(), name, address.clone(), module_key.clone()); + } + + set_user_balance::(&caller); + + #[extrinsic_call] + add_stake_multiple(RawOrigin::Signed(caller), netuid, module_keys, amounts); + + Ok(()) + } + + #[benchmark] + fn transfer_stake() -> Result<(), BenchmarkError> { + let (_, _, _, new_module_key, _) = default_register_helper::(); + + let network: Vec = b"network".to_vec(); + let old_name: Vec = b"old_name".to_vec(); + let old_address: Vec = b"old_address".to_vec(); + let old_module_key: T::AccountId = account("old_key", 0, SEED); + + let netuid = add_stake_helper::( + network.clone(), + old_name.clone(), + old_address.clone(), + old_module_key.clone(), + MIN_STAKE, + ); + + #[extrinsic_call] transfer_stake( - RawOrigin::Signed(old_module_key.clone()), + RawOrigin::Signed(old_module_key.clone()), netuid, old_module_key.clone(), - new_module_key.clone(), - MIN_STAKE, - ); - - Ok(()) - } - - #[benchmark] - fn transfer_multiple() -> Result<(), BenchmarkError> { - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let module_key: T::AccountId = account("key", 0, SEED); - - let netuid = add_stake_helper::( - network.clone(), - name.clone(), - address.clone(), - module_key.clone(), - MIN_STAKE - ); - - let new_module_keys: Vec = (0..10).map(|i| account("new_key", i, SEED)).collect(); - let amounts: Vec = (0..10).map(|i| i + MIN_STAKE).collect(); - - for (index, new_module_key) in new_module_keys.iter().enumerate() { - let mut new_name: Vec = b"name".to_vec(); - new_name.extend(vec![index as u8]); - - register_helper::( - network.clone(), - new_name, - address.clone(), - new_module_key.clone() - ); - } - - #[extrinsic_call] - transfer_multiple( - RawOrigin::Signed(module_key.clone()), - new_module_keys, - amounts, - ); - - Ok(()) - } - - #[benchmark] - fn remove_stake() -> Result<(), BenchmarkError> { - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let module_key: T::AccountId = account("key", 0, SEED); - - let netuid = add_stake_helper::( - network.clone(), - name.clone(), - address.clone(), - module_key.clone(), - MIN_STAKE - ); - - #[extrinsic_call] - remove_stake( - RawOrigin::Signed(module_key.clone()), - netuid, + new_module_key.clone(), + MIN_STAKE, + ); + + Ok(()) + } + + #[benchmark] + fn transfer_multiple() -> Result<(), BenchmarkError> { + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let module_key: T::AccountId = account("key", 0, SEED); + + let netuid = add_stake_helper::( + network.clone(), + name.clone(), + address.clone(), module_key.clone(), - MIN_STAKE, - ); + MIN_STAKE, + ); + + let new_module_keys: Vec = + (0..10).map(|i| account("new_key", i, SEED)).collect(); + let amounts: Vec = (0..10).map(|i| i + MIN_STAKE).collect(); + + for (index, new_module_key) in new_module_keys.iter().enumerate() { + let mut new_name: Vec = b"name".to_vec(); + new_name.extend(vec![index as u8]); + + register_helper::( + network.clone(), + new_name, + address.clone(), + new_module_key.clone(), + ); + } + + #[extrinsic_call] + transfer_multiple(RawOrigin::Signed(module_key.clone()), new_module_keys, amounts); + + Ok(()) + } + + #[benchmark] + fn remove_stake() -> Result<(), BenchmarkError> { + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let module_key: T::AccountId = account("key", 0, SEED); + + let netuid = add_stake_helper::( + network.clone(), + name.clone(), + address.clone(), + module_key.clone(), + MIN_STAKE, + ); - Ok(()) - } + #[extrinsic_call] + remove_stake(RawOrigin::Signed(module_key.clone()), netuid, module_key.clone(), MIN_STAKE); - #[benchmark] - fn remove_stake_multiple() -> Result<(), BenchmarkError> { - let caller: T::AccountId = account("caller", 0, SEED); + Ok(()) + } - let (netuid, module_keys, amounts) = add_stake_multiple_helper::(caller.clone()); + #[benchmark] + fn remove_stake_multiple() -> Result<(), BenchmarkError> { + let caller: T::AccountId = account("caller", 0, SEED); - #[extrinsic_call] - remove_stake_multiple( - RawOrigin::Signed(caller.clone()), - netuid, - module_keys, - amounts, - ); + let (netuid, module_keys, amounts) = add_stake_multiple_helper::(caller.clone()); - Ok(()) - } + #[extrinsic_call] + remove_stake_multiple(RawOrigin::Signed(caller.clone()), netuid, module_keys, amounts); - #[benchmark] - fn update_subnet() -> Result<(), BenchmarkError> { - let (network, name, address, module_key, netuid) = default_register_helper::(); + Ok(()) + } - let subnet_params = >::subnet_params(netuid); - let tempo = 5; + #[benchmark] + fn update_subnet() -> Result<(), BenchmarkError> { + let (network, name, address, module_key, netuid) = default_register_helper::(); + + let subnet_params = >::subnet_params(netuid); + let tempo = 5; let min_stake = 0; - #[extrinsic_call] + #[extrinsic_call] update_subnet( - RawOrigin::Signed(module_key.clone()), + RawOrigin::Signed(module_key.clone()), netuid, subnet_params.name.clone(), tempo, @@ -370,275 +314,206 @@ mod benchmarks { subnet_params.min_allowed_weights, subnet_params.max_allowed_weights, subnet_params.max_allowed_uids, - min_stake, + min_stake, subnet_params.founder, - ); + ); - Ok(()) - } + Ok(()) + } - #[benchmark] - fn remote_subnet() -> Result<(), BenchmarkError> { - let (network, name, address, module_key, netuid) = default_register_helper::(); + #[benchmark] + fn remote_subnet() -> Result<(), BenchmarkError> { + let (network, name, address, module_key, netuid) = default_register_helper::(); - #[extrinsic_call] - remote_subnet( - RawOrigin::Signed(module_key.clone()), - netuid - ); + #[extrinsic_call] + remote_subnet(RawOrigin::Signed(module_key.clone()), netuid); - Ok(()) - } + Ok(()) + } - #[benchmark] - fn update_module() -> Result<(), BenchmarkError> { - let (network, name, address, module_key, netuid) = default_register_helper::(); + #[benchmark] + fn update_module() -> Result<(), BenchmarkError> { + let (network, name, address, module_key, netuid) = default_register_helper::(); - let mut new_name = name.clone(); - new_name.extend(vec![1u8]); + let mut new_name = name.clone(); + new_name.extend(vec![1u8]); - #[extrinsic_call] + #[extrinsic_call] update_module( - RawOrigin::Signed(module_key.clone()), + RawOrigin::Signed(module_key.clone()), + netuid, + new_name, + address, + Option::None, + ); + + Ok(()) + } + + #[benchmark] + fn update_global() -> Result<(), BenchmarkError> { + #[extrinsic_call] + update_global(RawOrigin::Root, 1, 1, 1, 1, 1, 1); + + Ok(()) + } + + #[benchmark] + fn add_global_update() -> Result<(), BenchmarkError> { + #[extrinsic_call] + add_global_update(RawOrigin::Root, 1, 1, 1, 1, 1, 1); + + Ok(()) + } + + #[benchmark] + fn vote_global_update() -> Result<(), BenchmarkError> { + >::add_global_update(RawOrigin::Root.into(), 1, 1, 1, 1, 1, 1); + + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let caller: T::AccountId = account("key", 0, SEED); + + add_stake_helper::( + network.clone(), + name.clone(), + address.clone(), + caller.clone(), + MIN_STAKE, + ); + + #[extrinsic_call] + vote_global_update(RawOrigin::Signed(caller.clone()), 1); + + Ok(()) + } + + #[benchmark] + fn accept_global_update() -> Result<(), BenchmarkError> { + >::add_global_update(RawOrigin::Root.into(), 1, 1, 1, 1, 1, 1); + + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let caller: T::AccountId = account("key", 0, SEED); + + add_stake_helper::( + network.clone(), + name.clone(), + address.clone(), + caller.clone(), + MIN_STAKE, + ); + + >::vote_global_update(RawOrigin::Signed(caller.clone()).into(), 1); + + #[extrinsic_call] + accept_global_update(RawOrigin::Signed(caller.clone()), 1); + + Ok(()) + } + + #[benchmark] + fn add_subnet_update() -> Result<(), BenchmarkError> { + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let caller: T::AccountId = account("key", 0, SEED); + + add_stake_helper::( + network.clone(), + name.clone(), + address.clone(), + caller.clone(), + MIN_STAKE, + ); + + let netuid = >::get_netuid_for_name(network.clone()); + + #[extrinsic_call] + add_subnet_update(RawOrigin::Root, netuid, name, 1, 1, 1, 1, 1, 1, 1, 1, 1); + + Ok(()) + } + + #[benchmark] + fn vote_subnet_update() -> Result<(), BenchmarkError> { + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let caller: T::AccountId = account("key", 0, SEED); + + add_stake_helper::( + network.clone(), + name.clone(), + address.clone(), + caller.clone(), + MIN_STAKE, + ); + + let netuid = >::get_netuid_for_name(network.clone()); + + >::add_subnet_update( + RawOrigin::Root.into(), netuid, - new_name, - address, - Option::None - ); - - - Ok(()) - } - - #[benchmark] - fn update_global() -> Result<(), BenchmarkError> { - #[extrinsic_call] - update_global( - RawOrigin::Root, + name, 1, - 1, - 1, - 1, - 1, - 1 - ); - - Ok(()) - } - - #[benchmark] - fn add_global_update() -> Result<(), BenchmarkError> { - #[extrinsic_call] - add_global_update( - RawOrigin::Root, 1, - 1, - 1, - 1, - 1, - 1 - ); - - Ok(()) - } - - #[benchmark] - fn vote_global_update() -> Result<(), BenchmarkError> { - >::add_global_update( - RawOrigin::Root.into(), 1, - 1, - 1, - 1, - 1, - 1 - ); - - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let caller: T::AccountId = account("key", 0, SEED); - - add_stake_helper::( - network.clone(), - name.clone(), - address.clone(), - caller.clone(), - MIN_STAKE - ); - - #[extrinsic_call] - vote_global_update( - RawOrigin::Signed(caller.clone()), - 1 - ); - - Ok(()) - } - - #[benchmark] - fn accept_global_update() -> Result<(), BenchmarkError> { - >::add_global_update( - RawOrigin::Root.into(), 1, - 1, - 1, - 1, - 1, - 1 - ); - - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let caller: T::AccountId = account("key", 0, SEED); - - add_stake_helper::( - network.clone(), - name.clone(), - address.clone(), - caller.clone(), - MIN_STAKE - ); - - >::vote_global_update( - RawOrigin::Signed(caller.clone()).into(), - 1 - ); - - #[extrinsic_call] - accept_global_update( - RawOrigin::Signed(caller.clone()), - 1 - ); - - Ok(()) - } - - #[benchmark] - fn add_subnet_update() -> Result<(), BenchmarkError> { - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let caller: T::AccountId = account("key", 0, SEED); - - add_stake_helper::( - network.clone(), - name.clone(), - address.clone(), - caller.clone(), - MIN_STAKE - ); - - let netuid = >::get_netuid_for_name(network.clone()); - - #[extrinsic_call] - add_subnet_update( - RawOrigin::Root, - netuid, - name, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - ); - - Ok(()) - } - - #[benchmark] - fn vote_subnet_update() -> Result<(), BenchmarkError> { - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let caller: T::AccountId = account("key", 0, SEED); - - add_stake_helper::( - network.clone(), - name.clone(), - address.clone(), - caller.clone(), - MIN_STAKE - ); - - let netuid = >::get_netuid_for_name(network.clone()); + 1, + 1, + 1, + 1, + 1, + ); - >::add_subnet_update( - RawOrigin::Root.into(), - netuid, - name, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - ); - - - #[extrinsic_call] - vote_subnet_update( - RawOrigin::Signed(caller.clone()), - 1 - ); - - Ok(()) - } - - #[benchmark] - fn accept_subnet_update() -> Result<(), BenchmarkError> { - let network: Vec = b"network".to_vec(); - let name: Vec = b"name".to_vec(); - let address: Vec = b"address".to_vec(); - let caller: T::AccountId = account("key", 0, SEED); - - add_stake_helper::( - network.clone(), - name.clone(), - address.clone(), - caller.clone(), - MIN_STAKE - ); - - let netuid = >::get_netuid_for_name(network.clone()); + #[extrinsic_call] + vote_subnet_update(RawOrigin::Signed(caller.clone()), 1); + + Ok(()) + } + + #[benchmark] + fn accept_subnet_update() -> Result<(), BenchmarkError> { + let network: Vec = b"network".to_vec(); + let name: Vec = b"name".to_vec(); + let address: Vec = b"address".to_vec(); + let caller: T::AccountId = account("key", 0, SEED); + + add_stake_helper::( + network.clone(), + name.clone(), + address.clone(), + caller.clone(), + MIN_STAKE, + ); + + let netuid = >::get_netuid_for_name(network.clone()); >::add_subnet_update( - RawOrigin::Root.into(), + RawOrigin::Root.into(), netuid, - name, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - ); - - - >::vote_subnet_update( - RawOrigin::Signed(caller.clone()).into(), - 1 - ); - - #[extrinsic_call] - accept_subnet_update( - RawOrigin::Signed(caller.clone()), - 1 - ); - - Ok(()) - } - - impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test); + name, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + ); + + >::vote_subnet_update(RawOrigin::Signed(caller.clone()).into(), 1); + + #[extrinsic_call] + accept_subnet_update(RawOrigin::Signed(caller.clone()), 1); + + Ok(()) + } + + impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test); } diff --git a/pallets/subspace/src/global.rs b/pallets/subspace/src/global.rs index b741ad057..931b9d2d6 100644 --- a/pallets/subspace/src/global.rs +++ b/pallets/subspace/src/global.rs @@ -1,11 +1,10 @@ use super::*; +use crate::utils::is_vec_str; use frame_support::pallet_prelude::DispatchResult; use sp_runtime::DispatchError; use system::ensure_root; -use crate::utils::is_vec_str; impl Pallet { - pub fn global_params() -> GlobalParams { GlobalParams { max_name_length: Self::get_global_max_name_length(), @@ -25,18 +24,21 @@ impl Pallet { } } - pub fn check_global_params(params: GlobalParams) -> DispatchResult{ - // checks if params are valid + pub fn check_global_params(params: GlobalParams) -> DispatchResult { + // checks if params are valid let og_params = Self::global_params(); - // check if the name already exists - ensure!(params.max_name_length > 0, Error::::InvalidMaxNameLength); - - ensure!(params.max_allowed_subnets > 0, Error::::InvalidMaxAllowedSubnets); + // check if the name already exists + ensure!(params.max_name_length > 0, Error::::InvalidMaxNameLength); + + ensure!(params.max_allowed_subnets > 0, Error::::InvalidMaxAllowedSubnets); ensure!(params.max_allowed_modules > 0, Error::::InvalidMaxAllowedModules); - ensure!(params.max_registrations_per_block > 0, Error::::InvalidMaxRegistrationsPerBlock); + ensure!( + params.max_registrations_per_block > 0, + Error::::InvalidMaxRegistrationsPerBlock + ); ensure!(params.vote_threshold < 100, Error::::InvalidVoteThreshold); @@ -47,17 +49,13 @@ impl Pallet { ensure!(params.tx_rate_limit > 0, Error::::InvalidTxRateLimit); ensure!(params.burn_rate <= 100, Error::::InvalidBurnRate); - - ensure!(params.min_burn <= 100, Error::::InvalidMinBurn); + ensure!(params.min_burn <= 100, Error::::InvalidMinBurn); - - Ok(()) - } - + Ok(()) + } pub fn set_global_params(params: GlobalParams) { - Self::set_global_max_name_length(params.max_name_length); Self::set_global_max_allowed_subnets(params.max_allowed_subnets); Self::set_max_allowed_modules(params.max_allowed_modules); @@ -68,17 +66,15 @@ impl Pallet { Self::set_max_proposals(params.max_proposals); Self::set_vote_mode_global(params.vote_mode); Self::set_burn_rate(params.burn_rate); - Self::set_min_burn( params.min_burn); + Self::set_min_burn(params.min_burn); Self::set_min_weight_stake(params.min_weight_stake); Self::set_min_stake_global(params.min_stake); - - } pub fn get_min_weight_stake() -> u64 { return MinWeightStake::::get() } - pub fn set_min_weight_stake(min_weight_stake: u64) { + pub fn set_min_weight_stake(min_weight_stake: u64) { MinWeightStake::::put(min_weight_stake) } @@ -97,7 +93,6 @@ impl Pallet { MinStakeGlobal::::put(min_stake) } - pub fn set_vote_mode_global(vote_mode: Vec) { VoteModeGlobal::::put(vote_mode); } @@ -112,7 +107,7 @@ impl Pallet { pub fn set_burn_rate(mut burn_rate: u16) { BurnRate::::put(burn_rate.min(100)); } - + pub fn set_max_proposals(max_proposals: u64) { MaxProposals::::put(max_proposals); } @@ -138,39 +133,32 @@ impl Pallet { MaxNameLength::::put(max_name_length) } - pub fn do_update_global( - origin: T::RuntimeOrigin, - params: GlobalParams, - ) -> DispatchResult { + pub fn do_update_global(origin: T::RuntimeOrigin, params: GlobalParams) -> DispatchResult { ensure_root(origin)?; - ensure!(is_vec_str(Self::get_vote_mode_global(),"authority"), Error::::InvalidVoteMode); + ensure!(is_vec_str(Self::get_vote_mode_global(), "authority"), Error::::InvalidVoteMode); Self::set_global_params(params.clone()); Ok(()) } pub fn global_n() -> u16 { - let mut global_n : u16 = 0; + let mut global_n: u16 = 0; for netuid in Self::netuids() { global_n += N::::get(netuid); } return global_n } - - pub fn get_global_stake_to( - key: &T::AccountId, - ) -> u64 { + pub fn get_global_stake_to(key: &T::AccountId) -> u64 { // get all of the stake to - let total_networks: u16 = TotalSubnets::::get(); - let mut total_stake_to = 0; - - for netuid in 0..total_networks { - total_stake_to += Self::get_total_stake_to(netuid, key); - } + let total_networks: u16 = TotalSubnets::::get(); + let mut total_stake_to = 0; - total_stake_to - } + for netuid in 0..total_networks { + total_stake_to += Self::get_total_stake_to(netuid, key); + } + total_stake_to + } // Configure tx rate limiting pub fn get_tx_rate_limit() -> u64 { @@ -180,7 +168,7 @@ impl Pallet { TxRateLimit::::put(tx_rate_limit) } - pub fn set_min_burn( min_burn: u64) { + pub fn set_min_burn(min_burn: u64) { MinBurn::::put(min_burn); } diff --git a/pallets/subspace/src/lib.rs b/pallets/subspace/src/lib.rs index 3c6935859..9d952fc51 100644 --- a/pallets/subspace/src/lib.rs +++ b/pallets/subspace/src/lib.rs @@ -38,15 +38,15 @@ mod mock; // ========================= mod global; mod math; -mod utils; pub mod module; -mod subnet; +mod profit_share; mod registration; mod staking; mod step; -mod weights; +mod subnet; +mod utils; mod voting; -mod profit_share; +mod weights; #[frame_support::pallet] pub mod pallet { @@ -83,7 +83,6 @@ pub mod pallet { // ==== Defaults ==== // ======================================= - #[pallet::type_value] pub fn EmptyU16Vec() -> Vec { vec![] @@ -101,78 +100,103 @@ pub mod pallet { // ==== Global Variables ==== // ============================ #[pallet::type_value] - pub fn DefaultUnitEmission() -> u64 {23148148148} + pub fn DefaultUnitEmission() -> u64 { + 23148148148 + } #[pallet::storage] // --- ITEM ( unit_emission ) pub(super) type UnitEmission = StorageValue<_, u64, ValueQuery, DefaultUnitEmission>; #[pallet::type_value] - pub fn DefaultTxRateLimit() -> u64 {1} + pub fn DefaultTxRateLimit() -> u64 { + 1 + } #[pallet::storage] // --- ITEM ( tx_rate_limit ) pub(super) type TxRateLimit = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit>; // FIXME: NOT IN USE - #[pallet::type_value] - pub fn DefaultBurnRate() -> u16 {0} + pub fn DefaultBurnRate() -> u16 { + 0 + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights - pub type BurnRate = StorageValue<_ , u16, ValueQuery, DefaultBurnRate>; + pub type BurnRate = StorageValue<_, u16, ValueQuery, DefaultBurnRate>; #[pallet::type_value] - pub fn DefaultMinBurn() -> u64 {0} + pub fn DefaultMinBurn() -> u64 { + 0 + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights pub type MinBurn = StorageValue<_, u64, ValueQuery, DefaultMinBurn>; - #[pallet::type_value] - pub fn DefaultLastTxBlock() -> u64 { 0 } + pub fn DefaultLastTxBlock() -> u64 { + 0 + } #[pallet::storage] // --- MAP ( key ) --> last_block - pub(super) type LastTxBlock = StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock>; + pub(super) type LastTxBlock = + StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock>; #[pallet::type_value] - pub fn DefaultMaxNameLength() -> u16 { 32 } + pub fn DefaultMaxNameLength() -> u16 { + 32 + } #[pallet::storage] // --- ITEM ( max_name_length ) - pub(super) type MaxNameLength = StorageValue<_, u16, ValueQuery, DefaultMaxNameLength>; + pub(super) type MaxNameLength = + StorageValue<_, u16, ValueQuery, DefaultMaxNameLength>; #[pallet::type_value] - pub fn DefaultMaxAllowedSubnets() -> u16 { 256 } + pub fn DefaultMaxAllowedSubnets() -> u16 { + 256 + } #[pallet::storage] // --- ITEM ( max_allowed_subnets ) - pub(super) type MaxAllowedSubnets = StorageValue<_, u16, ValueQuery, DefaultMaxAllowedSubnets>; + pub(super) type MaxAllowedSubnets = + StorageValue<_, u16, ValueQuery, DefaultMaxAllowedSubnets>; #[pallet::type_value] - pub fn DefaultMaxAllowedModules() -> u16 { 10_000 } + pub fn DefaultMaxAllowedModules() -> u16 { + 10_000 + } #[pallet::storage] // --- ITEM ( max_allowed_modules ) - pub(super) type MaxAllowedModules = StorageValue<_, u16, ValueQuery, DefaultMaxAllowedModules>; - + pub(super) type MaxAllowedModules = + StorageValue<_, u16, ValueQuery, DefaultMaxAllowedModules>; + #[pallet::type_value] - pub fn DefaultRegistrationsPerBlock() -> u16 { 0 } + pub fn DefaultRegistrationsPerBlock() -> u16 { + 0 + } #[pallet::storage] // --- ITEM ( registrations_this block ) pub type RegistrationsPerBlock = StorageValue<_, u16, ValueQuery, DefaultRegistrationsPerBlock>; - + #[pallet::type_value] - pub fn DefaultMaxRegistrationsPerBlock() -> u16 { 10 } + pub fn DefaultMaxRegistrationsPerBlock() -> u16 { + 10 + } #[pallet::storage] // --- ITEM( global_max_registrations_per_block ) pub type MaxRegistrationsPerBlock = StorageValue<_, u16, ValueQuery, DefaultMaxRegistrationsPerBlock>; #[pallet::type_value] - pub fn DefaultMinStakeGlobal() -> u64 { 100 } + pub fn DefaultMinStakeGlobal() -> u64 { + 100 + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights pub type MinStakeGlobal = StorageValue<_, u64, ValueQuery, DefaultMinStake>; - - #[pallet::type_value] - pub fn DefaultMinWeightStake() -> u64 { 0 } + pub fn DefaultMinWeightStake() -> u64 { + 0 + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights pub type MinWeightStake = StorageValue<_, u64, ValueQuery, DefaultMinWeightStake>; - #[pallet::type_value] - pub fn DefaultMaxAllowedWeightsGlobal() -> u16 { 512 } + pub fn DefaultMaxAllowedWeightsGlobal() -> u16 { + 512 + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights - pub type MaxAllowedWeightsGlobal = StorageValue<_, u16, ValueQuery, DefaultMaxAllowedWeightsGlobal>; - + pub type MaxAllowedWeightsGlobal = + StorageValue<_, u16, ValueQuery, DefaultMaxAllowedWeightsGlobal>; #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] #[scale_info(skip_type_params(T))] @@ -188,24 +212,23 @@ pub mod pallet { pub struct GlobalParams { pub burn_rate: u16, // max - pub max_name_length: u16, // max length of a network name - pub max_allowed_subnets: u16, // max number of subnets allowed - pub max_allowed_modules: u16, // max number of modules allowed per subnet + pub max_name_length: u16, // max length of a network name + pub max_allowed_subnets: u16, // max number of subnets allowed + pub max_allowed_modules: u16, // max number of modules allowed per subnet pub max_registrations_per_block: u16, // max number of registrations per block - pub max_allowed_weights: u16, // max number of weights per module - pub max_proposals: u64, // max number of proposals per block + pub max_allowed_weights: u16, // max number of weights per module + pub max_proposals: u64, // max number of proposals per block // mins - pub min_burn: u64, // min burn required - pub min_stake: u64, // min stake required + pub min_burn: u64, // min burn required + pub min_stake: u64, // min stake required pub min_weight_stake: u64, // min weight stake required // other - pub unit_emission: u64, // emission per block - pub tx_rate_limit: u64, // tx rate limit + pub unit_emission: u64, // emission per block + pub tx_rate_limit: u64, // tx rate limit pub vote_threshold: u16, // out of 100 - pub vote_mode: Vec, // out of 100 - + pub vote_mode: Vec, // out of 100 } #[pallet::type_value] @@ -221,15 +244,13 @@ pub mod pallet { min_burn: DefaultMinBurn::::get(), min_stake: DefaultMinStakeGlobal::::get(), min_weight_stake: DefaultMinWeightStake::::get(), - unit_emission: DefaultUnitEmission::::get(), + unit_emission: DefaultUnitEmission::::get(), tx_rate_limit: DefaultTxRateLimit::::get(), vote_threshold: DefaultVoteThreshold::::get(), vote_mode: DefaultVoteMode::::get(), - } } - // ========================= // ==== Subnet PARAMS ==== // ========================= @@ -239,18 +260,22 @@ pub mod pallet { pub struct SubnetParams { // --- parameters pub founder: T::AccountId, - pub founder_share: u16, // out of 100 - pub immunity_period: u16, // immunity period - pub incentive_ratio : u16, // out of 100 - pub max_allowed_uids: u16, // max number of weights allowed to be registered in this pub max_allowed_uids: u16, // max number of uids allowed to be registered in this subne - pub max_allowed_weights: u16, // max number of weights allowed to be registered in this pub max_allowed_uids: u16, // max number of uids allowed to be registered in this subnet + pub founder_share: u16, // out of 100 + pub immunity_period: u16, // immunity period + pub incentive_ratio: u16, // out of 100 + pub max_allowed_uids: u16, /* max number of weights allowed to be registered in this + * pub max_allowed_uids: u16, // max number of uids + * allowed to be registered in this subne */ + pub max_allowed_weights: u16, /* max number of weights allowed to be registered in this + * pub max_allowed_uids: u16, // max number of uids + * allowed to be registered in this subnet */ pub min_allowed_weights: u16, // min number of weights allowed to be registered in this - pub max_stake: u64, // max stake allowed - pub max_weight_age: u64, // max age of a weight - pub min_stake: u64, // min stake required + pub max_stake: u64, // max stake allowed + pub max_weight_age: u64, // max age of a weight + pub min_stake: u64, // min stake required pub name: Vec, - pub self_vote: bool, // - pub tempo: u16, // how many blocks to wait before rewarding models + pub self_vote: bool, // + pub tempo: u16, // how many blocks to wait before rewarding models pub trust_ratio: u16, pub vote_threshold: u16, // out of 100 pub vote_mode: Vec, @@ -272,92 +297,118 @@ pub mod pallet { trust_ratio: DefaultTrustRatio::::get(), self_vote: DefaultSelfVote::::get(), founder_share: DefaultFounderShare::::get(), - incentive_ratio : DefaultIncentiveRatio::::get(), - min_stake : DefaultMinStake::::get(), + incentive_ratio: DefaultIncentiveRatio::::get(), + min_stake: DefaultMinStake::::get(), founder: DefaultFounder::::get(), } } - #[pallet::type_value] - pub fn DefaultMaxAllowedUids() -> u16 { 4096 } + pub fn DefaultMaxAllowedUids() -> u16 { + 4096 + } #[pallet::storage] // --- MAP ( netuid ) --> max_allowed_uids - pub type MaxAllowedUids = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultMaxAllowedUids>; + pub type MaxAllowedUids = + StorageMap<_, Identity, u16, u16, ValueQuery, DefaultMaxAllowedUids>; #[pallet::type_value] - pub fn DefaultImmunityPeriod() -> u16 { 40 } + pub fn DefaultImmunityPeriod() -> u16 { + 40 + } #[pallet::storage] // --- MAP ( netuid ) --> immunity_period - pub type ImmunityPeriod = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultImmunityPeriod>; - + pub type ImmunityPeriod = + StorageMap<_, Identity, u16, u16, ValueQuery, DefaultImmunityPeriod>; #[pallet::type_value] - pub fn DefaultMinAllowedWeights() -> u16 {1} + pub fn DefaultMinAllowedWeights() -> u16 { + 1 + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights - pub type MinAllowedWeights = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultMinAllowedWeights>; + pub type MinAllowedWeights = + StorageMap<_, Identity, u16, u16, ValueQuery, DefaultMinAllowedWeights>; #[pallet::type_value] - pub fn DefaultSelfVote() -> bool {true} + pub fn DefaultSelfVote() -> bool { + true + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights pub type SelfVote = StorageMap<_, Identity, u16, bool, ValueQuery, DefaultSelfVote>; - #[pallet::type_value] - pub fn DefaultMinStake() -> u64 {0} + pub fn DefaultMinStake() -> u64 { + 0 + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights pub type MinStake = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultMinStake>; - #[pallet::type_value] - pub fn DefaultMaxStake() -> u64 {u64::MAX} + pub fn DefaultMaxStake() -> u64 { + u64::MAX + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights pub type MaxStake = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultMaxStake>; - #[pallet::type_value] - pub fn DefaultMaxWeightAge() -> u64 {u64::MAX} + pub fn DefaultMaxWeightAge() -> u64 { + u64::MAX + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights - pub type MaxWeightAge = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultMaxWeightAge>; - - + pub type MaxWeightAge = + StorageMap<_, Identity, u16, u64, ValueQuery, DefaultMaxWeightAge>; #[pallet::type_value] - pub fn DefaultMaxAllowedWeights() -> u16 {420} + pub fn DefaultMaxAllowedWeights() -> u16 { + 420 + } #[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights - pub type MaxAllowedWeights =StorageMap<_, Identity, u16, u16, ValueQuery, DefaultMaxAllowedWeights>; - + pub type MaxAllowedWeights = + StorageMap<_, Identity, u16, u16, ValueQuery, DefaultMaxAllowedWeights>; #[pallet::type_value] - pub fn DefaultFounder() -> T::AccountId {T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap()} + pub fn DefaultFounder() -> T::AccountId { + T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap() + } #[pallet::storage] // --- DMAP ( key, netuid ) --> bool - pub type Founder = StorageMap<_, Identity, u16, T::AccountId, ValueQuery, DefaultFounder>; - + pub type Founder = + StorageMap<_, Identity, u16, T::AccountId, ValueQuery, DefaultFounder>; #[pallet::type_value] - pub fn DefaultFounderShare() -> u16 {0} + pub fn DefaultFounderShare() -> u16 { + 0 + } #[pallet::storage] // --- DMAP ( key, netuid ) --> bool - pub type FounderShare = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultFounderShare>; - + pub type FounderShare = + StorageMap<_, Identity, u16, u16, ValueQuery, DefaultFounderShare>; #[pallet::type_value] - pub fn DefaultIncentiveRatio() -> u16 {50} + pub fn DefaultIncentiveRatio() -> u16 { + 50 + } #[pallet::storage] // --- DMAP ( key, netuid ) --> bool - pub type IncentiveRatio = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultIncentiveRatio>; - - + pub type IncentiveRatio = + StorageMap<_, Identity, u16, u16, ValueQuery, DefaultIncentiveRatio>; + #[pallet::type_value] - pub fn DefaultTempo() -> u16 {100} + pub fn DefaultTempo() -> u16 { + 100 + } #[pallet::storage] // --- MAP ( netuid ) --> epoch pub type Tempo = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultTempo>; #[pallet::type_value] - pub fn DefaultTrustRatio() -> u16 {0} + pub fn DefaultTrustRatio() -> u16 { + 0 + } #[pallet::storage] // --- MAP ( netuid ) --> epoch pub type TrustRatio = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultTrustRatio>; #[pallet::type_value] - pub fn DefaultQuadraticVoting() -> bool {false} + pub fn DefaultQuadraticVoting() -> bool { + false + } #[pallet::storage] // --- MAP ( netuid ) --> epoch - pub type QuadraticVoting = StorageMap<_, Identity, u16, bool, ValueQuery, DefaultQuadraticVoting>; + pub type QuadraticVoting = + StorageMap<_, Identity, u16, bool, ValueQuery, DefaultQuadraticVoting>; // ======================================= // ==== Voting ==== @@ -372,39 +423,44 @@ pub mod pallet { } #[pallet::type_value] - pub fn DefaultVoterInfo() -> VoterInfo { - VoterInfo { - proposal_id: u64::MAX, - votes: 0, - participant_index: u16::MAX, - } + pub fn DefaultVoterInfo() -> VoterInfo { + VoterInfo { proposal_id: u64::MAX, votes: 0, participant_index: u16::MAX } } #[pallet::storage] // --- MAP ( netuid ) --> epoch - pub type Voter2Info =StorageMap<_, Identity, T::AccountId, VoterInfo, ValueQuery, DefaultVoterInfo>; + pub type Voter2Info = + StorageMap<_, Identity, T::AccountId, VoterInfo, ValueQuery, DefaultVoterInfo>; - pub fn DefaultVoteStake() -> u64 {0} // out of 100 + pub fn DefaultVoteStake() -> u64 { + 0 + } // out of 100 // VOTING THRESHOOLD #[pallet::type_value] - pub fn DefaultVoteThreshold() -> u16 {50} // out of 100 + pub fn DefaultVoteThreshold() -> u16 { + 50 + } // out of 100 #[pallet::storage] // --- MAP ( netuid ) --> epoch - pub type VoteThresholdSubnet = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultVoteThreshold>; + pub type VoteThresholdSubnet = + StorageMap<_, Identity, u16, u16, ValueQuery, DefaultVoteThreshold>; #[pallet::storage] // --- MAP ( netuid ) --> epoch pub type GlobalVoteThreshold = StorageValue<_, u16, ValueQuery, DefaultVoteThreshold>; - - // VOTING MODE + + // VOTING MODE // OPTIONS -> [stake, authority, quadratic] #[pallet::type_value] - pub fn DefaultVoteMode() -> Vec {"authority".as_bytes().to_vec()} + pub fn DefaultVoteMode() -> Vec { + "authority".as_bytes().to_vec() + } #[pallet::storage] // --- MAP ( netuid ) --> epoch - pub type VoteModeSubnet =StorageMap<_, Identity, u16, Vec, ValueQuery, DefaultVoteMode>; + pub type VoteModeSubnet = + StorageMap<_, Identity, u16, Vec, ValueQuery, DefaultVoteMode>; #[pallet::storage] // --- MAP ( netuid ) --> epoch - pub type VoteModeGlobal =StorageValue<_, Vec, ValueQuery, DefaultVoteMode>; + pub type VoteModeGlobal = StorageValue<_, Vec, ValueQuery, DefaultVoteMode>; #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SubnetInfo { // --- parameters - pub params: SubnetParams , + pub params: SubnetParams, pub netuid: u16, // --- unique id of the network pub n: u16, pub stake: u64, @@ -412,25 +468,31 @@ pub mod pallet { pub founder: T::AccountId, } - #[pallet::storage] // --- ITEM( tota_number_of_existing_networks ) pub type TotalSubnets = StorageValue<_, u16, ValueQuery>; #[pallet::type_value] - pub fn DefaultEmission() -> u64 { 0 } + pub fn DefaultEmission() -> u64 { + 0 + } #[pallet::storage] // --- MAP( netuid ) --> subnet_emission pub type SubnetEmission = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultEmission>; - + #[pallet::type_value] - pub fn DefaultN() -> u16 {0} + pub fn DefaultN() -> u16 { + 0 + } #[pallet::storage] // --- MAP ( netuid ) --> subnetwork_n (Number of UIDs in the network). pub type N = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultN>; - + #[pallet::type_value] - pub fn DefaultPendingEmission() -> u64 {0} + pub fn DefaultPendingEmission() -> u64 { + 0 + } #[pallet::storage] // --- MAP ( netuid ) --> pending_emission - pub type PendingEmission = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultPendingEmission>; - + pub type PendingEmission = + StorageMap<_, Identity, u16, u64, ValueQuery, DefaultPendingEmission>; + #[pallet::storage] // --- MAP ( network_name ) --> netuid pub type SubnetNames = StorageMap<_, Identity, u16, Vec, ValueQuery>; @@ -438,43 +500,60 @@ pub mod pallet { // ==== Module Variables ==== // ======================================= - #[pallet::storage] // --- DMAP ( netuid, module_key ) --> uid pub(super) type Uids = StorageDoubleMap<_, Identity, u16, Blake2_128Concat, T::AccountId, u16, OptionQuery>; - - #[pallet::storage] // --- DMAP ( netuid, module_key ) --> uid - pub(super) type Key2Controller = - StorageDoubleMap<_, Identity, T::AccountId, Blake2_128Concat, T::AccountId, u16, OptionQuery>; + pub(super) type Key2Controller = StorageDoubleMap< + _, + Identity, + T::AccountId, + Blake2_128Concat, + T::AccountId, + u16, + OptionQuery, + >; #[pallet::storage] // --- DMAP ( netuid, module_key ) --> uid - pub(super) type Controller2Keys = - StorageDoubleMap<_, Identity, T::AccountId, Blake2_128Concat, Vec, u16, OptionQuery>; + pub(super) type Controller2Keys = StorageDoubleMap< + _, + Identity, + T::AccountId, + Blake2_128Concat, + Vec, + u16, + OptionQuery, + >; - - #[pallet::type_value] pub fn DefaultKey() -> T::AccountId { T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap() } #[pallet::storage] // --- DMAP ( netuid, uid ) --> module_key - pub(super) type Keys = StorageDoubleMap<_, Identity, u16, Identity, u16, T::AccountId, ValueQuery, DefaultKey>; + pub(super) type Keys = + StorageDoubleMap<_, Identity, u16, Identity, u16, T::AccountId, ValueQuery, DefaultKey>; #[pallet::type_value] - pub fn DefaultName() -> Vec {vec![]} + pub fn DefaultName() -> Vec { + vec![] + } #[pallet::storage] // --- DMAP ( netuid, uid ) --> module_name - pub type Name =StorageDoubleMap<_, Twox64Concat, u16, Twox64Concat, u16, Vec, ValueQuery>; + pub type Name = + StorageDoubleMap<_, Twox64Concat, u16, Twox64Concat, u16, Vec, ValueQuery>; #[pallet::type_value] - pub fn DefaultAddress() -> Vec {vec![]} + pub fn DefaultAddress() -> Vec { + vec![] + } #[pallet::storage] // --- DMAP ( netuid, uid ) --> module_address - pub type Address = StorageDoubleMap<_, Twox64Concat, u16, Twox64Concat, u16, Vec, ValueQuery>; - + pub type Address = + StorageDoubleMap<_, Twox64Concat, u16, Twox64Concat, u16, Vec, ValueQuery>; #[pallet::type_value] - pub fn DefaultDelegationFee() -> Percent { Percent::from_percent(20u8)} + pub fn DefaultDelegationFee() -> Percent { + Percent::from_percent(20u8) + } #[pallet::storage] // -- DMAP(netuid, module_key) -> delegation_fee pub(super) type DelegationFee = StorageDoubleMap< _, @@ -490,7 +569,9 @@ pub mod pallet { // STATE OF THE MODULE #[pallet::type_value] - pub fn DefaultBlockAtRegistration() -> u64 { 0 } + pub fn DefaultBlockAtRegistration() -> u64 { + 0 + } #[pallet::storage] // --- DMAP ( netuid, uid ) --> block number that the module is registered pub type RegistrationBlock = StorageDoubleMap< _, @@ -508,7 +589,9 @@ pub mod pallet { // ======================================= #[pallet::type_value] - pub fn DefaultStake() -> u64 { 0 } + pub fn DefaultStake() -> u64 { + 0 + } #[pallet::storage] // --- DMAP ( netuid, module_key ) --> stake | Returns the stake under a module. pub type Stake = StorageDoubleMap< @@ -548,44 +631,40 @@ pub mod pallet { #[pallet::storage] // --- MAP ( netuid ) --> subnet_total_stake pub type TotalStake = StorageMap<_, Identity, u16, u64, ValueQuery>; - // LOAN VARIABLES #[pallet::storage] // --- DMAP ( netuid, module_key ) --> Vec<(delegater, stake )> | Returns the list of delegates - pub type LoanTo = StorageMap< - _, - Identity, - T::AccountId, - Vec<(T::AccountId, u64)>, - ValueQuery, - >; + pub type LoanTo = + StorageMap<_, Identity, T::AccountId, Vec<(T::AccountId, u64)>, ValueQuery>; #[pallet::storage] // --- DMAP ( netuid, module_key ) --> Vec<(delegater, stake )> | Returns the list of delegates - pub type LoanFrom = StorageMap< + pub type LoanFrom = + StorageMap<_, Identity, T::AccountId, Vec<(T::AccountId, u64)>, ValueQuery>; + + // PROFIT SHARE VARIABLES + + #[pallet::type_value] + pub fn DefaultProfitShares() -> Vec<(T::AccountId, u16)> { + vec![] + } + + #[pallet::storage] // --- DMAP ( netuid, account_id ) --> Vec<(module_key, stake )> | Returns the list of the + pub type ProfitShares = StorageMap< _, Identity, T::AccountId, - Vec<(T::AccountId, u64)>, + Vec<(T::AccountId, u16)>, ValueQuery, + DefaultProfitShares, >; - // PROFIT SHARE VARIABLES - #[pallet::type_value] - pub fn DefaultProfitShares() -> Vec<(T::AccountId, u16)> {vec![]} - - #[pallet::storage] // --- DMAP ( netuid, account_id ) --> Vec<(module_key, stake )> | Returns the list of the - pub type ProfitShares = StorageMap<_, - Identity, - T::AccountId, - Vec<(T::AccountId, u16)>, - ValueQuery, - DefaultProfitShares>; - - #[pallet::type_value] - pub fn DefaultProfitShareUnit() -> u16 {u16::MAX} + pub fn DefaultProfitShareUnit() -> u16 { + u16::MAX + } #[pallet::storage] // --- DMAP ( netuid, account_id ) --> Vec<(module_key, stake )> | Returns the list of the - pub type ProfitShareUnit = StorageValue<_, u16, ValueQuery, DefaultProfitShareUnit>; + pub type ProfitShareUnit = + StorageValue<_, u16, ValueQuery, DefaultProfitShareUnit>; // ======================================= // ==== Module Consensus Variables ==== @@ -607,7 +686,9 @@ pub mod pallet { StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU64Vec>; #[pallet::type_value] - pub fn DefaultWeights() -> Vec<(u16, u16)> {vec![]} + pub fn DefaultWeights() -> Vec<(u16, u16)> { + vec![] + } #[pallet::storage] // --- DMAP ( netuid, uid ) --> weights pub(super) type Weights = StorageDoubleMap< _, @@ -620,7 +701,6 @@ pub mod pallet { DefaultWeights, >; - #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { @@ -656,8 +736,8 @@ pub mod pallet { MaxAllowedModulesSet(u16), // --- Event created when setting the maximum allowed modules MaxRegistrationsPerBlockSet(u16), // --- Event created when we set max registrations per block GlobalUpdate(u16, u16, u16, u16, u64, u64), - GlobalProposalAccepted(u64), // (id) - CustomProposalAccepted(u64), // (id) + GlobalProposalAccepted(u64), // (id) + CustomProposalAccepted(u64), // (id) SubnetProposalAccepted(u64, u16), // (id, netuid) } @@ -667,10 +747,10 @@ pub mod pallet { ModuleNameAlreadyExists, // --- Thrown when a module name already exists. NetworkDoesNotExist, // --- Thrown when the network does not exist. TooFewVotesForNewProposal, - ModuleAddressTooLong, - NetworkExist, // --- Thrown when the network already exist. - InvalidIpType, /* ---- Thrown when the user tries to serve an module which - * is not of type 4 (IPv4) or 6 (IPv6). */ + ModuleAddressTooLong, + NetworkExist, // --- Thrown when the network already exist. + InvalidIpType, /* ---- Thrown when the user tries to serve an module which + * is not of type 4 (IPv4) or 6 (IPv6). */ InvalidIpAddress, /* --- Thrown when an invalid IP address is passed to the serve * function. */ NotRegistered, /* ---- Thrown when the caller requests setting or removing data from a @@ -749,8 +829,8 @@ pub mod pallet { NotEnoughVotesToAccept, NotEnoughBalanceToTransfer, NotAuthorityMode, - InvalidTrustRatio, - InvalidMinAllowedWeights, + InvalidTrustRatio, + InvalidMinAllowedWeights, InvalidMaxAllowedWeights, InvalidMinStake, @@ -776,14 +856,9 @@ pub mod pallet { InvalidVoteMode, InvalidMaxWeightAge, InvalidMaxStake, - - AlreadyControlled, - AlreadyController - - - - + AlreadyControlled, + AlreadyController, } // ================== @@ -833,13 +908,13 @@ pub mod pallet { vote_mode: default_params.vote_mode.clone(), trust_ratio: default_params.trust_ratio, self_vote: default_params.self_vote, - founder_share: default_params.founder_share, + founder_share: default_params.founder_share, incentive_ratio: default_params.incentive_ratio, max_weight_age: default_params.max_weight_age, }; - + self::Pallet::::add_subnet(params.clone()); - + for (uid_usize, (key, name, address, weights)) in self.modules[subnet_idx].iter().enumerate() { @@ -877,9 +952,6 @@ pub mod pallet { pub mode: Vec, // "global", "subnet", "custom" } - - - #[pallet::type_value] pub fn DefaultProposal() -> Proposal { Proposal { @@ -890,20 +962,22 @@ pub mod pallet { participants: vec![], accepted: false, mode: vec![], - data: vec![] + data: vec![], } } - #[pallet::storage] // --- MAP ( global_proposal_id ) --> global_update_proposal pub(super) type Proposals = StorageMap<_, Identity, u64, Proposal, ValueQuery, DefaultProposal>; #[pallet::type_value] - pub fn DefaultMaxProposals() -> u64 {128} + pub fn DefaultMaxProposals() -> u64 { + 128 + } #[pallet::storage] - pub(super) type MaxProposals = StorageValue<_, u64, ValueQuery, DefaultMaxProposals>; - + pub(super) type MaxProposals = + StorageValue<_, u64, ValueQuery, DefaultMaxProposals>; + // ================ // ==== Hooks ===== // ================ @@ -978,7 +1052,6 @@ pub mod pallet { Self::do_remove_stake_multiple(origin, netuid, module_keys, amounts) } - #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] pub fn transfer_stake( origin: OriginFor, // --- The account that is calling this function. @@ -999,8 +1072,6 @@ pub mod pallet { Self::do_transfer_multiple(origin, destinations, amounts) } - - #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] pub fn update_module( origin: OriginFor, @@ -1011,7 +1082,7 @@ pub mod pallet { ) -> DispatchResult { let key = ensure_signed(origin.clone())?; ensure!(Self::is_registered(netuid, &key), Error::::NotRegistered); - let uid : u16 = Self::get_uid_for_key(netuid, &key); + let uid: u16 = Self::get_uid_for_key(netuid, &key); let mut params = Self::module_params(netuid, uid); params.name = name; params.address = address; @@ -1034,10 +1105,7 @@ pub mod pallet { } #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] - pub fn deregister( - origin: OriginFor, - netuid : u16, - ) -> DispatchResult { + pub fn deregister(origin: OriginFor, netuid: u16) -> DispatchResult { Self::do_deregister(origin, netuid) } @@ -1045,9 +1113,9 @@ pub mod pallet { pub fn add_profit_shares( origin: OriginFor, keys: Vec, - shares: Vec) -> DispatchResult { + shares: Vec, + ) -> DispatchResult { Self::do_add_profit_shares(origin, keys, shares) - } #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] @@ -1063,8 +1131,8 @@ pub mod pallet { min_stake: u64, min_weight_stake: u64, tx_rate_limit: u64, - unit_emission: u64, - vote_mode: Vec, + unit_emission: u64, + vote_mode: Vec, vote_threshold: u16, ) -> DispatchResult { let mut params = Self::global_params(); @@ -1082,14 +1150,13 @@ pub mod pallet { params.unit_emission = unit_emission; params.vote_mode = vote_mode; params.vote_threshold = vote_threshold; - Self::do_update_global(origin, params) } #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] - pub fn add_global_proposal( - origin: OriginFor, + pub fn add_global_proposal( + origin: OriginFor, // params burn_rate: u16, max_name_length: u16, @@ -1100,13 +1167,13 @@ pub mod pallet { min_burn: u64, min_stake: u64, min_weight_stake: u64, - unit_emission: u64, + unit_emission: u64, tx_rate_limit: u64, vote_threshold: u16, vote_mode: Vec, ) -> DispatchResult { let mut params = Self::global_params(); - + params.burn_rate = burn_rate; params.max_allowed_modules = max_allowed_modules; params.max_allowed_subnets = max_allowed_subnets; @@ -1119,11 +1186,10 @@ pub mod pallet { params.tx_rate_limit = tx_rate_limit; params.unit_emission = unit_emission; params.vote_mode = vote_mode; - params.vote_threshold = vote_threshold; - - Self::do_add_global_proposal(origin, params) - } + params.vote_threshold = vote_threshold; + Self::do_add_global_proposal(origin, params) + } #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] pub fn update_subnet( @@ -1139,12 +1205,12 @@ pub mod pallet { max_stake: u64, min_allowed_weights: u16, max_weight_age: u64, - min_stake : u64, + min_stake: u64, name: Vec, self_vote: bool, tempo: u16, trust_ratio: u16, - vote_mode: Vec, + vote_mode: Vec, vote_threshold: u16, ) -> DispatchResult { let mut params = Self::subnet_params(netuid); @@ -1165,13 +1231,12 @@ pub mod pallet { params.vote_mode = vote_mode; params.vote_threshold = vote_threshold; - Self::do_update_subnet(origin,netuid,params) + Self::do_update_subnet(origin, netuid, params) } - #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] - pub fn add_subnet_proposal( - origin: OriginFor, + pub fn add_subnet_proposal( + origin: OriginFor, netuid: u16, // FOR SUBNET PROPOSAL ONLY founder: T::AccountId, founder_share: u16, @@ -1182,12 +1247,12 @@ pub mod pallet { max_stake: u64, max_weight_age: u64, min_allowed_weights: u16, - min_stake : u64, + min_stake: u64, name: Vec, self_vote: bool, tempo: u16, trust_ratio: u16, - vote_mode: Vec, + vote_mode: Vec, vote_threshold: u16, ) -> DispatchResult { let mut params = Self::subnet_params(netuid); @@ -1208,37 +1273,23 @@ pub mod pallet { params.vote_mode = vote_mode; params.vote_threshold = vote_threshold; - Self::do_add_subnet_proposal(origin, netuid, params) - } + Self::do_add_subnet_proposal(origin, netuid, params) + } #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] - pub fn add_custom_proposal( - origin: OriginFor, - data: Vec, - ) -> DispatchResult { - Self::do_add_custom_proposal(origin, data) - } + pub fn add_custom_proposal(origin: OriginFor, data: Vec) -> DispatchResult { + Self::do_add_custom_proposal(origin, data) + } #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] - pub fn vote_proposal( - origin: OriginFor, - proposal_id: u64 - ) -> DispatchResult { - Self::do_vote_proposal( - origin, - proposal_id - ) - } + pub fn vote_proposal(origin: OriginFor, proposal_id: u64) -> DispatchResult { + Self::do_vote_proposal(origin, proposal_id) + } #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))] - pub fn unvote_proposal( - origin: OriginFor, - ) -> DispatchResult { - Self::do_unregister_voter( - origin - ) - } - + pub fn unvote_proposal(origin: OriginFor) -> DispatchResult { + Self::do_unregister_voter(origin) + } } // ---- Subspace helper functions. diff --git a/pallets/subspace/src/math.rs b/pallets/subspace/src/math.rs index d3db601a7..f80136ef9 100644 --- a/pallets/subspace/src/math.rs +++ b/pallets/subspace/src/math.rs @@ -1,8 +1,8 @@ +use sp_std::{vec, vec::Vec}; use substrate_fixed::{ transcendental::exp, types::{I32F32, I64F64}, }; -use sp_std::{vec, vec::Vec}; #[allow(dead_code)] pub fn bottom_k_u16(vector: &Vec, k: usize) -> Vec { @@ -33,7 +33,6 @@ pub fn top_percentile_u16(vector: &Vec, percentile: I32F32) -> Vec { vec_clone.into_iter().rev().take(k).collect() } - #[allow(dead_code)] pub fn fixed(val: f32) -> I32F32 { I32F32::from_num(val) @@ -548,7 +547,6 @@ pub fn matmul_sparse( // Compute trust scores: t_j = SUM(i) w_ij * s_i // result_j = SUM(i) vector_i * matrix_ij result[*j as usize] += vector[i] * value; - } } result @@ -2502,6 +2500,4 @@ mod tests { epsilon, ); } - - } diff --git a/pallets/subspace/src/mock.rs b/pallets/subspace/src/mock.rs index fec0b4264..bbdfc2dc9 100644 --- a/pallets/subspace/src/mock.rs +++ b/pallets/subspace/src/mock.rs @@ -2,17 +2,17 @@ #![allow(warnings)] use crate as pallet_subspace; use frame_support::{ - assert_ok, parameter_types, + assert_ok, parameter_types, traits::{Everything, Hash, Hooks, StorageMapShim}, weights, }; use frame_system as system; use frame_system::{limits, Config, EnsureNever, EnsureRoot, RawOrigin}; -use sp_core::{Get, H256, U256, ConstU32}; +use sp_core::{ConstU32, Get, H256, U256}; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, - DispatchResult, BuildStorage, + BuildStorage, DispatchResult, }; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; @@ -67,21 +67,21 @@ impl pallet_balances::Config for Test { type WeightInfo = (); type MaxReserves = MaxReserves; type ReserveIdentifier = (); - type RuntimeHoldReason = (); - type FreezeIdentifier = (); - type MaxHolds = frame_support::traits::ConstU32<16>; + type RuntimeHoldReason = (); + type FreezeIdentifier = (); + type MaxHolds = frame_support::traits::ConstU32<16>; type MaxFreezes = frame_support::traits::ConstU32<16>; } impl system::Config for Test { type BaseCallFilter = Everything; - type Block = Block; + type Block = Block; type BlockWeights = (); type BlockLength = (); type DbWeight = (); type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; - type Nonce = u32; + type Nonce = u32; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = U256; @@ -102,12 +102,12 @@ impl system::Config for Test { impl pallet_subspace::Config for Test { type RuntimeEvent = RuntimeEvent; type Currency = Balances; - type WeightInfo = (); + type WeightInfo = (); } // Build genesis storage according to the mock runtime. #[allow(dead_code)] pub fn new_test_ext() -> sp_io::TestExternalities { sp_tracing::try_init_simple(); - >::default().build_storage().unwrap().into() + >::default().build_storage().unwrap().into() } diff --git a/pallets/subspace/src/module.rs b/pallets/subspace/src/module.rs index 1b6d3583a..b6b430bb3 100644 --- a/pallets/subspace/src/module.rs +++ b/pallets/subspace/src/module.rs @@ -1,20 +1,21 @@ use super::*; use frame_support::{ - pallet_prelude::{Decode, Encode, DispatchResult}, + pallet_prelude::{Decode, DispatchResult, Encode}, storage::IterableStorageMap, }; extern crate alloc; use alloc::vec::Vec; use codec::Compact; -use sp_std::vec; use sp_arithmetic::per_things::Percent; +use sp_std::vec; #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] pub struct ModuleStats { pub last_update: u64, pub registration_block: u64, - pub stake_from: Vec<(T::AccountId, u64)>, /* map of key to stake on this module/key * (includes delegations) */ + pub stake_from: Vec<(T::AccountId, u64)>, /* map of key to stake on this module/key * + * (includes delegations) */ pub emission: u64, pub incentive: u16, pub dividends: u16, @@ -27,10 +28,7 @@ pub struct ModuleInfo { stats: ModuleStats, } - impl Pallet { - - pub fn do_update_module( origin: T::RuntimeOrigin, netuid: u16, @@ -45,21 +43,24 @@ impl Pallet { Ok(()) } - - pub fn check_module_params(netuid: u16, params: ModuleParams) -> DispatchResult { - // if len(name) > 0, then we update the name. assert!(params.name.len() > 0); - ensure!(params.name.len() <= MaxNameLength::::get() as usize, Error::::ModuleNameTooLong); + ensure!( + params.name.len() <= MaxNameLength::::get() as usize, + Error::::ModuleNameTooLong + ); assert!(params.address.len() > 0); - ensure!(params.address.len() <= MaxNameLength::::get() as usize, Error::::ModuleAddressTooLong); + ensure!( + params.address.len() <= MaxNameLength::::get() as usize, + Error::::ModuleAddressTooLong + ); // delegation fee is a percent Ok(()) } - pub fn module_params(netuid: u16, uid:u16) -> ModuleParams { - let module_params : ModuleParams = ModuleParams { + pub fn module_params(netuid: u16, uid: u16) -> ModuleParams { + let module_params: ModuleParams = ModuleParams { name: Self::get_module_name(netuid, uid), address: Self::get_module_address(netuid, uid), delegation_fee: Self::get_module_delegation_fee(netuid, uid), @@ -71,26 +72,24 @@ impl Pallet { pub fn set_module_params(netuid: u16, uid: u16, module_params: ModuleParams) { Self::set_module_name(netuid, uid, module_params.name); Self::set_module_address(netuid, uid, module_params.address); - Self::set_module_delegation_fee( netuid, uid, module_params.delegation_fee); + Self::set_module_delegation_fee(netuid, uid, module_params.delegation_fee); } - pub fn get_module_address(netuid: u16, uid: u16) -> Vec { return Address::::get(netuid, uid) } - - pub fn set_module_address( netuid: u16, uid: u16, address: Vec) { + pub fn set_module_address(netuid: u16, uid: u16, address: Vec) { Address::::insert(netuid, uid, address); } - + pub fn get_module_delegation_fee(netuid: u16, uid: u16) -> Percent { let key = Self::get_key_for_uid(netuid, uid); let mut delegation_fee: Percent = DelegationFee::::get(netuid, key); return delegation_fee } - pub fn set_module_delegation_fee( netuid: u16, uid: u16, delegation_fee: Percent) { + pub fn set_module_delegation_fee(netuid: u16, uid: u16, delegation_fee: Percent) { let key = Self::get_key_for_uid(netuid, uid); DelegationFee::::insert(netuid, key, delegation_fee); } @@ -99,7 +98,7 @@ impl Pallet { return Name::::get(netuid, uid) } - pub fn set_module_name( netuid: u16, uid: u16, name: Vec) { + pub fn set_module_name(netuid: u16, uid: u16, name: Vec) { Name::::insert(netuid, uid, name.clone()); } @@ -159,7 +158,11 @@ impl Pallet { Weights::::remove(netuid, replace_uid); // Make uid - key association. // HANDLE THE REGISTRATION BLOCK - RegistrationBlock::::insert(netuid,uid,RegistrationBlock::::get(netuid, replace_uid),); // Fill block at registration. + RegistrationBlock::::insert( + netuid, + uid, + RegistrationBlock::::get(netuid, replace_uid), + ); // Fill block at registration. RegistrationBlock::::remove(netuid, replace_uid); // Fill block at registration. // HANDLE THE ADDRESS @@ -171,7 +174,11 @@ impl Pallet { Name::::remove(netuid, replace_uid); // Fill module namespace. // HANDLE THE DELEGATION FEE - DelegationFee::::insert(netuid,replace_key.clone(),DelegationFee::::get(netuid, uid_key.clone())); // Make uid - key association. + DelegationFee::::insert( + netuid, + replace_key.clone(), + DelegationFee::::get(netuid, uid_key.clone()), + ); // Make uid - key association. DelegationFee::::remove(netuid, uid_key.clone()); // Make uid - key association. // 3. Remove the network if it is empty. @@ -184,14 +191,8 @@ impl Pallet { // remove stake from old key and add to new key Self::remove_stake_from_storage(netuid, &uid_key); - } - - - - - // Appends the uid to the network (without increasing stake). pub fn append_module(netuid: u16, key: &T::AccountId, name: Vec, address: Vec) -> u16 { // 1. Get the next uid. This is always equal to subnetwork_n. @@ -218,10 +219,9 @@ impl Pallet { ); // Make uid - key association. N::::insert(netuid, N::::get(netuid) + 1); // Decrease the number of modules in the network. - // increase the stake of the new key + // increase the stake of the new key Self::increase_stake(netuid, &key, &key, 0); - return uid } @@ -258,16 +258,15 @@ impl Pallet { let registration_block = Self::get_registration_block_for_uid(netuid, uid as u16); let module_stats = ModuleStats { - stake_from: stake_from, + stake_from, emission: emission.into(), incentive: incentive.into(), dividends: dividends.into(), last_update: last_update.into(), registration_block: registration_block.into(), - weights: weights, + weights, }; return module_stats } - } diff --git a/pallets/subspace/src/profit_share.rs b/pallets/subspace/src/profit_share.rs index caca280cc..ccd2fa104 100644 --- a/pallets/subspace/src/profit_share.rs +++ b/pallets/subspace/src/profit_share.rs @@ -1,103 +1,92 @@ - -use frame_support::{pallet_prelude::DispatchResult}; +use frame_support::pallet_prelude::DispatchResult; use substrate_fixed::types::{I110F18, I32F32, I64F64, I96F32}; use super::*; impl Pallet { - pub fn do_add_profit_shares( - origin: T::RuntimeOrigin, - keys: Vec, - shares: Vec - ) -> DispatchResult { - - let key = ensure_signed(origin)?; - - // needs to be registered as a network - ensure!( - Self::is_key_registered_on_any_network(&key), - Error::::NotRegistered - ); - assert!(keys.len() > 0); - assert!(keys.len() == shares.len()); // make sure the keys and shares are the same length - - // make sure the keys are unique and the shares are unique - - - let mut total_shares: u32 = shares.iter().map(|x| *x as u32).sum(); - assert!(total_shares > 0); - let mut normalized_shares_float: Vec = Vec::new(); - // normalize shares - let mut total_normalized_length: u32 = 0; - for share in shares.iter() { - - let normalized_share = (I64F64::from(*share) / I64F64::from(total_shares as u16))* I64F64::from(u16::MAX); - total_normalized_length = total_normalized_length + normalized_share.to_num::(); - normalized_shares_float.push(normalized_share ); - } - // make sure the normalized shares add up to the unit - // convert the normalized shares to u16 - let mut normalize_shares: Vec = normalized_shares_float.iter().map(|x| x.to_num::()).collect::>(); - - let mut total_normalized_shares: u16 = normalize_shares.iter().sum::(); - - // ensure the profit shares add up to the unit - if total_normalized_shares < u16::MAX { - let diff = u16::MAX - total_normalized_shares; - for i in 0..diff { - let idx = (i % normalize_shares.len() as u16) as usize; - normalize_shares[idx] = normalize_shares[idx] + 1; - } - total_normalized_shares = normalize_shares.iter().sum::(); - } - - assert!(total_normalized_shares == u16::MAX, "normalized shares {} vs {} do not add up to the unit", total_normalized_shares, u16::MAX); - - // check tssat the normalized shares add up to the unit - let total_normalized_shares: u16 = normalize_shares.iter().sum::(); - - // now send the normalized shares to the profit share pallet - let profit_share_tuples : Vec<(T::AccountId, u16)> = keys.iter().zip(normalize_shares.iter()).map(|(x, y)| (x.clone(), *y)).collect(); - - - - ProfitShares::::insert(&key, profit_share_tuples.clone()); - - assert!(ProfitShares::::get(&key).len() == profit_share_tuples.len(), "profit shares not added"); - - Ok(()) - - - } - - pub fn get_profit_share_emissions( - key: T::AccountId, - emission: u64, - ) -> Vec<(T::AccountId, u64)> { - - let profit_shares = ProfitShares::::get(&key); - let mut emission_shares: Vec<(T::AccountId, u64)> = Vec::new(); - for (share_key, share_ratio) in profit_shares.iter() { - let share_emission_float: I96F32 = I96F32::from(emission) * (I96F32::from(*share_ratio) / I96F32::from(u16::MAX)); - let share_emission: u64 = share_emission_float.to_num::(); - emission_shares.push((share_key.clone(), share_emission)); - } - - return emission_shares; - - - } - - - - - pub fn get_profit_shares( - key: T::AccountId, - ) -> Vec<(T::AccountId, u16)> { - return ProfitShares::::get(&key);; - - } - + pub fn do_add_profit_shares( + origin: T::RuntimeOrigin, + keys: Vec, + shares: Vec, + ) -> DispatchResult { + let key = ensure_signed(origin)?; + + // needs to be registered as a network + ensure!(Self::is_key_registered_on_any_network(&key), Error::::NotRegistered); + assert!(keys.len() > 0); + assert!(keys.len() == shares.len()); // make sure the keys and shares are the same length + + // make sure the keys are unique and the shares are unique + + let mut total_shares: u32 = shares.iter().map(|x| *x as u32).sum(); + assert!(total_shares > 0); + let mut normalized_shares_float: Vec = Vec::new(); + // normalize shares + let mut total_normalized_length: u32 = 0; + for share in shares.iter() { + let normalized_share = + (I64F64::from(*share) / I64F64::from(total_shares as u16)) * I64F64::from(u16::MAX); + total_normalized_length = total_normalized_length + normalized_share.to_num::(); + normalized_shares_float.push(normalized_share); + } + // make sure the normalized shares add up to the unit + // convert the normalized shares to u16 + let mut normalize_shares: Vec = + normalized_shares_float.iter().map(|x| x.to_num::()).collect::>(); + + let mut total_normalized_shares: u16 = normalize_shares.iter().sum::(); + + // ensure the profit shares add up to the unit + if total_normalized_shares < u16::MAX { + let diff = u16::MAX - total_normalized_shares; + for i in 0..diff { + let idx = (i % normalize_shares.len() as u16) as usize; + normalize_shares[idx] = normalize_shares[idx] + 1; + } + total_normalized_shares = normalize_shares.iter().sum::(); + } + + assert!( + total_normalized_shares == u16::MAX, + "normalized shares {} vs {} do not add up to the unit", + total_normalized_shares, + u16::MAX + ); + + // check tssat the normalized shares add up to the unit + let total_normalized_shares: u16 = normalize_shares.iter().sum::(); + + // now send the normalized shares to the profit share pallet + let profit_share_tuples: Vec<(T::AccountId, u16)> = + keys.iter().zip(normalize_shares.iter()).map(|(x, y)| (x.clone(), *y)).collect(); + + ProfitShares::::insert(&key, profit_share_tuples.clone()); + + assert!( + ProfitShares::::get(&key).len() == profit_share_tuples.len(), + "profit shares not added" + ); + + Ok(()) + } + + pub fn get_profit_share_emissions( + key: T::AccountId, + emission: u64, + ) -> Vec<(T::AccountId, u64)> { + let profit_shares = ProfitShares::::get(&key); + let mut emission_shares: Vec<(T::AccountId, u64)> = Vec::new(); + for (share_key, share_ratio) in profit_shares.iter() { + let share_emission_float: I96F32 = + I96F32::from(emission) * (I96F32::from(*share_ratio) / I96F32::from(u16::MAX)); + let share_emission: u64 = share_emission_float.to_num::(); + emission_shares.push((share_key.clone(), share_emission)); + } + + return emission_shares; + } + + pub fn get_profit_shares(key: T::AccountId) -> Vec<(T::AccountId, u16)> { + return ProfitShares::::get(&key); + } } - - diff --git a/pallets/subspace/src/staking.rs b/pallets/subspace/src/staking.rs index 6f59cc624..b4ad80fa4 100644 --- a/pallets/subspace/src/staking.rs +++ b/pallets/subspace/src/staking.rs @@ -25,8 +25,6 @@ impl Pallet { Ok(()) } - - pub fn do_transfer_multiple( origin: T::RuntimeOrigin, destinations: Vec, @@ -34,18 +32,22 @@ impl Pallet { ) -> dispatch::DispatchResult { let key = ensure_signed(origin.clone())?; let amounts_sum: u64 = amounts.iter().sum(); - ensure!(Self::has_enough_balance(&key, amounts_sum), Error::::NotEnoughBalanceToTransfer); + ensure!( + Self::has_enough_balance(&key, amounts_sum), + Error::::NotEnoughBalanceToTransfer + ); ensure!(amounts.len() == destinations.len(), Error::::DifferentLengths); for (i, m_key) in destinations.iter().enumerate() { - ensure!(Self::has_enough_balance(&key, amounts[i as usize]), Error::::NotEnoughBalanceToTransfer); + ensure!( + Self::has_enough_balance(&key, amounts[i as usize]), + Error::::NotEnoughBalanceToTransfer + ); Self::transfer_balance_to_account(&key, &m_key.clone(), amounts[i as usize]); } Ok(()) } - - pub fn do_remove_stake_multiple( origin: T::RuntimeOrigin, netuid: u16, @@ -75,7 +77,10 @@ impl Pallet { let key = ensure_signed(origin.clone())?; ensure!(Self::is_registered(netuid, &module_key.clone()), Error::::NotRegistered); ensure!(Self::is_registered(netuid, &new_module_key.clone()), Error::::NotRegistered); - ensure!(Self::has_enough_stake(netuid, &key, &module_key, amount),Error::::NotEnoughStaketoWithdraw); + ensure!( + Self::has_enough_stake(netuid, &key, &module_key, amount), + Error::::NotEnoughStaketoWithdraw + ); Self::do_remove_stake(origin.clone(), netuid, module_key.clone(), amount)?; Self::do_add_stake(origin.clone(), netuid, new_module_key.clone(), amount)?; @@ -103,17 +108,27 @@ impl Pallet { let stake_before_add: u64 = Self::get_stake_to_module(netuid, &key, &module_key.clone()); let balance_before_add: u64 = Self::get_balance_u64(&key); let module_stake_before_add: u64 = Self::get_stake_for_key(netuid, &module_key); - let removed_balance: bool = Self::remove_balance_from_account(&key, Self::u64_to_balance(amount).unwrap()); + let removed_balance: bool = + Self::remove_balance_from_account(&key, Self::u64_to_balance(amount).unwrap()); ensure!(removed_balance, Error::::BalanceNotRemoved); Self::increase_stake(netuid, &key, &module_key, amount); - + let stake_after_add: u64 = Self::get_stake_to_module(netuid, &key, &module_key.clone()); let balance_after_add: u64 = Self::get_balance_u64(&key); let module_stake_after_add = Self::get_stake_for_key(netuid, &module_key); - ensure!(stake_after_add == stake_before_add.saturating_add(amount), Error::::StakeNotAdded); - ensure!(balance_after_add == balance_before_add.saturating_sub(amount), Error::::BalanceNotRemoved); - ensure!(module_stake_after_add == module_stake_before_add.saturating_add(amount), Error::::StakeNotAdded); + ensure!( + stake_after_add == stake_before_add.saturating_add(amount), + Error::::StakeNotAdded + ); + ensure!( + balance_after_add == balance_before_add.saturating_sub(amount), + Error::::BalanceNotRemoved + ); + ensure!( + module_stake_after_add == module_stake_before_add.saturating_add(amount), + Error::::StakeNotAdded + ); // --- 5. Emit the staking event. Self::deposit_event(Event::StakeAdded(key, module_key, amount)); @@ -158,9 +173,18 @@ impl Pallet { let balance_after_remove: u64 = Self::get_balance_u64(&key); let module_stake_after_remove = Self::get_stake_for_key(netuid, &module_key); - ensure!(stake_after_remove == stake_before_remove.saturating_sub(amount), Error::::StakeNotRemoved); - ensure!(balance_after_remove == balance_before_remove.saturating_add(amount), Error::::BalanceNotAdded); - ensure!(module_stake_after_remove == module_stake_before_remove.saturating_sub(amount), Error::::StakeNotRemoved); + ensure!( + stake_after_remove == stake_before_remove.saturating_sub(amount), + Error::::StakeNotRemoved + ); + ensure!( + balance_after_remove == balance_before_remove.saturating_add(amount), + Error::::BalanceNotAdded + ); + ensure!( + module_stake_after_remove == module_stake_before_remove.saturating_sub(amount), + Error::::StakeNotRemoved + ); Self::deposit_event(Event::StakeRemoved(key, module_key, amount)); @@ -189,12 +213,10 @@ impl Pallet { return Stake::::get(netuid, key) } - - pub fn get_stakes(netuid: u16) -> Vec { let n = Self::get_subnet_n(netuid); - let mut stakes : Vec = Vec::new(); - let mut uid_key_tuples: Vec<(u16, T::AccountId)> = Self::get_uid_key_tuples(netuid); + let mut stakes: Vec = Vec::new(); + let mut uid_key_tuples: Vec<(u16, T::AccountId)> = Self::get_uid_key_tuples(netuid); for (uid, key) in uid_key_tuples { let stake: u64 = Self::get_stake(netuid, &key); stakes.push(stake); @@ -207,7 +229,6 @@ impl Pallet { return Uids::::contains_key(netuid, &key) } - // Returns the delegation fee of a module pub fn get_delegation_fee(netuid: u16, module_key: &T::AccountId) -> Percent { DelegationFee::::get(netuid, module_key) @@ -232,7 +253,7 @@ impl Pallet { let mut total_stake_to: u64 = 0; for (k, v) in Self::get_stake_to_vector(netuid, key) { total_stake_to += v; - } + } return total_stake_to } @@ -528,7 +549,6 @@ impl Pallet { return total_balance; } - pub fn has_enough_balance(key: &T::AccountId, amount: u64) -> bool { return Self::get_balance_u64(key) > amount || amount == 0 } @@ -585,5 +605,4 @@ impl Pallet { // least_staked_module_uid return Self::get_uid_for_key(netuid, &Self::least_staked_module_key(netuid)) } - } diff --git a/pallets/subspace/src/step.rs b/pallets/subspace/src/step.rs index 3ec6b3a2c..1b457c8b0 100644 --- a/pallets/subspace/src/step.rs +++ b/pallets/subspace/src/step.rs @@ -1,10 +1,8 @@ use super::*; use crate::math::*; -use frame_support::{ - storage::{IterableStorageDoubleMap, IterableStorageMap}, -}; -use substrate_fixed::types::{I110F18, I32F32, I64F64, I96F32}; +use frame_support::storage::{IterableStorageDoubleMap, IterableStorageMap}; use sp_std::vec; +use substrate_fixed::types::{I110F18, I32F32, I64F64, I96F32}; impl Pallet { pub fn block_step() { @@ -12,7 +10,6 @@ impl Pallet { RegistrationsPerBlock::::mutate(|val| *val = 0); log::debug!("block_step for block: {:?} ", block_number); for (netuid, tempo) in as IterableStorageMap>::iter() { - let new_queued_emission: u64 = Self::calculate_network_emission(netuid); PendingEmission::::mutate(netuid, |mut queued| *queued += new_queued_emission); log::debug!("netuid_i: {:?} queued_emission: +{:?} ", netuid, new_queued_emission); @@ -25,7 +22,6 @@ impl Pallet { } } - pub fn epoch(netuid: u16, mut token_emission: u64) { // Get subnetwork size. @@ -37,20 +33,22 @@ impl Pallet { let block_at_registration: Vec = Self::get_block_at_registration(netuid); if n == 0 { - // + // return } - - // FOUNDER DIVIDENDS + + // FOUNDER DIVIDENDS let founder_key = Self::get_founder(netuid); let is_founder_registered = Self::key_registered(netuid, &founder_key); let founder_uid = u16::MAX; let mut founder_emission: u64 = 0; if is_founder_registered { - let founder_share : u16 = Self::get_founder_share(netuid); + let founder_share: u16 = Self::get_founder_share(netuid); if founder_share > 0 { - let founder_emission_ratio: I64F64 = I64F64::from_num(founder_share.min(100))/I64F64::from_num(100); - founder_emission = (founder_emission_ratio * I64F64::from_num(token_emission)).to_num::(); + let founder_emission_ratio: I64F64 = + I64F64::from_num(founder_share.min(100)) / I64F64::from_num(100); + founder_emission = + (founder_emission_ratio * I64F64::from_num(token_emission)).to_num::(); token_emission = token_emission.saturating_sub(founder_emission); } } @@ -61,7 +59,7 @@ impl Pallet { let mut uid_key_tuples: Vec<(u16, T::AccountId)> = Self::get_uid_key_tuples(netuid); let mut stake_64: Vec = vec![I64F64::from_num(0.0); n as usize]; - let mut total_stake_u64: u64 =Self::get_total_subnet_stake(netuid).clone(); + let mut total_stake_u64: u64 = Self::get_total_subnet_stake(netuid).clone(); if total_stake_u64 == 0 { total_stake_u64 = 1; } @@ -73,8 +71,12 @@ impl Pallet { .map(|(_, key)| Self::get_stake_for_key(netuid, key).min(max_stake)) .collect(); // clip it to the max stake - let mut stake_f64: Vec = stake_u64.iter().map(|x| I64F64::from_num(x.clone()) /I64F64::from_num(total_stake_u64)).collect(); - let mut stake : Vec = stake_f64.iter().map(|x| I32F32::from_num(x.clone())).collect(); + let mut stake_f64: Vec = stake_u64 + .iter() + .map(|x| I64F64::from_num(x.clone()) / I64F64::from_num(total_stake_u64)) + .collect(); + let mut stake: Vec = + stake_f64.iter().map(|x| I32F32::from_num(x.clone())).collect(); // Normalize active stake. inplace_normalize(&mut stake); @@ -88,11 +90,10 @@ impl Pallet { let min_weight_stake_f64: I64F64 = I64F64::from_num(global_params.min_weight_stake); - for (uid_i, mut weights_i) in as IterableStorageDoubleMap>>::iter_prefix(netuid) { - let mut weight_changed : bool = false; + let mut weight_changed: bool = false; // watchout for the overflow let weight_age: u64 = current_block.saturating_sub(last_update_vector[uid_i as usize]); @@ -105,7 +106,8 @@ impl Pallet { if (pos as u16) <= subnet_params.max_allowed_weights && *uid_j < n { // okay , we passed the positioonal check, now check the weight let weight_f64 = I64F64::from_num(*weight_ij) / I64F64::from_num(u16::MAX); - let weight_stake = (stake_f64[uid_i as usize] * weight_f64) * I64F64::from_num(total_stake_u64); + let weight_stake = (stake_f64[uid_i as usize] * weight_f64) * + I64F64::from_num(total_stake_u64); if weight_stake > min_weight_stake_f64 { weights[uid_i as usize].push((*uid_j, *weight_ij)); } else { @@ -114,26 +116,27 @@ impl Pallet { } else { weight_changed = true; } + } } - } - - if weight_changed { // update the weights if it was changed >::insert(netuid, uid_i, weights[uid_i as usize].clone()); } } - - let mut weights : Vec> = weights.iter().map(|x| x.iter().map(|(uid, weight)| (*uid, u16_proportion_to_fixed(*weight))).collect()).collect(); - + let mut weights: Vec> = weights + .iter() + .map(|x| { + x.iter().map(|(uid, weight)| (*uid, u16_proportion_to_fixed(*weight))).collect() + }) + .collect(); // enabling self voting (if enabled) if (!subnet_params.self_vote) { weights = mask_diag_sparse(&weights); } - + // Normalize remaining weights. inplace_row_normalize_sparse(&mut weights); @@ -167,32 +170,36 @@ impl Pallet { // trust that acts as a multiplier for the incentive let trust_ratio: u16 = Self::get_trust_ratio(netuid); if trust_ratio > 0 { - - let trust_share : I32F32 = I32F32::from_num(trust_ratio)/I32F32::from_num(100); - let incentive_share : I32F32 = I32F32::from_num(1.0).saturating_sub(trust_share); + let trust_share: I32F32 = I32F32::from_num(trust_ratio) / I32F32::from_num(100); + let incentive_share: I32F32 = I32F32::from_num(1.0).saturating_sub(trust_share); let mut trust: Vec = vec![I32F32::from_num(0.0); n as usize]; - for (i, weights_i) in weights.iter().enumerate() { for (j, weight_ij) in weights_i.iter() { // Compute trust scores: t_j = SUM(i) w_ij * s_i // result_j = SUM(i) vector_i * matrix_ij - if *weight_ij > 0 && - stake[i] > I32F32::from_num(subnet_params.min_stake) { + if *weight_ij > 0 && stake[i] > I32F32::from_num(subnet_params.min_stake) { trust[*j as usize] += I32F32::from_num(1.0); } } } inplace_normalize(&mut trust); - incentive = incentive.iter().zip(trust.iter()).map(|(inc, tru)| (inc * incentive_share) + (tru * trust_share)).collect(); + incentive = incentive + .iter() + .zip(trust.iter()) + .map(|(inc, tru)| (inc * incentive_share) + (tru * trust_share)) + .collect(); // save the trust into the trust vector - Trust::::insert(netuid, trust.iter().map(|xi| fixed_proportion_to_u16(*xi)).collect::>()); + Trust::::insert( + netuid, + trust.iter().map(|xi| fixed_proportion_to_u16(*xi)).collect::>(), + ); } - // store the incentive - let cloned_incentive: Vec = incentive.iter().map(|xi| fixed_proportion_to_u16(*xi)).collect::>(); + let cloned_incentive: Vec = + incentive.iter().map(|xi| fixed_proportion_to_u16(*xi)).collect::>(); Incentive::::insert(netuid, cloned_incentive); // ================================= @@ -237,24 +244,30 @@ impl Pallet { } inplace_normalize(&mut dividends); - let cloned_dividends: Vec = dividends.iter().map(|xi| fixed_proportion_to_u16(*xi)).collect::>(); + let cloned_dividends: Vec = + dividends.iter().map(|xi| fixed_proportion_to_u16(*xi)).collect::>(); Dividends::::insert(netuid, cloned_dividends); // ================================= // == Emission== // ================================= - let mut incentive_ratio: I64F64 = I64F64::from_num(Self::get_incentive_ratio(netuid) as u64) / I64F64::from_num(100); + let mut incentive_ratio: I64F64 = + I64F64::from_num(Self::get_incentive_ratio(netuid) as u64) / I64F64::from_num(100); let dividend_ratio: I64F64 = I64F64::from_num(1.0) - incentive_ratio; let incentive_emission_float: Vec = incentive .clone() .iter() - .map(|x| I64F64::from_num(x.clone()) * I64F64::from_num(token_emission) * incentive_ratio) + .map(|x| { + I64F64::from_num(x.clone()) * I64F64::from_num(token_emission) * incentive_ratio + }) .collect(); let dividends_emission_float: Vec = dividends .clone() .iter() - .map(|x| I64F64::from_num(x.clone()) * I64F64::from_num(token_emission) * dividend_ratio) + .map(|x| { + I64F64::from_num(x.clone()) * I64F64::from_num(token_emission) * dividend_ratio + }) .collect(); let mut incentive_emission: Vec = @@ -266,68 +279,72 @@ impl Pallet { if is_founder_registered { let founder_uid = Self::get_uid_for_key(netuid, &founder_key); - incentive_emission[founder_uid as usize] = incentive_emission[founder_uid as usize].saturating_add(founder_emission); + incentive_emission[founder_uid as usize] = + incentive_emission[founder_uid as usize].saturating_add(founder_emission); } - // burn the amount + // burn the amount + let mut emission: Vec = vec![0; n as usize]; - let mut emission : Vec = vec![0; n as usize]; - // Emission tuples ( uid_key_tuples, u64 emission) let mut founder_share_added: bool = false; // avoid double counting the founder share for (module_uid, module_key) in uid_key_tuples.iter() { - let mut owner_emission_incentive: u64 = incentive_emission[*module_uid as usize]; let mut owner_dividends_emission: u64 = dividends_emission[*module_uid as usize]; let mut owner_emission: u64 = owner_emission_incentive + owner_dividends_emission; - + // if the owner emission is less than the burn amount if burn_amount_per_epoch > owner_emission { let burn_into_stake: u64 = burn_amount_per_epoch.saturating_sub(owner_emission); - + // decrease the stake if there is remainder if burn_into_stake > 0 { Self::decrease_stake(netuid, module_key, module_key, burn_into_stake); - } + } owner_emission_incentive = 0; - owner_dividends_emission = 0; - // skip the rest of the loop + owner_dividends_emission = 0; + // skip the rest of the loop } else { // eat into incentive first and then into the incentive if burn_amount_per_epoch > owner_emission_incentive { - owner_emission_incentive = 0; + owner_emission_incentive = 0; // correct the burn amount - let left_burn_amount_per_epoch = burn_amount_per_epoch.saturating_sub(owner_emission_incentive); + let left_burn_amount_per_epoch = + burn_amount_per_epoch.saturating_sub(owner_emission_incentive); // apply the burn to the incentive - owner_dividends_emission = owner_dividends_emission.saturating_sub(left_burn_amount_per_epoch); - + owner_dividends_emission = + owner_dividends_emission.saturating_sub(left_burn_amount_per_epoch); } else { // apply the burn to the emission only - owner_emission_incentive = owner_emission_incentive.saturating_sub(burn_amount_per_epoch); + owner_emission_incentive = + owner_emission_incentive.saturating_sub(burn_amount_per_epoch); } - emission[*module_uid as usize] = owner_emission_incentive + owner_dividends_emission; - + emission[*module_uid as usize] = + owner_emission_incentive + owner_dividends_emission; + if owner_dividends_emission > 0 { // get the ownership emission for this key - let ownership_vector: Vec<(T::AccountId, I64F64)> = Self::get_ownership_ratios(netuid, module_key); - + let ownership_vector: Vec<(T::AccountId, I64F64)> = + Self::get_ownership_ratios(netuid, module_key); + let delegation_fee = Self::get_delegation_fee(netuid, module_key); - + // add the ownership for (delegate_key, delegate_ratio) in ownership_vector.iter() { - if delegate_key == module_key { continue } - let mut dividends_from_delegate : u64 = (I64F64::from_num(owner_dividends_emission) * delegate_ratio).to_num::(); + let mut dividends_from_delegate: u64 = + (I64F64::from_num(owner_dividends_emission) * delegate_ratio) + .to_num::(); let to_module: u64 = delegation_fee.mul_floor(dividends_from_delegate); let to_delegate: u64 = dividends_from_delegate.saturating_sub(to_module); Self::increase_stake(netuid, delegate_key, module_key, to_delegate); - owner_dividends_emission = owner_dividends_emission.saturating_sub(to_delegate); - + owner_dividends_emission = + owner_dividends_emission.saturating_sub(to_delegate); } } @@ -335,26 +352,32 @@ impl Pallet { // add the emisssion and rm the burn amount if owner_emission > 0 { // generate the profit shares - let profit_share_emissions: Vec<(T::AccountId, u64)> = Self::get_profit_share_emissions(module_key.clone(), owner_emission); + let profit_share_emissions: Vec<(T::AccountId, u64)> = + Self::get_profit_share_emissions(module_key.clone(), owner_emission); // if there are profit shares, then increase the balance of the profit share key if profit_share_emissions.len() > 0 { - // if there are profit shares, then increase the balance of the profit share key - for (profit_share_key, profit_share_emission) in profit_share_emissions.iter() { + // if there are profit shares, then increase the balance of the profit share + // key + for (profit_share_key, profit_share_emission) in + profit_share_emissions.iter() + { // increase the balance of the profit share key - Self::increase_stake(netuid, profit_share_key, module_key, *profit_share_emission); + Self::increase_stake( + netuid, + profit_share_key, + module_key, + *profit_share_emission, + ); } } else { // increase it to the module key Self::increase_stake(netuid, module_key, module_key, owner_emission); } } - } } - - Emission::::insert(netuid, emission.clone()); } @@ -370,7 +393,6 @@ impl Pallet { block_at_registration } - pub fn blocks_until_next_epoch(netuid: u16, tempo: u16, block_number: u64) -> u64 { if tempo == 0 { return 0 @@ -427,21 +449,17 @@ impl Pallet { return emission_vector } - - pub fn get_burn_per_epoch(netuid:u16) -> u64{ - + pub fn get_burn_per_epoch(netuid: u16) -> u64 { let n = Self::get_subnet_n(netuid); let token_emission: u64 = PendingEmission::::get(netuid).clone(); let burn_rate: u16 = Self::get_burn_rate().min(100); - let mut burn_amount_per_epoch : u64 = 0; + let mut burn_amount_per_epoch: u64 = 0; // get the float and convert to u64token_emission if burn_rate > 0 { - let burn_rate_float : I64F64 = (I64F64::from_num(burn_rate)/I64F64::from_num(100)) * (I64F64::from_num(token_emission) / I64F64::from_num(n)); + let burn_rate_float: I64F64 = (I64F64::from_num(burn_rate) / I64F64::from_num(100)) * + (I64F64::from_num(token_emission) / I64F64::from_num(n)); burn_amount_per_epoch = burn_rate_float.to_num::(); } return burn_amount_per_epoch - } - - } diff --git a/pallets/subspace/src/subnet.rs b/pallets/subspace/src/subnet.rs index fe1713283..5990d24ff 100644 --- a/pallets/subspace/src/subnet.rs +++ b/pallets/subspace/src/subnet.rs @@ -1,4 +1,5 @@ use super::*; +use crate::utils::is_vec_str; use codec::Compact; use frame_support::{ pallet_prelude::{Decode, DispatchError, DispatchResult, Encode}, @@ -6,7 +7,6 @@ use frame_support::{ traits::Currency, IterableStorageDoubleMap, }; -use crate::utils::is_vec_str; use frame_system::ensure_root; pub use sp_std::{vec, vec::Vec}; use substrate_fixed::types::{I32F32, I64F64}; @@ -16,7 +16,6 @@ impl Pallet { // Returns true if the subnetwork exists. // - pub fn do_remote_subnet(origin: T::RuntimeOrigin, netuid: u16) -> DispatchResult { let key = ensure_signed(origin)?; // --- 1. Ensure the network name does not already exist. @@ -36,8 +35,11 @@ impl Pallet { ) -> DispatchResult { let key = ensure_signed(origin)?; // only the founder can update the network on authority mode - - ensure!(is_vec_str(Self::get_vote_mode_subnet(netuid), "authority"), Error::::NotAuthorityMode); + + ensure!( + is_vec_str(Self::get_vote_mode_subnet(netuid), "authority"), + Error::::NotAuthorityMode + ); ensure!(Self::if_subnet_netuid_exists(netuid), Error::::SubnetNameAlreadyExists); ensure!(Self::is_subnet_founder(netuid, &key), Error::::NotFounder); ensure!(Self::if_subnet_netuid_exists(netuid), Error::::SubnetNameAlreadyExists); @@ -56,18 +58,23 @@ impl Pallet { return idx } - - pub fn check_subnet_params(params: SubnetParams) -> DispatchResult{ - // checks if params are valid + pub fn check_subnet_params(params: SubnetParams) -> DispatchResult { + // checks if params are valid let global_params = Self::global_params(); - // check valid tempo - ensure!(params.min_allowed_weights <= params.max_allowed_weights, Error::::InvalidMinAllowedWeights); - + // check valid tempo + ensure!( + params.min_allowed_weights <= params.max_allowed_weights, + Error::::InvalidMinAllowedWeights + ); + ensure!(params.min_allowed_weights >= 1, Error::::InvalidMinAllowedWeights); - ensure!(params.max_allowed_weights <= global_params.max_allowed_weights, Error::::InvalidMaxAllowedWeights); + ensure!( + params.max_allowed_weights <= global_params.max_allowed_weights, + Error::::InvalidMaxAllowedWeights + ); // the global params must be larger than the global min_stake ensure!(params.min_stake >= global_params.min_stake, Error::::InvalidMinStake); @@ -76,21 +83,19 @@ impl Pallet { ensure!(params.tempo > 0, Error::::InvalidTempo); - ensure!(params.max_weight_age > params.tempo as u64, Error::::InvalidMaxWeightAge); - + ensure!(params.max_weight_age > params.tempo as u64, Error::::InvalidMaxWeightAge); + // ensure the trust_ratio is between 0 and 100 ensure!(params.trust_ratio <= 100, Error::::InvalidTrustRatio); // ensure the vode_mode is in "authority", "stake" ensure!( - is_vec_str(params.vote_mode.clone(),"authority") || - is_vec_str(params.vote_mode.clone(),"stake"), - Error::::InvalidVoteMode); - Ok(()) - - - } - + is_vec_str(params.vote_mode.clone(), "authority") || + is_vec_str(params.vote_mode.clone(), "stake"), + Error::::InvalidVoteMode + ); + Ok(()) + } pub fn subnet_params(netuid: u16) -> SubnetParams { SubnetParams { @@ -104,21 +109,16 @@ impl Pallet { tempo: Tempo::::get(netuid), name: >::new(), vote_threshold: VoteThresholdSubnet::::get(netuid), - vote_mode:VoteModeSubnet::::get(netuid), + vote_mode: VoteModeSubnet::::get(netuid), trust_ratio: TrustRatio::::get(netuid), self_vote: SelfVote::::get(netuid), founder_share: FounderShare::::get(netuid), incentive_ratio: IncentiveRatio::::get(netuid), founder: Founder::::get(netuid), - } } - - - pub fn set_subnet_params(netuid: u16, mut params: SubnetParams) { - // TEMPO, IMMUNITY_PERIOD, MIN_ALLOWED_WEIGHTS, MAX_ALLOWED_WEIGHTS, MAX_ALLOWED_UIDS, // MAX_IMMUNITY_RATIO Self::set_founder(netuid, params.founder); @@ -136,12 +136,9 @@ impl Pallet { Self::set_vote_threshold_subnet(netuid, params.vote_threshold); Self::set_vote_mode_subnet(netuid, params.vote_mode); Self::set_self_vote(netuid, params.self_vote); - Self::set_incentive_ratio(netuid, params.incentive_ratio); - - + Self::set_incentive_ratio(netuid, params.incentive_ratio); } - pub fn if_subnet_exist(netuid: u16) -> bool { return N::::contains_key(netuid) } @@ -159,7 +156,6 @@ impl Pallet { MinStake::::insert(netuid, stake) } - pub fn get_max_stake(netuid: u16) -> u64 { return MaxStake::::get(netuid) } @@ -181,23 +177,26 @@ impl Pallet { return min_stake_netuid } - pub fn address_vector(netuid: u16) -> Vec>{ + pub fn address_vector(netuid: u16) -> Vec> { let mut addresses: Vec> = Vec::new(); - for (uid, address) in as IterableStorageDoubleMap>>::iter_prefix(netuid) { + for (uid, address) in + as IterableStorageDoubleMap>>::iter_prefix(netuid) + { addresses.push(address); } return addresses } - pub fn name_vector(netuid: u16) -> Vec>{ + pub fn name_vector(netuid: u16) -> Vec> { let mut names: Vec> = Vec::new(); - for (uid, name) in as IterableStorageDoubleMap>>::iter_prefix(netuid) { + for (uid, name) in + as IterableStorageDoubleMap>>::iter_prefix(netuid) + { names.push(name); } return names } - // get the least staked network pub fn min_subnet_stake() -> u64 { let mut min_stake: u64 = u64::MAX; @@ -213,9 +212,6 @@ impl Pallet { return TotalStake::::get(netuid) } - - - pub fn set_max_allowed_uids(netuid: u16, mut max_allowed_uids: u16) { let n: u16 = Self::get_subnet_n(netuid); if max_allowed_uids < n { @@ -223,35 +219,28 @@ impl Pallet { let mut remainder_n: u16 = (n - max_allowed_uids); let max_remainder = 256; - if remainder_n > max_remainder { + if remainder_n > max_remainder { // remove the modules in small amounts, as this can be a heavy load on the chain remainder_n = max_remainder; max_allowed_uids = n - remainder_n; } // remove the modules by adding the to the deregister queue for i in 0..remainder_n { - let next_uid: u16= n - 1 - i; + let next_uid: u16 = n - 1 - i; Self::remove_module(netuid, next_uid); } } MaxAllowedUids::::insert(netuid, max_allowed_uids); - - } - - pub fn set_name_subnet(netuid: u16, name: Vec) { // set the name if it doesnt exist - if !Self::subnet_name_exists(name.clone()) - { + if !Self::subnet_name_exists(name.clone()) { SubnetNames::::insert(netuid, name.clone()); } } - - pub fn uid_in_immunity(netuid: u16, uid: u16) -> bool { let block_at_registration: u64 = Self::get_module_registration_block(netuid, uid); let immunity_period: u64 = Self::get_immunity_period(netuid) as u64; @@ -260,7 +249,7 @@ impl Pallet { } pub fn default_subnet_params() -> SubnetParams { - // get an invalid + // get an invalid let default_netuid: u16 = Self::num_subnets() + 1; return Self::subnet_params(default_netuid) } @@ -286,7 +275,6 @@ impl Pallet { return Founder::::get(netuid) == *key } - pub fn get_subnet_founder(netuid: u16) -> T::AccountId { return Founder::::get(netuid) } @@ -299,7 +287,6 @@ impl Pallet { return SelfVote::::get(netuid) } - // pub fn total_balance() -> u64 { // let mut total_balance: u64 = 0; // // iterate through all of the accounts with balance (noo stake) @@ -323,7 +310,6 @@ impl Pallet { UnitEmission::::put(unit_emission) } - pub fn get_unit_emission_per_block() -> u64 { return UnitEmission::::get() * 4 } @@ -384,7 +370,6 @@ impl Pallet { } pub fn add_subnet(params: SubnetParams) -> u16 { - // --- 1. Enfnsure that the network name does not already exist. let total_networks: u16 = TotalSubnets::::get(); let max_networks = MaxAllowedSubnets::::get(); @@ -400,7 +385,7 @@ impl Pallet { return netuid } - + // Initializes a new subnetwork under netuid with parameters. // pub fn subnet_name_exists(name: Vec) -> bool { @@ -476,7 +461,6 @@ impl Pallet { LastUpdate::::remove(netuid); DelegationFee::::clear_prefix(netuid, u32::max_value(), None); RegistrationBlock::::clear_prefix(netuid, u32::max_value(), None); - // --- 2. Erase subnet parameters. Founder::::remove(netuid); @@ -534,10 +518,7 @@ impl Pallet { return Uids::::contains_key(netuid, key) } - - - - pub fn is_key_registered_on_any_network( key: &T::AccountId) -> bool { + pub fn is_key_registered_on_any_network(key: &T::AccountId) -> bool { for netuid in Self::netuids() { if Uids::::contains_key(netuid, key) { return true @@ -558,7 +539,6 @@ impl Pallet { return Uids::::get(netuid, key).unwrap_or(0) } - pub fn get_trust_ratio(netuid: u16) -> u16 { return TrustRatio::::get(netuid) } @@ -567,7 +547,6 @@ impl Pallet { TrustRatio::::insert(netuid, trust_ratio); } - pub fn get_quadradic_voting(netuid: u16) -> bool { return QuadraticVoting::::get(netuid) } @@ -595,7 +574,7 @@ impl Pallet { pub fn set_vote_mode_subnet(netuid: u16, vote_mode: Vec) { VoteModeSubnet::::insert(netuid, vote_mode); } - + pub fn get_subnet_vote_threshold(netuid: u16) -> u16 { return VoteThresholdSubnet::::get(netuid) } @@ -615,15 +594,14 @@ impl Pallet { } pub fn netuids() -> Vec { - - let mut netuids : Vec = Vec::new(); + let mut netuids: Vec = Vec::new(); for (netuid, _net_n) in as IterableStorageMap>::iter() { netuids.push(netuid); } return netuids } - pub fn random_netuid() -> u16{ + pub fn random_netuid() -> u16 { // get the number of subnets let netuids = Self::netuids(); // get a random number between 0 and number_of_subnets @@ -653,7 +631,7 @@ impl Pallet { pub fn get_registration_block_for_uid(netuid: u16, uid: u16) -> u64 { return RegistrationBlock::::get(netuid, uid) } - + pub fn get_incentive_ratio(netuid: u16) -> u16 { return IncentiveRatio::::get(netuid).min(100) } @@ -669,7 +647,6 @@ impl Pallet { Founder::::insert(netuid, founder); } - pub fn get_burn_emission_per_epoch(netuid: u16) -> u64 { let burn_rate: u16 = BurnRate::::get(); let epoch_emission: u64 = Self::get_subnet_emission(netuid); @@ -678,8 +655,9 @@ impl Pallet { if n == 0 { return 0 } - let burn_rate_float : I64F64 = I64F64::from_num(burn_rate) / I64F64::from_num(n * 100); - let burn_emission_per_epoch: u64 = (I64F64::from_num(epoch_emission) * burn_rate_float).to_num::(); + let burn_rate_float: I64F64 = I64F64::from_num(burn_rate) / I64F64::from_num(n * 100); + let burn_emission_per_epoch: u64 = + (I64F64::from_num(epoch_emission) * burn_rate_float).to_num::(); return burn_emission_per_epoch } @@ -797,7 +775,10 @@ impl Pallet { } pub fn set_max_allowed_weights(netuid: u16, mut max_allowed_weights: u16) { let global_params = Self::global_params(); - MaxAllowedWeights::::insert(netuid, max_allowed_weights.min(global_params.max_allowed_weights)); + MaxAllowedWeights::::insert( + netuid, + max_allowed_weights.min(global_params.max_allowed_weights), + ); } pub fn get_max_allowed_uids(netuid: u16) -> u16 { @@ -840,7 +821,7 @@ impl Pallet { pub fn get_uid_key_tuples(netuid: u16) -> Vec<(u16, T::AccountId)> { let n = Self::get_subnet_n(netuid); let mut uid_key_tuples = Vec::<(u16, T::AccountId)>::new(); - for uid in 0..n{ + for uid in 0..n { let key = Self::get_key_for_uid(netuid, uid); uid_key_tuples.push((uid, key)); } @@ -871,7 +852,6 @@ impl Pallet { return Self::check_subnet_storage(netuid) } - pub fn check_subnet_storage(netuid: u16) -> bool { let n = Self::get_subnet_n(netuid); let mut uids = Self::get_uids(netuid); @@ -882,7 +862,6 @@ impl Pallet { let mut incentives = Self::get_incentives(netuid); let mut dividends = Self::get_dividends(netuid); let mut last_update = Self::get_last_update(netuid); - if (n as usize) != uids.len() { return false @@ -920,7 +899,7 @@ impl Pallet { if (n as usize) != address_vector.len() { return false } - + return true } @@ -951,7 +930,6 @@ impl Pallet { return Keys::::contains_key(netuid, uid) } - pub fn get_max_weight_age(netuid: u16) -> u64 { return MaxWeightAge::::get(netuid) } @@ -959,5 +937,4 @@ impl Pallet { pub fn set_max_weight_age(netuid: u16, max_weight_age: u64) { MaxWeightAge::::insert(netuid, max_weight_age); } - } diff --git a/pallets/subspace/src/utils.rs b/pallets/subspace/src/utils.rs index 72e4e67db..f09c1b49e 100644 --- a/pallets/subspace/src/utils.rs +++ b/pallets/subspace/src/utils.rs @@ -1,30 +1,30 @@ +use sp_std::{vec, vec::Vec}; use substrate_fixed::{ transcendental::exp, types::{I32F32, I64F64}, }; -use sp_std::{vec, vec::Vec}; #[allow(dead_code)] fn string2vec(s: &str) -> Vec { - let mut v: Vec = Vec::new(); - for c in s.chars() { - v.push(c as u8); - } - return v; + let mut v: Vec = Vec::new(); + for c in s.chars() { + v.push(c as u8); + } + return v; } #[allow(dead_code)] pub fn is_string_equal(s1: &str, s2: &str) -> bool { - let v1: Vec = string2vec(s1); - let v2: Vec = string2vec(s2); - return v1 == v2; + let v1: Vec = string2vec(s1); + let v2: Vec = string2vec(s2); + return v1 == v2; } #[allow(dead_code)] pub fn is_string_vec(s1: &str, v2: Vec) -> bool { - let v1: Vec = string2vec(s1); - return v1 == v2.clone(); + let v1: Vec = string2vec(s1); + return v1 == v2.clone(); } #[allow(dead_code)] pub fn is_vec_str(v1: Vec, s2: &str) -> bool { - let v2: Vec = string2vec(s2); - return v1 == v2.clone(); + let v2: Vec = string2vec(s2); + return v1 == v2.clone(); } diff --git a/pallets/subspace/src/voting.rs b/pallets/subspace/src/voting.rs index e3e6d4412..7af3d0db6 100644 --- a/pallets/subspace/src/voting.rs +++ b/pallets/subspace/src/voting.rs @@ -1,399 +1,348 @@ use core::ops::Add; -use frame_support::{pallet_prelude::DispatchResult}; -use scale_info::prelude::string::String; use super::*; -use crate::utils::{is_vec_str}; +use crate::utils::is_vec_str; +use frame_support::pallet_prelude::DispatchResult; +use scale_info::prelude::string::String; impl Pallet { - - pub fn do_unregister_voter( - origin: T::RuntimeOrigin, - ) -> DispatchResult { - let key = ensure_signed(origin)?; - ensure!(Self::is_voter_registered(&key), Error::::VoterIsNotRegistered); - Self::unregister_voter(&key); - ensure!(!Self::is_voter_registered(&key), Error::::VoterIsRegistered); - Ok(()) - } - - - pub fn do_add_proposal( - origin: T::RuntimeOrigin, - mut proposal:Proposal, - ) -> DispatchResult { - let key = ensure_signed(origin)?; - // get the voting power of the proposal owner - if Self::is_voter_registered(&key.clone()) { - // unregister voter if they are already registered - Self::unregister_voter(&key.clone()); - } - - let proposal_id = Self::next_proposal_id(); - let voting_power = Self::get_voting_power(&key, proposal.clone()); - let mut voter_info = Voter2Info::::get(key.clone()); - - voter_info.proposal_id = proposal_id; - voter_info.participant_index = proposal.participants.len() as u16; - voter_info.votes = voting_power; - // register the voter to avoid double voting - proposal.participants.push(key.clone()); - proposal.votes = proposal.votes.saturating_add(voting_power); - - Self::check_proposal(proposal.clone())?; // check if proposal is valid - - // update the proposal - Voter2Info::::insert(key, voter_info); - Proposals::::insert(proposal_id, proposal); - - Self::check_proposal_approval(proposal_id); - Ok(()) - } - - - // GLOBAL LAND - - pub fn do_add_global_proposal( - origin: T::RuntimeOrigin, - // params - params: GlobalParams, - ) -> DispatchResult { - let mut proposal = Self::default_proposal(); - proposal.global_params = params; - proposal.mode = "global".as_bytes().to_vec(); - Self::do_add_proposal(origin, proposal)?; - Ok(()) - } - - // CUSTOM LAND - - pub fn do_add_custom_proposal( - origin: T::RuntimeOrigin, - // params - data: Vec, - ) -> DispatchResult { - let mut proposal = Self::default_proposal(); - proposal.data = data; - proposal.mode = "custom".as_bytes().to_vec(); - - Self::do_add_proposal(origin, proposal)?; - Ok(()) - } - - // SUBNET LAND - - pub fn do_add_subnet_proposal( - origin: T::RuntimeOrigin, - // params - netuid: u16, - params: SubnetParams, - ) -> DispatchResult { - let mut proposal = Self::default_proposal(); - proposal.subnet_params = params; - proposal.netuid = netuid; - proposal.mode = "subnet".as_bytes().to_vec(); - Self::do_add_proposal(origin, proposal)?; - Ok(()) - } - - - pub fn num_subnet_proposals( - netuid: u16 - ) -> u64 { - let subnet_proposals = Self::get_subnet_proposals(netuid); - return subnet_proposals.len() as u64; - } - - - pub fn is_proposal_participant( - key: &T::AccountId, - proposal_id: u64, - ) -> bool { - let proposal: Proposal = Proposals::::get(proposal_id); - return proposal.participants.contains(key); - } - - - pub fn do_vote_proposal( - origin: T::RuntimeOrigin, - proposal_id: u64 - ) -> DispatchResult { - let key = ensure_signed(origin)?; - - ensure!(Self::proposal_exists(proposal_id), Error::::ProposalDoesNotExist); - - // if you vote the proposal on a subnet, you are no longer a participant - - if Self::is_voter_registered(&key.clone()) { - // unregister voter - Self::unregister_voter(&key.clone()); - } - - let mut proposal = Proposals::::get(proposal_id); - - - let mut voting_power : u64 = Self::get_voting_power(&key, proposal.clone()); - ensure!(voting_power > 0, Error::::VotingPowerIsZero); - - // register the voter to avoid double voting - - - let mut voter_info = Voter2Info::::get(key.clone()); - voter_info.proposal_id = proposal_id; - voter_info.participant_index = proposal.participants.len() as u16; - voter_info.votes = voting_power; - - // register the voter to avoid double voting - proposal.participants.push(key.clone()); - proposal.votes = proposal.votes.saturating_add(voting_power); - - // update the proposal - Voter2Info::::insert(key, voter_info); - Proposals::::insert(proposal_id, proposal); - - Self::check_proposal_approval(proposal_id); - - Ok(()) - } - pub fn num_proposals() -> u64 { - return Proposals::::iter().count() as u64; - } - - pub fn next_proposal_id() -> u64 { - let mut next_proposal_id: u64 = 0; - // add proposal id until it is not in the map - while Self::proposal_exists(next_proposal_id) { - next_proposal_id = next_proposal_id + 1; - } - return next_proposal_id; - } - - pub fn has_max_proposals() -> bool { - return Self::num_proposals() >= Self::get_max_proposals(); - } - - - pub fn check_proposal(proposal: Proposal) -> DispatchResult { - - // remove lowest voted proposal - if Self::has_max_proposals() { - let mut least_voted_proposal_id: u64 = u64::MAX; - let mut least_votes: u64 = u64::MAX; - - for (proposal_id, proposal) in Proposals::::iter() { - - // if proposal is accepted, remove it - if proposal.accepted || proposal.votes == 0{ - least_votes = 0; - least_voted_proposal_id = proposal_id; - break - } - - if proposal.votes < least_votes { - least_votes = proposal.votes; - least_voted_proposal_id = proposal_id; - } - } - - ensure!(proposal.votes > least_votes, Error::::TooFewVotesForNewProposal); - - // remove proposal participants - let proposal = Proposals::::get(least_voted_proposal_id); - // pop the participants - for participant in proposal.participants { - Voter2Info::::remove(participant); - } - Proposals::::remove(least_voted_proposal_id); - } - - let mode = proposal.mode.clone(); - - // check if proposal is valid - if is_vec_str(mode.clone(), "global") { - Self::check_global_params(proposal.global_params)?; - } else if is_vec_str(mode.clone(), "subnet") { - Self::check_subnet_params(proposal.subnet_params.clone())?; - // check if vote mode is valid - let subnet_params: SubnetParams = Self::subnet_params(proposal.netuid); - ensure!( - is_vec_str(subnet_params.vote_mode.clone(),"stake") - , Error::::InvalidVoteMode); - } else { - ensure!(proposal.data.len() > 0, Error::::InvalidProposalData); - } - // check if proposal is valid - ensure!(proposal.data.len() < 256, Error::::ProposalDataTooLarge); - // avoid an exploit with large data, cap it at 256 bytes - Ok(()) - } - - pub fn is_proposal_owner( - // check if the key is the owner of the proposal - key: &T::AccountId, - proposal_id: u64, - ) -> bool { - let proposal: Proposal = Proposals::::get(proposal_id); - if proposal.participants.len() == 0 { - return false; - } - return proposal.participants[0] == *key; - } - pub fn default_proposal() -> Proposal { - let mut proposal = Proposals::::get(u64::MAX); - return proposal; - } - - pub fn get_proposal( - proposal_id: u64, - ) -> Proposal { - return Proposals::::get(proposal_id); - } - - - pub fn unregister_voter(key: &T::AccountId) { - // unregister voter - - // get the proposal id for the voter - let voter_info = Self::get_voter_info(key); - // update the proposal votes - let mut proposal = Self::get_proposal(voter_info.proposal_id); - - // remove the voter from the participants - let index = voter_info.participant_index as usize; - proposal.participants.remove(index); - - // update the votes - proposal.votes = proposal.votes.saturating_sub(voter_info.votes); - - // remove proposal if there are no participants - if proposal.participants.len() == 0 || proposal.votes == 0 { - // remove proposal if there are no participants - Proposals::::remove(voter_info.proposal_id); - } else { - // update proposal - Proposals::::insert(voter_info.proposal_id, proposal); - } - - Voter2Info::::remove(key); - } - - - - pub fn is_voter_registered(key: &T::AccountId) -> bool { - // check if voter is registered - return Voter2Info::::contains_key(key); - } - - pub fn get_voter_info(key: &T::AccountId) -> VoterInfo{ - // get the proposal id for the voter - return Voter2Info::::get(key); - } - - - - pub fn get_voting_power( - key: &T::AccountId, - proposal: Proposal, - ) -> u64 { - let mut voting_power: u64 = 0; - if is_vec_str(proposal.mode.clone(),"subnet") { - voting_power = Self::get_total_stake_to(proposal.netuid, key); - } else { - // get all of the stake for the key - voting_power = Self::get_global_stake_to(key); - } - return voting_power; - } - - pub fn get_proposal_vote_threshold( - proposal_id: u64, - ) -> u64 { - let proposal: Proposal = Proposals::::get(proposal_id); - let mut vote_threshold: u64 = 0; - if is_vec_str(proposal.mode.clone(),"subnet") { - let total_stake = Self::get_total_subnet_stake(proposal.netuid); - vote_threshold = (total_stake * proposal.subnet_params.vote_threshold as u64) / 100; - } else { - let total_stake = Self::total_stake(); - vote_threshold = (total_stake * proposal.global_params.vote_threshold as u64) / 100; - } - return vote_threshold; - } - - - - pub fn check_proposal_approval(proposal_id: u64) { - - let proposal = Proposals::::get(proposal_id); - let mut stake_threshold: u64 = Self::get_proposal_vote_threshold(proposal_id); - if proposal.votes > stake_threshold { - // unregister all voters - - for participant in proposal.participants { - Voter2Info::::remove(participant); - } - Proposals::::mutate(proposal_id, |proposal| { - proposal.accepted = true; - proposal.participants = Vec::new(); - }); - - if is_vec_str(proposal.mode.clone(), "subnet") { - Self::set_subnet_params(proposal.netuid, proposal.subnet_params); - Self::deposit_event(Event::SubnetProposalAccepted(proposal_id, proposal.netuid)); - - } else if is_vec_str(proposal.mode.clone(), "global") { - Self::set_global_params(proposal.global_params); - Self::deposit_event(Event::GlobalProposalAccepted(proposal_id)); - } else { - Self::deposit_event(Event::CustomProposalAccepted(proposal_id)); - } - } - } - - - pub fn get_subnet_proposals( - netuid: u16 - ) -> Vec> { - let mut proposals: Vec> = Vec::new(); - for (proposal_id, proposal) in Proposals::::iter() { - if is_vec_str(proposal.mode.clone(), "subnet") && proposal.netuid == netuid { - proposals.push(proposal); - } - } - return proposals; - } - - pub fn get_global_proposals() -> Vec> { - let mut proposals: Vec> = Vec::new(); - for (proposal_id, proposal) in Proposals::::iter() { - if is_vec_str(proposal.mode.clone(), "global") { - proposals.push(proposal); - } - } - return proposals; - } - - - - pub fn num_global_proposals() -> u64 { - let global_proposals = Self::get_global_proposals(); - return global_proposals.len() as u64; - } - - - pub fn proposal_exists( - proposal_id: u64 - ) -> bool { - Proposals::::contains_key(proposal_id) - } - - pub fn is_vote_available( - key: &T::AccountId, - proposal_id: u64, - ) -> bool { - let proposal: Proposal = Proposals::::get(proposal_id); - let is_vote_available: bool = !proposal.participants.contains(key) && !proposal.accepted; - return is_vote_available; - } + pub fn do_unregister_voter(origin: T::RuntimeOrigin) -> DispatchResult { + let key = ensure_signed(origin)?; + ensure!(Self::is_voter_registered(&key), Error::::VoterIsNotRegistered); + Self::unregister_voter(&key); + ensure!(!Self::is_voter_registered(&key), Error::::VoterIsRegistered); + Ok(()) + } + + pub fn do_add_proposal(origin: T::RuntimeOrigin, mut proposal: Proposal) -> DispatchResult { + let key = ensure_signed(origin)?; + // get the voting power of the proposal owner + if Self::is_voter_registered(&key.clone()) { + // unregister voter if they are already registered + Self::unregister_voter(&key.clone()); + } + + let proposal_id = Self::next_proposal_id(); + let voting_power = Self::get_voting_power(&key, proposal.clone()); + let mut voter_info = Voter2Info::::get(key.clone()); + + voter_info.proposal_id = proposal_id; + voter_info.participant_index = proposal.participants.len() as u16; + voter_info.votes = voting_power; + // register the voter to avoid double voting + proposal.participants.push(key.clone()); + proposal.votes = proposal.votes.saturating_add(voting_power); + + Self::check_proposal(proposal.clone())?; // check if proposal is valid + + // update the proposal + Voter2Info::::insert(key, voter_info); + Proposals::::insert(proposal_id, proposal); + + Self::check_proposal_approval(proposal_id); + Ok(()) + } + + // GLOBAL LAND + + pub fn do_add_global_proposal( + origin: T::RuntimeOrigin, + // params + params: GlobalParams, + ) -> DispatchResult { + let mut proposal = Self::default_proposal(); + proposal.global_params = params; + proposal.mode = "global".as_bytes().to_vec(); + Self::do_add_proposal(origin, proposal)?; + Ok(()) + } + + // CUSTOM LAND + + pub fn do_add_custom_proposal( + origin: T::RuntimeOrigin, + // params + data: Vec, + ) -> DispatchResult { + let mut proposal = Self::default_proposal(); + proposal.data = data; + proposal.mode = "custom".as_bytes().to_vec(); + + Self::do_add_proposal(origin, proposal)?; + Ok(()) + } + + // SUBNET LAND + + pub fn do_add_subnet_proposal( + origin: T::RuntimeOrigin, + // params + netuid: u16, + params: SubnetParams, + ) -> DispatchResult { + let mut proposal = Self::default_proposal(); + proposal.subnet_params = params; + proposal.netuid = netuid; + proposal.mode = "subnet".as_bytes().to_vec(); + Self::do_add_proposal(origin, proposal)?; + Ok(()) + } + + pub fn num_subnet_proposals(netuid: u16) -> u64 { + let subnet_proposals = Self::get_subnet_proposals(netuid); + return subnet_proposals.len() as u64; + } + + pub fn is_proposal_participant(key: &T::AccountId, proposal_id: u64) -> bool { + let proposal: Proposal = Proposals::::get(proposal_id); + return proposal.participants.contains(key); + } + + pub fn do_vote_proposal(origin: T::RuntimeOrigin, proposal_id: u64) -> DispatchResult { + let key = ensure_signed(origin)?; + + ensure!(Self::proposal_exists(proposal_id), Error::::ProposalDoesNotExist); + + // if you vote the proposal on a subnet, you are no longer a participant + + if Self::is_voter_registered(&key.clone()) { + // unregister voter + Self::unregister_voter(&key.clone()); + } + + let mut proposal = Proposals::::get(proposal_id); + + let mut voting_power: u64 = Self::get_voting_power(&key, proposal.clone()); + ensure!(voting_power > 0, Error::::VotingPowerIsZero); + + // register the voter to avoid double voting + + let mut voter_info = Voter2Info::::get(key.clone()); + voter_info.proposal_id = proposal_id; + voter_info.participant_index = proposal.participants.len() as u16; + voter_info.votes = voting_power; + + // register the voter to avoid double voting + proposal.participants.push(key.clone()); + proposal.votes = proposal.votes.saturating_add(voting_power); + + // update the proposal + Voter2Info::::insert(key, voter_info); + Proposals::::insert(proposal_id, proposal); + + Self::check_proposal_approval(proposal_id); + + Ok(()) + } + pub fn num_proposals() -> u64 { + return Proposals::::iter().count() as u64; + } + + pub fn next_proposal_id() -> u64 { + let mut next_proposal_id: u64 = 0; + // add proposal id until it is not in the map + while Self::proposal_exists(next_proposal_id) { + next_proposal_id = next_proposal_id + 1; + } + return next_proposal_id; + } + + pub fn has_max_proposals() -> bool { + return Self::num_proposals() >= Self::get_max_proposals(); + } + + pub fn check_proposal(proposal: Proposal) -> DispatchResult { + // remove lowest voted proposal + if Self::has_max_proposals() { + let mut least_voted_proposal_id: u64 = u64::MAX; + let mut least_votes: u64 = u64::MAX; + + for (proposal_id, proposal) in Proposals::::iter() { + // if proposal is accepted, remove it + if proposal.accepted || proposal.votes == 0 { + least_votes = 0; + least_voted_proposal_id = proposal_id; + break + } + + if proposal.votes < least_votes { + least_votes = proposal.votes; + least_voted_proposal_id = proposal_id; + } + } + + ensure!(proposal.votes > least_votes, Error::::TooFewVotesForNewProposal); + + // remove proposal participants + let proposal = Proposals::::get(least_voted_proposal_id); + // pop the participants + for participant in proposal.participants { + Voter2Info::::remove(participant); + } + Proposals::::remove(least_voted_proposal_id); + } + + let mode = proposal.mode.clone(); + + // check if proposal is valid + if is_vec_str(mode.clone(), "global") { + Self::check_global_params(proposal.global_params)?; + } else if is_vec_str(mode.clone(), "subnet") { + Self::check_subnet_params(proposal.subnet_params.clone())?; + // check if vote mode is valid + let subnet_params: SubnetParams = Self::subnet_params(proposal.netuid); + ensure!( + is_vec_str(subnet_params.vote_mode.clone(), "stake"), + Error::::InvalidVoteMode + ); + } else { + ensure!(proposal.data.len() > 0, Error::::InvalidProposalData); + } + // check if proposal is valid + ensure!(proposal.data.len() < 256, Error::::ProposalDataTooLarge); + // avoid an exploit with large data, cap it at 256 bytes + Ok(()) + } + + pub fn is_proposal_owner( + // check if the key is the owner of the proposal + key: &T::AccountId, + proposal_id: u64, + ) -> bool { + let proposal: Proposal = Proposals::::get(proposal_id); + if proposal.participants.len() == 0 { + return false; + } + return proposal.participants[0] == *key; + } + pub fn default_proposal() -> Proposal { + let mut proposal = Proposals::::get(u64::MAX); + return proposal; + } + + pub fn get_proposal(proposal_id: u64) -> Proposal { + return Proposals::::get(proposal_id); + } + + pub fn unregister_voter(key: &T::AccountId) { + // unregister voter + + // get the proposal id for the voter + let voter_info = Self::get_voter_info(key); + // update the proposal votes + let mut proposal = Self::get_proposal(voter_info.proposal_id); + + // remove the voter from the participants + let index = voter_info.participant_index as usize; + proposal.participants.remove(index); + + // update the votes + proposal.votes = proposal.votes.saturating_sub(voter_info.votes); + + // remove proposal if there are no participants + if proposal.participants.len() == 0 || proposal.votes == 0 { + // remove proposal if there are no participants + Proposals::::remove(voter_info.proposal_id); + } else { + // update proposal + Proposals::::insert(voter_info.proposal_id, proposal); + } + + Voter2Info::::remove(key); + } + + pub fn is_voter_registered(key: &T::AccountId) -> bool { + // check if voter is registered + return Voter2Info::::contains_key(key); + } + + pub fn get_voter_info(key: &T::AccountId) -> VoterInfo { + // get the proposal id for the voter + return Voter2Info::::get(key); + } + + pub fn get_voting_power(key: &T::AccountId, proposal: Proposal) -> u64 { + let mut voting_power: u64 = 0; + if is_vec_str(proposal.mode.clone(), "subnet") { + voting_power = Self::get_total_stake_to(proposal.netuid, key); + } else { + // get all of the stake for the key + voting_power = Self::get_global_stake_to(key); + } + return voting_power; + } + + pub fn get_proposal_vote_threshold(proposal_id: u64) -> u64 { + let proposal: Proposal = Proposals::::get(proposal_id); + let mut vote_threshold: u64 = 0; + if is_vec_str(proposal.mode.clone(), "subnet") { + let total_stake = Self::get_total_subnet_stake(proposal.netuid); + vote_threshold = (total_stake * proposal.subnet_params.vote_threshold as u64) / 100; + } else { + let total_stake = Self::total_stake(); + vote_threshold = (total_stake * proposal.global_params.vote_threshold as u64) / 100; + } + return vote_threshold; + } + + pub fn check_proposal_approval(proposal_id: u64) { + let proposal = Proposals::::get(proposal_id); + let mut stake_threshold: u64 = Self::get_proposal_vote_threshold(proposal_id); + if proposal.votes > stake_threshold { + // unregister all voters + + for participant in proposal.participants { + Voter2Info::::remove(participant); + } + Proposals::::mutate(proposal_id, |proposal| { + proposal.accepted = true; + proposal.participants = Vec::new(); + }); + + if is_vec_str(proposal.mode.clone(), "subnet") { + Self::set_subnet_params(proposal.netuid, proposal.subnet_params); + Self::deposit_event(Event::SubnetProposalAccepted(proposal_id, proposal.netuid)); + } else if is_vec_str(proposal.mode.clone(), "global") { + Self::set_global_params(proposal.global_params); + Self::deposit_event(Event::GlobalProposalAccepted(proposal_id)); + } else { + Self::deposit_event(Event::CustomProposalAccepted(proposal_id)); + } + } + } + + pub fn get_subnet_proposals(netuid: u16) -> Vec> { + let mut proposals: Vec> = Vec::new(); + for (proposal_id, proposal) in Proposals::::iter() { + if is_vec_str(proposal.mode.clone(), "subnet") && proposal.netuid == netuid { + proposals.push(proposal); + } + } + return proposals; + } + + pub fn get_global_proposals() -> Vec> { + let mut proposals: Vec> = Vec::new(); + for (proposal_id, proposal) in Proposals::::iter() { + if is_vec_str(proposal.mode.clone(), "global") { + proposals.push(proposal); + } + } + return proposals; + } + + pub fn num_global_proposals() -> u64 { + let global_proposals = Self::get_global_proposals(); + return global_proposals.len() as u64; + } + + pub fn proposal_exists(proposal_id: u64) -> bool { + Proposals::::contains_key(proposal_id) + } + + pub fn is_vote_available(key: &T::AccountId, proposal_id: u64) -> bool { + let proposal: Proposal = Proposals::::get(proposal_id); + let is_vote_available: bool = !proposal.participants.contains(key) && !proposal.accepted; + return is_vote_available; + } } - diff --git a/pallets/subspace/tests/test_burn.rs b/pallets/subspace/tests/test_burn.rs index f047a6f1a..89ba9d2cb 100644 --- a/pallets/subspace/tests/test_burn.rs +++ b/pallets/subspace/tests/test_burn.rs @@ -6,152 +6,155 @@ use frame_support::{ use frame_system::Config; use pallet_subspace::Error; use sp_core::U256; -use test_mock::*; use sp_std::vec; +use test_mock::*; #[test] fn test_burn() { - new_test_ext().execute_with(|| { - - let netuid = 0; - let n = 20; - let initial_stake: u64 = 1000; - let tempo = 100; - - - let keys : Vec = (0..n).into_iter().map(|x| U256::from(x)).collect(); - let stakes : Vec = (0..n).into_iter().map(|x| initial_stake * 1_000_000_000).collect(); - - let mut subnet_params = SubspaceModule::subnet_params(netuid); - subnet_params.tempo = tempo; - SubspaceModule::set_subnet_params(netuid, subnet_params); - subnet_params = SubspaceModule::subnet_params(netuid); - - - for i in 0..n { - assert_ok!(register_module(netuid, keys[i], stakes[i])); - let stake_from_vector = SubspaceModule::get_stake_to_vector(netuid, &keys[i]); - println!("{:?}", stake_from_vector); - } + new_test_ext().execute_with(|| { + let netuid = 0; + let n = 20; + let initial_stake: u64 = 1000; + let tempo = 100; + let keys: Vec = (0..n).into_iter().map(|x| U256::from(x)).collect(); + let stakes: Vec = (0..n).into_iter().map(|x| initial_stake * 1_000_000_000).collect(); - for burn_rate in [0, 10, 20, 30, 40, 50, 60, 70, 80, 90].iter() { - - let mut params = SubspaceModule::global_params(); - params.burn_rate = *burn_rate; - SubspaceModule::set_global_params(params); - params = SubspaceModule::global_params(); - - let voter_key = keys[1]; + let mut subnet_params = SubspaceModule::subnet_params(netuid); + subnet_params.tempo = tempo; + SubspaceModule::set_subnet_params(netuid, subnet_params); + subnet_params = SubspaceModule::subnet_params(netuid); - - // vote to avoid key[0] as we want to see the key[0] burn - let mut votes : Vec = vec![]; - let mut uids : Vec = vec![]; for i in 0..n { - votes.push(1); - uids.push(i as u16); - + assert_ok!(register_module(netuid, keys[i], stakes[i])); + let stake_from_vector = SubspaceModule::get_stake_to_vector(netuid, &keys[i]); + println!("{:?}", stake_from_vector); } - println!("{:?}", SubspaceModule::get_stake_for_key(netuid, &voter_key)); - assert_ok!(SubspaceModule::set_weights(get_origin(voter_key),netuid, uids, votes)); - println!("burn : {:?}", SubspaceModule::get_burn_emission_per_epoch(netuid)); - - let mut total_stake = SubspaceModule::get_total_subnet_stake(netuid); - - let dividends = SubspaceModule::get_dividends(netuid); - let incentives = SubspaceModule::get_incentives(netuid); - let emissions = SubspaceModule::get_emissions(netuid); - - println!("dividends: {:?} incentives: {:?} emissions: {:?}", - dividends, - incentives, - emissions); - - let burn_per_epoch = SubspaceModule::get_burn_per_epoch(netuid); - - println!("burn_per_epoch: {:?}", burn_per_epoch); - - let mut stake_vector_before = SubspaceModule::get_stakes(netuid); - let total_stake_before = SubspaceModule::get_total_subnet_stake(netuid); - step_epoch(netuid); - let mut stake_vector_after = SubspaceModule::get_stakes(netuid); - let total_stake_after = SubspaceModule::get_total_subnet_stake(netuid); - - println!("stake_vector_before: {:?} stake_vector_after: {:?}", stake_vector_before, stake_vector_after); - println!("total_stake: {:?} total_stake_after: {:?}", total_stake_before, total_stake_after); - let subnet_params = SubspaceModule::subnet_params(netuid); - - let burn_per_epoch = SubspaceModule::get_burn_emission_per_epoch(netuid); - let dividends = SubspaceModule::get_dividends(netuid); - let incentives = SubspaceModule::get_incentives(netuid); - let emissions = SubspaceModule::get_emissions(netuid); - - println!("burn_per_epoch: {:?}", burn_per_epoch); - println!("dividends: {:?} incentives: {:?} emissions: {:?}", - dividends, - incentives, - emissions); - - - let calculated_subnet_emission = emissions.iter().sum::(); - let expected_subnet_emission: u64 = ((SubspaceModule::get_subnet_emission(netuid) as f64 * (subnet_params.tempo as f64)) * (((100 - burn_rate) as f64) / 100.0)) as u64; - - let delta_ratio = 0.01; - let delta = (total_stake_before as f64 * delta_ratio) as u64; - - let expected_subnet_emission = total_stake_before.saturating_add(expected_subnet_emission); - assert!(total_stake_after.saturating_sub(delta) < expected_subnet_emission && - total_stake_after + delta > expected_subnet_emission, - "total_stake_after: {:?} expected_subnet_emission: {:?}", - total_stake_after, expected_subnet_emission); - - } + for burn_rate in [0, 10, 20, 30, 40, 50, 60, 70, 80, 90].iter() { + let mut params = SubspaceModule::global_params(); + params.burn_rate = *burn_rate; + SubspaceModule::set_global_params(params); + params = SubspaceModule::global_params(); + + let voter_key = keys[1]; + + // vote to avoid key[0] as we want to see the key[0] burn + let mut votes: Vec = vec![]; + let mut uids: Vec = vec![]; + for i in 0..n { + votes.push(1); + uids.push(i as u16); + } + println!("{:?}", SubspaceModule::get_stake_for_key(netuid, &voter_key)); + assert_ok!(SubspaceModule::set_weights(get_origin(voter_key), netuid, uids, votes)); + println!("burn : {:?}", SubspaceModule::get_burn_emission_per_epoch(netuid)); + + let mut total_stake = SubspaceModule::get_total_subnet_stake(netuid); + + let dividends = SubspaceModule::get_dividends(netuid); + let incentives = SubspaceModule::get_incentives(netuid); + let emissions = SubspaceModule::get_emissions(netuid); + + println!( + "dividends: {:?} incentives: {:?} emissions: {:?}", + dividends, incentives, emissions + ); + + let burn_per_epoch = SubspaceModule::get_burn_per_epoch(netuid); + + println!("burn_per_epoch: {:?}", burn_per_epoch); + + let mut stake_vector_before = SubspaceModule::get_stakes(netuid); + let total_stake_before = SubspaceModule::get_total_subnet_stake(netuid); + step_epoch(netuid); + let mut stake_vector_after = SubspaceModule::get_stakes(netuid); + let total_stake_after = SubspaceModule::get_total_subnet_stake(netuid); + + println!( + "stake_vector_before: {:?} stake_vector_after: {:?}", + stake_vector_before, stake_vector_after + ); + println!( + "total_stake: {:?} total_stake_after: {:?}", + total_stake_before, total_stake_after + ); + let subnet_params = SubspaceModule::subnet_params(netuid); + + let burn_per_epoch = SubspaceModule::get_burn_emission_per_epoch(netuid); + let dividends = SubspaceModule::get_dividends(netuid); + let incentives = SubspaceModule::get_incentives(netuid); + let emissions = SubspaceModule::get_emissions(netuid); + + println!("burn_per_epoch: {:?}", burn_per_epoch); + + println!( + "dividends: {:?} incentives: {:?} emissions: {:?}", + dividends, incentives, emissions + ); + + let calculated_subnet_emission = emissions.iter().sum::(); + let expected_subnet_emission: u64 = ((SubspaceModule::get_subnet_emission(netuid) + as f64 * (subnet_params.tempo as f64)) * + (((100 - burn_rate) as f64) / 100.0)) as u64; + + let delta_ratio = 0.01; + let delta = (total_stake_before as f64 * delta_ratio) as u64; + + let expected_subnet_emission = + total_stake_before.saturating_add(expected_subnet_emission); + assert!( + total_stake_after.saturating_sub(delta) < expected_subnet_emission && + total_stake_after + delta > expected_subnet_emission, + "total_stake_after: {:?} expected_subnet_emission: {:?}", + total_stake_after, + expected_subnet_emission + ); + } }); } - - #[test] fn test_min_burn() { - new_test_ext().execute_with(|| { - - let netuid = 0; - let n = 10; - - let initial_stake: u64 = 1000; - - let keys : Vec = (0..n).into_iter().map(|x| U256::from(x)).collect(); - let stakes : Vec = (0..n).into_iter().map(|x| initial_stake * 1_000_000_000).collect(); - - // founder register_module(netuid, keys[i]); - let founder_initial_stake = stakes[0]; - assert_ok!(register_module(netuid, keys[0], stakes[0])); - let founder_current_stake = SubspaceModule::get_total_stake_to(netuid,&keys[0]); - assert_eq!(founder_initial_stake, founder_current_stake, - "founder_initial_stake: {:?} founder_current_stake: {:?}", - founder_initial_stake, founder_current_stake); - - // set the burn min to 1000000000 - // register_module(netuid, keys[i]); - let mut params = SubspaceModule::global_params(); - params.min_burn = 100; - SubspaceModule::set_global_params(params.clone()); - params = SubspaceModule::global_params(); - for i in 1..n { - assert_ok!(register_module(netuid, keys[i], stakes[i])); - println!("params: {:?}", params); - - let key_stake_after = SubspaceModule::get_total_stake_to(netuid,&keys[i]); - assert_eq!(key_stake_after, stakes[i] - params.min_burn, - "key_stake_after: {:?} stakes[i]: {:?}", - key_stake_after, stakes[i]); - } - - - let voter_key = keys[1]; - + new_test_ext().execute_with(|| { + let netuid = 0; + let n = 10; + + let initial_stake: u64 = 1000; + + let keys: Vec = (0..n).into_iter().map(|x| U256::from(x)).collect(); + let stakes: Vec = (0..n).into_iter().map(|x| initial_stake * 1_000_000_000).collect(); + + // founder register_module(netuid, keys[i]); + let founder_initial_stake = stakes[0]; + assert_ok!(register_module(netuid, keys[0], stakes[0])); + let founder_current_stake = SubspaceModule::get_total_stake_to(netuid, &keys[0]); + assert_eq!( + founder_initial_stake, founder_current_stake, + "founder_initial_stake: {:?} founder_current_stake: {:?}", + founder_initial_stake, founder_current_stake + ); + + // set the burn min to 1000000000 + // register_module(netuid, keys[i]); + let mut params = SubspaceModule::global_params(); + params.min_burn = 100; + SubspaceModule::set_global_params(params.clone()); + params = SubspaceModule::global_params(); + for i in 1..n { + assert_ok!(register_module(netuid, keys[i], stakes[i])); + println!("params: {:?}", params); + + let key_stake_after = SubspaceModule::get_total_stake_to(netuid, &keys[i]); + assert_eq!( + key_stake_after, + stakes[i] - params.min_burn, + "key_stake_after: {:?} stakes[i]: {:?}", + key_stake_after, + stakes[i] + ); + } + let voter_key = keys[1]; }); -} \ No newline at end of file +} diff --git a/pallets/subspace/tests/test_delegate_staking.rs b/pallets/subspace/tests/test_delegate_staking.rs index b5e66f29b..df6d02e07 100644 --- a/pallets/subspace/tests/test_delegate_staking.rs +++ b/pallets/subspace/tests/test_delegate_staking.rs @@ -37,7 +37,6 @@ use substrate_fixed::types::{I32F32, I64F64}; // }); // } - #[test] fn test_ownership_ratio() { new_test_ext().execute_with(|| { @@ -51,50 +50,84 @@ fn test_ownership_ratio() { let keys = SubspaceModule::get_keys(netuid); let voter_key = keys[0]; let miner_keys = keys[1..].to_vec(); - let miner_uids: Vec = miner_keys.iter().map(|k| SubspaceModule::get_uid_for_key(netuid, k)).collect(); + let miner_uids: Vec = + miner_keys.iter().map(|k| SubspaceModule::get_uid_for_key(netuid, k)).collect(); let miner_weights = vec![1; miner_uids.len()]; - - let delegate_keys: Vec = (0..num_modules).map(|i| U256::from(i + num_modules + 1)).collect(); for d in delegate_keys.iter() { add_balance(*d, stake_per_module + 1); } - let pre_delegate_stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, &voter_key); + let pre_delegate_stake_from_vector = + SubspaceModule::get_stake_from_vector(netuid, &voter_key); assert_eq!(pre_delegate_stake_from_vector.len(), 1); // +1 for the module itself, +1 for the delegate key on for (i, d) in delegate_keys.iter().enumerate() { println!("DELEGATE KEY: {}", d); - assert_ok!(SubspaceModule::add_stake(get_origin(*d), netuid, voter_key, stake_per_module)); + assert_ok!(SubspaceModule::add_stake( + get_origin(*d), + netuid, + voter_key, + stake_per_module + )); let stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, &voter_key); assert_eq!(stake_from_vector.len(), pre_delegate_stake_from_vector.len() + i + 1); } - let ownership_ratios: Vec<(U256, I64F64)> =SubspaceModule::get_ownership_ratios(netuid, &voter_key); + let ownership_ratios: Vec<(U256, I64F64)> = + SubspaceModule::get_ownership_ratios(netuid, &voter_key); assert_eq!(ownership_ratios.len(), delegate_keys.len() + 1); - let founder_tokens_before = SubspaceModule::get_balance(&voter_key) + SubspaceModule::get_stake_to_module(netuid, &voter_key, &voter_key); - - let delegate_balances_before = delegate_keys.iter().map(|k| SubspaceModule::get_balance(k)).collect::>(); - let delegate_stakes_before = delegate_keys.iter().map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &voter_key)).collect::>(); - let delegate_total_tokens_before = delegate_balances_before.iter().zip(delegate_stakes_before.clone().into_iter()).map(|(a, x)| a + x).fold(0, |acc, x| acc + x); - - - let total_balance = keys.iter().map(|k| SubspaceModule::get_balance(k)).collect::>().iter().fold(0, |acc, x| acc + x); - let total_stake = keys.iter().map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &k)).collect::>().iter().fold(0, |acc, x| acc + x); - let total_delegate_stake = delegate_keys.iter().map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &voter_key)).collect::>().iter().fold(0, |acc, x| acc + x); - let total_delegate_balance = delegate_keys.iter().map(|k| SubspaceModule::get_balance(k)).collect::>().iter().fold(0, |acc, x| acc + x); - let total_tokens_before = total_balance + total_stake + total_delegate_stake + total_delegate_balance; + let founder_tokens_before = SubspaceModule::get_balance(&voter_key) + + SubspaceModule::get_stake_to_module(netuid, &voter_key, &voter_key); + + let delegate_balances_before = delegate_keys + .iter() + .map(|k| SubspaceModule::get_balance(k)) + .collect::>(); + let delegate_stakes_before = delegate_keys + .iter() + .map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &voter_key)) + .collect::>(); + let delegate_total_tokens_before = delegate_balances_before + .iter() + .zip(delegate_stakes_before.clone().into_iter()) + .map(|(a, x)| a + x) + .fold(0, |acc, x| acc + x); + + let total_balance = keys + .iter() + .map(|k| SubspaceModule::get_balance(k)) + .collect::>() + .iter() + .fold(0, |acc, x| acc + x); + let total_stake = keys + .iter() + .map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &k)) + .collect::>() + .iter() + .fold(0, |acc, x| acc + x); + let total_delegate_stake = delegate_keys + .iter() + .map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &voter_key)) + .collect::>() + .iter() + .fold(0, |acc, x| acc + x); + let total_delegate_balance = delegate_keys + .iter() + .map(|k| SubspaceModule::get_balance(k)) + .collect::>() + .iter() + .fold(0, |acc, x| acc + x); + let total_tokens_before = + total_balance + total_stake + total_delegate_stake + total_delegate_balance; println!("total_tokens_before: {:?}", total_tokens_before); - println!("delegate_balances before: {:?}", delegate_balances_before); println!("delegate_stakes before: {:?}", delegate_stakes_before); println!("delegate_total_tokens before: {:?}", delegate_total_tokens_before); - - let result = SubspaceModule::set_weights( get_origin(voter_key), netuid, @@ -117,16 +150,31 @@ fn test_ownership_ratio() { println!("total_emissions: {:?}", total_emissions); - let delegate_balances = delegate_keys.iter().map(|k| SubspaceModule::get_balance(k)).collect::>(); - let delegate_stakes = delegate_keys.iter().map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &voter_key)).collect::>(); - let delegate_total_tokens = delegate_balances.iter().zip(delegate_stakes.clone().into_iter()).map(|(a, x)| a + x).fold(0, |acc, x| acc + x); - let founder_tokens = SubspaceModule::get_balance(&voter_key) + SubspaceModule::get_stake_to_module(netuid, &voter_key, &voter_key); + let delegate_balances = delegate_keys + .iter() + .map(|k| SubspaceModule::get_balance(k)) + .collect::>(); + let delegate_stakes = delegate_keys + .iter() + .map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &voter_key)) + .collect::>(); + let delegate_total_tokens = delegate_balances + .iter() + .zip(delegate_stakes.clone().into_iter()) + .map(|(a, x)| a + x) + .fold(0, |acc, x| acc + x); + let founder_tokens = SubspaceModule::get_balance(&voter_key) + + SubspaceModule::get_stake_to_module(netuid, &voter_key, &voter_key); let founder_new_tokens = founder_tokens - founder_tokens_before; - let delegate_new_tokens: Vec = delegate_stakes.iter().zip(delegate_stakes_before.clone().into_iter()).map(|(a, x)| a - x).collect::>(); + let delegate_new_tokens: Vec = delegate_stakes + .iter() + .zip(delegate_stakes_before.clone().into_iter()) + .map(|(a, x)| a - x) + .collect::>(); + let total_new_tokens = + founder_new_tokens + delegate_new_tokens.iter().fold(0, |acc, x| acc + x); - let total_new_tokens = founder_new_tokens + delegate_new_tokens.iter().fold(0, |acc, x| acc + x); - println!("owner_ratios: {:?}", ownership_ratios); println!("total_new_tokens: {:?}", total_new_tokens); println!("founder_tokens: {:?}", founder_tokens); @@ -136,21 +184,41 @@ fn test_ownership_ratio() { println!("founder_new_tokens: {:?}", founder_new_tokens); println!("delegate_new_tokens: {:?}", delegate_new_tokens); - let total_balance = keys.iter().map(|k| SubspaceModule::get_balance(k)).collect::>().iter().fold(0, |acc, x| acc + x); - let total_stake = keys.iter().map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &k)).collect::>().iter().fold(0, |acc, x| acc + x); - let total_delegate_stake = delegate_keys.iter().map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &voter_key)).collect::>().iter().fold(0, |acc, x| acc + x); - let total_delegate_balance = delegate_keys.iter().map(|k| SubspaceModule::get_balance(k)).collect::>().iter().fold(0, |acc, x| acc + x); - let total_tokens_after = total_balance + total_stake + total_delegate_stake + total_delegate_balance; + let total_balance = keys + .iter() + .map(|k| SubspaceModule::get_balance(k)) + .collect::>() + .iter() + .fold(0, |acc, x| acc + x); + let total_stake = keys + .iter() + .map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &k)) + .collect::>() + .iter() + .fold(0, |acc, x| acc + x); + let total_delegate_stake = delegate_keys + .iter() + .map(|k| SubspaceModule::get_stake_to_module(netuid, &k, &voter_key)) + .collect::>() + .iter() + .fold(0, |acc, x| acc + x); + let total_delegate_balance = delegate_keys + .iter() + .map(|k| SubspaceModule::get_balance(k)) + .collect::>() + .iter() + .fold(0, |acc, x| acc + x); + let total_tokens_after = + total_balance + total_stake + total_delegate_stake + total_delegate_balance; let total_new_tokens = total_tokens_after - total_tokens_before; println!("total_tokens_after: {:?}", total_tokens_before); println!("total_new_tokens: {:?}", total_new_tokens); assert_eq!(total_new_tokens, total_emissions); - let stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, &voter_key); let stake: u64 = SubspaceModule::get_stake(netuid, &voter_key); let sumed_stake: u64 = stake_from_vector.iter().fold(0, |acc, (a, x)| acc + x); let total_stake: u64 = SubspaceModule::get_total_subnet_stake(netuid); println!("stake_from_vector: {:?}", stake_from_vector); }); -} \ No newline at end of file +} diff --git a/pallets/subspace/tests/test_mock.rs b/pallets/subspace/tests/test_mock.rs index d3cb9bd4a..749dff13c 100644 --- a/pallets/subspace/tests/test_mock.rs +++ b/pallets/subspace/tests/test_mock.rs @@ -72,9 +72,9 @@ impl pallet_balances::Config for Test { type WeightInfo = (); type MaxReserves = MaxReserves; type ReserveIdentifier = (); - type RuntimeHoldReason = (); - type FreezeIdentifier = (); - type MaxHolds = frame_support::traits::ConstU32<16>; + type RuntimeHoldReason = (); + type FreezeIdentifier = (); + type MaxHolds = frame_support::traits::ConstU32<16>; type MaxFreezes = frame_support::traits::ConstU32<16>; } diff --git a/pallets/subspace/tests/test_profit_share.rs b/pallets/subspace/tests/test_profit_share.rs index 6945498f3..39e3b6cf6 100644 --- a/pallets/subspace/tests/test_profit_share.rs +++ b/pallets/subspace/tests/test_profit_share.rs @@ -6,34 +6,36 @@ use frame_support::{ use frame_system::Config; use pallet_subspace::Error; use sp_core::U256; -use test_mock::*; use sp_std::vec; +use test_mock::*; /* TO DO SAM: write test for LatuUpdate after it is set */ #[test] fn test_add_profit_share() { - new_test_ext().execute_with(|| { - + new_test_ext().execute_with(|| { let netuid = 0; - let miner_key = U256::from(0); + let miner_key = U256::from(0); let voter_key = U256::from(1); - register_module(netuid, miner_key, 1_000_000_000); + register_module(netuid, miner_key, 1_000_000_000); register_module(netuid, voter_key, 1_000_000_000); - let miner_uid = SubspaceModule::get_uid_for_key(netuid,&miner_key); - let voter_uid = SubspaceModule::get_uid_for_key(netuid,&voter_key); + let miner_uid = SubspaceModule::get_uid_for_key(netuid, &miner_key); + let voter_uid = SubspaceModule::get_uid_for_key(netuid, &voter_key); - SubspaceModule::set_min_allowed_weights(netuid, 1); - let profit_sharer_keys = vec![U256::from(2), U256::from(3)]; - let shares = vec![2,2]; - let result = SubspaceModule::add_profit_shares(get_origin(miner_key), profit_sharer_keys.clone(), shares.clone()); + let shares = vec![2, 2]; + let result = SubspaceModule::add_profit_shares( + get_origin(miner_key), + profit_sharer_keys.clone(), + shares.clone(), + ); assert_ok!(result); let profit_shares = SubspaceModule::get_profit_shares(miner_key); - assert_eq!(profit_shares.len(), shares.len(), "profit shares not added"); + assert_eq!(profit_shares.len(), shares.len(), "profit shares not added"); println!("founder profit shares: {:?}", profit_shares); - let result = SubspaceModule::set_weights(get_origin(voter_key), netuid, vec![miner_uid], vec![1]); + let result = + SubspaceModule::set_weights(get_origin(voter_key), netuid, vec![miner_uid], vec![1]); assert_ok!(result); let params = SubspaceModule::subnet_params(netuid); @@ -41,8 +43,8 @@ fn test_add_profit_share() { let miner_emission = SubspaceModule::get_emission_for_key(netuid, &miner_key); let voter_emission = SubspaceModule::get_emission_for_key(netuid, &voter_key); assert_eq!(miner_emission, voter_emission, "emission not equal"); - assert!(miner_emission==0, "emission not equal"); - assert!(voter_emission==0, "emission not equal"); + assert!(miner_emission == 0, "emission not equal"); + assert!(voter_emission == 0, "emission not equal"); let miner_stake = SubspaceModule::get_stake_for_key(netuid, &miner_key); let voter_stake = SubspaceModule::get_stake_for_key(netuid, &voter_key); println!("miner stake before: {:?}", miner_stake); @@ -50,8 +52,8 @@ fn test_add_profit_share() { step_epoch(netuid); let miner_emission = SubspaceModule::get_emission_for_key(netuid, &miner_key); let voter_emission = SubspaceModule::get_emission_for_key(netuid, &voter_key); - assert!(miner_emission>0, "emission not equal"); - assert!(voter_emission>0, "emission not equal"); + assert!(miner_emission > 0, "emission not equal"); + assert!(voter_emission > 0, "emission not equal"); assert_eq!(miner_emission, voter_emission, "emission not equal"); println!("miner emission: {:?}", miner_emission); @@ -70,26 +72,24 @@ fn test_add_profit_share() { println!("profit share emissions: {:?}", profit_share_emissions); // check the profit sharers - let mut profit_share_balances : Vec = Vec::new(); + let mut profit_share_balances: Vec = Vec::new(); for profit_sharer_key in profit_sharer_keys.iter() { - - let profit_share_balance = SubspaceModule::get_stake_to_total(netuid, profit_sharer_key); + let profit_share_balance = + SubspaceModule::get_stake_to_total(netuid, profit_sharer_key); let stake_to_vector = SubspaceModule::get_stake_to_vector(netuid, profit_sharer_key); println!("profit share balance: {:?}", stake_to_vector); println!("profit share balance: {:?}", profit_share_balance); profit_share_balances.push(profit_share_balance); - assert!(profit_share_balances[0]>0, "profit share balance is zero"); + assert!(profit_share_balances[0] > 0, "profit share balance is zero"); } - // sum of profit shares should be equal to the emission - let sum_profit_share_balances : u64 = profit_share_balances.iter().sum(); + let sum_profit_share_balances: u64 = profit_share_balances.iter().sum(); let delta = 1000; - assert!(sum_profit_share_balances > miner_emission - delta || + assert!( + sum_profit_share_balances > miner_emission - delta || sum_profit_share_balances < miner_emission + delta, - "profit share balances do not add up to the emission"); - - - - }) -} \ No newline at end of file + "profit share balances do not add up to the emission" + ); + }) +} diff --git a/pallets/subspace/tests/test_registration.rs b/pallets/subspace/tests/test_registration.rs index 557e17697..089d46c99 100644 --- a/pallets/subspace/tests/test_registration.rs +++ b/pallets/subspace/tests/test_registration.rs @@ -1,10 +1,9 @@ use crate::test_mock::*; use frame_support::{ assert_ok, - dispatch::{DispatchInfo, GetDispatchInfo}, + dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays}, sp_runtime::DispatchError, traits::Currency, - dispatch::{DispatchClass, Pays}, }; use frame_system::Config; use ndarray::stack_new_axis; @@ -33,14 +32,14 @@ fn test_min_stake() { SubspaceModule::set_max_registrations_per_block(max_registrations_per_block); step_block(1); assert_eq!(SubspaceModule::get_registrations_this_block(), 0); - + let n = U256::from(reg_this_block); // Example: if you want a list of numbers from 1 to 9 let keys_list: Vec = (1..n.as_u64()) // Assuming n fits into a u64 for simplicity .map(U256::from) - .collect(); - + .collect(); + let min_stake_to_register = SubspaceModule::get_min_stake(netuid); - + for key in keys_list { register_module(netuid, key, min_stake_to_register); println!( @@ -51,13 +50,12 @@ fn test_min_stake() { let registrations_this_block = SubspaceModule::get_registrations_this_block(); println!("registrations_this_block: {:?}", registrations_this_block); assert_eq!(registrations_this_block, max_registrations_per_block); - + step_block(1); assert_eq!(SubspaceModule::get_registrations_this_block(), 0); }); } - #[test] fn test_max_registration() { new_test_ext().execute_with(|| { @@ -72,7 +70,7 @@ fn test_max_registration() { SubspaceModule::set_min_stake(netuid, min_stake); SubspaceModule::set_max_registrations_per_block(max_registrations_per_block); - + assert_eq!(SubspaceModule::get_registrations_this_block(), 0); for i in 1..n { @@ -93,7 +91,6 @@ fn test_max_registration() { }); } - #[test] fn test_delegate_register() { new_test_ext().execute_with(|| { diff --git a/pallets/subspace/tests/test_staking.rs b/pallets/subspace/tests/test_staking.rs index 56c876468..beb9fa97e 100644 --- a/pallets/subspace/tests/test_staking.rs +++ b/pallets/subspace/tests/test_staking.rs @@ -266,8 +266,6 @@ fn test_delegate_stake() { }); } - - #[test] fn test_ownership_ratio() { new_test_ext().execute_with(|| { @@ -323,8 +321,6 @@ fn test_ownership_ratio() { }); } - - #[test] fn test_min_stake() { new_test_ext().execute_with(|| { @@ -336,16 +332,10 @@ fn test_min_stake() { let keys = SubspaceModule::get_keys(netuid); register_n_modules(netuid, num_modules, min_stake); - SubspaceModule::set_min_stake( netuid, min_stake - 100); + SubspaceModule::set_min_stake(netuid, min_stake - 100); // SubspaceModule::set_min_stake( netuid, min_stake - 100); SubspaceModule::remove_stake(get_origin(keys[0]), netuid, keys[0], 10_000_000_000); - - }); } - - - - diff --git a/pallets/subspace/tests/test_step.rs b/pallets/subspace/tests/test_step.rs index 6e95afa7b..26386eaee 100644 --- a/pallets/subspace/tests/test_step.rs +++ b/pallets/subspace/tests/test_step.rs @@ -10,8 +10,6 @@ use substrate_fixed::{ }; mod test_mock; - - fn check_network_stats(netuid: u16) { let emission_buffer: u64 = 1_000; // the numbers arent perfect but we want to make sure they fall within a range (10_000 / 2**64) @@ -38,7 +36,6 @@ fn check_network_stats(netuid: u16) { ); } - #[test] fn test_stale_weights() { new_test_ext().execute_with(|| { @@ -47,13 +44,9 @@ fn test_stale_weights() { let subnet_params = SubspaceModule::subnet_params(netuid); let keys = SubspaceModule::get_keys(netuid); let uids = SubspaceModule::get_uids(netuid); - - }); } - - #[test] fn test_no_weights() { new_test_ext().execute_with(|| { @@ -100,12 +93,11 @@ fn test_dividends_same_stake() { // do a list of ones for weights let weight_uids: Vec = [2, 3].to_vec(); // do a list of ones for weights - let weight_values: Vec = [2,1].to_vec(); + let weight_values: Vec = [2, 1].to_vec(); set_weights(netuid, keys[0], weight_uids.clone(), weight_values.clone()); set_weights(netuid, keys[1], weight_uids.clone(), weight_values.clone()); - - let stakes_before : Vec = SubspaceModule::get_stakes(netuid); + let stakes_before: Vec = SubspaceModule::get_stakes(netuid); step_epoch(netuid); let incentives: Vec = SubspaceModule::get_incentives(netuid); let dividends: Vec = SubspaceModule::get_dividends(netuid); @@ -114,20 +106,30 @@ fn test_dividends_same_stake() { // evaluate votees assert!(incentives[2] > 0); - assert_eq!(dividends[2] , dividends[3]); - let delta : u64 = 100; - assert!((incentives[2] as u64) > (weight_values[0] as u64 * incentives[3] as u64) - delta); - assert!((incentives[2] as u64) < (weight_values[0] as u64 * incentives[3] as u64) + delta); + assert_eq!(dividends[2], dividends[3]); + let delta: u64 = 100; + assert!((incentives[2] as u64) > (weight_values[0] as u64 * incentives[3] as u64) - delta); + assert!((incentives[2] as u64) < (weight_values[0] as u64 * incentives[3] as u64) + delta); - assert!((emissions[2] as u64) > (weight_values[0] as u64 * emissions[3] as u64) - delta); - assert!((emissions[2] as u64) < (weight_values[0] as u64 * emissions[3] as u64) + delta); + assert!((emissions[2] as u64) > (weight_values[0] as u64 * emissions[3] as u64) - delta); + assert!((emissions[2] as u64) < (weight_values[0] as u64 * emissions[3] as u64) + delta); // evaluate voters - assert!(dividends[0] == dividends[1] , "dividends[0]: {} != dividends[1]: {}", dividends[0], dividends[1]); - assert!(dividends[0] == dividends[1] , "dividends[0]: {} != dividends[1]: {}", dividends[0], dividends[1]); - - assert_eq!(incentives[0],incentives[1]); - assert_eq!(dividends[2],dividends[3]); + assert!( + dividends[0] == dividends[1], + "dividends[0]: {} != dividends[1]: {}", + dividends[0], + dividends[1] + ); + assert!( + dividends[0] == dividends[1], + "dividends[0]: {} != dividends[1]: {}", + dividends[0], + dividends[1] + ); + + assert_eq!(incentives[0], incentives[1]); + assert_eq!(dividends[2], dividends[3]); println!("emissions: {:?}", emissions); @@ -163,7 +165,7 @@ fn test_dividends_diff_stake() { let n_list: Vec = vec![10, 50, 100, 1000]; let blocks_per_epoch_list: u64 = 1; let stake_per_module: u64 = 10_000; - let tempo : u16 = 100; + let tempo: u16 = 100; // SETUP NETWORK for i in 0..n { @@ -187,16 +189,15 @@ fn test_dividends_diff_stake() { // } let keys = SubspaceModule::get_keys(netuid); let uids = SubspaceModule::get_uids(netuid); - + // do a list of ones for weights let weight_uids: Vec = [2, 3].to_vec(); // do a list of ones for weights - let weight_values: Vec = [1,1].to_vec(); + let weight_values: Vec = [1, 1].to_vec(); set_weights(netuid, keys[0], weight_uids.clone(), weight_values.clone()); set_weights(netuid, keys[1], weight_uids.clone(), weight_values.clone()); - - let stakes_before : Vec = SubspaceModule::get_stakes(netuid); + let stakes_before: Vec = SubspaceModule::get_stakes(netuid); step_epoch(netuid); let incentives: Vec = SubspaceModule::get_incentives(netuid); let dividends: Vec = SubspaceModule::get_dividends(netuid); @@ -205,21 +206,21 @@ fn test_dividends_diff_stake() { // evaluate votees assert!(incentives[2] > 0); - assert_eq!(dividends[2] , dividends[3]); - let delta : u64 = 100; - assert!((incentives[2] as u64) > (weight_values[0] as u64 * incentives[3] as u64) - delta); - assert!((incentives[2] as u64) < (weight_values[0] as u64 * incentives[3] as u64) + delta); + assert_eq!(dividends[2], dividends[3]); + let delta: u64 = 100; + assert!((incentives[2] as u64) > (weight_values[0] as u64 * incentives[3] as u64) - delta); + assert!((incentives[2] as u64) < (weight_values[0] as u64 * incentives[3] as u64) + delta); - assert!((emissions[2] as u64) > (weight_values[0] as u64 * emissions[3] as u64) - delta); - assert!((emissions[2] as u64) < (weight_values[0] as u64 * emissions[3] as u64) + delta); + assert!((emissions[2] as u64) > (weight_values[0] as u64 * emissions[3] as u64) - delta); + assert!((emissions[2] as u64) < (weight_values[0] as u64 * emissions[3] as u64) + delta); // evaluate voters - let delta : u64 = 100; + let delta: u64 = 100; assert!((dividends[0] as u64) > (dividends[1] as u64 * 2) - delta); - assert!((dividends[0] as u64) < (dividends[1] as u64 * 2) + delta ); + assert!((dividends[0] as u64) < (dividends[1] as u64 * 2) + delta); - assert_eq!(incentives[0],incentives[1]); - assert_eq!(dividends[2],dividends[3]); + assert_eq!(incentives[0], incentives[1]); + assert_eq!(dividends[2], dividends[3]); println!("emissions: {:?}", emissions); @@ -246,7 +247,6 @@ fn test_dividends_diff_stake() { }); } - fn test_pruning() { new_test_ext().execute_with(|| { // CONSSTANTS @@ -324,7 +324,7 @@ fn test_lowest_priority_mechanism() { let n_list: Vec = vec![10, 50, 100, 1000]; let blocks_per_epoch_list: u64 = 1; let stake_per_module: u64 = 10_000; - let tempo : u16 = 100; + let tempo: u16 = 100; // SETUP NETWORK register_n_modules(netuid, n, stake_per_module); @@ -497,8 +497,6 @@ fn test_blocks_until_epoch() { }); } - - #[test] fn test_incentives() { new_test_ext().execute_with(|| { @@ -516,7 +514,6 @@ fn test_incentives() { params.max_allowed_weights = n; params.tempo = 100; - let keys = SubspaceModule::get_keys(netuid); let uids = SubspaceModule::get_uids(netuid); @@ -536,7 +533,6 @@ fn test_incentives() { assert!(incentives[1] == incentives[2]); assert!(emissions[1] == emissions[2]); - // do a list of ones for weights let weight_values: Vec = [1, 2].to_vec(); @@ -549,19 +545,18 @@ fn test_incentives() { let emissions: Vec = SubspaceModule::get_emissions(netuid); // evaluate votees - let delta : u64 = 100 * params.tempo as u64; + let delta: u64 = 100 * params.tempo as u64; assert!(incentives[1] > 0); - assert!(emissions[2] > 2 * emissions[1] - delta && - emissions[2] < 2 * emissions[1] + delta , - "emissions[1]: {} != emissions[2]: {}", emissions[1], emissions[2]); - - - + assert!( + emissions[2] > 2 * emissions[1] - delta && emissions[2] < 2 * emissions[1] + delta, + "emissions[1]: {} != emissions[2]: {}", + emissions[1], + emissions[2] + ); }); } - #[test] fn test_trust() { new_test_ext().execute_with(|| { @@ -583,7 +578,6 @@ fn test_trust() { SubspaceModule::set_subnet_params(netuid, params.clone()); - let keys = SubspaceModule::get_keys(netuid); let uids = SubspaceModule::get_uids(netuid); @@ -599,27 +593,21 @@ fn test_trust() { step_block(params.tempo); let trust: Vec = SubspaceModule::get_trust(netuid); - let emission : Vec = SubspaceModule::get_emissions(netuid); - + let emission: Vec = SubspaceModule::get_emissions(netuid); // evaluate votees println!("trust: {:?}", trust); assert!(trust[1] as u32 > 0); - assert!(trust[2] as u32 > 2*(trust[1] as u32) - 10 ); + assert!(trust[2] as u32 > 2 * (trust[1] as u32) - 10); // evaluate votees println!("trust: {:?}", emission); - assert!(emission[1] > 0); - assert!(emission[2] > 2*(emission[1] ) - 1000 ); + assert!(emission[1] > 0); + assert!(emission[2] > 2 * (emission[1]) - 1000); // assert!(trust[2] as u32 < 2*(trust[1] as u32) ); - - }); } - - - // TODO: // #[test] // fn simulation_final_boss() { @@ -849,82 +837,88 @@ fn test_trust() { // }); // } - - #[test] fn test_founder_share() { - new_test_ext().execute_with(|| { - - let netuid = 0; - let n = 20; - let initial_stake: u64 = 1000; - let keys : Vec = (0..n).into_iter().map(|x| U256::from(x)).collect(); - let stakes : Vec = (0..n).into_iter().map(|x| initial_stake * 1_000_000_000).collect(); - - - let founder_key = keys[0]; - for i in 0..n { - assert_ok!(register_module(netuid, keys[i], stakes[i])); - let stake_from_vector = SubspaceModule::get_stake_to_vector(netuid, &keys[i]); - println!("{:?}", stake_from_vector); - } - SubspaceModule::set_founder_share(netuid, 50); - let founder_share = SubspaceModule::get_founder_share(netuid); - let founder_ratio: f64 = founder_share as f64 / 100.0; - - let subnet_params = SubspaceModule::subnet_params(netuid); - - - let founder_stake_before = SubspaceModule::get_stake_for_key(netuid, &founder_key); - println!("founder_stake_before: {:?}", founder_stake_before); - // vote to avoid key[0] as we want to see the key[0] burn - step_epoch(netuid); - let total_emission = SubspaceModule::get_subnet_emission(netuid) * subnet_params.tempo as u64; - let expected_emission = total_emission as u64; - let expected_founder_share = (expected_emission as f64 * founder_ratio) as u64; - let emissions = SubspaceModule::get_emissions(netuid); - let dividends = SubspaceModule::get_dividends(netuid); - let incentives = SubspaceModule::get_incentives(netuid); - let total_dividends: u64 = dividends.iter().sum::() as u64; - let total_incentives : u64 = incentives.iter().sum::() as u64; - - println!("total_dividends: {:?}", total_dividends); - println!("total_incentives: {:?}", total_incentives); - let expected_emission_after_founder_share = expected_emission - expected_founder_share; - let founder_dividend_emission = ((dividends[0] as f64 / total_dividends as f64) * (expected_emission_after_founder_share/2) as f64) as u64; - let founder_incentive_emission = ((incentives[0] as f64 / total_incentives as f64) * (expected_emission_after_founder_share/2) as f64) as u64; - let founder_emission = founder_incentive_emission + founder_dividend_emission; - - println!("emissions: {:?}", emissions); - println!("dividends: {:?}", dividends); - println!("incentives: {:?}", incentives); - println!("founder_emission FAM: {:?}", founder_emission); - let calcualted_total_emission = emissions.iter().sum::(); - + new_test_ext().execute_with(|| { + let netuid = 0; + let n = 20; + let initial_stake: u64 = 1000; + let keys: Vec = (0..n).into_iter().map(|x| U256::from(x)).collect(); + let stakes: Vec = (0..n).into_iter().map(|x| initial_stake * 1_000_000_000).collect(); - let calculated_founder_share = SubspaceModule::get_stake_for_key(netuid, &founder_key) - founder_stake_before - founder_emission; - let delta: u64 = 100000; + let founder_key = keys[0]; + for i in 0..n { + assert_ok!(register_module(netuid, keys[i], stakes[i])); + let stake_from_vector = SubspaceModule::get_stake_to_vector(netuid, &keys[i]); + println!("{:?}", stake_from_vector); + } + SubspaceModule::set_founder_share(netuid, 50); + let founder_share = SubspaceModule::get_founder_share(netuid); + let founder_ratio: f64 = founder_share as f64 / 100.0; - - println!("expected_emission: {:?}", expected_emission); - println!("total_emission: {:?}", total_emission); - assert!(expected_emission > calcualted_total_emission - delta , "expected_emission: {} != calcualted_total_emission: {}", expected_emission, calcualted_total_emission); - assert!(expected_emission < calcualted_total_emission + delta , "expected_emission: {} != calcualted_total_emission: {}", expected_emission, calcualted_total_emission); + let subnet_params = SubspaceModule::subnet_params(netuid); - println!("expected_founder_share: {:?}", expected_founder_share); - assert!(expected_founder_share > calculated_founder_share - delta , "expected_founder_share: {} != calculated_founder_share: {}", expected_founder_share, calculated_founder_share); - assert!(expected_founder_share < calculated_founder_share + delta , "expected_founder_share: {} != calculated_founder_share: {}", expected_founder_share, calculated_founder_share); + let founder_stake_before = SubspaceModule::get_stake_for_key(netuid, &founder_key); + println!("founder_stake_before: {:?}", founder_stake_before); + // vote to avoid key[0] as we want to see the key[0] burn + step_epoch(netuid); + let total_emission = + SubspaceModule::get_subnet_emission(netuid) * subnet_params.tempo as u64; + let expected_emission = total_emission as u64; + let expected_founder_share = (expected_emission as f64 * founder_ratio) as u64; + let emissions = SubspaceModule::get_emissions(netuid); + let dividends = SubspaceModule::get_dividends(netuid); + let incentives = SubspaceModule::get_incentives(netuid); + let total_dividends: u64 = dividends.iter().sum::() as u64; + let total_incentives: u64 = incentives.iter().sum::() as u64; + + println!("total_dividends: {:?}", total_dividends); + println!("total_incentives: {:?}", total_incentives); + let expected_emission_after_founder_share = expected_emission - expected_founder_share; + let founder_dividend_emission = ((dividends[0] as f64 / total_dividends as f64) * + (expected_emission_after_founder_share / 2) as f64) as u64; + let founder_incentive_emission = ((incentives[0] as f64 / total_incentives as f64) * + (expected_emission_after_founder_share / 2) as f64) as u64; + let founder_emission = founder_incentive_emission + founder_dividend_emission; + println!("emissions: {:?}", emissions); + println!("dividends: {:?}", dividends); + println!("incentives: {:?}", incentives); + println!("founder_emission FAM: {:?}", founder_emission); + let calcualted_total_emission = emissions.iter().sum::(); + + let calculated_founder_share = SubspaceModule::get_stake_for_key(netuid, &founder_key) - + founder_stake_before - + founder_emission; + let delta: u64 = 100000; + + println!("expected_emission: {:?}", expected_emission); + println!("total_emission: {:?}", total_emission); + assert!( + expected_emission > calcualted_total_emission - delta, + "expected_emission: {} != calcualted_total_emission: {}", + expected_emission, + calcualted_total_emission + ); + assert!( + expected_emission < calcualted_total_emission + delta, + "expected_emission: {} != calcualted_total_emission: {}", + expected_emission, + calcualted_total_emission + ); + + println!("expected_founder_share: {:?}", expected_founder_share); + assert!( + expected_founder_share > calculated_founder_share - delta, + "expected_founder_share: {} != calculated_founder_share: {}", + expected_founder_share, + calculated_founder_share + ); + assert!( + expected_founder_share < calculated_founder_share + delta, + "expected_founder_share: {} != calculated_founder_share: {}", + expected_founder_share, + calculated_founder_share + ); }); - - - - } - - - - - - - diff --git a/pallets/subspace/tests/test_subnets.rs b/pallets/subspace/tests/test_subnets.rs index 5f7742029..f094d4587 100644 --- a/pallets/subspace/tests/test_subnets.rs +++ b/pallets/subspace/tests/test_subnets.rs @@ -6,8 +6,8 @@ use frame_support::{ use frame_system::Config; use pallet_subspace::Error; use sp_core::U256; -use test_mock::*; use sp_std::vec; +use test_mock::*; /* TO DO SAM: write test for LatuUpdate after it is set */ @@ -21,13 +21,12 @@ fn test_add_subnets() { let mut n = 20; let num_subnets: u16 = n; - - for i in 0..num_subnets{ + for i in 0..num_subnets { register_module(i, U256::from(i), stake_per_module); for j in 0..n { if j != i { let n = SubspaceModule::get_subnet_n(i); - println!("registering module i:{} j:{} n:{}", i,j,n); + println!("registering module i:{} j:{} n:{}", i, j, n); register_module(i, U256::from(j), stake_per_module); } } @@ -42,30 +41,27 @@ fn test_add_subnets() { expected_subnets, "number of subnets is not equal to expected subnets" ); - } - - for netuid in 0..num_subnets { let total_stake = SubspaceModule::get_total_subnet_stake(netuid); let total_balance = SubspaceModule::get_total_subnet_balance(netuid); let total_tokens_before = total_stake + total_balance; - + let keys = SubspaceModule::get_keys(netuid); println!("total stake {}", total_stake); println!("total balance {}", total_balance); println!("total tokens before {}", total_tokens_before); - assert_eq!(keys.len() as u16, n); assert!(SubspaceModule::check_subnet_storage(netuid)); SubspaceModule::remove_subnet(netuid); assert_eq!(SubspaceModule::get_subnet_n(netuid), 0); assert!(SubspaceModule::check_subnet_storage(netuid)); - let total_tokens_after: u64 = keys.iter().map(|key| SubspaceModule::get_balance_u64(key)).sum(); + let total_tokens_after: u64 = + keys.iter().map(|key| SubspaceModule::get_balance_u64(key)).sum(); println!("total tokens after {}", total_tokens_after); assert_eq!(total_tokens_after, total_tokens_before); @@ -75,14 +71,10 @@ fn test_add_subnets() { expected_subnets, "number of subnets is not equal to expected subnets" ); - - } - }); } - fn test_set_single_temple(tempo: u16) { new_test_ext().execute_with(|| { // creates a subnet when you register a module @@ -100,13 +92,10 @@ fn test_set_single_temple(tempo: u16) { let tempo = 5; let min_stake = 1_000_000_000; - let result = SubspaceModule::set_subnet_params( - netuid, - params.clone() - ); + let result = SubspaceModule::set_subnet_params(netuid, params.clone()); let subnet_params = SubspaceModule::subnet_params(netuid); - + assert_eq!(subnet_params.tempo, tempo); assert_eq!(subnet_params.min_stake, min_stake); assert_eq!(subnet_params.max_allowed_uids, params.max_allowed_uids); @@ -298,8 +287,14 @@ fn test_set_max_allowed_uids_shrinking() { assert_ok!(result); let params = SubspaceModule::subnet_params(netuid); let mut n = SubspaceModule::get_subnet_n(netuid); - assert_eq!(params.max_allowed_uids, max_uids, "max allowed uids is not equal to expected max allowed uids"); - assert_eq!(params.max_allowed_uids, n, "min allowed weights is not equal to expected min allowed weights"); + assert_eq!( + params.max_allowed_uids, max_uids, + "max allowed uids is not equal to expected max allowed uids" + ); + assert_eq!( + params.max_allowed_uids, n, + "min allowed weights is not equal to expected min allowed weights" + ); let mut new_total_subnet_balance: u64 = 0; for key in og_keys.clone() { @@ -307,15 +302,14 @@ fn test_set_max_allowed_uids_shrinking() { new_total_subnet_balance + SubspaceModule::get_balance_u64(&key); } // let expected_total_subnet_balance: u64 = - // (extra_uids as u64) * (stake + 1) + max_uids as u64; // this is weitd, but we needed to add 1 to make sure that the stake is not 0 - // assert!( + // (extra_uids as u64) * (stake + 1) + max_uids as u64; // this is weitd, but we needed to + // add 1 to make sure that the stake is not 0 assert!( // new_total_subnet_balance == expected_total_subnet_balance, // "new total subnet balance {} is not equal to expected total subnet balance {}", // new_total_subnet_balance, // expected_total_subnet_balance // ); - n = SubspaceModule::get_subnet_n(netuid); let stake_vector: Vec = SubspaceModule::get_stakes(netuid); let calc_stake: u64 = stake_vector.iter().sum(); @@ -343,50 +337,52 @@ fn test_set_max_allowed_modules() { SubspaceModule::set_max_allowed_modules(max_allowed_modules); // set max_total modules - for i in 1..(2*max_allowed_modules ) { + for i in 1..(2 * max_allowed_modules) { assert_ok!(register_module(netuid, U256::from(i), stake)); n = SubspaceModule::get_subnet_n(netuid); - assert!(n <= max_allowed_modules,"subnet_n {:?} is not less than max_allowed_modules {:?}", n, max_allowed_modules); + assert!( + n <= max_allowed_modules, + "subnet_n {:?} is not less than max_allowed_modules {:?}", + n, + max_allowed_modules + ); } }) - } - - #[test] - fn test_global_max_allowed_subnets() { - new_test_ext().execute_with(|| { - let max_allowed_subnets: u16 = 100; - - let mut params = SubspaceModule::global_params().clone(); - params.max_allowed_subnets = max_allowed_subnets; - SubspaceModule::set_global_params(params); - let params = SubspaceModule::global_params(); - assert_eq!(params.max_allowed_subnets, max_allowed_subnets); - let mut stake: u64 = 1_000_000_000; - - // set max_total modules - - for i in 1..(2*max_allowed_subnets ) { - let netuid = i as u16; - stake = stake + i as u64; - let least_staked_netuid = SubspaceModule::least_staked_netuid(); - - if i > 1 { - println!("least staked netuid {}", least_staked_netuid); - assert!(SubspaceModule::if_subnet_exist(least_staked_netuid)); +} - } +#[test] +fn test_global_max_allowed_subnets() { + new_test_ext().execute_with(|| { + let max_allowed_subnets: u16 = 100; + + let mut params = SubspaceModule::global_params().clone(); + params.max_allowed_subnets = max_allowed_subnets; + SubspaceModule::set_global_params(params); + let params = SubspaceModule::global_params(); + assert_eq!(params.max_allowed_subnets, max_allowed_subnets); + let mut stake: u64 = 1_000_000_000; - assert_ok!(register_module(netuid, U256::from(i), stake)); - let n_subnets = SubspaceModule::num_subnets(); + // set max_total modules + for i in 1..(2 * max_allowed_subnets) { + let netuid = i as u16; + stake = stake + i as u64; + let least_staked_netuid = SubspaceModule::least_staked_netuid(); - if i > max_allowed_subnets { - assert!(!SubspaceModule::if_subnet_exist(least_staked_netuid)); + if i > 1 { + println!("least staked netuid {}", least_staked_netuid); + assert!(SubspaceModule::if_subnet_exist(least_staked_netuid)); + } - } - println!("n_subnets {}", n_subnets); - println!("max_allowed_subnets {}", max_allowed_subnets); - assert!(n_subnets <= max_allowed_subnets); + assert_ok!(register_module(netuid, U256::from(i), stake)); + let n_subnets = SubspaceModule::num_subnets(); + + if i > max_allowed_subnets { + assert!(!SubspaceModule::if_subnet_exist(least_staked_netuid)); } - }) - } \ No newline at end of file + println!("n_subnets {}", n_subnets); + println!("max_allowed_subnets {}", max_allowed_subnets); + assert!(n_subnets <= max_allowed_subnets); + } + }) +} diff --git a/pallets/subspace/tests/test_voting.rs b/pallets/subspace/tests/test_voting.rs index 0a406db42..6255430b8 100644 --- a/pallets/subspace/tests/test_voting.rs +++ b/pallets/subspace/tests/test_voting.rs @@ -6,232 +6,210 @@ use frame_support::{ use frame_system::Config; use pallet_subspace::Error; use sp_core::U256; -use test_mock::*; use sp_std::vec; +use test_mock::*; /* TO DO SAM: write test for LatuUpdate after it is set */ #[test] fn test_subnet_porposal() { - new_test_ext().execute_with(|| { - - let netuid = 0; - let keys = vec![U256::from(0), U256::from(1), U256::from(2)]; - let stakes= vec![1_000_000_000, 1_000_000_000, 1_000_000_000]; - - for (i, key) in keys.iter().enumerate() { - assert_ok!(register_module(netuid, *key, stakes[i])); - } - let mut params = SubspaceModule::subnet_params(netuid); - assert_eq!(params.vote_mode, "authority".as_bytes().to_vec(), "vote mode not set"); - params.vote_mode = "stake".as_bytes().to_vec(); - println!("params: {:?}", params); - SubspaceModule::set_subnet_params(netuid, params.clone()); - let mut params = SubspaceModule::subnet_params(netuid); - let initial_tempo = params.tempo; - let final_tempo = 1000; - params.tempo = final_tempo; - - - - assert_eq!(params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set"); - assert_ok!(SubspaceModule::do_add_subnet_proposal( - get_origin(keys[0]), - netuid, - params - )); - // we have not passed the threshold yet - let proposals = SubspaceModule::get_subnet_proposals(netuid); - - println!("proposals: {:?}", proposals); - - assert_eq!(proposals.len(), 1, "proposal not added"); - assert_eq!(proposals[0].votes, stakes[0], "proposal not added"); - - let proposal = SubspaceModule::get_proposal(0); - assert_eq!(proposal.netuid, netuid, "proposal not added"); - assert_eq!(proposal.accepted, false, "proposal not added"); - // now vote for the proposal - - assert_ok!(SubspaceModule::vote_proposal(get_origin(keys[1]), 0)); - let proposal = SubspaceModule::get_proposal(0); - assert_eq!(proposal.votes, stakes[0] + stakes[1], "proposal not voted"); - assert_eq!(proposal.accepted, true, "proposal not voted"); - - println!("proposal: {:?}", proposal); - - let params = SubspaceModule::subnet_params(netuid); - assert_eq!(params.tempo, final_tempo, "proposal not voted"); + new_test_ext().execute_with(|| { + let netuid = 0; + let keys = vec![U256::from(0), U256::from(1), U256::from(2)]; + let stakes = vec![1_000_000_000, 1_000_000_000, 1_000_000_000]; - }); - -} - -#[test] -fn test_max_proposals() { - new_test_ext().execute_with(|| { - - let netuid = 0; - let n = 100; - let keys: Vec = (0..n).into_iter().map(|x| U256::from(x)).collect(); - let mut stakes= vec![1_000_000_000; n]; - // increase incrementally to avoid overflow - let mut stakes = stakes.iter_mut().enumerate().map(|(i, x)| *x + i as u64).collect::>(); - - for (i, key) in keys.iter().enumerate() { - assert_ok!(register_module(netuid, *key, stakes[i])); - } - - let mut params = SubspaceModule::global_params(); - assert_eq!(params.vote_mode, "authority".as_bytes().to_vec(), "vote mode not set"); - params.vote_mode = "stake".as_bytes().to_vec(); - params.max_proposals = (n / 2) as u64; - println!("params: {:?}", params); - SubspaceModule::set_global_params(params.clone()); - - assert_eq!(params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set"); - let max_proposals = SubspaceModule::get_max_proposals(); - let modes = ["authority".as_bytes().to_vec(), "stake".as_bytes().to_vec()]; - - let mut subnet_params = SubspaceModule::subnet_params(netuid); - subnet_params.vote_mode = "stake".as_bytes().to_vec(); - SubspaceModule::set_subnet_params(netuid, subnet_params.clone()); - subnet_params = SubspaceModule::subnet_params(netuid); - assert_eq!(subnet_params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set"); - - for i in 0..n { - if i % 2 == 0 { - assert_ok!(SubspaceModule::do_add_global_proposal(get_origin(keys[i as usize]), params.clone())); - } else { - assert_ok!(SubspaceModule::do_add_subnet_proposal(get_origin(keys[i as usize]), netuid, subnet_params.clone())); - } - let num_proposals = SubspaceModule::num_proposals(); - let proposals = SubspaceModule::get_global_proposals(); - let has_max_proposals = SubspaceModule::has_max_proposals(); - // assert_eq!(max_proposals, (n - 1) as u64, "proposal not added"); - println!("max_proposals: {:?}", max_proposals); - println!("has_max_proposals: {:?}", has_max_proposals); - println!("num_proposals: {:?}", num_proposals); - println!("proposals: {:?}", proposals.len()); - - let num_subnet_proposals = SubspaceModule::num_subnet_proposals(netuid); - let num_global_proposals = SubspaceModule::num_global_proposals(); - assert_eq!(num_subnet_proposals + num_global_proposals, num_proposals, "proposal not added"); - - if num_proposals >= max_proposals { - assert_eq!(SubspaceModule::has_max_proposals(), true, "proposal not added"); - } else { - assert_eq!(SubspaceModule::has_max_proposals(), false, "proposal not added"); + for (i, key) in keys.iter().enumerate() { + assert_ok!(register_module(netuid, *key, stakes[i])); } + let mut params = SubspaceModule::subnet_params(netuid); + assert_eq!(params.vote_mode, "authority".as_bytes().to_vec(), "vote mode not set"); + params.vote_mode = "stake".as_bytes().to_vec(); + println!("params: {:?}", params); + SubspaceModule::set_subnet_params(netuid, params.clone()); + let mut params = SubspaceModule::subnet_params(netuid); + let initial_tempo = params.tempo; + let final_tempo = 1000; + params.tempo = final_tempo; + + assert_eq!(params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set"); + assert_ok!(SubspaceModule::do_add_subnet_proposal(get_origin(keys[0]), netuid, params)); + // we have not passed the threshold yet + let proposals = SubspaceModule::get_subnet_proposals(netuid); - assert!(proposals.len() as u64 <= max_proposals, "proposal not added"); - } + println!("proposals: {:?}", proposals); - assert_eq!(SubspaceModule::has_max_proposals(), true, "proposal not added"); - assert_eq!(SubspaceModule::num_proposals(), max_proposals, "proposal not added"); + assert_eq!(proposals.len(), 1, "proposal not added"); + assert_eq!(proposals[0].votes, stakes[0], "proposal not added"); + let proposal = SubspaceModule::get_proposal(0); + assert_eq!(proposal.netuid, netuid, "proposal not added"); + assert_eq!(proposal.accepted, false, "proposal not added"); + // now vote for the proposal + assert_ok!(SubspaceModule::vote_proposal(get_origin(keys[1]), 0)); + let proposal = SubspaceModule::get_proposal(0); + assert_eq!(proposal.votes, stakes[0] + stakes[1], "proposal not voted"); + assert_eq!(proposal.accepted, true, "proposal not voted"); - }); + println!("proposal: {:?}", proposal); + let params = SubspaceModule::subnet_params(netuid); + assert_eq!(params.tempo, final_tempo, "proposal not voted"); + }); } +#[test] +fn test_max_proposals() { + new_test_ext().execute_with(|| { + let netuid = 0; + let n = 100; + let keys: Vec = (0..n).into_iter().map(|x| U256::from(x)).collect(); + let mut stakes = vec![1_000_000_000; n]; + // increase incrementally to avoid overflow + let mut stakes = + stakes.iter_mut().enumerate().map(|(i, x)| *x + i as u64).collect::>(); + + for (i, key) in keys.iter().enumerate() { + assert_ok!(register_module(netuid, *key, stakes[i])); + } + let mut params = SubspaceModule::global_params(); + assert_eq!(params.vote_mode, "authority".as_bytes().to_vec(), "vote mode not set"); + params.vote_mode = "stake".as_bytes().to_vec(); + params.max_proposals = (n / 2) as u64; + println!("params: {:?}", params); + SubspaceModule::set_global_params(params.clone()); + + assert_eq!(params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set"); + let max_proposals = SubspaceModule::get_max_proposals(); + let modes = ["authority".as_bytes().to_vec(), "stake".as_bytes().to_vec()]; + + let mut subnet_params = SubspaceModule::subnet_params(netuid); + subnet_params.vote_mode = "stake".as_bytes().to_vec(); + SubspaceModule::set_subnet_params(netuid, subnet_params.clone()); + subnet_params = SubspaceModule::subnet_params(netuid); + assert_eq!(subnet_params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set"); + + for i in 0..n { + if i % 2 == 0 { + assert_ok!(SubspaceModule::do_add_global_proposal( + get_origin(keys[i as usize]), + params.clone() + )); + } else { + assert_ok!(SubspaceModule::do_add_subnet_proposal( + get_origin(keys[i as usize]), + netuid, + subnet_params.clone() + )); + } + let num_proposals = SubspaceModule::num_proposals(); + let proposals = SubspaceModule::get_global_proposals(); + let has_max_proposals = SubspaceModule::has_max_proposals(); + // assert_eq!(max_proposals, (n - 1) as u64, "proposal not added"); + println!("max_proposals: {:?}", max_proposals); + println!("has_max_proposals: {:?}", has_max_proposals); + println!("num_proposals: {:?}", num_proposals); + println!("proposals: {:?}", proposals.len()); + + let num_subnet_proposals = SubspaceModule::num_subnet_proposals(netuid); + let num_global_proposals = SubspaceModule::num_global_proposals(); + assert_eq!( + num_subnet_proposals + num_global_proposals, + num_proposals, + "proposal not added" + ); + + if num_proposals >= max_proposals { + assert_eq!(SubspaceModule::has_max_proposals(), true, "proposal not added"); + } else { + assert_eq!(SubspaceModule::has_max_proposals(), false, "proposal not added"); + } + + assert!(proposals.len() as u64 <= max_proposals, "proposal not added"); + } + assert_eq!(SubspaceModule::has_max_proposals(), true, "proposal not added"); + assert_eq!(SubspaceModule::num_proposals(), max_proposals, "proposal not added"); + }); +} #[test] fn test_global_porposal() { - new_test_ext().execute_with(|| { - - let netuid = 0; - let keys = vec![U256::from(1), U256::from(2), U256::from(3)]; - let stakes= vec![1_000_000_000, 1_000_000_000, 1_000_000_000]; - - // register on seperate subnets - for (i, key) in keys.iter().enumerate() { - register_module(netuid + i as u16 , *key, stakes[i]); - } - - let mut params = SubspaceModule::global_params(); - let initial_max_registrations_per_block = params.max_registrations_per_block; - let max_registrations_per_block = 1000; - - params.max_registrations_per_block = max_registrations_per_block; - assert_ok!(SubspaceModule::do_add_global_proposal(get_origin(keys[0]), - params - )); - - - - // we have not passed the threshold yet - let proposals = SubspaceModule::get_global_proposals(); - - assert_eq!(proposals.len(), 1, "proposal not added"); - assert_eq!(proposals[0].votes, stakes[0], "proposal not added"); - - let proposal = SubspaceModule::get_proposal(0); - assert_eq!(proposal.accepted, false, "proposal not added"); + new_test_ext().execute_with(|| { + let netuid = 0; + let keys = vec![U256::from(1), U256::from(2), U256::from(3)]; + let stakes = vec![1_000_000_000, 1_000_000_000, 1_000_000_000]; + + // register on seperate subnets + for (i, key) in keys.iter().enumerate() { + register_module(netuid + i as u16, *key, stakes[i]); + } - // now vote for the proposal + let mut params = SubspaceModule::global_params(); + let initial_max_registrations_per_block = params.max_registrations_per_block; + let max_registrations_per_block = 1000; - assert_ok!(SubspaceModule::vote_proposal(get_origin(keys[1]), 0)); - let proposal = SubspaceModule::get_proposal(0); - assert_eq!(proposal.votes, stakes[0] + stakes[1], "proposal not voted"); - assert_eq!(proposal.accepted, true, "proposal not voted"); + params.max_registrations_per_block = max_registrations_per_block; + assert_ok!(SubspaceModule::do_add_global_proposal(get_origin(keys[0]), params)); - println!("proposal: {:?}", proposal); + // we have not passed the threshold yet + let proposals = SubspaceModule::get_global_proposals(); - let params = SubspaceModule::global_params(); - assert_eq!(params.max_registrations_per_block, max_registrations_per_block, "proposal not voted"); + assert_eq!(proposals.len(), 1, "proposal not added"); + assert_eq!(proposals[0].votes, stakes[0], "proposal not added"); + let proposal = SubspaceModule::get_proposal(0); + assert_eq!(proposal.accepted, false, "proposal not added"); - }); + // now vote for the proposal -} + assert_ok!(SubspaceModule::vote_proposal(get_origin(keys[1]), 0)); + let proposal = SubspaceModule::get_proposal(0); + assert_eq!(proposal.votes, stakes[0] + stakes[1], "proposal not voted"); + assert_eq!(proposal.accepted, true, "proposal not voted"); + println!("proposal: {:?}", proposal); + let params = SubspaceModule::global_params(); + assert_eq!( + params.max_registrations_per_block, max_registrations_per_block, + "proposal not voted" + ); + }); +} #[test] fn test_unvote() { - new_test_ext().execute_with(|| { - - let netuid = 0; - let keys = vec![U256::from(0), U256::from(1), U256::from(2)]; - let stakes= vec![1_000_000_000, 1_000_000_000, 1_000_000_000]; - - for (i, key) in keys.iter().enumerate() { - assert_ok!(register_module(netuid, *key, stakes[i])); - } - let mut params = SubspaceModule::subnet_params(netuid); - assert_eq!(params.vote_mode, "authority".as_bytes().to_vec(), "vote mode not set"); - params.vote_mode = "stake".as_bytes().to_vec(); - println!("params: {:?}", params); - SubspaceModule::set_subnet_params(netuid, params.clone()); - let mut params = SubspaceModule::subnet_params(netuid); - let initial_tempo = params.tempo; - let final_tempo = 1000; - params.tempo = final_tempo; - - assert_eq!(params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set"); - assert_ok!(SubspaceModule::do_add_subnet_proposal(get_origin(keys[0]), netuid, - params - - )); - assert!(SubspaceModule::proposal_exists(0)); - assert!(SubspaceModule::is_proposal_owner(&keys[0], 0)); - assert_ok!(SubspaceModule::unvote_proposal(get_origin(keys[0]))); - - // we have not passed the threshold yet - let proposals = SubspaceModule::get_subnet_proposals(netuid); - - println!("proposals: {:?}", proposals); - - assert_eq!(proposals.len(), 0, "proposal not added"); - - }); - -} + new_test_ext().execute_with(|| { + let netuid = 0; + let keys = vec![U256::from(0), U256::from(1), U256::from(2)]; + let stakes = vec![1_000_000_000, 1_000_000_000, 1_000_000_000]; + for (i, key) in keys.iter().enumerate() { + assert_ok!(register_module(netuid, *key, stakes[i])); + } + let mut params = SubspaceModule::subnet_params(netuid); + assert_eq!(params.vote_mode, "authority".as_bytes().to_vec(), "vote mode not set"); + params.vote_mode = "stake".as_bytes().to_vec(); + println!("params: {:?}", params); + SubspaceModule::set_subnet_params(netuid, params.clone()); + let mut params = SubspaceModule::subnet_params(netuid); + let initial_tempo = params.tempo; + let final_tempo = 1000; + params.tempo = final_tempo; + + assert_eq!(params.vote_mode, "stake".as_bytes().to_vec(), "vote mode not set"); + assert_ok!(SubspaceModule::do_add_subnet_proposal(get_origin(keys[0]), netuid, params)); + assert!(SubspaceModule::proposal_exists(0)); + assert!(SubspaceModule::is_proposal_owner(&keys[0], 0)); + assert_ok!(SubspaceModule::unvote_proposal(get_origin(keys[0]))); + // we have not passed the threshold yet + let proposals = SubspaceModule::get_subnet_proposals(netuid); + println!("proposals: {:?}", proposals); + assert_eq!(proposals.len(), 0, "proposal not added"); + }); +} diff --git a/pallets/subspace/tests/weights.rs b/pallets/subspace/tests/weights.rs index ac1c2e632..6301c8f2a 100644 --- a/pallets/subspace/tests/weights.rs +++ b/pallets/subspace/tests/weights.rs @@ -14,8 +14,6 @@ use test_mock::*; pub fn set_weights() tests *****************************/ - - // Test ensures that uids -- weights must have the same size. #[test] fn test_weights_err_weights_vec_not_equal_size() { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 29aa341d5..0221a2e5d 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -7,7 +7,7 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); -use codec::{Encode, Decode}; +use codec::{Decode, Encode}; use pallet_grandpa::{ fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList, }; @@ -18,21 +18,22 @@ use smallvec::smallvec; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_core::{ - crypto::{KeyTypeId, ByteArray}, + crypto::{ByteArray, KeyTypeId}, OpaqueMetadata, H160, H256, U256, }; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{ - AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, One, Verify, - Dispatchable, IdentityLookup, PostDispatchInfoOf, UniqueSaturatedInto, DispatchInfoOf + AccountIdLookup, BlakeTwo256, Block as BlockT, DispatchInfoOf, Dispatchable, + IdentifyAccount, IdentityLookup, NumberFor, One, PostDispatchInfoOf, UniqueSaturatedInto, + Verify, }, transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError}, - ApplyExtrinsicResult, MultiSignature, ConsensusEngineId, + ApplyExtrinsicResult, ConsensusEngineId, MultiSignature, }; use sp_std::{marker::PhantomData, prelude::*}; -use subspace_runtime_api::{ ModuleInfo, ModuleStats, ModuleParams }; +use subspace_runtime_api::{ModuleInfo, ModuleParams, ModuleStats}; #[cfg(feature = "std")] use sp_version::NativeVersion; @@ -42,12 +43,13 @@ use sp_version::RuntimeVersion; pub use frame_support::{ construct_runtime, parameter_types, traits::{ - ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, KeyOwnerProofSystem, Randomness, - StorageInfo, FindAuthor, OnFinalize + ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, FindAuthor, KeyOwnerProofSystem, + OnFinalize, Randomness, StorageInfo, }, weights::{ constants::{ - BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND, WEIGHT_REF_TIME_PER_MILLIS + BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_REF_TIME_PER_MILLIS, + WEIGHT_REF_TIME_PER_SECOND, }, IdentityFee, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, @@ -72,9 +74,7 @@ use fp_rpc::TransactionStatus; use pallet_ethereum::{ Call::transact, PostLogContent, Transaction as EthereumTransaction, TransactionAction, }; -use pallet_evm::{ - Account as EVMAccount, FeeCalculator, Runner, -}; +use pallet_evm::{Account as EVMAccount, FeeCalculator, Runner}; pub mod precompiles; use precompiles::FrontierPrecompiles; @@ -362,10 +362,8 @@ impl pallet_subspace::Config for Runtime { // EVM Support pub const WEIGHT_MILLISECS_PER_BLOCK: u64 = 2000; -pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( - WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS, - u64::MAX, -); +pub const MAXIMUM_BLOCK_WEIGHT: Weight = + Weight::from_parts(WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS, u64::MAX); pub const MAXIMUM_BLOCK_LENGTH: u32 = 5 * 1024 * 1024; impl pallet_evm_chain_id::Config for Runtime {} @@ -537,7 +535,6 @@ pub type Executive = frame_executive::Executive< AllPalletsWithSystem, >; - impl fp_self_contained::SelfContainedCall for RuntimeCall { type SignedInfo = H160; @@ -574,9 +571,8 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { len: usize, ) -> Option> { match self { - RuntimeCall::Ethereum(call) => { - call.pre_dispatch_self_contained(info, dispatch_info, len) - } + RuntimeCall::Ethereum(call) => + call.pre_dispatch_self_contained(info, dispatch_info, len), _ => None, } } @@ -586,11 +582,10 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { info: Self::SignedInfo, ) -> Option>> { match self { - call @ RuntimeCall::Ethereum(pallet_ethereum::Call::transact { .. }) => { + call @ RuntimeCall::Ethereum(pallet_ethereum::Call::transact { .. }) => Some(call.dispatch(RuntimeOrigin::from( pallet_ethereum::RawOrigin::EthereumTransaction(info), - ))) - } + ))), _ => None, } } @@ -822,7 +817,7 @@ impl_runtime_apis! { access_list: Option)>>, ) -> Result { use pallet_evm::GasWeightMapping; - + let config = if estimate { let mut config = ::config().clone(); config.estimate = true; diff --git a/runtime/src/precompiles.rs b/runtime/src/precompiles.rs index c312fe0af..5b753ac7a 100644 --- a/runtime/src/precompiles.rs +++ b/runtime/src/precompiles.rs @@ -18,17 +18,7 @@ where Self(Default::default()) } pub fn used_addresses() -> impl Iterator { - sp_std::vec![ - 1, - 2, - 3, - 4, - 5, - 1024, - 1025, - ] - .into_iter() - .map(hash) + sp_std::vec![1, 2, 3, 4, 5, 1024, 1025,].into_iter().map(hash) } } impl PrecompileSet for FrontierPrecompiles @@ -60,4 +50,4 @@ where fn hash(a: u64) -> H160 { H160::from_low_u64_be(a) -} \ No newline at end of file +}