Skip to content

Commit

Permalink
Add ic-admin getters for DCs and Node Rewards Table
Browse files Browse the repository at this point in the history
  • Loading branch information
rstout committed Dec 8, 2021
1 parent 18f1e28 commit a34e3c0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 58 deletions.
4 changes: 2 additions & 2 deletions rs/protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ fn build_registry_proto() {

config.type_attribute(
".registry.node_rewards.v2",
"#[derive(candid::CandidType, candid::Deserialize)]",
"#[derive(candid::CandidType, serde::Serialize, candid::Deserialize)]",
);

config.type_attribute(
".registry.dc",
"#[derive(candid::CandidType, candid::Deserialize)]",
"#[derive(candid::CandidType, serde::Serialize, candid::Deserialize)]",
);

config.type_attribute(
Expand Down
4 changes: 0 additions & 4 deletions rs/protobuf/def/registry/dc/v1/dc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ syntax = "proto3";
package registry.dc.v1;

// Identifies a Data Center where IC nodes reside
//
// Any changes to this struct should also be reflected in its `Display` impl
message DataCenterRecord {
string id = 1;
string region = 2;
Expand All @@ -22,8 +20,6 @@ message Gps {
}

// The proposal payload used to add or remove data centers to/from the Registry
//
// Any changes to this struct should also be reflected in its `Display` impl
message AddOrRemoveDataCentersProposalPayload {
repeated DataCenterRecord data_centers_to_add = 1;
// The IDs of data centers to remove
Expand Down
53 changes: 6 additions & 47 deletions rs/protobuf/src/registry/dc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,58 +33,17 @@ pub mod v1 {

impl fmt::Display for DataCenterRecord {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "DataCenterRecord:")?;
writeln!(f, "\tid: {}", &self.id)?;
writeln!(f, "\tregion: {}", &self.region)?;
writeln!(f, "\towner: {}", &self.owner)?;

let (latitude, longitude) = &self.gps
.as_ref()
.map(|gps| (gps.latitude.to_string(), gps.longitude.to_string(),))
.unwrap_or_else(|| ("Not specified".to_string(), "Not specified".to_string()));

writeln!(f, "\tgps.latitude: {}", latitude)?;
writeln!(f, "\tgps.longitude: {}", longitude)
let json = serde_json::to_string_pretty(&self)
.unwrap_or_else(|e| format!("Error when serializing: {}", e));
writeln!(f, "{}", json)
}
}

/// Used to display and validate input to the ic-admin command
/// to submit a `AddOrRemoveDataCentersProposalPayload`
///
/// Example output:
/// ```text
/// AddOrRemoveDataCentersProposalPayload {
/// Data Centers to add:
/// DataCenterRecord:
/// id: AN1
/// region: us-west
/// owner: DC Corp
/// gps.latitude: 37.77493
/// gps.longitude: -122.41942
///
/// DataCenterRecord:
/// id: BC1
/// region: ca-west
/// owner: BC Corp
/// gps.latitude: 38.77493
/// gps.longitude: -125.41942
///
/// Data Centers to remove:
/// FM1, IT1,
/// }
/// ```
impl fmt::Display for AddOrRemoveDataCentersProposalPayload {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "AddOrRemoveDataCentersProposalPayload {{")?;
writeln!(f, "Data Centers to add:")?;
for dc in &self.data_centers_to_add {
writeln!(f, "\t{}", dc)?;
}
write!(f, "Data Centers to remove:\n\t")?;
for dc_id in &self.data_centers_to_remove {
write!(f, "{}, ", dc_id)?;
}
writeln!(f, "\n}}")
let json = serde_json::to_string_pretty(&self)
.unwrap_or_else(|e| format!("Error when serializing: {}", e));
writeln!(f, "{}", json)
}
}
}
9 changes: 9 additions & 0 deletions rs/protobuf/src/registry/node_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod v2 {
include!(std::concat!("../../gen/registry/registry.node_rewards.v2.rs"));
use std::iter::Extend;
use std::collections::BTreeMap;
use std::fmt;

impl UpdateNodeRewardsTableProposalPayload {
pub fn get_rewards_table(&self) -> NodeRewardsTable {
Expand Down Expand Up @@ -44,6 +45,14 @@ pub mod v2 {
}
}

impl fmt::Display for NodeRewardsTable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let json = serde_json::to_string_pretty(&self)
.unwrap_or_else(|e| format!("Error when serializing: {}", e));
writeln!(f, "{}", json)
}
}

mod tests {
#[allow(unused_imports)]
use super::*;
Expand Down
40 changes: 35 additions & 5 deletions rs/registry/admin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ use ic_nns_handler_root::{
use ic_nns_init::make_hsm_sender;
use ic_nns_test_utils::ids::TEST_NEURON_1_ID;
use ic_protobuf::registry::dc::v1::{AddOrRemoveDataCentersProposalPayload, DataCenterRecord};
use ic_protobuf::registry::node_rewards::v2::UpdateNodeRewardsTableProposalPayload;
use ic_protobuf::registry::node_rewards::v2::{
NodeRewardsTable, UpdateNodeRewardsTableProposalPayload,
};
use ic_protobuf::registry::{
conversion_rate::v1::IcpXdrConversionRateRecord,
crypto::v1::{PublicKey, X509PublicKeyCert},
Expand All @@ -72,10 +74,11 @@ use ic_registry_common::registry::RegistryCanister;
use ic_registry_keys::{
get_node_record_node_id, is_node_record_key, make_blessed_replica_version_key,
make_crypto_node_key, make_crypto_threshold_signing_pubkey_key, make_crypto_tls_cert_key,
make_icp_xdr_conversion_rate_record_key, make_node_operator_record_key, make_node_record_key,
make_provisional_whitelist_record_key, make_replica_version_key, make_routing_table_record_key,
make_subnet_list_record_key, make_subnet_record_key, make_unassigned_nodes_config_record_key,
NODE_OPERATOR_RECORD_KEY_PREFIX, ROOT_SUBNET_ID_KEY,
make_data_center_record_key, make_icp_xdr_conversion_rate_record_key,
make_node_operator_record_key, make_node_record_key, make_provisional_whitelist_record_key,
make_replica_version_key, make_routing_table_record_key, make_subnet_list_record_key,
make_subnet_record_key, make_unassigned_nodes_config_record_key,
NODE_OPERATOR_RECORD_KEY_PREFIX, NODE_REWARDS_TABLE_KEY, ROOT_SUBNET_ID_KEY,
};
use ic_registry_subnet_features::SubnetFeatures;
use ic_registry_subnet_type::SubnetType;
Expand Down Expand Up @@ -252,9 +255,13 @@ enum SubCommand {
GetPendingRootProposalsToUpgradeGovernanceCanister,
// Vote on a pending root proposal to upgrade the governance canister.
VoteOnRootProposalToUpgradeGovernanceCanister(VoteOnRootProposalToUpgradeGovernanceCanisterCmd),
/// Get a DataCenterRecord
GetDataCenter(GetDataCenterCmd),
/// Submit a proposal to add data centers and/or remove data centers from
/// the Registry
ProposeToAddOrRemoveDataCenters(ProposeToAddOrRemoveDataCentersCmd),
/// Get the node rewards table
GetNodeRewardsTable,
/// Submit a proposal to update the node rewards table
ProposeToUpdateNodeRewardsTable(ProposeToUpdateNodeRewardsTableCmd),
/// Submit a proposal to update the unassigned nodes
Expand Down Expand Up @@ -1654,6 +1661,11 @@ fn parse_rewardable_nodes(json: &str) -> BTreeMap<String, u32> {
map
}

#[derive(Clap)]
struct GetDataCenterCmd {
pub dc_id: String,
}

/// Sub-command to submit a proposal to add or remove a data center.
#[derive_common_proposal_fields]
#[derive(ProposalMetadata, Clap)]
Expand Down Expand Up @@ -2480,6 +2492,15 @@ async fn main() {
SubCommand::VoteOnRootProposalToUpgradeGovernanceCanister(cmd) => {
vote_on_root_proposal_to_upgrade_governance_canister(cmd, opts.nns_url, sender).await
}
SubCommand::GetDataCenter(cmd) => {
let (bytes, _) = registry_canister
.get_value(make_data_center_record_key(&cmd.dc_id).into_bytes(), None)
.await
.unwrap();

let dc = decode_registry_value::<DataCenterRecord>(bytes);
println!("{}", dc);
}
SubCommand::ProposeToAddOrRemoveDataCenters(cmd) => {
propose_external_proposal_from_command(
cmd,
Expand All @@ -2489,6 +2510,15 @@ async fn main() {
)
.await;
}
SubCommand::GetNodeRewardsTable => {
let (bytes, _) = registry_canister
.get_value(NODE_REWARDS_TABLE_KEY.as_bytes().to_vec(), None)
.await
.unwrap();

let table = decode_registry_value::<NodeRewardsTable>(bytes);
println!("{}", table);
}
SubCommand::ProposeToUpdateNodeRewardsTable(cmd) => {
propose_external_proposal_from_command(
cmd,
Expand Down

0 comments on commit a34e3c0

Please sign in to comment.