use canic::method::Contract;
use canic::metrics::{ChildMetrics, Request};
#[cfg(feature = "sharding")]
use canic::sharding::{Allocation, BeginShardDrain, InspectShard, ShardTarget};
#[cfg(feature = "role-issuer")]
use canic::topology::attestation::CloseRoleIssuance;
use clap::{Parser, Subcommand};
#[cfg(feature = "sharding")]
use ic_platform_types::Principal;
use serde_json::{Value, json};
#[cfg(feature = "sharding")]
use std::fmt::Write as _;
use std::io::Read as _;
#[derive(Parser)]
struct Args {
#[command(subcommand)]
action: Action,
}
#[derive(Subcommand)]
enum Action {
Encode {
#[command(subcommand)]
command: Command,
},
Decode,
}
#[derive(Subcommand)]
enum Command {
Logs { before: Option<u64> },
ChildMetrics {
slot: u8,
#[command(subcommand)]
source: Source,
},
#[cfg(feature = "role-issuer")]
PrepareRoleKey,
#[cfg(feature = "role-issuer")]
CloseRoleIssuance {
shard: Principal,
#[arg(value_parser = generation)]
generation: [u8; 32],
},
#[cfg(feature = "role-issuer")]
RetireShard { approval: String },
#[cfg(feature = "sharding")]
DrainShard {
shard: Principal,
slot: u32,
#[arg(value_parser = generation)]
generation: [u8; 32],
},
#[cfg(feature = "sharding")]
ConfigureCapacity { approval: String },
#[cfg(feature = "sharding")]
ConfigureChildFunding { approval: String },
#[cfg(feature = "sharding")]
StopChildFunding { child: Principal },
#[cfg(feature = "sharding")]
ConfigureStandby { approval: String },
#[cfg(feature = "sharding")]
ExportStandby {
principal: Principal,
destination: Principal,
expires_at_ns: u64,
},
#[cfg(feature = "sharding")]
ImportStandby { principal: Principal },
#[cfg(feature = "sharding")]
InspectStandby,
#[cfg(feature = "sharding")]
ConfirmWorkerRelease { target: String },
#[cfg(feature = "sharding")]
ResetWorker { target: String, expires_at_ns: u64 },
#[cfg(feature = "sharding")]
RefillStandby {
root: Principal,
sequence: u8,
cycles: u64,
expires_at_ns: u64,
},
#[cfg(feature = "sharding")]
InspectShard { shard: Principal },
}
#[derive(Subcommand)]
enum Source {
Db,
Entropy,
Jobs,
Timers,
Blob,
Endpoints { after: Option<String> },
}
#[cfg(feature = "sharding")]
fn generation(value: &str) -> Result<[u8; 32], String> {
if value.len() != 64 || !value.bytes().all(|b| b.is_ascii_hexdigit()) {
return Err("generation must contain 64 hex digits".into());
}
let mut bytes = [0; 32];
for (i, byte) in bytes.iter_mut().enumerate() {
*byte = u8::from_str_radix(&value[i * 2..i * 2 + 2], 16).map_err(|e| e.to_string())?;
}
Ok(bytes)
}
fn request<M: Contract>(input: &M::Input) -> Result<Value, String> {
let arguments =
M::encode_request(input).map_err(|e| format!("invalid {} request: {e:?}", M::NAME))?;
Ok(json!({"method":M::NAME, "arguments":arguments}))
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let value = match Args::parse().action {
Action::Encode {
command: Command::Logs { before },
} => request::<canic::logging::Logs>(&before)?,
#[cfg(feature = "role-issuer")]
Action::Encode {
command: Command::PrepareRoleKey,
} => request::<canic::topology::attestation::PrepareRoleKey>(&())?,
#[cfg(feature = "role-issuer")]
Action::Encode {
command: Command::CloseRoleIssuance { shard, generation },
} => request::<CloseRoleIssuance>(&(
shard,
ic_wire::Bytes::new(generation.to_vec()).expect("32-byte generation"),
))?,
#[cfg(feature = "role-issuer")]
Action::Encode {
command: Command::RetireShard { approval },
} => request::<canic_management_protocol::stored::RetireShard>(&serde_json::from_str(
&approval,
)?)?,
Action::Encode {
command: Command::ChildMetrics { slot, source },
} => request::<ChildMetrics>(&(
slot,
match source {
Source::Db => Request::Db,
Source::Entropy => Request::Entropy,
Source::Jobs => Request::Jobs,
Source::Timers => Request::Timers,
Source::Blob => Request::Blob,
Source::Endpoints { after } => Request::Endpoints(after),
},
))?,
#[cfg(feature = "sharding")]
Action::Encode {
command: Command::InspectShard { shard },
} => request::<InspectShard>(&shard)?,
#[cfg(feature = "sharding")]
Action::Encode {
command:
Command::DrainShard {
shard,
slot,
generation,
},
} => request::<BeginShardDrain>(&ShardTarget {
shard,
allocation: Allocation { slot, generation },
})?,
#[cfg(feature = "sharding")]
Action::Encode {
command: Command::ConfirmWorkerRelease { target },
} => request::<canic::sharding::standby::ConfirmRelease>(&serde_json::from_str(&target)?)?,
#[cfg(feature = "sharding")]
Action::Encode {
command:
Command::ResetWorker {
target,
expires_at_ns,
},
} => request::<canic::sharding::standby::ResetWorker>(&(
serde_json::from_str(&target)?,
expires_at_ns,
))?,
#[cfg(feature = "sharding")]
Action::Encode {
command: Command::ConfigureCapacity { approval },
} => request::<canic_management_protocol::stored::ConfigureCapacity>(
&serde_json::from_str(&approval)?,
)?,
#[cfg(feature = "sharding")]
Action::Encode {
command: Command::ConfigureChildFunding { approval },
} => request::<canic_management_protocol::stored::ConfigureChildFunding>(
&serde_json::from_str(&approval)?,
)?,
#[cfg(feature = "sharding")]
Action::Encode {
command: Command::StopChildFunding { child },
} => request::<canic_management_protocol::stored::StopChildFunding>(&child)?,
#[cfg(feature = "sharding")]
Action::Encode {
command: Command::ConfigureStandby { approval },
} => request::<canic_management_protocol::stored::ConfigureStandby>(
&serde_json::from_str(&approval)?,
)?,
#[cfg(feature = "sharding")]
Action::Encode {
command:
Command::ExportStandby {
principal,
destination,
expires_at_ns,
},
} => request::<canic::sharding::standby::ExportStandby>(&(
principal,
destination,
expires_at_ns,
))?,
#[cfg(feature = "sharding")]
Action::Encode {
command: Command::ImportStandby { principal },
} => request::<canic::sharding::standby::ImportStandby>(&principal)?,
#[cfg(feature = "sharding")]
Action::Encode {
command: Command::InspectStandby,
} => request::<canic::sharding::standby::InspectStandby>(&())?,
#[cfg(feature = "sharding")]
Action::Encode {
command:
Command::RefillStandby {
root,
sequence,
cycles,
expires_at_ns,
},
} => {
request::<canic::sharding::standby::RefillStandby>(&canic::sharding::standby::Refill {
root,
sequence,
cycles,
expires_at_ns,
})?
}
Action::Decode => {
let mut bytes = Vec::new();
std::io::stdin()
.take((reply_limit() + 1) as u64)
.read_to_end(&mut bytes)?;
decode(&bytes)?
}
};
println!("{value}");
Ok(())
}
fn reply_limit() -> usize {
let limit = ChildMetrics::REPLY
.bytes
.max(canic::logging::Logs::REPLY.bytes);
#[cfg(feature = "sharding")]
let limit = canic::sharding::standby::InspectStandby::REPLY
.bytes
.max(canic::sharding::LIMITS.bytes)
.max(limit);
limit
}
fn decode(bytes: &[u8]) -> Result<Value, Box<dyn std::error::Error>> {
if let Ok(page) = canic::logging::Logs::decode_reply(bytes) {
return Ok(serde_json::to_value(page)?);
}
if let Ok(page) = ChildMetrics::decode_reply(bytes) {
return Ok(serde_json::to_value(page)?);
}
#[cfg(feature = "role-issuer")]
if let Ok(reply) = canic::topology::attestation::PrepareRoleKey::decode_reply(bytes) {
return Ok(json!({"issuer_key": reply?.into_vec()}));
}
#[cfg(feature = "role-issuer")]
if let Ok(reply) = CloseRoleIssuance::decode_reply(bytes) {
return Ok(json!({"proof_deadline_ns": reply?.to_string()}));
}
#[cfg(feature = "sharding")]
if let Ok(reply) = InspectShard::decode_reply(bytes) {
let status = reply?;
let mut generation = String::with_capacity(64);
for b in status.target.allocation.generation {
write!(generation, "{b:02x}")?;
}
return Ok(json!({
"shard": status.target.shard.to_text(),
"slot": status.target.allocation.slot,
"generation": generation,
"draining": status.draining,
"live_placements": status.live_placements,
}));
}
#[cfg(feature = "sharding")]
if let Ok(reply) = BeginShardDrain::decode_reply(bytes) {
return Ok(json!({"draining": true, "live_placements": reply?}));
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic_management_protocol::stored::ConfigureCapacity::decode_reply(bytes) {
reply.map_err(|e| format!("capacity: {e:?}"))?;
return Ok(json!({"approved":true}));
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic_management_protocol::stored::ConfigureChildFunding::decode_reply(bytes)
{
return Ok(serde_json::to_value(reply?)?);
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic_management_protocol::stored::StopChildFunding::decode_reply(bytes) {
return Ok(serde_json::to_value(reply?)?);
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic_management_protocol::stored::ConfigureStandby::decode_reply(bytes) {
reply?;
return Ok(json!({"approved":true}));
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic::sharding::standby::ExportStandby::decode_reply(bytes) {
return Ok(serde_json::to_value(reply?)?);
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic::sharding::standby::ImportStandby::decode_reply(bytes) {
return Ok(serde_json::to_value(reply?)?);
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic::sharding::standby::InspectStandby::decode_reply(bytes) {
return Ok(serde_json::to_value(reply)?);
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic::sharding::standby::RefillStandby::decode_reply(bytes) {
return Ok(serde_json::to_value(reply?)?);
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic::sharding::standby::ConfirmRelease::decode_reply(bytes) {
reply?;
return Ok(json!({"released": true}));
}
#[cfg(feature = "sharding")]
if let Ok(reply) = canic::sharding::standby::ResetWorker::decode_reply(bytes) {
return Ok(json!({"reset": reply?}));
}
#[cfg(feature = "role-issuer")]
if let Ok(reply) = canic_management_protocol::stored::RetireShard::decode_reply(bytes) {
return Ok(json!({"retirement":reply?}));
}
Err("unknown Root reply".into())
}