Skip to content

Commit af561e6

Browse files
committed
Added query for sparsed weights
1 parent 6e3db47 commit af561e6

File tree

7 files changed

+28
-4
lines changed

7 files changed

+28
-4
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cybernet"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = ["C H <[email protected]>"]
55
edition = "2021"
66

src/contract.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use crate::utils::{
5656
do_sudo_set_validator_prune_len, do_sudo_set_weights_set_rate_limit,
5757
do_sudo_set_weights_version_key,
5858
};
59-
use crate::weights::{do_set_weights, get_network_weights};
59+
use crate::weights::{do_set_weights, get_network_weights, get_network_weights_sparse};
6060

6161
// use cw2::set_contract_version;
6262

@@ -622,6 +622,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
622622
QueryMsg::GetWeights { netuid } => {
623623
to_json_binary(&get_network_weights(deps.storage, netuid)?)
624624
}
625+
QueryMsg::GetWeightsSparse { netuid } => {
626+
to_json_binary(&get_network_weights_sparse(deps.storage, netuid)?)
627+
}
625628
}
626629
}
627630

src/delegate_info.rs

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub fn get_delegate(deps: Deps, delegate: String) -> StdResult<Option<DelegateIn
9797
return Ok(Some(delegate_info));
9898
}
9999

100+
// TODO add pagination and limit
100101
pub fn get_delegates(deps: Deps) -> StdResult<Vec<DelegateInfo>> {
101102
let mut delegates = Vec::<DelegateInfo>::new();
102103
for item in DELEGATES
@@ -114,6 +115,7 @@ pub fn get_delegates(deps: Deps) -> StdResult<Vec<DelegateInfo>> {
114115
pub fn get_delegated(deps: Deps, delegatee: String) -> StdResult<Vec<(DelegateInfo, u64)>> {
115116
let delegatee = deps.api.addr_validate(&delegatee)?;
116117
let mut delegates: Vec<(DelegateInfo, u64)> = Vec::new();
118+
// TODO iterator over all delegates? rewrite
117119
for item in DELEGATES
118120
.range(deps.storage, None, None, Order::Ascending)
119121
.into_iter()

src/msg.rs

+2
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ pub enum QueryMsg {
332332
// TODO added for debugging, remove later
333333
#[returns(Vec<Vec<u16>>)]
334334
GetWeights { netuid: u16 },
335+
#[returns(Vec<Vec<(u16, u16)>>)]
336+
GetWeightsSparse { netuid: u16 },
335337

336338
#[returns(crate::state_info::StateInfo)]
337339
GetState {},

src/staking.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ pub fn get_total_stake_for_coldkey(store: &dyn Storage, coldkey: &Addr) -> u64 {
393393
// Returns the stake under the cold - hot pairing in the staking table.
394394
//
395395
pub fn get_stake_for_coldkey_and_hotkey(store: &dyn Storage, coldkey: &Addr, hotkey: &Addr) -> u64 {
396-
STAKE.load(store, (hotkey, coldkey)).unwrap()
396+
// Added default, see delegate_info:125
397+
STAKE.load(store, (hotkey, coldkey)).unwrap_or_default()
397398
}
398399

399400
// Creates a cold - hot pairing account if the hotkey is not already an active account.

src/weights.rs

+16
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,19 @@ pub fn get_network_weights(store: &dyn Storage, netuid: u16) -> StdResult<Vec<Ve
405405
}
406406
Ok(weights)
407407
}
408+
409+
// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16.
410+
pub fn get_network_weights_sparse(store: &dyn Storage, netuid:u16 ) -> StdResult<Vec<Vec<(u16, u16)>>> {
411+
let n: usize = get_subnetwork_n(store, netuid) as usize;
412+
let mut weights: Vec<Vec<(u16, u16)>> = vec![ vec![]; n ];
413+
for item in WEIGHTS
414+
.prefix(netuid)
415+
.range(store, None, None, Order::Ascending)
416+
{
417+
let (uid_i, weights_i) = item.unwrap();
418+
for (uid_j, weight_ij) in weights_i.iter() {
419+
weights [ uid_i as usize ].push( ( *uid_j, *weight_ij ));
420+
}
421+
}
422+
Ok(weights)
423+
}

0 commit comments

Comments
 (0)