Skip to content

Commit

Permalink
Refactoring of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyborgshead committed Nov 22, 2023
1 parent 147d92b commit 5f086be
Show file tree
Hide file tree
Showing 16 changed files with 3,641 additions and 1,447 deletions.
2,780 changes: 2,513 additions & 267 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ primitive-types = {version = "0.12.2", default-features = false, features = ["by
#sha2 = { version = "0.9.5", default-features = false }
#sha3 = "0.9"
#sp-core = { version = "7.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39", features = ["serde", "std"] }
#sp-io = { version = "7.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39", features = ["std"] }
sp-io = { version = "7.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39", features = ["std"] }
cw-utils = { version = "1.0.2" }

[dev-dependencies]
Expand Down
10 changes: 6 additions & 4 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ pub fn instantiate(
None => Ok(vec![0]),
}
};
for (coldkey, hotkeys) in msg.stakes.iter() {
for (hotkey, stake_uid) in hotkeys.iter() {
for (coldkey_address, hotkeys) in msg.stakes.iter() {
let coldkey = deps.api.addr_validate(&coldkey_address)?;
for (hotkey_address, stake_uid) in hotkeys.iter() {
let hotkey = deps.api.addr_validate(&hotkey_address)?;
let (stake, uid) = stake_uid;
RANK.update(deps.storage, netuid.clone(), action)?;
TRUST.update(deps.storage, netuid.clone(), action)?;
Expand Down Expand Up @@ -235,13 +237,13 @@ pub fn instantiate(
}
})?;
KEYS.save(deps.storage, (netuid.clone(), uid.clone()), &hotkey.clone())?; // Make hotkey - uid association.
UIDS.save(deps.storage, (netuid.clone(), hotkey), &uid.clone())?; // Make uid - hotkey association.
UIDS.save(deps.storage, (netuid.clone(), &hotkey), &uid.clone())?; // Make uid - hotkey association.
BLOCK_AT_REGISTRATION.save(
deps.storage,
(netuid.clone(), uid.clone()),
&env.block.height,
)?;
IS_NETWORK_MEMBER.save(deps.storage, (hotkey, netuid), &true)?;
IS_NETWORK_MEMBER.save(deps.storage, (&hotkey, netuid), &true)?;
OWNER.save(deps.storage, &hotkey, &coldkey)?;

increase_stake_on_coldkey_hotkey_account(
Expand Down
20 changes: 10 additions & 10 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cosmwasm_std::{Addr, CosmosMsg, Empty};

#[cw_serde]
pub struct InstantiateMsg {
pub stakes: Vec<(Addr, Vec<(Addr, (u64, u16))>)>,
pub stakes: Vec<(String, Vec<(String, (u64, u16))>)>,
pub balances_issuance: u64,
}

Expand All @@ -19,15 +19,15 @@ pub enum ExecuteMsg {
version_key: u64,
},
BecomeDelegate {
hotkey: Addr,
hotkey: String,
take: u16,
},
AddStake {
hotkey: Addr,
hotkey: String,
amount_staked: u64,
},
RemoveStake {
hotkey: Addr,
hotkey: String,
amount_unstaked: u64,
},
ServeAxon {
Expand All @@ -52,15 +52,15 @@ pub enum ExecuteMsg {
block_number: u64,
nonce: u64,
work: Vec<u8>,
hotkey: Addr,
coldkey: Addr,
hotkey: String,
coldkey: String,
},
RootRegister {
hotkey: Addr,
hotkey: String,
},
BurnedRegister {
netuid: u16,
hotkey: Addr,
hotkey: String,
},

RegisterNetwork {},
Expand All @@ -70,8 +70,8 @@ pub enum ExecuteMsg {
// Faucet { block_number: u64, nonce: u64, work: Vec<u8> },
SudoRegister {
netuid: u16,
hotkey: Addr,
coldkey: Addr,
hotkey: String,
coldkey: String,
stake: u64,
balance: u64,
},
Expand Down
67 changes: 40 additions & 27 deletions src/registration.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::root::{get_root_netuid, if_subnet_allows_registration, if_subnet_exist};
use crate::staking::{
add_balance_to_coldkey_account, create_account_if_non_existent,
increase_stake_on_coldkey_hotkey_account, u64_to_balance,
};
use cosmwasm_std::{ensure, Addr, Api, DepsMut, Env, MessageInfo, Response, StdResult, Storage};
use primitive_types::{H256, U256};
use sp_io::hashing::{keccak_256, sha2_256};
use crate::root::{get_root_netuid, if_subnet_allows_registration, if_subnet_exist};
use crate::staking::{
can_remove_balance_from_coldkey_account, coldkey_owns_hotkey,
remove_balance_from_coldkey_account,
Expand All @@ -16,19 +13,30 @@ use crate::state::{
TOTAL_ISSUANCE, UIDS, USED_WORK,
};
use crate::uids::{append_neuron, get_subnetwork_n, replace_neuron};
use crate::utils::{burn_tokens, ensure_root, get_burn_as_u64, get_difficulty_as_u64, get_immunity_period, get_max_allowed_uids, get_max_registrations_per_block, get_neuron_block_at_registration, get_pruning_score_for_uid, get_registrations_this_block, get_registrations_this_interval, get_target_registrations_per_interval, increase_rao_recycled, set_pruning_score_for_uid};
use crate::utils::{
burn_tokens, ensure_root, get_burn_as_u64, get_difficulty_as_u64, get_immunity_period,
get_max_allowed_uids, get_max_registrations_per_block, get_neuron_block_at_registration,
get_pruning_score_for_uid, get_registrations_this_block, get_registrations_this_interval,
get_target_registrations_per_interval, increase_rao_recycled, set_pruning_score_for_uid,
};
use crate::ContractError;
use cosmwasm_std::{ensure, Addr, Api, DepsMut, Env, MessageInfo, Response, StdResult, Storage};
use primitive_types::{H256, U256};
use sp_io::hashing::{keccak_256, sha2_256};

pub fn do_sudo_registration(
deps: DepsMut,
env: Env,
info: MessageInfo,
netuid: u16,
hotkey: Addr,
coldkey: Addr,
hotkey_address: String,
coldkey_address: String,
stake: u64,
balance: u64,
) -> Result<Response, ContractError> {
let hotkey = deps.api.addr_validate(&hotkey_address)?;
let coldkey = deps.api.addr_validate(&coldkey_address)?;

ensure_root(deps.storage, &info.sender)?;

ensure!(
Expand Down Expand Up @@ -137,10 +145,12 @@ pub fn do_burned_registration(
env: Env,
info: MessageInfo,
netuid: u16,
hotkey: Addr,
hotkey_address: String,
) -> Result<Response, ContractError> {
// --- 1. Check that the caller has signed the transaction. (the coldkey of the pairing)
let coldkey = info.sender;
let hotkey = deps.api.addr_validate(&hotkey_address)?;

deps.api.debug(&format!(
"do_registration( coldkey:{:?} netuid:{:?} hotkey:{:?} )",
coldkey, netuid, hotkey
Expand Down Expand Up @@ -343,13 +353,16 @@ pub fn do_registration(
block_number: u64,
nonce: u64,
work: Vec<u8>,
hotkey: Addr,
coldkey: Addr,
hotkey_address: String,
coldkey_address: String,
) -> Result<Response, ContractError> {
// --- 1. Check that the caller has signed the transaction.
// TODO( const ): This not be the hotkey signature or else an exterior actor can register the hotkey and potentially control it?
// TODO seriously consider this. Add signatures to the registration.
let signing_origin = info.sender;
let hotkey = deps.api.addr_validate(&hotkey_address)?;
let coldkey = deps.api.addr_validate(&coldkey_address)?;

deps.api.debug(&format!(
"do_registration( origin:{:?} netuid:{:?} hotkey:{:?}, coldkey:{:?} )",
signing_origin, netuid, hotkey, coldkey
Expand Down Expand Up @@ -410,17 +423,17 @@ pub fn do_registration(
);

// --- 8. Ensure the supplied work passes the difficulty.
// let difficulty = get_difficulty(deps.storage, netuid);
// let work_hash: H256 = vec_to_hash(work.clone());
// ensure!(
// hash_meets_difficulty(&work_hash, difficulty),
// ContractError::InvalidDifficulty{}
// ); // Check that the work meets difficulty.
let difficulty = get_difficulty(deps.storage, netuid);
let work_hash: H256 = vec_to_hash(work.clone());
ensure!(
hash_meets_difficulty(&work_hash, difficulty),
ContractError::InvalidDifficulty {}
); // Check that the work meets difficulty.

// --- 7. Check Work is the product of the nonce, the block number, and hotkey. Add this as used work.
// let seal: H256 = create_seal_hash(block_number, nonce, hotkey.clone());
// ensure!(seal == work_hash, ContractError::InvalidSeal{});
// USED_WORK.save(deps.storage, work.clone(), &current_block_number)?;
let seal: H256 = create_seal_hash(block_number, nonce, hotkey.as_str());
ensure!(seal == work_hash, ContractError::InvalidSeal {});
USED_WORK.save(deps.storage, work.clone(), &current_block_number)?;

// --- 9. If the network account does not exist we will create it here.
create_account_if_non_existent(deps.storage, &coldkey, &hotkey);
Expand Down Expand Up @@ -541,12 +554,12 @@ pub fn do_faucet(
let work_hash: H256 = vec_to_hash(work.clone());
ensure!(
hash_meets_difficulty(&work_hash, difficulty),
ContractError::InvalidDifficulty{}
ContractError::InvalidDifficulty {}
); // Check that the work meets difficulty.

// --- 4. Check Work is the product of the nonce, the block number, and hotkey. Add this as used work.
let seal: H256 = create_seal_hash(block_number, nonce, &coldkey);
ensure!(seal == work_hash, ContractError::InvalidSeal{});
let seal: H256 = create_seal_hash(block_number, nonce, coldkey.as_str());
ensure!(seal == work_hash, ContractError::InvalidSeal {});
USED_WORK.save(deps.storage, work.clone(), &current_block_number)?;

// --- 5. Add Balance via faucet.
Expand Down Expand Up @@ -695,7 +708,7 @@ pub fn hash_to_vec(hash: H256) -> Vec<u8> {
return hash_as_vec;
}

pub fn hash_block_and_hotkey(block_hash_bytes: &[u8], hotkey: &Addr) -> H256 {
pub fn hash_block_and_hotkey(block_hash_bytes: &[u8], hotkey: &str) -> H256 {
// Get the public key from the account id.
// let hotkey_pubkey: MultiAddress<Addr, ()> = MultiAddress::Id(hotkey.clone());
// let binding = hotkey_pubkey.encode();
Expand Down Expand Up @@ -776,7 +789,7 @@ pub fn hash_block_and_hotkey(block_hash_bytes: &[u8], hotkey: &Addr) -> H256 {
return seal_hash;
}

pub fn create_seal_hash(block_number_u64: u64, nonce_u64: u64, hotkey: &Addr) -> H256 {
pub fn create_seal_hash(block_number_u64: u64, nonce_u64: u64, hotkey: &str) -> H256 {
let nonce = U256::from(nonce_u64);
let block_hash_at_number: H256 = get_block_hash_from_u64(block_number_u64);
let block_hash_bytes: &[u8] = block_hash_at_number.as_bytes();
Expand Down Expand Up @@ -855,14 +868,14 @@ pub fn create_work_for_block_number(
netuid: u16,
block_number: u64,
start_nonce: u64,
hotkey: &Addr,
hotkey: &str,
) -> (u64, Vec<u8>) {
let difficulty = get_difficulty(store, netuid);
let mut nonce: u64 = start_nonce;
let mut work: H256 = create_seal_hash(block_number, nonce, &hotkey);
let mut work: H256 = create_seal_hash(block_number, nonce, hotkey);
while !hash_meets_difficulty(&work, difficulty) {
nonce = nonce + 1;
work = create_seal_hash(block_number, nonce, &hotkey);
work = create_seal_hash(block_number, nonce, hotkey);
}
let vec_work: Vec<u8> = hash_to_vec(work);
return (nonce, vec_work);
Expand Down
4 changes: 3 additions & 1 deletion src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,10 @@ pub fn do_root_register(
deps: DepsMut,
env: Env,
info: MessageInfo,
hotkey: Addr,
hotkey_address: String,
) -> Result<Response, ContractError> {
let hotkey = deps.api.addr_validate(&hotkey_address)?;

// --- 0. Get the unique identifier (UID) for the root network.
let root_netuid: u16 = get_root_netuid();
let current_block_number: u64 = env.block.height;
Expand Down
12 changes: 9 additions & 3 deletions src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ pub fn do_become_delegate(
deps: DepsMut,
env: Env,
info: MessageInfo,
hotkey: Addr,
hotkey_address: String,
take: u16,
) -> Result<Response, ContractError> {
// --- 1. We check the coldkey signuture.
let coldkey = info.sender;
let hotkey = deps.api.addr_validate(&hotkey_address)?;

deps.api.debug(&format!(
"do_become_delegate( origin:{:?} hotkey:{:?}, take:{:?} )",
coldkey, hotkey, take
Expand Down Expand Up @@ -133,11 +135,13 @@ pub fn do_add_stake(
deps: DepsMut,
env: Env,
info: MessageInfo,
hotkey: Addr,
hotkey_address: String,
stake_to_be_added: u64,
) -> Result<Response, ContractError> {
// --- 1. We check that the transaction is signed by the caller and retrieve the T::AccountId coldkey information.
let coldkey = info.sender;
let hotkey = deps.api.addr_validate(&hotkey_address)?;

deps.api.debug(&format!(
"do_add_stake( origin:{:?} hotkey:{:?}, stake_to_be_added:{:?} )",
coldkey, hotkey, stake_to_be_added
Expand Down Expand Up @@ -238,11 +242,13 @@ pub fn do_remove_stake(
deps: DepsMut,
env: Env,
info: MessageInfo,
hotkey: Addr,
hotkey_address: String,
stake_to_be_removed: u64,
) -> Result<Response, ContractError> {
// --- 1. We check the transaction is signed by the caller and retrieve the T::AccountId coldkey information.
let coldkey = info.sender;
let hotkey = deps.api.addr_validate(&hotkey_address)?;

deps.api.debug(&format!(
"do_remove_stake( origin:{:?} hotkey:{:?}, stake_to_be_removed:{:?} )",
coldkey, hotkey, stake_to_be_removed
Expand Down
Loading

0 comments on commit 5f086be

Please sign in to comment.