Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom rpc #48

Merged
merged 10 commits into from
Mar 26, 2024
Merged

feat: add custom rpc #48

merged 10 commits into from
Mar 26, 2024

Conversation

xfactor-toml
Copy link
Contributor

@xfactor-toml xfactor-toml commented Mar 25, 2024

Add Custom RPC

  • subspace_getGlobalInfo
    Returns global params and stats
    params: burn_rate, max_name_length, max_allowed_subnets, max_allowed_modules, max_registrations_per_block, max_allowed_weights, max_proposals, max_burn, min_burn, min_stake, floor_delegation_fee, min_weight_stake, target_registrations_per_interval, target_registrations_interval, adjustment_alpha, unit_emission, tx_rate_limit, vote_threshold, vote_mode
    stats: registrations_per_block, total_subnets
  • subspace_getSubnetInfo
    Returns subnet params and stats
    params: founder, founder_share, immunity_period, incentive_ratio, max_allowed_uids, max_allowed_weights, min_allowed_weights, max_stake, max_weight_age, min_stake, name, tempo, trust_ratio, vote_threshold, vote_mode
    stats: emission, n_uids, pending_emission, total_stake
  • subspace_getKeyInfo
    Returns user key's balance, total_stake and the stake_to info across all subnets user staked
    stake_to: netuid, subnet_name, Vec<module_name, stake_amount_to_module>
  • subspace_getModuleInfo
    Returns module uid, params and stats
    params: name, address, delegation_fee, controller
    stats: last_update, registration_block, stake_from, emission, incentive, dividends, weights

runtime/src/lib.rs:1027~1157

Test Custom RPC

  1. build subspace
    cargo build --release

  2. run dev node
    ./target/release/node-subspace --dev

  3. test rpc endpoints using curl

curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "subspace_getGlobalInfo", "params": []}' http://localhost:9944/
curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "subspace_getSubnetInfo", "params": [0]}' http://localhost:9944/
curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "subspace_getKeyInfo", "params": ["5FgfC2DY4yreEWEughz46RZYQ8oBhHVqD9fVq6gV89E6z4Ea"]}' http://localhost:9944/
curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "subspace_getModuleInfo", "params": ["5FgfC2DY4yreEWEughz46RZYQ8oBhHVqD9fVq6gV89E6z4Ea", 0]}' http://localhost:9944/

expected results

subspace_getGlobalInfo

{"jsonrpc":"2.0","result":{"params":{"burn_rate":0,"max_name_length":32,"max_allowed_subnets":256,"max_allowed_modules":10000,"max_registrations_per_block":10,"max_allowed_weights":512,"max_proposals":128,"max_burn":250000000000,"min_burn":4000000000,"min_stake":0,"floor_delegation_fee":5,"min_weight_stake":0,"target_registrations_per_interval":100,"target_registrations_interval":200,"adjustment_alpha":9223372036854775807,"unit_emission":23148148148,"tx_rate_limit":1,"vote_threshold":50,"vote_mode":"authority"},"stats":{"registrations_per_block":0,"total_subnets":19}},"id":1}

subspace_getSubnetInfo

{"jsonrpc":"2.0","result":{"params":{"founder":"5HarzAYD37Sp3vJs385CLvhDPN52Cb1Q352yxZnDZchznPaS","founder_share":0,"immunity_period":100,"incentive_ratio":50,"max_allowed_uids":8144,"max_allowed_weights":512,"min_allowed_weights":32,"max_stake":18446744073709551615,"max_weight_age":18446744073709551615,"min_stake":256000000000,"name":"commune","tempo":100,"trust_ratio":0,"vote_threshold":50,"vote_mode":"authority"},"stats":{"emission":22722463335,"n_uids":8144,"pending_emission":45444926670,"total_stake":64894595914874480}},"id":1}

subspace_getKeyInfo

{"jsonrpc":"2.0","result":{"balance":187930054,"total_stake":294394828613,"stake_to":[{"netuid":0,"subnet_name":"commune","stake_to_module":[["vali.text.realfake::freepalestine0",294394828613]]}]},"id":1}

subspace_getModuleInfo

{"jsonrpc":"2.0","result":{"uid":0,"params":{"name":"vali.text.realfake::freepalestine0","address":"165.22.186.112:50089","delegation_fee":20,"controller":"5FgfC2DY4yreEWEughz46RZYQ8oBhHVqD9fVq6gV89E6z4Ea"},"stats":{"last_update":0,"registration_block":0,"stake_from":[["5FgfC2DY4yreEWEughz46RZYQ8oBhHVqD9fVq6gV89E6z4Ea",294394828613],["5CMNEDouxNdMUEM6NE9HRYaJwCSBarwr765jeLdHvWEE15NH",101571763015],["5Di9U9qgdxA47hQD3dPcb9yhGcnrNDqUv1vFgDgMVAx8i1RF",102239987211],["5DqfRn3hfxLbTmCZhYU1ih5WV5xkycp9cdFyKrhYLUwNxRVo",59600476360205],["5DtXrcgoUzVwWp9F2VdZpswcDuMEYFCtemPkQo4bB2VkAvUb",58077419110],["5DyfLNNL3Mo7FP11wAQZwSEAQaDm6sBBeCm2vPGwX7qRNHTF",0],["5EByH5gURCxqiyVpyzLDh8XiVSZQto3epr2DN2hSsQS24etz",24481945],["5EJVrSjiwMmi9qhKTHVePMUvut1GnZSNjiCqgDzFSA9F1Qdg",1],["5EUPK5qrbFU5wFzfAMe1xVT4SLAV53YgeeRB97Z4aPYZUWQk",2042486848417],["5EWNKTWmEBri63JA6frvd4DvZVXNaG2F3eysZ6UXLC8otyx9",20797523],["5EtCWcm3qVsHGfo4PRWdyXbEK2Hgpirc9SJS23Rd2apm7KVq",2938629147],["5FA1Kx3V8fqrZN8X3PbnPvic5TUdhfGgefpzAQ6wGxAeBS9n",6394851300532],["5FqPreexEuPd9Jbd8EeCtui9sJLZKAxQGns3uN45wbjFCUgW",10547573],["5FzxXTsJ4jqKPrAYXNz8czepDcfRBe2SBWEvMUz6z4Emn3ro",63244],["5GZBhMZZRMWCiqgqdDGZCGo16Kg5aUQUcpuUGWwSgHn9HbRC",373827093776978],["5GeEDhYi8xog11dcfD87jE1xrVTLcAo1LVZXoBczaK5VapTZ",77849171564408],["5Gj1eMB7QBvHccxtRZQBQFWmXnM2XpJQgP1hZ9wVsNbWD2UM",41628],["5GukjUqfH5tV8g3qkMHaFHRMmLdqDQVdwom8PDiB7TeQo9en",4628477],["5Gx98uAtUtFuL3y3knim1EXXBTevG8cGk1tFtfogh6uUEm3Q",1],["5HBofENgmAj2kLLXuaauxj1EPxqnDygFky8CcEdQtghVRmgP",0]],"emission":0,"incentive":0,"dividends":0,"weights":[]}},"id":1}

@salvivona
Copy link
Contributor

could you ensure the keys show the address they are staked to? I don't see this in the subspace_getKeyInfo example

@xfactor-toml
Copy link
Contributor Author

could you ensure the keys show the address they are staked to? I don't see this in the subspace_getKeyInfo example

subspace_getKeyInfo updated

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using documentation comments when adding descriptions to fields:

struct Foo {
	/// This field defines the BLA. Allowed range is [0, 100).
	field: usize,
}

Notice the three slashes there.

Comment on lines 63 to 64
let value = api.get_global_info(at).map_err(runtime_error_into_rpc_err);
Ok(value.unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let value = api.get_global_info(at).map_err(runtime_error_into_rpc_err);
Ok(value.unwrap())
api.get_global_info(at).map_err(runtime_error_into_rpc_err)

Comment on lines 75 to 76
let value = api.get_subnet_info(at, netuid).map_err(runtime_error_into_rpc_err);
Ok(value.unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let value = api.get_subnet_info(at, netuid).map_err(runtime_error_into_rpc_err);
Ok(value.unwrap())
api.get_subnet_info(at, netuid).map_err(runtime_error_into_rpc_err)

Comment on lines 100 to 101
let value = api.get_key_info(at, key).map_err(runtime_error_into_rpc_err);
Ok(value.unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let value = api.get_key_info(at, key).map_err(runtime_error_into_rpc_err);
Ok(value.unwrap())
api.get_key_info(at, key).map_err(runtime_error_into_rpc_err)

Comment on lines 365 to 367
pub fn get_subnet_name(netuid: u16) -> Vec<u8> {
Self::subnet_params(netuid).name
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is reaaaaally inefficient. If someone wishes to fetch only the name of a subnet, let them access it directly:

Suggested change
pub fn get_subnet_name(netuid: u16) -> Vec<u8> {
Self::subnet_params(netuid).name
}
pub fn get_subnet_name(netuid: u16) -> Vec<u8> {
SubnetNames::<T>::get(netuid)
}

I would be even better if we didn't need this function at all. I'd say we access SubnetNames directly when we need to find a name.

delegation_fee: params.delegation_fee,
controller: params.controller,
}
}
}

fn get_key_info(key: AccountId) -> KeyInfo {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also really inefficient. You should iterate through StakeTo with iter_prefix, to avoid iterating for modules that the key doesn't have connections to.

Currently, every time someone calls this, it will iterate through every subnet and module.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better if you figure out a way to only iterate through subnets the key is present.

@xfactor-toml
Copy link
Contributor Author

fix according to recommendations

pallets/subspace/src/subnet.rs Outdated Show resolved Hide resolved
runtime/src/lib.rs Show resolved Hide resolved
runtime/src/lib.rs Outdated Show resolved Hide resolved
runtime/src/lib.rs Outdated Show resolved Hide resolved
runtime/src/lib.rs Show resolved Hide resolved
@saiintbrisson saiintbrisson merged commit be056b9 into main Mar 26, 2024
2 checks passed
@saiintbrisson saiintbrisson deleted the rpc/update branch March 26, 2024 23:17
steinerkelvin pushed a commit that referenced this pull request Apr 22, 2024
feat(pallets/subspace): add arbitrary uri parameter
functor-flow added a commit that referenced this pull request Jan 16, 2025
feat(pallets/subspace): add arbitrary uri parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants