add kfetch

This commit is contained in:
dr-frmr 2024-05-26 16:12:59 -06:00
parent b6c54c884b
commit 24e0282475
No known key found for this signature in database
6 changed files with 224 additions and 22 deletions

12
Cargo.lock generated
View File

@ -3044,6 +3044,18 @@ dependencies = [
"sha3-asm",
]
[[package]]
name = "kfetch"
version = "0.1.0"
dependencies = [
"anyhow",
"kinode_process_lib 0.8.0 (git+https://github.com/kinode-dao/process_lib?rev=010e175)",
"rmp-serde",
"serde",
"serde_json",
"wit-bindgen",
]
[[package]]
name = "kill"
version = "0.1.0"

View File

@ -22,7 +22,7 @@ members = [
"kinode/packages/kns_indexer/kns_indexer", "kinode/packages/kns_indexer/get_block", "kinode/packages/kns_indexer/state",
"kinode/packages/settings/settings",
"kinode/packages/terminal/terminal",
"kinode/packages/terminal/alias", "kinode/packages/terminal/cat", "kinode/packages/terminal/echo", "kinode/packages/terminal/hi", "kinode/packages/terminal/kill", "kinode/packages/terminal/m", "kinode/packages/terminal/top",
"kinode/packages/terminal/alias", "kinode/packages/terminal/cat", "kinode/packages/terminal/echo", "kinode/packages/terminal/hi", "kinode/packages/terminal/kfetch", "kinode/packages/terminal/kill", "kinode/packages/terminal/m", "kinode/packages/terminal/top",
"kinode/packages/terminal/namehash_to_name", "kinode/packages/terminal/net_diagnostics", "kinode/packages/terminal/peer", "kinode/packages/terminal/peers",
"kinode/packages/tester/tester", "kinode/packages/tester/test_runner",
]

View File

@ -0,0 +1,21 @@
[package]
name = "kfetch"
version = "0.1.0"
edition = "2021"
[features]
simulation-mode = []
[dependencies]
anyhow = "1.0"
kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "010e175" }
rmp-serde = "1.1.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
wit-bindgen = "0.24.0"
[lib]
crate-type = ["cdylib"]
[package.metadata.component]
package = "kinode:process"

View File

@ -0,0 +1,166 @@
use kinode_process_lib::kernel_types::{
KernelCommand, KernelPrint, KernelPrintResponse, KernelResponse,
};
use kinode_process_lib::{call_init, eth, net, println, Address, Message, Request};
use std::collections::HashSet;
/// Fetching OS version from main package.. LMK if there's a better way...
const CARGO_TOML: &str = include_str!("../../../../Cargo.toml");
wit_bindgen::generate!({
path: "target/wit",
world: "process-v0",
});
call_init!(init);
fn init(our: Address) {
// get identity
let Ok(Ok(Message::Response { body, .. })) = Request::to(("our", "net", "distro", "sys"))
.body(rmp_serde::to_vec(&net::NetAction::GetPeer(our.node.clone())).unwrap())
.send_and_await_response(60)
else {
println!("failed to get response from net");
return;
};
let Ok(net::NetResponse::Peer(Some(our_id))) = rmp_serde::from_slice(&body) else {
println!("got malformed response from net");
return;
};
// get eth providers
let Ok(Message::Response { body, .. }) = Request::new()
.target(("our", "eth", "distro", "sys"))
.body(serde_json::to_vec(&eth::EthConfigAction::GetProviders).unwrap())
.send_and_await_response(60)
.unwrap()
else {
println!("failed to get response from eth");
return;
};
let Ok(eth::EthConfigResponse::Providers(providers)) = serde_json::from_slice(&body) else {
println!("failed to parse eth response");
return;
};
// get eth subs
let Ok(Message::Response { body, .. }) = Request::new()
.target(("our", "eth", "distro", "sys"))
.body(serde_json::to_vec(&eth::EthConfigAction::GetState).unwrap())
.send_and_await_response(60)
.unwrap()
else {
println!("failed to get response from eth");
return;
};
let Ok(eth::EthConfigResponse::State {
active_subscriptions,
outstanding_requests,
}) = serde_json::from_slice(&body)
else {
println!("failed to parse eth response");
return;
};
// get number of processes
let Ok(Message::Response { body, .. }) = Request::new()
.target(("our", "kernel", "distro", "sys"))
.body(serde_json::to_vec(&KernelCommand::Debug(KernelPrint::ProcessMap)).unwrap())
.send_and_await_response(60)
.unwrap()
else {
println!("failed to get response from kernel");
return;
};
let Ok(KernelResponse::Debug(KernelPrintResponse::ProcessMap(map))) =
serde_json::from_slice::<KernelResponse>(&body)
else {
println!("failed to parse kernel response");
return;
};
let num_processes = map.len();
print_bird(
&our,
our_id,
providers,
// sum up all the subscriptions
active_subscriptions
.values()
.map(|v| v.len())
.sum::<usize>(),
outstanding_requests.len() as usize,
num_processes,
);
}
fn print_bird(
our: &Address,
our_id: net::Identity,
providers: HashSet<eth::ProviderConfig>,
active_subscriptions: usize,
outstanding_requests: usize,
num_processes: usize,
) {
println!(
r#"
.`
`@@,, ,* {}
`@%@@@, ,~-##`
~@@#@%#@@, ##### Kinode {}
~-%######@@@, #####
-%%#######@#####, pubkey: {}
~^^%##########@ routing: {}
>^#########@
`>#######` {} eth providers for chain IDs {}
.>######% {} active eth subscriptions
/###%^#% {} outstanding eth requests
/##%@# `
./######`
/.^`.#^#^`
` ,#`#`#, {} running processes
,/ /` `
.*`
"#,
our.node(),
version_from_cargo_toml(),
our_id.networking_key,
routing_to_string(our_id.routing),
providers.len(),
providers
.into_iter()
.map(|p| p.chain_id.to_string())
// remove duplicates
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>()
.join(", "),
active_subscriptions,
outstanding_requests,
num_processes
)
}
fn routing_to_string(routing: net::NodeRouting) -> String {
match routing {
net::NodeRouting::Direct { ip, ports } => format!(
"direct at {} with {}",
ip,
ports.into_keys().into_iter().collect::<Vec<_>>().join(", ")
),
net::NodeRouting::Routers(routers) => format!("{} routers", routers.len()),
}
}
fn version_from_cargo_toml() -> String {
let version = CARGO_TOML
.lines()
.find(|line| line.starts_with("version = "))
.expect("Failed to find version in Cargo.toml");
version
.split('=')
.last()
.expect("Failed to parse version from Cargo.toml")
.trim()
.trim_matches('"')
.to_string()
}

View File

@ -9,14 +9,6 @@
"grant_capabilities": [],
"wit_version": 0
},
"echo.wasm": {
"root": false,
"public": false,
"request_networking": false,
"request_capabilities": [],
"grant_capabilities": [],
"wit_version": 0
},
"cat.wasm": {
"root": false,
"public": false,
@ -33,6 +25,14 @@
"grant_capabilities": [],
"wit_version": 0
},
"echo.wasm": {
"root": false,
"public": false,
"request_networking": false,
"request_capabilities": [],
"grant_capabilities": [],
"wit_version": 0
},
"hi.wasm": {
"root": false,
"public": false,
@ -45,18 +45,23 @@
],
"wit_version": 0
},
"kill.wasm": {
"root": false,
"kfetch.wasm": {
"root": true,
"public": false,
"request_networking": false,
"request_capabilities": [
"kernel:distro:sys"
],
"grant_capabilities": [
"kernel:distro:sys"
"eth:distro:sys",
"kernel:distro:sys",
"net:distro:sys"
],
"wit_version": 0
},
"kill.wasm": {
"root": true,
"public": false,
"request_networking": false,
"wit_version": 0
},
"m.wasm": {
"root": true,
"public": true,
@ -112,15 +117,9 @@
"wit_version": 0
},
"top.wasm": {
"root": false,
"root": true,
"public": false,
"request_networking": false,
"request_capabilities": [
"kernel:distro:sys"
],
"grant_capabilities": [
"kernel:distro:sys"
],
"wit_version": 0
}
}

View File

@ -75,6 +75,10 @@ fn init(our: Address) {
"kill".to_string(),
ProcessId::new(Some("kill"), "terminal", "sys"),
),
(
"kfetch".to_string(),
ProcessId::new(Some("kfetch"), "terminal", "sys"),
),
(
"m".to_string(),
ProcessId::new(Some("m"), "terminal", "sys"),