From af561e6b1666bdbd2748711f3d97ac975b11b8db Mon Sep 17 00:00:00 2001 From: C H Date: Tue, 5 Dec 2023 01:05:29 +0800 Subject: [PATCH] Added query for sparsed weights --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/contract.rs | 5 ++++- src/delegate_info.rs | 2 ++ src/msg.rs | 2 ++ src/staking.rs | 3 ++- src/weights.rs | 16 ++++++++++++++++ 7 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 777127e..ebe6bf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -497,7 +497,7 @@ dependencies = [ [[package]] name = "cybernet" -version = "0.1.0" +version = "0.2.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", diff --git a/Cargo.toml b/Cargo.toml index c83171a..e9468c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cybernet" -version = "0.1.0" +version = "0.2.0" authors = ["C H "] edition = "2021" diff --git a/src/contract.rs b/src/contract.rs index 6f88f7c..a04c6dc 100644 --- a/src/contract.rs +++ b/src/contract.rs @@ -56,7 +56,7 @@ use crate::utils::{ do_sudo_set_validator_prune_len, do_sudo_set_weights_set_rate_limit, do_sudo_set_weights_version_key, }; -use crate::weights::{do_set_weights, get_network_weights}; +use crate::weights::{do_set_weights, get_network_weights, get_network_weights_sparse}; // use cw2::set_contract_version; @@ -622,6 +622,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { QueryMsg::GetWeights { netuid } => { to_json_binary(&get_network_weights(deps.storage, netuid)?) } + QueryMsg::GetWeightsSparse { netuid } => { + to_json_binary(&get_network_weights_sparse(deps.storage, netuid)?) + } } } diff --git a/src/delegate_info.rs b/src/delegate_info.rs index 8b0b023..3ad8518 100644 --- a/src/delegate_info.rs +++ b/src/delegate_info.rs @@ -97,6 +97,7 @@ pub fn get_delegate(deps: Deps, delegate: String) -> StdResult StdResult> { let mut delegates = Vec::::new(); for item in DELEGATES @@ -114,6 +115,7 @@ pub fn get_delegates(deps: Deps) -> StdResult> { pub fn get_delegated(deps: Deps, delegatee: String) -> StdResult> { let delegatee = deps.api.addr_validate(&delegatee)?; let mut delegates: Vec<(DelegateInfo, u64)> = Vec::new(); + // TODO iterator over all delegates? rewrite for item in DELEGATES .range(deps.storage, None, None, Order::Ascending) .into_iter() diff --git a/src/msg.rs b/src/msg.rs index e03f1b4..5612a40 100644 --- a/src/msg.rs +++ b/src/msg.rs @@ -332,6 +332,8 @@ pub enum QueryMsg { // TODO added for debugging, remove later #[returns(Vec>)] GetWeights { netuid: u16 }, + #[returns(Vec>)] + GetWeightsSparse { netuid: u16 }, #[returns(crate::state_info::StateInfo)] GetState {}, diff --git a/src/staking.rs b/src/staking.rs index 739929a..caa1e3a 100644 --- a/src/staking.rs +++ b/src/staking.rs @@ -393,7 +393,8 @@ pub fn get_total_stake_for_coldkey(store: &dyn Storage, coldkey: &Addr) -> u64 { // Returns the stake under the cold - hot pairing in the staking table. // pub fn get_stake_for_coldkey_and_hotkey(store: &dyn Storage, coldkey: &Addr, hotkey: &Addr) -> u64 { - STAKE.load(store, (hotkey, coldkey)).unwrap() + // Added default, see delegate_info:125 + STAKE.load(store, (hotkey, coldkey)).unwrap_or_default() } // Creates a cold - hot pairing account if the hotkey is not already an active account. diff --git a/src/weights.rs b/src/weights.rs index 1284d81..60a6fc7 100644 --- a/src/weights.rs +++ b/src/weights.rs @@ -405,3 +405,19 @@ pub fn get_network_weights(store: &dyn Storage, netuid: u16) -> StdResult StdResult>> { + let n: usize = get_subnetwork_n(store, netuid) as usize; + let mut weights: Vec> = vec![ vec![]; n ]; + for item in WEIGHTS + .prefix(netuid) + .range(store, None, None, Order::Ascending) + { + let (uid_i, weights_i) = item.unwrap(); + for (uid_j, weight_ij) in weights_i.iter() { + weights [ uid_i as usize ].push( ( *uid_j, *weight_ij )); + } + } + Ok(weights) +} \ No newline at end of file