From f60f68fd196fcf6a286d550fd8543cc7df9721b8 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 16 Aug 2024 14:01:59 +0400 Subject: [PATCH 1/2] chore: give funds to alice , bob and charlie --- node/src/chain_spec/raonet.rs | 34 ++++++++++++++++++++++++++++++++++ scripts/specs/raonet.json | 5 ++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/node/src/chain_spec/raonet.rs b/node/src/chain_spec/raonet.rs index 99c4eb327..1266bd8c8 100644 --- a/node/src/chain_spec/raonet.rs +++ b/node/src/chain_spec/raonet.rs @@ -3,6 +3,17 @@ use super::*; +/// Generates the configuration for the Raonet chain. +/// +/// # Returns +/// +/// * `Result` - The chain specification or an error message. +/// +/// # Example +/// +/// ```rust +/// let chain_spec = raonet_config().expect("Failed to generate Raonet config"); +/// ``` pub fn raonet_config() -> Result { let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?; @@ -44,11 +55,34 @@ pub fn raonet_config() -> Result { .build()) } +/// Generates the genesis configuration for the Raonet chain. +/// +/// # Arguments +/// +/// * `initial_authorities` - A vector of initial authorities (AuraId, GrandpaId). +/// * `_enable_println` - A boolean flag to enable println (currently unused). +/// +/// # Returns +/// +/// * `serde_json::Value` - The genesis configuration as a JSON value. fn raonet_genesis( initial_authorities: Vec<(AuraId, GrandpaId)>, _enable_println: bool, ) -> serde_json::Value { let mut balances = vec![ + // Add Alice, Bob, and Charlie with 10 trillion tokens each + ( + get_account_id_from_seed::("Alice"), + 10_000_000_000_000u128, + ), + ( + get_account_id_from_seed::("Bob"), + 10_000_000_000_000u128, + ), + ( + get_account_id_from_seed::("Charlie"), + 10_000_000_000_000u128, + ), ( ::from_ss58check("5FRo4vab84LM3aiK4DijnVawGDKagLGLzfn95j9tjDaHja8Z").unwrap(), 100_000_000_000u128, diff --git a/scripts/specs/raonet.json b/scripts/specs/raonet.json index c233f949d..9616108bb 100644 --- a/scripts/specs/raonet.json +++ b/scripts/specs/raonet.json @@ -29,6 +29,7 @@ "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da91b167e23cbebb7878d58f0cfbe24fade1e7baeac6e4320c5c6b73a5eb854500b7ff2b8687ed317db704b4abad0a45f65": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da938dd096bcb24cc8c4e13c4599ef5c3b81493fb01f6bc22dc61058060bda668bd9176f1d65b6d66d2049e89977e5bd668": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9429fc86d7333582b998a35c8896a77ce3ede627d972b461687bb5b61e787755834a8766c2a97cdad7c3d0057b21e931e": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", + "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da94f9aea1afa791265fae359272badc1cf8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48": "0x0000000000000000010000000000000000a0724e180900000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da964e5f34e3743dc8069c20d13b30c5fbddc84503cd7f06da23828bf915f198ea549d0d4f4398c32c4d84154e819a8c23b": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da96a55c6839a54c8ca50de2626b6f039dce0730733a54b0595ceddaf2b0d4089a777c1dd8bdeba334656beda1567e7bc0c": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9716d4f62e450a2bd8928b2962b8c0501e41ff5d2aa23e35a4830fdd5a4191abb9c9c804f0aa315d257bcd132e91a332d": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", @@ -39,8 +40,10 @@ "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da98f65b4d1e52ab839c9d2ac69e6a0e41f3e31d977c148833c83e2ee95e74034a1b3b32946e5c5ae4ae9ce4d1ac2f0b444": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9a027f81e14ea99ce4073009c0d87ae906a7952ece498544ab3bd8978d5350f0f475846289d80dceafca38070c9f27832": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9a7a93cd301b14fb55b5551dbf15d9f361ccfe35ab623e7eaec1590656d36a74bc8582dc3f1f769a1838d075ff9e1cd6a": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", + "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9b0edae20838083f2cde1c4080db8cf8090b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22": "0x0000000000000000010000000000000000a0724e180900000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9b65645a40eeb88fbfc80422e63f5bb88ba5aad47261de88951fda9fd78998be184cd208349267ad95320470daaf0694b": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9cfa073cf1f3eb4ae5db2029ea6f52ebc9095a72dd4aa58af5f43914ae500754b4bc20d89ba9c44e84eb386f222b77b6a": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", + "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9de1e86a9a8c739864cf3cc5ec2bea59fd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d": "0x0000000000000000010000000000000000a0724e180900000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9df3cf604c2c25efbc864feb8f6d4feed48ea0f98e9c83ff536e748efa8cc4a708009d9896c9adf2910395a22742dcb38": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9e99a494997e77bbb23d2c85322a5f7e5d4974d8d2d4c8f7005f81c70f222738c6d37010ae219037481fe75c5cceb886f": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", "0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9f128d73105ad2727b45028c061fe9141b099776c4958437cd72bfefe9f73788f3a06734dc9c4350d2acc886eecabb10b": "0x0000000000000000010000000000000000e87648170000000000000000000000000000000000000000000000000000000000000000000080", @@ -83,7 +86,7 @@ "0xb8c7f96c134ebb49eb7e77df71f098ad4e7b9012096b41c4eb3aaf947f6ea429": "0x0000", "0xbd2a529379475088d3e29a918cd478724e7b9012096b41c4eb3aaf947f6ea429": "0x0000", "0xc2261276cc9d1f8598ea4b6a74b15c2f4e7b9012096b41c4eb3aaf947f6ea429": "0x0100", - "0xc2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80": "0x0008c1f1e8010000", + "0xc2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80": "0x00e818dd311d0000", "0xca407206ec1ab726b2636c4b145ac2874e7b9012096b41c4eb3aaf947f6ea429": "0x0000", "0xd5e1a2fa16732ce6906189438c0a82c64e7b9012096b41c4eb3aaf947f6ea429": "0x0000", "0xd8f314b7f4e6b095f0f8ee4656a448254e7b9012096b41c4eb3aaf947f6ea429": "0x0100", From ba38cf349021c9403c9f4f945c4e3ab0f0fe39d4 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 16 Aug 2024 17:55:39 +0400 Subject: [PATCH 2/2] chore: clippy --- node/src/chain_spec/raonet.rs | 107 ++++++++++++++---- pallets/subtensor/rpc/src/lib.rs | 13 ++- pallets/subtensor/src/coinbase/block_step.rs | 3 +- .../subtensor/src/coinbase/run_coinbase.rs | 15 +-- pallets/subtensor/src/lib.rs | 6 +- pallets/subtensor/src/macros/dispatches.rs | 8 +- .../subtensor/src/rpc_info/dynamic_info.rs | 28 ++--- pallets/subtensor/src/rpc_info/mod.rs | 2 +- pallets/subtensor/src/rpc_info/subnet_info.rs | 2 +- pallets/subtensor/src/staking/lock.rs | 43 ++++--- pallets/subtensor/src/staking/move_stake.rs | 3 +- pallets/subtensor/src/staking/stake_ops.rs | 1 + pallets/subtensor/src/subnets/subnet.rs | 8 +- pallets/subtensor/src/subnets/tempo.rs | 8 +- pallets/subtensor/src/utils/misc.rs | 4 +- pallets/subtensor/tests/block_emission.rs | 7 ++ pallets/subtensor/tests/emission.rs | 24 ++-- pallets/subtensor/tests/lock.rs | 7 ++ pallets/subtensor/tests/tempo.rs | 7 ++ runtime/src/lib.rs | 4 +- 20 files changed, 206 insertions(+), 94 deletions(-) diff --git a/node/src/chain_spec/raonet.rs b/node/src/chain_spec/raonet.rs index 1266bd8c8..35a57607a 100644 --- a/node/src/chain_spec/raonet.rs +++ b/node/src/chain_spec/raonet.rs @@ -84,89 +84,152 @@ fn raonet_genesis( 10_000_000_000_000u128, ), ( - ::from_ss58check("5FRo4vab84LM3aiK4DijnVawGDKagLGLzfn95j9tjDaHja8Z").unwrap(), + ::from_ss58check( + "5FRo4vab84LM3aiK4DijnVawGDKagLGLzfn95j9tjDaHja8Z", + ) + .unwrap(), 100_000_000_000u128, ), ( - ::from_ss58check("5H3qhPGzKMNV9fTPuizxzp8azyFRMd4BnheSuwN9Qxb5Cz3u").unwrap(), // Greg + ::from_ss58check( + "5H3qhPGzKMNV9fTPuizxzp8azyFRMd4BnheSuwN9Qxb5Cz3u", + ) + .unwrap(), // Greg 100_000_000_000u128, ), ( - ::from_ss58check("5H8zkg8K9hkM5PSeumivNXGuK8J8cUjtwEL9PfyqmgWELPka").unwrap(), // William + ::from_ss58check( + "5H8zkg8K9hkM5PSeumivNXGuK8J8cUjtwEL9PfyqmgWELPka", + ) + .unwrap(), // William 100_000_000_000u128, ), ( - ::from_ss58check("5Ckg2z5NdrfbXpsPtZuVYXxRWh283QWw1gbXfN8CJC3tmxnY").unwrap(), // Dick + ::from_ss58check( + "5Ckg2z5NdrfbXpsPtZuVYXxRWh283QWw1gbXfN8CJC3tmxnY", + ) + .unwrap(), // Dick 100_000_000_000u128, ), ( - ::from_ss58check("5CiUxGA5dTh1cPdgH67Kt62x4w5aKubnmWZMYSrZzoB4hpQi").unwrap(), // Michal + ::from_ss58check( + "5CiUxGA5dTh1cPdgH67Kt62x4w5aKubnmWZMYSrZzoB4hpQi", + ) + .unwrap(), // Michal 100_000_000_000u128, ), ( - ::from_ss58check("5FLHD4eZkPStKUG7p9B1VPjD4w93Fxncf6JG5EK2uRcELmJy").unwrap(), // Carlos + ::from_ss58check( + "5FLHD4eZkPStKUG7p9B1VPjD4w93Fxncf6JG5EK2uRcELmJy", + ) + .unwrap(), // Carlos 100_000_000_000u128, ), ( - ::from_ss58check("5G4FseBtaQd8sqeC98ZEL7xgtF2GSdueMXwUs8vsBENs4Ysn").unwrap(), // Sai + ::from_ss58check( + "5G4FseBtaQd8sqeC98ZEL7xgtF2GSdueMXwUs8vsBENs4Ysn", + ) + .unwrap(), // Sai 100_000_000_000u128, ), ( - ::from_ss58check("5DiJqQoQdpgKaLz97Fk8ZkChAxMqf2mF2pVa6xDKYH8Cf9Sx").unwrap(), // Xavier + ::from_ss58check( + "5DiJqQoQdpgKaLz97Fk8ZkChAxMqf2mF2pVa6xDKYH8Cf9Sx", + ) + .unwrap(), // Xavier 100_000_000_000u128, ), ( - ::from_ss58check("5EUJyRh3x9QftnUw7taFk3Xen6fCQgdc9ko8ort51RnR6LCn").unwrap(), // Elo + ::from_ss58check( + "5EUJyRh3x9QftnUw7taFk3Xen6fCQgdc9ko8ort51RnR6LCn", + ) + .unwrap(), // Elo 100_000_000_000u128, ), ( - ::from_ss58check("5FREUpwG4wJYr1Usyp1i5XmxM3ycGTfvtjJtrSZETPgvh4Hx").unwrap(), // Roman + ::from_ss58check( + "5FREUpwG4wJYr1Usyp1i5XmxM3ycGTfvtjJtrSZETPgvh4Hx", + ) + .unwrap(), // Roman 100_000_000_000u128, ), ( - ::from_ss58check("5GxREgL1Kvuv1kixbY2oJe36Q2HnWqTcfakBZqTPKWiPpVxf").unwrap(), // Abe + ::from_ss58check( + "5GxREgL1Kvuv1kixbY2oJe36Q2HnWqTcfakBZqTPKWiPpVxf", + ) + .unwrap(), // Abe 100_000_000_000u128, ), ( - ::from_ss58check("5GsSxM8p1TYrpXfCx7Un5cTp1fr1RwHJczYDUUn8Xjnqj9Sk").unwrap(), // Nico + ::from_ss58check( + "5GsSxM8p1TYrpXfCx7Un5cTp1fr1RwHJczYDUUn8Xjnqj9Sk", + ) + .unwrap(), // Nico 100_000_000_000u128, ), ( - ::from_ss58check("5HjEUemUaXSkxPcxGYiLykHmi5VfXBh5NCeNXYMbj9akYHbn").unwrap(), // Jip + ::from_ss58check( + "5HjEUemUaXSkxPcxGYiLykHmi5VfXBh5NCeNXYMbj9akYHbn", + ) + .unwrap(), // Jip 100_000_000_000u128, ), ( - ::from_ss58check("5DUFdkP4rJrkXq9pfrWMHQS8zgiwXBZRgw2MMEAnBot59Taz").unwrap(), // Bob + ::from_ss58check( + "5DUFdkP4rJrkXq9pfrWMHQS8zgiwXBZRgw2MMEAnBot59Taz", + ) + .unwrap(), // Bob 100_000_000_000u128, ), ( - ::from_ss58check("5CoSC9nRDT4CbnEGLCHcC8PxgpJsWpSdF3RLnTx2aBvX3qPu").unwrap(), // Faybian + ::from_ss58check( + "5CoSC9nRDT4CbnEGLCHcC8PxgpJsWpSdF3RLnTx2aBvX3qPu", + ) + .unwrap(), // Faybian 100_000_000_000u128, ), ( - ::from_ss58check("5CXgmrp6Ts5igz9uxSdQQy9ERUVaJFtswzaSBUXhb3Ci7drK").unwrap(), // Special K + ::from_ss58check( + "5CXgmrp6Ts5igz9uxSdQQy9ERUVaJFtswzaSBUXhb3Ci7drK", + ) + .unwrap(), // Special K 100_000_000_000u128, ), ( - ::from_ss58check("5HDpGZLNYSxHGWVhsPgKRDKVM6oob7MMnwxdpU8dBP7N51dX").unwrap(), // Michal + ::from_ss58check( + "5HDpGZLNYSxHGWVhsPgKRDKVM6oob7MMnwxdpU8dBP7N51dX", + ) + .unwrap(), // Michal 100_000_000_000u128, ), ( - ::from_ss58check("5GH3haJWuJjcZWuC7iFGtaVajJNEpNg2Guaqyf71y9uDfFrt").unwrap(), // Isa + ::from_ss58check( + "5GH3haJWuJjcZWuC7iFGtaVajJNEpNg2Guaqyf71y9uDfFrt", + ) + .unwrap(), // Isa 100_000_000_000u128, ), ( - ::from_ss58check("5HTukLb2y59rrL5tM9RMw3baziCFZUeSZATWMWHgSrNknc9A").unwrap(), // Gus + ::from_ss58check( + "5HTukLb2y59rrL5tM9RMw3baziCFZUeSZATWMWHgSrNknc9A", + ) + .unwrap(), // Gus 100_000_000_000u128, ), ( - ::from_ss58check("5CkV7PPFWh8EihTK5uLm7VNF4C9hiKJ9UeJJwQuByn3bx82L").unwrap(), // Carrot + ::from_ss58check( + "5CkV7PPFWh8EihTK5uLm7VNF4C9hiKJ9UeJJwQuByn3bx82L", + ) + .unwrap(), // Carrot 100_000_000_000u128, ), ( - ::from_ss58check("5DV8tTjq8EXE6KmoCbJ3xaN54HTXsfev5ZyKJEQPyTcm4MmE").unwrap(), // Paul + ::from_ss58check( + "5DV8tTjq8EXE6KmoCbJ3xaN54HTXsfev5ZyKJEQPyTcm4MmE", + ) + .unwrap(), // Paul 100_000_000_000u128, - ) + ), ]; // Check if the environment variable is set diff --git a/pallets/subtensor/rpc/src/lib.rs b/pallets/subtensor/rpc/src/lib.rs index 2e4d84414..1b28a755a 100644 --- a/pallets/subtensor/rpc/src/lib.rs +++ b/pallets/subtensor/rpc/src/lib.rs @@ -49,7 +49,6 @@ pub trait SubtensorCustomApi { fn get_dynamic_info(&self, netuid: u16, at: Option) -> RpcResult>; #[method(name = "subnetInfo_getAllDynamicInfo")] fn get_all_dynamic_info(&self, at: Option) -> RpcResult>; - } pub struct SubtensorCustom { @@ -216,12 +215,18 @@ where fn get_all_dynamic_info(&self, at: Option<::Hash>) -> RpcResult> { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_all_dynamic_info(at).map_err(|e| Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into()) + api.get_all_dynamic_info(at) + .map_err(|e| Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into()) } - fn get_dynamic_info(&self, netuid: u16, at: Option<::Hash>) -> RpcResult> { + fn get_dynamic_info( + &self, + netuid: u16, + at: Option<::Hash>, + ) -> RpcResult> { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_dynamic_info(at, netuid).map_err(|e| Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into()) + api.get_dynamic_info(at, netuid) + .map_err(|e| Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into()) } fn get_network_lock_cost(&self, at: Option<::Hash>) -> RpcResult { diff --git a/pallets/subtensor/src/coinbase/block_step.rs b/pallets/subtensor/src/coinbase/block_step.rs index 940d24bfb..217972115 100644 --- a/pallets/subtensor/src/coinbase/block_step.rs +++ b/pallets/subtensor/src/coinbase/block_step.rs @@ -15,7 +15,8 @@ impl Pallet { Self::run_coinbase(); // --- 4. Adjust tempos every day. // TODO(const) make this better. - if block_number.saturating_add(1) % 300 == 0 { // adjust every hour. + if block_number.saturating_add(1) % 300 == 0 { + // adjust every hour. Self::adjust_tempos(); } // Return ok. diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index a4d4f04b1..21184b33b 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -35,7 +35,8 @@ impl Pallet { } // Skip root network let mechid = SubnetMechanism::::get(*netuid); let subnet_tao = I96F32::from_num(SubnetTAO::::get(*netuid)); - *mechanism_tao.entry(mechid).or_insert(I96F32::from_num(0)) += subnet_tao; + let entry = mechanism_tao.entry(mechid).or_insert(I96F32::from_num(0)); + *entry = entry.saturating_add(subnet_tao); total_active_tao = total_active_tao.saturating_add(subnet_tao); } log::debug!("Mechanism TAO sums: {:?}", mechanism_tao); @@ -341,7 +342,7 @@ impl Pallet { hotkey_emission_tuples.push((parent, netuid, total_emission)); // Keep track of total emission distributed to parents - to_parents += total_emission; + to_parents = to_parents.saturating_add(total_emission); } // Calculate the final emission for the hotkey itself @@ -410,8 +411,8 @@ impl Pallet { I96F32::from_num(Alpha::::get((&hotkey, nominator.clone(), netuid))); let global_contribution: I96F32 = I96F32::from_num(Self::get_global_for_hotkey_and_coldkey(hotkey, &nominator)); - total_global += global_contribution; - total_alpha += alpha_contribution; + total_global = total_global.saturating_add(global_contribution); + total_alpha = total_alpha.saturating_add(alpha_contribution); contributions.push((nominator.clone(), alpha_contribution, global_contribution)); } @@ -438,7 +439,7 @@ impl Pallet { .to_num::(); if total_emission > 0 { // Record the emission for this nominator - to_nominators += total_emission; + to_nominators = to_nominators.saturating_add(total_emission); emission_tuples.push(( hotkey.clone(), nominator.clone(), @@ -455,7 +456,7 @@ impl Pallet { .to_num::() .saturating_sub(hotkey_take.to_num::()) .saturating_sub(to_nominators); - let final_hotkey_emission: u64 = hotkey_take.to_num::() + remainder; + let final_hotkey_emission: u64 = hotkey_take.to_num::().saturating_add(remainder); emission_tuples.push(( hotkey.clone(), hotkey_owner.clone(), @@ -553,7 +554,7 @@ impl Pallet { let netuid_plus_one = (netuid as u64).saturating_add(1); let tempo_plus_one = (tempo as u64).saturating_add(1); let adjusted_block = block_number.wrapping_add(netuid_plus_one); - let remainder = adjusted_block % tempo_plus_one; + let remainder = adjusted_block.rem_euclid(tempo_plus_one); (tempo as u64).saturating_sub(remainder) } diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 7291fd693..d0331852a 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -706,9 +706,11 @@ pub mod pallet { StorageMap<_, Identity, u16, T::AccountId, ValueQuery, DefaultSubnetOwner>; // DEPRECATED #[pallet::storage] // --- MAP ( netuid ) --> total_subnet_locked - pub type SubnetLocked = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64>; + pub type SubnetLocked = + StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64>; #[pallet::storage] // --- MAP ( netuid ) --> largest_locked - pub type LargestLocked = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64>; + pub type LargestLocked = + StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64>; #[pallet::storage] // --- ITEM( last_network_lock_cost ) pub type LockIntervalBlocks = StorageValue<_, u64, ValueQuery, DefaultLockIntervalBlocks>; #[pallet::storage] // --- NMAP ( netuid, cold, hot ) --> (amount, start, end) | Returns the lock associated with a hotkey. diff --git a/pallets/subtensor/src/macros/dispatches.rs b/pallets/subtensor/src/macros/dispatches.rs index 425f777f6..08b4b276d 100644 --- a/pallets/subtensor/src/macros/dispatches.rs +++ b/pallets/subtensor/src/macros/dispatches.rs @@ -1054,7 +1054,13 @@ mod dispatches { origin_netuid: u16, destination_netuid: u16, ) -> DispatchResult { - Self::do_move_stake(origin, origin_hotkey, destination_hotkey, origin_netuid, destination_netuid) + Self::do_move_stake( + origin, + origin_hotkey, + destination_hotkey, + origin_netuid, + destination_netuid, + ) } } } diff --git a/pallets/subtensor/src/rpc_info/dynamic_info.rs b/pallets/subtensor/src/rpc_info/dynamic_info.rs index 73b8bfb05..de4191e58 100644 --- a/pallets/subtensor/src/rpc_info/dynamic_info.rs +++ b/pallets/subtensor/src/rpc_info/dynamic_info.rs @@ -4,7 +4,7 @@ use codec::Compact; use frame_support::pallet_prelude::{Decode, Encode}; #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] -pub struct DynamicInfo{ +pub struct DynamicInfo { owner: T::AccountId, netuid: Compact, tempo: Compact, @@ -20,22 +20,24 @@ pub struct DynamicInfo{ impl Pallet { pub fn get_dynamic_info(netuid: u16) -> Option> { - if !Self::if_subnet_exist(netuid) {return None;} - let last_step: u64 = LastMechansimStepBlock::::get( netuid ); + if !Self::if_subnet_exist(netuid) { + return None; + } + let last_step: u64 = LastMechansimStepBlock::::get(netuid); let current_block: u64 = Pallet::::get_current_block_as_u64(); - let blocks_since_last_step: u64 = current_block - last_step; + let blocks_since_last_step: u64 = current_block.saturating_sub(last_step); Some(DynamicInfo { - owner: SubnetOwner::::get( netuid ).into(), + owner: SubnetOwner::::get(netuid), netuid: netuid.into(), - tempo: Tempo::::get( netuid ).into(), + tempo: Tempo::::get(netuid).into(), last_step: last_step.into(), blocks_since_last_step: blocks_since_last_step.into(), - emission: EmissionValues::::get( netuid ).into(), - alpha_in: SubnetAlphaIn::::get( netuid ).into(), - alpha_out: SubnetAlphaOut::::get( netuid ).into(), - tao_in: SubnetTAO::::get( netuid ).into(), - total_locked: SubnetLocked::::get( netuid ).into(), - owner_locked: LargestLocked::::get( netuid ).into(), + emission: EmissionValues::::get(netuid).into(), + alpha_in: SubnetAlphaIn::::get(netuid).into(), + alpha_out: SubnetAlphaOut::::get(netuid).into(), + tao_in: SubnetTAO::::get(netuid).into(), + total_locked: SubnetLocked::::get(netuid).into(), + owner_locked: LargestLocked::::get(netuid).into(), }) } pub fn get_all_dynamic_info() -> Vec>> { @@ -46,4 +48,4 @@ impl Pallet { } dynamic_info } -} \ No newline at end of file +} diff --git a/pallets/subtensor/src/rpc_info/mod.rs b/pallets/subtensor/src/rpc_info/mod.rs index 9ab751d26..d28753fe5 100644 --- a/pallets/subtensor/src/rpc_info/mod.rs +++ b/pallets/subtensor/src/rpc_info/mod.rs @@ -1,6 +1,6 @@ use super::*; pub mod delegate_info; +pub mod dynamic_info; pub mod neuron_info; pub mod stake_info; pub mod subnet_info; -pub mod dynamic_info; diff --git a/pallets/subtensor/src/rpc_info/subnet_info.rs b/pallets/subtensor/src/rpc_info/subnet_info.rs index 0a7a98cad..4e9e756a0 100644 --- a/pallets/subtensor/src/rpc_info/subnet_info.rs +++ b/pallets/subtensor/src/rpc_info/subnet_info.rs @@ -193,4 +193,4 @@ impl Pallet { liquid_alpha_enabled, }) } -} \ No newline at end of file +} diff --git a/pallets/subtensor/src/staking/lock.rs b/pallets/subtensor/src/staking/lock.rs index eabfcd038..4b8b6d154 100644 --- a/pallets/subtensor/src/staking/lock.rs +++ b/pallets/subtensor/src/staking/lock.rs @@ -1,6 +1,7 @@ use super::*; use crate::epoch::math::*; use alloc::collections::BTreeMap; +use core::ops::Neg; use substrate_fixed::types::I96F32; impl Pallet { @@ -77,7 +78,7 @@ impl Pallet { netuid: u16, ) -> u64 { let (locked, _, end) = Locks::::get((netuid, hotkey.clone(), coldkey.clone())); - + Self::calculate_conviction(locked, end, Self::get_current_block_as_u64()) } @@ -245,10 +246,13 @@ impl Pallet { // Normalize convictions and apply exponential function let mut powered_convictions: Vec = Vec::with_capacity(convictions.len()); for c in convictions.iter() { - let normalized = I96F32::from_num(*c) / I96F32::from_num(max_conviction); + let normalized: I96F32 = I96F32::from_num(*c) + .checked_div(I96F32::from_num(max_conviction)) + .unwrap_or(I96F32::from_num(0)); // Use checked_mul to prevent overflow in exponentiation let powered = exp_safe_f96( - I96F32::from_num(sharpness).saturating_mul(normalized - I96F32::from_num(1)), + I96F32::from_num(sharpness) + .saturating_mul(normalized.saturating_sub(I96F32::from_num(1))), ); powered_convictions.push(powered); } @@ -264,7 +268,7 @@ impl Pallet { // Calculate shares let shares: Vec = powered_convictions .into_iter() - .map(|pc| pc / total_powered) + .map(|pc: I96F32| pc.checked_div(total_powered).unwrap_or(I96F32::from_num(0))) .collect(); shares @@ -302,7 +306,10 @@ impl Pallet { // Calculate conviction for each lock let conviction = Self::calculate_conviction(lock_amount, end_block, current_block); // Add conviction to the hotkey's total - *hotkey_convictions.entry(hotkey.clone()).or_default() += conviction; + hotkey_convictions + .entry(hotkey.clone()) + .and_modify(|e| *e = e.saturating_add(conviction)) + .or_insert(conviction); // Add to the total conviction total_conviction = total_conviction.saturating_add(conviction); } @@ -318,8 +325,8 @@ impl Pallet { // Set the total subnet Conviction. let largest_conviction = convictions.iter().max().cloned().unwrap_or(1); - SubnetLocked::::insert( netuid, total_conviction ) ; - LargestLocked::::insert( netuid, largest_conviction ); + SubnetLocked::::insert(netuid, total_conviction); + LargestLocked::::insert(netuid, largest_conviction); // Calculate shares using the lion's share distribution let shares: Vec = Self::calculate_lions_share(convictions, 20); @@ -387,7 +394,7 @@ impl Pallet { ( amount, current_block, - current_block + Self::get_lock_interval_blocks(), + current_block.saturating_add(Self::get_lock_interval_blocks()), ), ); } @@ -409,7 +416,11 @@ impl Pallet { pub fn update_all_subnet_owners() { let current_block = Self::get_current_block_as_u64(); let update_interval = 7200 * 15; // Approx 15 days. - if current_block % (update_interval * 2) == 0 { + // Use checked_rem to avoid potential arithmetic overflow + if current_block + .checked_rem(update_interval * 2) + .map_or(false, |rem| rem == 0) + { for netuid in Self::get_all_subnet_netuids() { Self::update_subnet_owner(netuid); } @@ -486,8 +497,7 @@ impl Pallet { if tied_hotkeys.len() > 1 { // Use a deterministic method to break ties, e.g., lowest hotkey value - if let Some((winning_hotkey, _)) = - tied_hotkeys.iter().min_by_key(|(hotkey, _)| hotkey) + if let Some((winning_hotkey, _)) = tied_hotkeys.iter().min_by_key(|(hotkey, _)| hotkey) { let owning_coldkey = Self::get_owning_coldkey_for_hotkey(winning_hotkey); SubnetOwner::::insert(netuid, owning_coldkey.clone()); @@ -532,11 +542,14 @@ impl Pallet { pub fn calculate_conviction(lock_amount: u64, end_block: u64, current_block: u64) -> u64 { let lock_duration = end_block.saturating_sub(current_block); let lock_interval_blocks = Self::get_lock_interval_blocks(); - let time_factor = - -I96F32::from_num(lock_duration).saturating_div(I96F32::from_num(lock_interval_blocks)); - let exp_term = I96F32::from_num(1) - exp_safe_f96(I96F32::from_num(time_factor)); + let time_factor = I96F32::from_num(lock_duration) + .saturating_div(I96F32::from_num(lock_interval_blocks)) + .neg(); + let exp_term = I96F32::from_num(1) + .checked_sub(exp_safe_f96(time_factor)) + .unwrap_or(I96F32::from_num(0)); let conviction_score = I96F32::from_num(lock_amount).saturating_mul(exp_term); - + conviction_score.to_num::() } diff --git a/pallets/subtensor/src/staking/move_stake.rs b/pallets/subtensor/src/staking/move_stake.rs index f8a05769f..37072ab90 100644 --- a/pallets/subtensor/src/staking/move_stake.rs +++ b/pallets/subtensor/src/staking/move_stake.rs @@ -2,7 +2,6 @@ use super::*; // use substrate_fixed::types::I96F32; impl Pallet { - /// Moves stake from one hotkey to another across subnets. /// /// # Arguments @@ -21,7 +20,7 @@ impl Pallet { /// * Either the origin or destination subnet does not exist. /// * The `origin_hotkey` or `destination_hotkey` does not exist. /// * There are locked funds that cannot be moved across subnets. - /// + /// /// # Events /// Emits a `StakeMoved` event upon successful completion of the stake movement. pub fn do_move_stake( diff --git a/pallets/subtensor/src/staking/stake_ops.rs b/pallets/subtensor/src/staking/stake_ops.rs index 2671d0acb..607eb797c 100644 --- a/pallets/subtensor/src/staking/stake_ops.rs +++ b/pallets/subtensor/src/staking/stake_ops.rs @@ -1,3 +1,4 @@ +#![allow(clippy::doc_lazy_continuation)] use super::*; use substrate_fixed::types::I96F32; diff --git a/pallets/subtensor/src/subnets/subnet.rs b/pallets/subtensor/src/subnets/subnet.rs index e7f16e395..de2fa59aa 100644 --- a/pallets/subtensor/src/subnets/subnet.rs +++ b/pallets/subtensor/src/subnets/subnet.rs @@ -176,12 +176,8 @@ impl Pallet { // --- 14. Init the pool by putting the lock as the initial alpha. SubnetTAO::::insert(netuid_to_register, 1); // add the TAO to the pool. SubnetAlphaIn::::insert(netuid_to_register, actual_tao_lock_amount); // Set the alpha in based on the lock. - let alpha_out = Self::stake_into_subnet( - hotkey, - &coldkey, - netuid_to_register, - actual_tao_lock_amount, - ); + let alpha_out = + Self::stake_into_subnet(hotkey, &coldkey, netuid_to_register, actual_tao_lock_amount); SubnetOwner::::insert(netuid_to_register, coldkey.clone()); SubnetLocked::::insert(netuid_to_register, alpha_out); LargestLocked::::insert(netuid_to_register, alpha_out); diff --git a/pallets/subtensor/src/subnets/tempo.rs b/pallets/subtensor/src/subnets/tempo.rs index 8f8f99062..4a6ebb15c 100644 --- a/pallets/subtensor/src/subnets/tempo.rs +++ b/pallets/subtensor/src/subnets/tempo.rs @@ -105,7 +105,9 @@ impl Pallet { let norm_tao: Vec = tao .iter() .map(|&x| { - let normalized = I96F32::from_num(x) / total_tao; + let normalized = I96F32::from_num(x) + .checked_div(total_tao) + .unwrap_or(I96F32::from_num(0)); log::debug!("Normalized TAO: {}", normalized); normalized }) @@ -117,7 +119,9 @@ impl Pallet { .into_iter() .map(|value| { if value > 0 { - let tempo = (I96F32::from_num(k) / value) + let tempo = I96F32::from_num(k) + .checked_div(value) + .unwrap_or(I96F32::from_num(max_tempo)) .clamp(I96F32::from_num(min_tempo), I96F32::from_num(max_tempo)); log::debug!("Calculated tempo: {}", tempo); tempo diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index cb2b2e4a4..96e818f78 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -618,7 +618,9 @@ impl Pallet { SubnetOwnerCut::::get() } pub fn get_float_subnet_owner_cut() -> I96F32 { - I96F32::from_num(SubnetOwnerCut::::get()) / I96F32::from_num(u16::MAX) + I96F32::from_num(SubnetOwnerCut::::get()) + .checked_div(I96F32::from_num(u16::MAX)) + .unwrap_or(I96F32::from_num(0)) } pub fn set_subnet_owner_cut(subnet_owner_cut: u16) { SubnetOwnerCut::::set(subnet_owner_cut); diff --git a/pallets/subtensor/tests/block_emission.rs b/pallets/subtensor/tests/block_emission.rs index 4fa833cde..db3d1efcf 100644 --- a/pallets/subtensor/tests/block_emission.rs +++ b/pallets/subtensor/tests/block_emission.rs @@ -1,3 +1,10 @@ +#![allow( + clippy::unwrap_used, + clippy::panic, + clippy::arithmetic_side_effects, + clippy::indexing_slicing +)] + mod mock; use crate::mock::*; use pallet_subtensor::*; diff --git a/pallets/subtensor/tests/emission.rs b/pallets/subtensor/tests/emission.rs index 1860f7e29..05bfb0b15 100644 --- a/pallets/subtensor/tests/emission.rs +++ b/pallets/subtensor/tests/emission.rs @@ -1,3 +1,9 @@ +#![allow( + clippy::unwrap_used, + clippy::panic, + clippy::arithmetic_side_effects, + clippy::indexing_slicing +)] mod mock; use crate::mock::*; use pallet_subtensor::*; @@ -998,10 +1004,7 @@ fn test_basic_emission_distribution_scenario() { ParentKeys::::insert( hotkey, netuid, - vec![ - (u64::MAX / 2, parent1), - (u64::MAX / 2, parent2), - ], + vec![(u64::MAX / 2, parent1), (u64::MAX / 2, parent2)], ); let mut emission_tuples = Vec::new(); @@ -1303,10 +1306,7 @@ fn test_rounding_and_precision_scenario() { ParentKeys::::insert( hotkey, netuid, - vec![ - (u64::MAX / 2, parent1), - (u64::MAX / 2, parent2), - ], + vec![(u64::MAX / 2, parent1), (u64::MAX / 2, parent2)], ); SubtensorModule::stake_into_subnet(&parent1, &coldkey, netuid, 1000); SubtensorModule::stake_into_subnet(&parent2, &coldkey, netuid, 1000); @@ -1504,11 +1504,7 @@ fn test_edge_cases_parent_proportions() { ParentKeys::::insert( hotkey, netuid, - vec![ - (0, parent1), - (u64::MAX, parent2), - (u64::MAX / 2, parent3), - ], + vec![(0, parent1), (u64::MAX, parent2), (u64::MAX / 2, parent3)], ); for parent in [&parent1, &parent2, &parent3] { @@ -1672,4 +1668,4 @@ fn test_maximum_emission_value() { .iter() .any(|(acc, _, amount)| acc == &hotkey && *amount > 0)); }); -} \ No newline at end of file +} diff --git a/pallets/subtensor/tests/lock.rs b/pallets/subtensor/tests/lock.rs index 18ab66daf..b71a5bd09 100644 --- a/pallets/subtensor/tests/lock.rs +++ b/pallets/subtensor/tests/lock.rs @@ -1,3 +1,10 @@ +#![allow( + clippy::unwrap_used, + clippy::panic, + clippy::arithmetic_side_effects, + clippy::indexing_slicing +)] + mod mock; use frame_support::{assert_noop, assert_ok}; use mock::*; diff --git a/pallets/subtensor/tests/tempo.rs b/pallets/subtensor/tests/tempo.rs index 302ecdd13..8bbd05966 100644 --- a/pallets/subtensor/tests/tempo.rs +++ b/pallets/subtensor/tests/tempo.rs @@ -1,3 +1,10 @@ +#![allow( + clippy::unwrap_used, + clippy::panic, + clippy::arithmetic_side_effects, + clippy::indexing_slicing +)] + mod mock; use mock::*; use pallet_subtensor::*; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index e2c23f74c..f97bb8ae5 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1771,11 +1771,11 @@ impl_runtime_apis! { fn get_all_dynamic_info() -> Vec { let result = SubtensorModule::get_all_dynamic_info(); result.encode() - } + } fn get_dynamic_info(netuid: u16) -> Vec { let result = SubtensorModule::get_dynamic_info(netuid); result.encode() - } + } fn get_subnet_hyperparams(netuid: u16) -> Vec { let _result = SubtensorModule::get_subnet_hyperparams(netuid);