-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
362 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
use ic_base_types::PrincipalId; | ||
use ic_sns_root::{GetSnsCanistersSummaryRequest, GetSnsCanistersSummaryResponse}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::CallCanisters; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RootCanister { | ||
pub canister_id: PrincipalId, | ||
} | ||
|
||
impl RootCanister { | ||
pub async fn sns_canisters_summary<C: CallCanisters>( | ||
&self, | ||
agent: &C, | ||
) -> Result<GetSnsCanistersSummaryResponse, C::Error> { | ||
agent | ||
.call( | ||
self.canister_id, | ||
GetSnsCanistersSummaryRequest { | ||
update_canister_list: None, | ||
}, | ||
) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
use crate::table::{as_table, TableRow}; | ||
use crate::utils::{get_snses_with_metadata, SnsWithMetadata}; | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use futures::{stream, StreamExt}; | ||
use ic_agent::Agent; | ||
use ic_nervous_system_agent::nns::sns_wasm; | ||
use ic_sns_root::types::SnsCanisterType; | ||
use itertools::Itertools; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// The arguments used to configure the lint command | ||
#[derive(Debug, Parser)] | ||
pub struct LintArgs { | ||
/// Output the SNS information as JSON (instead of a human-friendly table). | ||
#[clap(long)] | ||
json: bool, | ||
/// Includes dapp canisters in the output. | ||
#[clap(long)] | ||
include_dapps: bool, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct SnsLintInfo { | ||
name: String, | ||
memory_consumption: Vec<(u64, SnsCanisterType)>, | ||
cycles: Vec<(u64, SnsCanisterType)>, | ||
num_remaining_upgrade_steps: usize, | ||
} | ||
|
||
impl TableRow for SnsLintInfo { | ||
fn column_names() -> Vec<&'static str> { | ||
vec!["Name", "Memory", "Cycles", "Upgrades Remaining"] | ||
} | ||
|
||
fn column_values(&self) -> Vec<String> { | ||
let high_memory_consumption = self | ||
.memory_consumption | ||
.iter() | ||
.filter(|(memory_consumption, _)| { | ||
(*memory_consumption as f64) > 2.5 * 1024.0 * 1024.0 * 1024.0 | ||
}) | ||
.map(|(memory_consumption, canister_type)| { | ||
format!( | ||
"{canister_type}: ({:.2} GiB)", | ||
*memory_consumption as f64 / 1024.0 / 1024.0 / 1024.0 | ||
) | ||
}) | ||
.join(", "); | ||
|
||
let high_memory_consumption = if !high_memory_consumption.is_empty() { | ||
format!("❌ {high_memory_consumption}") | ||
} else { | ||
"👍".to_string() | ||
}; | ||
|
||
let low_cycles = self | ||
.cycles | ||
.iter() | ||
.filter(|(cycles, _)| (*cycles as f64) < 10.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0) | ||
.map(|(cycles, canister_type)| { | ||
format!( | ||
"{canister_type}: ({:.2} TC)", | ||
*cycles as f64 / 1000.0 / 1000.0 / 1000.0 / 1000.0 | ||
) | ||
}) | ||
.join(", "); | ||
let low_cycles = if !low_cycles.is_empty() { | ||
format!("❌ {low_cycles}") | ||
} else { | ||
"👍".to_string() | ||
}; | ||
|
||
vec![ | ||
self.name.clone(), | ||
high_memory_consumption, | ||
low_cycles, | ||
format!("{}", self.num_remaining_upgrade_steps), | ||
] | ||
} | ||
} | ||
|
||
pub async fn exec(args: LintArgs, agent: &Agent) -> Result<()> { | ||
eprintln!("Linting SNSes..."); | ||
|
||
let snses = sns_wasm::list_deployed_snses(agent).await?; | ||
let num_total_snses = snses.len(); | ||
let snses_with_metadata = get_snses_with_metadata(agent, snses).await; | ||
|
||
let num_snses_with_metadata = snses_with_metadata.len(); | ||
|
||
let lint_info: Vec<SnsLintInfo> = stream::iter(snses_with_metadata) | ||
.map(|SnsWithMetadata { sns, name }| async move { | ||
let summary = sns.root.sns_canisters_summary(agent).await?; | ||
let statuses = summary | ||
.into_iter() | ||
.inspect(|(canister_summary, ctype)| { | ||
if canister_summary.is_none() { | ||
eprintln!("SNS {name} canister summary is missing {ctype}"); | ||
} | ||
}) | ||
.filter_map(|(canister_summary, ctype)| { | ||
canister_summary.map(|canister_summary| (canister_summary, ctype)) | ||
}) | ||
.inspect(|(canister_summary, ctype)| { | ||
if canister_summary.status.is_none() { | ||
eprintln!("SNS {name} canister {ctype} has no status"); | ||
} | ||
}) | ||
.filter_map(|(canister_summary, ctype)| { | ||
canister_summary | ||
.status | ||
.map(|canister_summary| (canister_summary, ctype)) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let statuses = if args.include_dapps { | ||
statuses | ||
} else { | ||
statuses | ||
.into_iter() | ||
.filter(|(_, ctype)| *ctype != SnsCanisterType::Dapp) | ||
.collect() | ||
}; | ||
|
||
let (memory_consumption, cycles) = statuses | ||
.into_iter() | ||
.map(|(canister_status, ctype)| { | ||
( | ||
(u64::try_from(canister_status.memory_size.0).unwrap(), ctype), | ||
(u64::try_from(canister_status.cycles.0).unwrap(), ctype), | ||
) | ||
}) | ||
.unzip(); | ||
|
||
let num_remaining_upgrade_steps = sns | ||
.remaining_upgrade_steps(agent) | ||
.await? | ||
.steps | ||
.len() | ||
.saturating_sub(1); | ||
|
||
Result::<SnsLintInfo, anyhow::Error>::Ok(SnsLintInfo { | ||
name, | ||
memory_consumption, | ||
cycles, | ||
num_remaining_upgrade_steps, | ||
}) | ||
}) | ||
.buffer_unordered(10) | ||
.collect::<Vec<Result<_>>>() | ||
.await | ||
.into_iter() | ||
.inspect(|result| { | ||
if let Err(e) = result { | ||
println!("Error: {}", e) | ||
} | ||
}) | ||
.filter_map(Result::ok) | ||
.sorted_by(|a, b| a.name.cmp(&b.name)) | ||
.collect::<Vec<_>>(); | ||
|
||
let output = if args.json { | ||
serde_json::to_string(&lint_info).unwrap() | ||
} else { | ||
as_table(lint_info.as_ref()) | ||
}; | ||
println!("{}", output); | ||
|
||
eprintln!( | ||
"Out of {num_total_snses} SNSes, {num_snses_with_metadata} had metadata and I linted {num_linted} of them.", | ||
num_linted = lint_info.len() | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.