nectar/src/main.rs

451 lines
15 KiB
Rust
Raw Normal View History

2023-11-14 23:34:17 +03:00
#![feature(btree_extract_if)]
2023-11-02 23:42:18 +03:00
use crate::types::*;
2023-10-02 23:02:53 +03:00
use anyhow::Result;
2023-11-14 20:51:17 +03:00
use clap::{arg, Command};
2023-10-02 23:02:53 +03:00
use std::env;
use std::sync::Arc;
use tokio::sync::{mpsc, oneshot};
use tokio::{fs, time::timeout};
mod encryptor;
mod eth_rpc;
mod filesystem;
mod http_client;
mod http_server;
mod kernel;
mod keygen;
2023-10-31 22:43:19 +03:00
mod net;
2023-10-02 23:02:53 +03:00
mod register;
mod terminal;
2023-11-14 23:34:17 +03:00
mod timer;
2023-10-02 23:02:53 +03:00
mod types;
mod vfs;
2023-11-13 23:11:47 +03:00
// extensions
#[cfg(feature = "llm")]
mod llm;
2023-10-02 23:02:53 +03:00
const EVENT_LOOP_CHANNEL_CAPACITY: usize = 10_000;
const EVENT_LOOP_DEBUG_CHANNEL_CAPACITY: usize = 50;
const TERMINAL_CHANNEL_CAPACITY: usize = 32;
const WEBSOCKET_SENDER_CHANNEL_CAPACITY: usize = 32;
const FILESYSTEM_CHANNEL_CAPACITY: usize = 32;
const HTTP_CHANNEL_CAPACITY: usize = 32;
const HTTP_CLIENT_CHANNEL_CAPACITY: usize = 32;
const ETH_RPC_CHANNEL_CAPACITY: usize = 32;
const VFS_CHANNEL_CAPACITY: usize = 1_000;
const ENCRYPTOR_CHANNEL_CAPACITY: usize = 32;
const CAP_CHANNEL_CAPACITY: usize = 1_000;
2023-11-13 23:11:47 +03:00
const LLM_CHANNEL_CAPACITY: usize = 32;
2023-10-02 23:02:53 +03:00
// const QNS_SEPOLIA_ADDRESS: &str = "0x9e5ed0e7873E0d7f10eEb6dE72E87fE087A12776";
2023-10-02 23:02:53 +03:00
const VERSION: &str = env!("CARGO_PKG_VERSION");
2023-10-31 23:27:41 +03:00
/// This can and should be an environment variable / setting. It configures networking
/// such that indirect nodes always use routers, even when target is a direct node,
/// such that only their routers can ever see their physical networking details.
const REVEAL_IP: bool = true;
2023-10-02 23:02:53 +03:00
#[tokio::main]
async fn main() {
// For use with https://github.com/tokio-rs/console
// console_subscriber::init();
// DEMO ONLY: remove all CLI arguments
2023-11-14 20:51:17 +03:00
let matches = Command::new("Uqbar")
.version("0.1.0")
.author("Uqbar DAO")
.about("A decentralized operating system")
.arg(arg!([home] "Path to home directory").required(true))
.arg(arg!(--rpc <WS_URL> "Ethereum RPC endpoint (must be wss://)").required(true))
.arg(arg!(--llm <LLM_URL> "LLM endpoint"))
.get_matches();
let home_directory_path = matches.get_one::<String>("home").unwrap();
let rpc_url = matches.get_one::<String>("rpc").unwrap();
let llm_url = matches.get_one::<String>("llm");
#[cfg(not(feature = "llm"))]
if let Some(llm_url) = llm_url {
panic!("You passed in --llm {:?} but you do not have the llm feature enabled. Please re-run with `--features llm`", llm_url);
2023-10-02 23:02:53 +03:00
}
// kernel receives system messages via this channel, all other modules send messages
let (kernel_message_sender, kernel_message_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(EVENT_LOOP_CHANNEL_CAPACITY);
// kernel informs other runtime modules of capabilities through this
2023-10-11 08:40:44 +03:00
let (caps_oracle_sender, caps_oracle_receiver): (CapMessageSender, CapMessageReceiver) =
mpsc::channel(CAP_CHANNEL_CAPACITY);
2023-10-02 23:02:53 +03:00
// networking module sends error messages to kernel
let (network_error_sender, network_error_receiver): (NetworkErrorSender, NetworkErrorReceiver) =
mpsc::channel(EVENT_LOOP_CHANNEL_CAPACITY);
// kernel receives debug messages via this channel, terminal sends messages
let (kernel_debug_message_sender, kernel_debug_message_receiver): (DebugSender, DebugReceiver) =
mpsc::channel(EVENT_LOOP_DEBUG_CHANNEL_CAPACITY);
// websocket sender receives send messages via this channel, kernel send messages
let (net_message_sender, net_message_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(WEBSOCKET_SENDER_CHANNEL_CAPACITY);
// filesystem receives request messages via this channel, kernel sends messages
let (fs_message_sender, fs_message_receiver): (MessageSender, MessageReceiver) =
2023-11-13 22:59:17 +03:00
mpsc::channel(FILESYSTEM_CHANNEL_CAPACITY);
2023-10-02 23:02:53 +03:00
// http server channel w/ websockets (eyre)
let (http_server_sender, http_server_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(HTTP_CHANNEL_CAPACITY);
2023-11-14 23:34:17 +03:00
let (timer_service_sender, timer_service_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(HTTP_CHANNEL_CAPACITY);
2023-10-02 23:02:53 +03:00
let (eth_rpc_sender, eth_rpc_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(ETH_RPC_CHANNEL_CAPACITY);
2023-11-14 23:34:17 +03:00
// http client performs http requests on behalf of processes
2023-10-02 23:02:53 +03:00
let (http_client_sender, http_client_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(HTTP_CLIENT_CHANNEL_CAPACITY);
// vfs maintains metadata about files in fs for processes
let (vfs_message_sender, vfs_message_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(VFS_CHANNEL_CAPACITY);
// encryptor handles end-to-end encryption for client messages
let (encryptor_sender, encryptor_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(ENCRYPTOR_CHANNEL_CAPACITY);
2023-11-13 23:11:47 +03:00
// optional llm extension
let (llm_sender, llm_receiver): (MessageSender, MessageReceiver) =
mpsc::channel(LLM_CHANNEL_CAPACITY);
2023-10-02 23:02:53 +03:00
// terminal receives prints via this channel, all other modules send prints
let (print_sender, print_receiver): (PrintSender, PrintReceiver) =
mpsc::channel(TERMINAL_CHANNEL_CAPACITY);
// fs config in .env file (todo add -- arguments cleanly (with clap?))
dotenv::dotenv().ok();
let mem_buffer_limit = env::var("MEM_BUFFER_LIMIT")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(1024 * 1024 * 5); // 5mb default
let chunk_size = env::var("CHUNK_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(1024 * 256); // 256kb default
let flush_to_cold_interval = env::var("FLUSH_TO_COLD_INTERVAL")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(60); // 60s default
let encryption = env::var("ENCRYPTION")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(true); // default true
let cloud_enabled = env::var("CLOUD_ENABLED")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(false); // default false
let s3_config = if let (Ok(access_key), Ok(secret_key), Ok(region), Ok(bucket), Ok(endpoint)) = (
env::var("S3_ACCESS_KEY"),
env::var("S3_SECRET_KEY"),
env::var("S3_REGION"),
env::var("S3_BUCKET"),
env::var("S3_ENDPOINT"),
) {
Some(S3Config {
access_key,
secret_key,
region,
bucket,
endpoint,
})
} else {
None
};
let fs_config = FsConfig {
s3_config,
mem_buffer_limit,
chunk_size,
flush_to_cold_interval,
encryption,
cloud_enabled,
};
// shutdown signal send and await to fs
let (fs_kill_send, fs_kill_recv) = oneshot::channel::<()>();
let (fs_kill_confirm_send, fs_kill_confirm_recv) = oneshot::channel::<()>();
println!("finding public IP address...");
2023-10-03 06:58:20 +03:00
let our_ip: std::net::Ipv4Addr = {
2023-10-02 23:02:53 +03:00
if let Ok(Some(ip)) = timeout(std::time::Duration::from_secs(5), public_ip::addr_v4()).await
{
2023-10-03 06:58:20 +03:00
ip
2023-10-02 23:02:53 +03:00
} else {
2023-10-17 00:45:33 +03:00
println!(
"\x1b[38;5;196mfailed to find public IPv4 address: booting as a routed node\x1b[0m"
);
2023-10-03 06:58:20 +03:00
std::net::Ipv4Addr::LOCALHOST
2023-10-02 23:02:53 +03:00
}
};
// check if we have keys saved on disk, encrypted
// if so, prompt user for "password" to decrypt with
// once password is received, use to decrypt local keys file,
// and pass the keys into boot process as is done in registration.
// NOTE: when we log in, we MUST check the PKI to make sure our
// information matches what we think it should be. this includes
// username, networking key, and routing info.
// if any do not match, we should prompt user to create a "transaction"
// that updates their PKI info on-chain.
let http_server_port = http_server::find_open_port(8080).await.unwrap();
println!("login or register at http://localhost:{}", http_server_port);
2023-10-02 23:02:53 +03:00
let (kill_tx, kill_rx) = oneshot::channel::<bool>();
let disk_keyfile = match fs::read(format!("{}/.keys", home_directory_path)).await {
Ok(keyfile) => keyfile,
Err(_) => Vec::new(),
};
2023-10-02 23:02:53 +03:00
let (tx, mut rx) = mpsc::channel::<(Identity, Keyfile, Vec<u8>)>(1);
let (our, decoded_keyfile, encoded_keyfile) = tokio::select! {
_ = register::register(tx, kill_rx, our_ip.to_string(), http_server_port, disk_keyfile)
=> panic!("registration failed"),
(our, decoded_keyfile, encoded_keyfile) = async {
2023-11-13 23:23:59 +03:00
rx.recv().await.expect("registration failed")
} => (our, decoded_keyfile, encoded_keyfile),
2023-10-02 23:02:53 +03:00
};
2023-10-17 00:45:33 +03:00
println!(
"saving encrypted networking keys to {}/.keys",
home_directory_path
);
fs::write(format!("{}/.keys", home_directory_path), encoded_keyfile)
2023-10-17 00:45:33 +03:00
.await
.unwrap();
println!("registration complete!");
2023-11-14 23:34:17 +03:00
// the boolean flag determines whether the runtime module is *public* or not,
// where public means that any process can always message it.
2023-11-13 23:11:47 +03:00
let mut runtime_extensions = vec![
2023-11-09 22:41:08 +03:00
(
ProcessId::new(Some("filesystem"), "sys", "uqbar"),
fs_message_sender,
false,
),
(
ProcessId::new(Some("http_server"), "sys", "uqbar"),
http_server_sender,
2023-11-09 22:44:19 +03:00
true,
2023-11-09 22:41:08 +03:00
),
(
ProcessId::new(Some("http_client"), "sys", "uqbar"),
http_client_sender,
2023-11-09 22:44:19 +03:00
false,
2023-11-09 22:41:08 +03:00
),
2023-11-14 23:34:17 +03:00
(
ProcessId::new(Some("timer"), "sys", "uqbar"),
timer_service_sender,
true,
),
2023-11-09 22:41:08 +03:00
(
ProcessId::new(Some("eth_rpc"), "sys", "uqbar"),
eth_rpc_sender,
2023-11-09 22:44:19 +03:00
true,
2023-11-09 22:41:08 +03:00
),
(
ProcessId::new(Some("vfs"), "sys", "uqbar"),
vfs_message_sender,
2023-11-09 22:44:19 +03:00
true,
2023-11-09 22:41:08 +03:00
),
(
ProcessId::new(Some("encryptor"), "sys", "uqbar"),
encryptor_sender,
false,
2023-11-13 23:12:16 +03:00
),
2023-11-09 22:41:08 +03:00
];
2023-11-13 23:11:47 +03:00
#[cfg(feature = "llm")]
{
2023-11-14 20:51:17 +03:00
if llm_url.is_none() {
panic!("You did not pass in --llm <LLM_URL> but you have the llm feature enabled. Please re-run with `--llm <LLM_URL>`");
}
2023-11-13 23:11:47 +03:00
runtime_extensions.push((
ProcessId::new(Some("llm"), "sys", "uqbar"), // TODO llm:extensions:uqbar ?
llm_sender,
true,
));
}
2023-10-10 14:48:57 +03:00
let (kernel_process_map, manifest, vfs_messages) = filesystem::load_fs(
2023-10-02 23:02:53 +03:00
our.name.clone(),
home_directory_path.clone(),
decoded_keyfile.file_key,
2023-10-02 23:02:53 +03:00
fs_config,
2023-11-09 22:41:08 +03:00
runtime_extensions.clone(),
2023-10-17 00:45:33 +03:00
)
.await
.expect("fs load failed!");
2023-10-02 23:02:53 +03:00
let _ = kill_tx.send(true);
let _ = print_sender
.send(Printout {
verbosity: 0,
content: format!("our networking public key: {}", our.networking_key),
})
.await;
/*
* the kernel module will handle our userspace processes and receives
* all "messages", the basic message format for uqbar.
*
* if any of these modules fail, the program exits with an error.
*/
let networking_keypair_arc = Arc::new(decoded_keyfile.networking_keypair);
2023-10-02 23:02:53 +03:00
let mut tasks = tokio::task::JoinSet::<Result<()>>::new();
tasks.spawn(kernel::kernel(
our.clone(),
networking_keypair_arc.clone(),
kernel_process_map.clone(),
caps_oracle_sender.clone(),
caps_oracle_receiver,
kernel_message_sender.clone(),
print_sender.clone(),
kernel_message_receiver,
network_error_receiver,
kernel_debug_message_receiver,
net_message_sender.clone(),
2023-11-09 22:41:08 +03:00
runtime_extensions,
2023-10-02 23:02:53 +03:00
));
2023-10-31 22:43:19 +03:00
tasks.spawn(net::networking(
2023-10-02 23:02:53 +03:00
our.clone(),
2023-10-03 06:58:20 +03:00
our_ip.to_string(),
2023-10-02 23:02:53 +03:00
networking_keypair_arc.clone(),
kernel_message_sender.clone(),
network_error_sender,
print_sender.clone(),
net_message_sender,
net_message_receiver,
2023-10-31 23:27:41 +03:00
REVEAL_IP,
2023-10-02 23:02:53 +03:00
));
tasks.spawn(filesystem::fs_sender(
our.name.clone(),
manifest,
kernel_message_sender.clone(),
print_sender.clone(),
fs_message_receiver,
fs_kill_recv,
fs_kill_confirm_send,
));
tasks.spawn(http_server::http_server(
our.name.clone(),
http_server_port,
decoded_keyfile.jwt_secret_bytes.clone(),
2023-10-02 23:02:53 +03:00
http_server_receiver,
kernel_message_sender.clone(),
print_sender.clone(),
));
tasks.spawn(http_client::http_client(
our.name.clone(),
kernel_message_sender.clone(),
http_client_receiver,
print_sender.clone(),
));
2023-11-14 23:34:17 +03:00
tasks.spawn(timer::timer_service(
our.name.clone(),
kernel_message_sender.clone(),
timer_service_receiver,
print_sender.clone(),
));
2023-10-02 23:02:53 +03:00
tasks.spawn(eth_rpc::eth_rpc(
our.name.clone(),
rpc_url.clone(),
kernel_message_sender.clone(),
eth_rpc_receiver,
print_sender.clone(),
));
tasks.spawn(vfs::vfs(
our.name.clone(),
kernel_message_sender.clone(),
print_sender.clone(),
vfs_message_receiver,
caps_oracle_sender.clone(),
2023-10-10 14:48:57 +03:00
vfs_messages,
2023-10-02 23:02:53 +03:00
));
tasks.spawn(encryptor::encryptor(
our.name.clone(),
networking_keypair_arc.clone(),
kernel_message_sender.clone(),
encryptor_receiver,
print_sender.clone(),
));
2023-11-13 23:11:47 +03:00
#[cfg(feature = "llm")]
{
tasks.spawn(llm::llm(
our.name.clone(),
kernel_message_sender.clone(),
llm_receiver,
2023-11-14 20:51:17 +03:00
llm_url.unwrap().to_string(),
2023-11-13 23:11:47 +03:00
print_sender.clone(),
));
}
2023-10-02 23:02:53 +03:00
// if a runtime task exits, try to recover it,
// unless it was terminal signaling a quit
let quit_msg: String = tokio::select! {
2023-11-13 23:49:12 +03:00
Some(Ok(res)) = tasks.join_next() => {
format!(
"\x1b[38;5;196muh oh, a kernel process crashed -- this should never happen: {:?}\x1b[0m",
res
)
2023-10-02 23:02:53 +03:00
}
quit = terminal::terminal(
our.clone(),
VERSION,
home_directory_path.into(),
kernel_message_sender.clone(),
kernel_debug_message_sender,
print_sender.clone(),
print_receiver,
) => {
match quit {
Ok(_) => "graceful exit".into(),
Err(e) => e.to_string(),
}
}
};
2023-11-13 23:49:12 +03:00
2023-10-02 23:02:53 +03:00
// shutdown signal to fs for flush
let _ = fs_kill_send.send(());
let _ = fs_kill_confirm_recv.await;
// gracefully abort all running processes in kernel
let _ = kernel_message_sender
.send(KernelMessage {
2023-10-07 02:25:05 +03:00
id: rand::random(),
2023-10-02 23:02:53 +03:00
source: Address {
node: our.name.clone(),
process: KERNEL_PROCESS_ID.clone(),
2023-10-02 23:02:53 +03:00
},
target: Address {
node: our.name.clone(),
process: KERNEL_PROCESS_ID.clone(),
2023-10-02 23:02:53 +03:00
},
rsvp: None,
message: Message::Request(Request {
inherit: false,
expects_response: None,
2023-10-26 07:11:03 +03:00
ipc: serde_json::to_vec(&KernelCommand::Shutdown).unwrap(),
2023-10-02 23:02:53 +03:00
metadata: None,
}),
payload: None,
signed_capabilities: None,
})
.await;
2023-11-13 23:49:12 +03:00
2023-10-02 23:02:53 +03:00
// abort all remaining tasks
tasks.shutdown().await;
let _ = crossterm::terminal::disable_raw_mode();
2023-11-13 23:49:12 +03:00
println!("\r\n\x1b[38;5;196m{}\x1b[0m", quit_msg);
2023-10-02 23:02:53 +03:00
return;
}