Skip to content

Commit

Permalink
Added queries, enabled and tested PoW from python side
Browse files Browse the repository at this point in the history
  • Loading branch information
cyborgshead committed Dec 2, 2023
1 parent dd85992 commit e19aada
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
40 changes: 34 additions & 6 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ pub fn instantiate(
BONDS_MOVING_AVERAGE.save(deps.storage, root_netuid, &900_000)?;
LAST_ADJUSTMENT_BLOCK.save(deps.storage, root_netuid, &0)?;
ADJUSTMENT_INTERVAL.save(deps.storage, root_netuid, &100)?;
BURN.save(deps.storage, root_netuid, &0)?;
MIN_BURN.save(deps.storage, root_netuid, &0)?;
MAX_BURN.save(deps.storage, root_netuid, &1_000_000_000)?;
BURN.save(deps.storage, root_netuid, &1_000_000_000)?;
MIN_BURN.save(deps.storage, root_netuid, &100_000_000)?;
MAX_BURN.save(deps.storage, root_netuid, &100_000_000_000)?;
REGISTRATIONS_THIS_BLOCK.save(deps.storage, root_netuid, &0)?;
MAX_REGISTRATION_PER_BLOCK.save(deps.storage, root_netuid, &1)?;
REGISTRATIONS_THIS_INTERVAL.save(deps.storage, root_netuid, &0)?;
Expand Down Expand Up @@ -180,9 +180,9 @@ pub fn instantiate(
BONDS_MOVING_AVERAGE.save(deps.storage, netuid, &900_000)?;
LAST_ADJUSTMENT_BLOCK.save(deps.storage, netuid, &0)?;
ADJUSTMENT_INTERVAL.save(deps.storage, netuid, &100)?;
BURN.save(deps.storage, netuid, &0)?;
MIN_BURN.save(deps.storage, netuid, &0)?;
MAX_BURN.save(deps.storage, netuid, &1_000_000_000)?;
BURN.save(deps.storage, netuid, &1_000_000_000)?;
MIN_BURN.save(deps.storage, netuid, &100_000_000)?;
MAX_BURN.save(deps.storage, netuid, &100_000_000_000)?;
REGISTRATIONS_THIS_BLOCK.save(deps.storage, netuid, &0)?;
MAX_REGISTRATION_PER_BLOCK.save(deps.storage, netuid, &3)?;
KAPPA.save(deps.storage, netuid, &32_767)?;
Expand Down Expand Up @@ -578,6 +578,13 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::GetAllSubnetNetuids {} => {
to_json_binary(&query_all_subnet_netuids(deps.storage)?)
},
QueryMsg::GetNetuidsForHotkey { hotkey } => {
let hotkey_address = deps.api.addr_validate(&hotkey)?;
to_json_binary(&query_netuids_for_hotkey(deps.storage, &hotkey_address)?)
},
QueryMsg::GetTotalIssuance {} => to_json_binary(&query_total_issuance(deps.storage)?),
QueryMsg::GetTotalStake {} => to_json_binary(&query_total_stake(deps.storage)?),
QueryMsg::GetTxRateLimit {} => to_json_binary(&query_tx_rate_limit(deps.storage)?),

// TODO added for debugging, remove later
QueryMsg::GetWeights { netuid } => {
Expand Down Expand Up @@ -725,6 +732,27 @@ pub fn query_all_subnet_netuids(store: &dyn Storage) -> StdResult<Vec<u16>> {
Ok(netuids)
}

pub fn query_netuids_for_hotkey(store: &dyn Storage, hotkey: &Addr) -> StdResult<Vec<u16>> {
let networks = get_registered_networks_for_hotkey(store, hotkey);

Ok(networks)
}

pub fn query_total_issuance(store: &dyn Storage) -> StdResult<u64> {
let issuance = TOTAL_ISSUANCE.load(store)?;
Ok(issuance)
}

pub fn query_total_stake(store: &dyn Storage) -> StdResult<u64> {
let stake = TOTAL_STAKE.load(store)?;
Ok(stake)
}

pub fn query_tx_rate_limit(store: &dyn Storage) -> StdResult<u64> {
let limit = TX_RATE_LIMIT.load(store)?;
Ok(limit)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
let storage_version: ContractVersion = get_contract_version(deps.storage)?;
Expand Down
12 changes: 12 additions & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ pub enum QueryMsg {
#[returns(Option<u16>)]
GetUidForHotkeyOnSubnet { hotkey: String, netuid: u16 },

#[returns(Option<Vec<u16>>)]
GetNetuidsForHotkey { hotkey: String },

#[returns(bool)]
GetSubnetExist { netuid: u16 },

Expand Down Expand Up @@ -300,6 +303,15 @@ pub enum QueryMsg {
#[returns(Vec<u16>)]
GetAllSubnetNetuids {},

#[returns(u64)]
GetTotalIssuance {},

#[returns(u64)]
GetTotalStake {},

#[returns(u64)]
GetTxRateLimit {},

// TODO added for debugging, remove later
#[returns(Vec<Vec<u16>>)]
GetWeights { netuid: u16 },
Expand Down
18 changes: 9 additions & 9 deletions src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,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.as_str());
// 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

0 comments on commit e19aada

Please sign in to comment.