Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyborgshead committed Jan 4, 2024
1 parent facc38f commit 30513af
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 121 deletions.
29 changes: 15 additions & 14 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cosmwasm_std::{
Uint128,
};
use cw2::{get_contract_version, set_contract_version, ContractVersion};
use cyber_std::Response;
use cyber_std::{create_creat_thought_msg, Load, Trigger};

use crate::block_step::block_step;
Expand All @@ -25,9 +26,9 @@ use crate::state::{
LAST_UPDATE, MAX_ALLOWED_UIDS, MAX_ALLOWED_VALIDATORS, MAX_BURN, MAX_DIFFICULTY,
MAX_REGISTRATION_PER_BLOCK, MAX_WEIGHTS_LIMIT, MIN_ALLOWED_WEIGHTS, MIN_BURN, MIN_DIFFICULTY,
NETWORKS_ADDED, NETWORK_IMMUNITY_PERIOD, NETWORK_LAST_LOCK_COST, NETWORK_LAST_REGISTERED,
NETWORK_LOCK_REDUCTION_INTERVAL, NETWORK_MIN_ALLOWED_UIDS, NETWORK_MIN_LOCK_COST,
NETWORK_MODALITY, NETWORK_RATE_LIMIT, NETWORK_REGISTERED_AT, NETWORK_REGISTRATION_ALLOWED,
OWNER, PENDING_EMISSION, POW_REGISTRATIONS_THIS_INTERVAL, PROMETHEUS, PRUNING_SCORES, RANK,
NETWORK_LOCK_REDUCTION_INTERVAL, NETWORK_MIN_LOCK_COST, NETWORK_MODALITY, NETWORK_RATE_LIMIT,
NETWORK_REGISTERED_AT, NETWORK_REGISTRATION_ALLOWED, OWNER, PENDING_EMISSION,
POW_REGISTRATIONS_THIS_INTERVAL, PROMETHEUS, PRUNING_SCORES, RANK,
RAO_RECYCLED_FOR_REGISTRATION, REGISTRATIONS_THIS_BLOCK, REGISTRATIONS_THIS_INTERVAL, RHO,
ROOT, SCALING_LAW_POWER, SERVING_RATE_LIMIT, STAKE, SUBNETWORK_N, SUBNET_LIMIT, SUBNET_LOCKED,
SUBNET_OWNER, SUBNET_OWNER_CUT, TARGET_REGISTRATIONS_PER_INTERVAL, TEMPO, TOTAL_COLDKEY_STAKE,
Expand All @@ -36,6 +37,7 @@ use crate::state::{
};
use crate::state_info::get_state_info;
use crate::subnet_info::{get_subnet_hyperparams, get_subnet_info, get_subnets_info};
use crate::uids::get_registered_networks_for_hotkey;
use crate::utils::{
do_sudo_set_activity_cutoff, do_sudo_set_adjustment_alpha, do_sudo_set_adjustment_interval,
do_sudo_set_block_emission, do_sudo_set_bonds_moving_average, do_sudo_set_default_take,
Expand All @@ -55,15 +57,9 @@ use crate::utils::{
};
use crate::weights::{do_set_weights, get_network_weights, get_network_weights_sparse};

// use cw2::set_contract_version;

// version info for migration info
const CONTRACT_NAME: &str = "cybernet";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

use crate::uids::get_registered_networks_for_hotkey;
use cyber_std::Response;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
Expand All @@ -75,7 +71,12 @@ pub fn instantiate(

ROOT.save(deps.storage, &info.sender)?;
ALLOW_FAUCET.save(deps.storage, &false)?;
DENOM.save(deps.storage, &"boot".to_string())?;

if info.funds.len() > 0 {
DENOM.save(deps.storage, &info.funds[0].denom)?;
} else {
DENOM.save(deps.storage, &"boot".to_string())?;
}

// TODO remove from InstantiateMsg
// // Set initial total issuance from balances
Expand All @@ -88,16 +89,16 @@ pub fn instantiate(
NETWORK_IMMUNITY_PERIOD.save(deps.storage, &7200)?;
BLOCK_EMISSION.save(deps.storage, &1_000_000_000)?;

NETWORK_MIN_ALLOWED_UIDS.save(deps.storage, &0)?;
SUBNET_OWNER_CUT.save(deps.storage, &0)?;
NETWORK_RATE_LIMIT.save(deps.storage, &0)?;

DEFAULT_TAKE.save(deps.storage, &11_796)?;
TX_RATE_LIMIT.save(deps.storage, &1000)?;
// 6.25% (2^12/2^16)
DEFAULT_TAKE.save(deps.storage, &4096)?;
TX_RATE_LIMIT.save(deps.storage, &0)?;

NETWORK_LAST_LOCK_COST.save(deps.storage, &100_000_000_000)?;
NETWORK_MIN_LOCK_COST.save(deps.storage, &100_000_000_000)?;
NETWORK_LOCK_REDUCTION_INTERVAL.save(deps.storage, &2)?; // test value, change to 14 * 7200;
NETWORK_LOCK_REDUCTION_INTERVAL.save(deps.storage, &(7 * 7200))?; // test value, change to 14 * 7200;

// -- Root network initialization --

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub enum ContractError {
#[error("Thrown if the vaule is invalid for MaxAllowedUids.")]
MaxAllowedUIdsNotAllowed {},

#[error("Thrown when the dispatch attempts to convert between a u64 and T::balance but the call fails.")]
#[error("Thrown when wrong denom passed to the contract as payment")]
CouldNotConvertToBalance {},

#[error("Thrown when the caller requests adding stake for a hotkey to the total stake which already added.")]
Expand Down
14 changes: 7 additions & 7 deletions src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ use crate::state::{
use crate::uids::{append_neuron, get_hotkey_for_net_and_uid, get_subnetwork_n, replace_neuron};
use crate::utils::{
get_block_emission, get_emission_value, get_max_allowed_uids, get_max_registrations_per_block,
get_registrations_this_block, get_registrations_this_interval, get_rho,
get_subnet_owner, get_target_registrations_per_interval, get_tempo,
set_subnet_locked_balance,
get_registrations_this_block, get_registrations_this_interval, get_rho, get_subnet_owner,
get_target_registrations_per_interval, get_tempo, set_subnet_locked_balance,
};
use crate::ContractError;

Expand Down Expand Up @@ -785,7 +784,6 @@ pub fn init_new_network(
TARGET_REGISTRATIONS_PER_INTERVAL.save(store, netuid, &1)?;
ADJUSTMENTS_ALPHA.save(store, netuid, &58000)?;
IMMUNITY_PERIOD.save(store, netuid, &7200)?;
MIN_BURN.save(store, netuid, &1)?;

DIFFICULTY.save(store, netuid, &10_000_000)?;
MIN_DIFFICULTY.save(store, netuid, &10_000_000)?;
Expand All @@ -810,9 +808,11 @@ pub fn init_new_network(
BONDS_MOVING_AVERAGE.save(store, netuid, &900_000)?;
LAST_ADJUSTMENT_BLOCK.save(store, netuid, &0)?;
ADJUSTMENT_INTERVAL.save(store, netuid, &100)?;
BURN.save(store, netuid, &0)?;
// MIN_BURN.save(store, netuid,&0)?;
MAX_BURN.save(store, netuid, &1_000_000_000)?;

BURN.save(store, netuid, &1_000_000_000)?;
MIN_BURN.save(store, netuid, &100_000_000)?;
MAX_BURN.save(store, netuid, &100_000_000_000)?;

REGISTRATIONS_THIS_BLOCK.save(store, netuid, &0)?;
// MAX_REGISTRATION_PER_BLOCK.save(store, netuid, &3)?;
KAPPA.save(store, netuid, &32_767)?;
Expand Down
6 changes: 4 additions & 2 deletions src/serving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ pub fn do_serve_axon(
AXONS.save(deps.storage, (netuid, &hotkey_id), &prev_axon)?;

// --- 8. We deposit axon served event.
deps.api
.debug(&format!("📡 AxonServed ( hotkey:{:?} ) ", hotkey_id.clone()));
deps.api.debug(&format!(
"📡 AxonServed ( hotkey:{:?} ) ",
hotkey_id.clone()
));

// --- 9. Return is successful dispatch.
Ok(Response::default()
Expand Down
32 changes: 14 additions & 18 deletions src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::state::{
DELEGATES, DENOM, OWNER, STAKE, TOTAL_COLDKEY_STAKE, TOTAL_HOTKEY_STAKE, TOTAL_ISSUANCE,
TOTAL_STAKE,
};
use crate::utils::{exceeds_tx_rate_limit, get_last_tx_block, set_last_tx_block};
use crate::utils::{exceeds_tx_rate_limit, get_default_take, get_last_tx_block, set_last_tx_block};
use crate::ContractError;
use cyber_std::Response;

Expand Down Expand Up @@ -47,8 +47,7 @@ pub fn do_become_delegate(
hotkey_address: String,
// take: u16,
) -> Result<Response, ContractError> {
// TODO set get_default_take() of custom take
let take = 11_796;
let take = get_default_take(deps.storage);

// --- 1. We check the coldkey signuture.
let coldkey = info.sender;
Expand Down Expand Up @@ -500,23 +499,20 @@ pub fn decrease_stake_on_coldkey_hotkey_account(
Ok(())
}

#[cfg(test)]
pub fn add_balance_to_coldkey_account(_coldkey: &Addr, _amount: u64) {}

#[cfg(test)]
pub fn can_remove_balance_from_coldkey_account(_coldkey: &Addr, _amount: u64) -> bool {
true
}
// #[cfg(test)]
// pub fn can_remove_balance_from_coldkey_account(_coldkey: &Addr, _amount: u64) -> bool {
// true
// }

#[cfg(test)]
pub fn get_coldkey_balance(_coldkey: &Addr) -> u64 {
return 0;
}
// #[cfg(test)]
// pub fn get_coldkey_balance(_coldkey: &Addr) -> u64 {
// return 0;
// }

#[cfg(test)]
pub fn remove_balance_from_coldkey_account(_coldkey: &Addr, _amount: u64) -> bool {
true
}
// #[cfg(test)]
// pub fn remove_balance_from_coldkey_account(_coldkey: &Addr, _amount: u64) -> bool {
// true
// }

pub fn unstake_all_coldkeys_from_hotkey_account(
store: &mut dyn Storage,
Expand Down
23 changes: 10 additions & 13 deletions src/state_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use crate::state::{
LAST_TX_BLOCK, LAST_UPDATE, LOADED_EMISSION, MAX_ALLOWED_UIDS, MAX_ALLOWED_VALIDATORS,
MAX_BURN, MAX_DIFFICULTY, MAX_REGISTRATION_PER_BLOCK, MAX_WEIGHTS_LIMIT, MIN_ALLOWED_WEIGHTS,
MIN_BURN, MIN_DIFFICULTY, NETWORKS_ADDED, NETWORK_IMMUNITY_PERIOD, NETWORK_LAST_LOCK_COST,
NETWORK_LAST_REGISTERED, NETWORK_LOCK_REDUCTION_INTERVAL, NETWORK_MIN_ALLOWED_UIDS,
NETWORK_MIN_LOCK_COST, NETWORK_MODALITY, NETWORK_RATE_LIMIT, NETWORK_REGISTERED_AT,
NETWORK_REGISTRATION_ALLOWED, NEURONS_TO_PRUNE_AT_NEXT_EPOCH, OWNER, PENDING_EMISSION,
POW_REGISTRATIONS_THIS_INTERVAL, PROMETHEUS, PRUNING_SCORES, RANK,
RAO_RECYCLED_FOR_REGISTRATION, REGISTRATIONS_THIS_BLOCK, REGISTRATIONS_THIS_INTERVAL, RHO,
ROOT, SCALING_LAW_POWER, SERVING_RATE_LIMIT, STAKE, SUBNETWORK_N, SUBNET_LIMIT, SUBNET_LOCKED,
SUBNET_OWNER, SUBNET_OWNER_CUT, TARGET_REGISTRATIONS_PER_INTERVAL, TEMPO, TOTAL_COLDKEY_STAKE,
TOTAL_HOTKEY_STAKE, TOTAL_ISSUANCE, TOTAL_NETWORKS, TOTAL_STAKE, TRUST, TX_RATE_LIMIT, UIDS,
USED_WORK, VALIDATOR_PERMIT, VALIDATOR_PRUNE_LEN, VALIDATOR_TRUST, WEIGHTS,
WEIGHTS_SET_RATE_LIMIT, WEIGHTS_VERSION_KEY,
NETWORK_LAST_REGISTERED, NETWORK_LOCK_REDUCTION_INTERVAL, NETWORK_MIN_LOCK_COST,
NETWORK_MODALITY, NETWORK_RATE_LIMIT, NETWORK_REGISTERED_AT, NETWORK_REGISTRATION_ALLOWED,
NEURONS_TO_PRUNE_AT_NEXT_EPOCH, OWNER, PENDING_EMISSION, POW_REGISTRATIONS_THIS_INTERVAL,
PROMETHEUS, PRUNING_SCORES, RANK, RAO_RECYCLED_FOR_REGISTRATION, REGISTRATIONS_THIS_BLOCK,
REGISTRATIONS_THIS_INTERVAL, RHO, ROOT, SCALING_LAW_POWER, SERVING_RATE_LIMIT, STAKE,
SUBNETWORK_N, SUBNET_LIMIT, SUBNET_LOCKED, SUBNET_OWNER, SUBNET_OWNER_CUT,
TARGET_REGISTRATIONS_PER_INTERVAL, TEMPO, TOTAL_COLDKEY_STAKE, TOTAL_HOTKEY_STAKE,
TOTAL_ISSUANCE, TOTAL_NETWORKS, TOTAL_STAKE, TRUST, TX_RATE_LIMIT, UIDS, USED_WORK,
VALIDATOR_PERMIT, VALIDATOR_PRUNE_LEN, VALIDATOR_TRUST, WEIGHTS, WEIGHTS_SET_RATE_LIMIT,
WEIGHTS_VERSION_KEY,
};

#[cw_serde]
Expand Down Expand Up @@ -55,7 +55,6 @@ pub struct StateInfo {
network_registered_at: Vec<(u16, u64)>,
network_immunity_period: u64,
network_last_registered: u64,
network_min_allowed_uids: u16,
network_min_lock_cost: u64,
network_last_lock_cost: u64,
network_lock_reduction_interval: u64,
Expand Down Expand Up @@ -212,7 +211,6 @@ pub fn get_state_info(store: &dyn Storage) -> StdResult<StateInfo> {
.unwrap();
let network_immunity_period: u64 = NETWORK_IMMUNITY_PERIOD.load(store)?;
let network_last_registered: u64 = NETWORK_LAST_REGISTERED.load(store)?;
let network_min_allowed_uids: u16 = NETWORK_MIN_ALLOWED_UIDS.load(store)?;
let network_min_lock_cost: u64 = NETWORK_MIN_LOCK_COST.load(store)?;
let network_last_lock_cost: u64 = NETWORK_LAST_LOCK_COST.load(store)?;
let network_lock_reduction_interval: u64 = NETWORK_LOCK_REDUCTION_INTERVAL.load(store)?;
Expand Down Expand Up @@ -445,7 +443,6 @@ pub fn get_state_info(store: &dyn Storage) -> StdResult<StateInfo> {
network_registered_at,
network_immunity_period,
network_last_registered,
network_min_allowed_uids,
network_min_lock_cost,
network_last_lock_cost,
network_lock_reduction_interval,
Expand Down
27 changes: 18 additions & 9 deletions src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ use std::fs::File;
use std::io::Write;

use cosmwasm_std::testing::{mock_env, mock_info, MockApi, MockQuerier};
use cosmwasm_std::{Addr, Coin, DepsMut, Empty, Env, MemoryStorage, OwnedDeps, Storage};
use cw_multi_test::{App, AppBuilder, BasicAppBuilder, Contract, ContractWrapper, Executor};
use cosmwasm_std::{coin, Addr, Coin, DepsMut, Empty, Env, OwnedDeps, Storage};
use cw_multi_test::{Contract, ContractWrapper, Executor};
use cw_storage_gas_meter::MemoryStorageWithGas;
use cyber_std::CyberMsgWrapper;
use cyber_std::Response;

use cyber_std_test::{CyberApp, CyberAppWrapped, CyberModule};
use cyber_std_test::CyberApp;

use crate::contract::{execute, instantiate, query};
use crate::msg::ExecuteMsg;
use crate::registration::create_work_for_block_number;
use crate::root::init_new_network;
use crate::root::{get_network_lock_cost, init_new_network};
use crate::utils::{
get_difficulty_as_u64, set_difficulty, set_network_registration_allowed,
get_burn_as_u64, get_difficulty_as_u64, set_difficulty, set_network_registration_allowed,
set_weights_set_rate_limit,
};
use crate::ContractError;
Expand Down Expand Up @@ -214,7 +214,7 @@ pub fn sudo_register_ok_neuron(deps: DepsMut, env: Env, netuid: u16, hotkey: &st

// TODO stake as funds
let env = mock_env();
let info = mock_info(&ROOT, &[]);
let info = mock_info(&ROOT, &[coin(1, "boot".to_string())]);
let res = execute(deps, env, info, msg);
assert_eq!(res.is_ok(), true);
}
Expand Down Expand Up @@ -247,7 +247,12 @@ pub fn burned_register_ok_neuron(
hotkey: hotkey.to_string(),
};

let info = mock_info(coldkey, &[]);
let mut amount = get_burn_as_u64(deps.storage, netuid);
// need to send at least 1 boot
if amount == 0 {
amount = 1;
}
let info = mock_info(coldkey, &[coin(amount as u128, "boot".to_string())]);
let result = execute(deps, env, info, msg);

result
Expand All @@ -266,16 +271,17 @@ pub fn add_stake(
};

// TODO Add funds here
let info = mock_info(coldkey, &[]);
let info = mock_info(coldkey, &[coin(amount as u128, "boot".to_string())]);
let result = execute(deps, env, info, msg);

result
}

pub fn register_network(deps: DepsMut, env: Env, key: &str) -> Result<Response, ContractError> {
let amount = get_network_lock_cost(deps.storage, deps.api, env.block.height).unwrap();
let msg = ExecuteMsg::RegisterNetwork {};

let info = mock_info(key, &[]);
let info = mock_info(key, &[coin(amount as u128, "boot".to_string())]);
let result = execute(deps, env, info, msg);

result
Expand Down Expand Up @@ -466,3 +472,6 @@ fn test_deps() {
let after = get_difficulty_as_u64(&deps.storage, 1);
assert_eq!(after, 1);
}

#[cfg(test)]
pub fn add_balance_to_coldkey_account(_coldkey: &Addr, _amount: u64) {}
Loading

0 comments on commit 30513af

Please sign in to comment.