Skip to content

Commit

Permalink
Added query for sparsed weights
Browse files Browse the repository at this point in the history
  • Loading branch information
cyborgshead committed Dec 4, 2023
1 parent 6e3db47 commit af561e6
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cybernet"
version = "0.1.0"
version = "0.2.0"
authors = ["C H <[email protected]>"]
edition = "2021"

Expand Down
5 changes: 4 additions & 1 deletion src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -622,6 +622,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
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)?)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/delegate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub fn get_delegate(deps: Deps, delegate: String) -> StdResult<Option<DelegateIn
return Ok(Some(delegate_info));
}

// TODO add pagination and limit
pub fn get_delegates(deps: Deps) -> StdResult<Vec<DelegateInfo>> {
let mut delegates = Vec::<DelegateInfo>::new();
for item in DELEGATES
Expand All @@ -114,6 +115,7 @@ pub fn get_delegates(deps: Deps) -> StdResult<Vec<DelegateInfo>> {
pub fn get_delegated(deps: Deps, delegatee: String) -> StdResult<Vec<(DelegateInfo, u64)>> {
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()
Expand Down
2 changes: 2 additions & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ pub enum QueryMsg {
// TODO added for debugging, remove later
#[returns(Vec<Vec<u16>>)]
GetWeights { netuid: u16 },
#[returns(Vec<Vec<(u16, u16)>>)]
GetWeightsSparse { netuid: u16 },

#[returns(crate::state_info::StateInfo)]
GetState {},
Expand Down
3 changes: 2 additions & 1 deletion src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,19 @@ pub fn get_network_weights(store: &dyn Storage, netuid: u16) -> StdResult<Vec<Ve
}
Ok(weights)
}

// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16.
pub fn get_network_weights_sparse(store: &dyn Storage, netuid:u16 ) -> StdResult<Vec<Vec<(u16, u16)>>> {
let n: usize = get_subnetwork_n(store, netuid) as usize;
let mut weights: Vec<Vec<(u16, u16)>> = 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)
}

0 comments on commit af561e6

Please sign in to comment.