82 lines
3.2 KiB
Rust
82 lines
3.2 KiB
Rust
use crate::native_node::{message, node};
|
|
use crate::log::*;
|
|
use tokio::sync::mpsc;
|
|
use crate::core;
|
|
use std::io::{self, Write};
|
|
|
|
impl node::NativeNode {
|
|
pub async fn cli(command_sender: mpsc::Sender<node::NodeCommand>) {
|
|
loop {
|
|
print!("\n> ");
|
|
io::stdout().flush().unwrap();
|
|
let mut input = String::new();
|
|
match io::stdin().read_line(&mut input) {
|
|
Ok(_) => {
|
|
let input = input.trim();
|
|
if input.is_empty() {
|
|
continue ;
|
|
}
|
|
|
|
let parts: Vec<&str> = input.split_whitespace().collect();
|
|
let command = parts[0];
|
|
let args = &parts[1..];
|
|
|
|
match command {
|
|
"id" => {
|
|
command_sender.send(node::NodeCommand::DebugShowId).await.unwrap();
|
|
},
|
|
"tx" => {
|
|
if args.len() != 4 {
|
|
log!(ERROR, "Invalid arg count! Expected {}, got {}", 4, args.len());
|
|
continue;
|
|
}
|
|
let from = args[0];
|
|
let to = args[1];
|
|
let value = args[2].parse::<u32>().unwrap();
|
|
let data = args[3];
|
|
|
|
let tx = core::Tx::new(
|
|
from.to_string(),
|
|
to.to_string(),
|
|
value,
|
|
data.to_string()
|
|
);
|
|
|
|
let cmd = node::NodeCommand::Transaction { tx };
|
|
|
|
command_sender.send(cmd).await.unwrap();
|
|
},
|
|
"block" => {
|
|
let cmd = node::NodeCommand::CreateBlock;
|
|
command_sender.send(cmd).await.unwrap();
|
|
},
|
|
"list" => {
|
|
if args.len() != 1 {
|
|
log!(ERROR, "{command}: Invalid arg! (blocks, peers)");
|
|
continue;
|
|
}
|
|
match args[0] {
|
|
"blocks" => command_sender.send(node::NodeCommand::DebugListBlocks).await.unwrap(),
|
|
"peers" => command_sender.send(node::NodeCommand::DebugListPeers).await.unwrap(),
|
|
_ => log!(ERROR, "Unkown arg: {}", args[0]),
|
|
}
|
|
},
|
|
"dump_blocks" => {
|
|
command_sender.send(node::NodeCommand::DebugDumpBlocks).await.unwrap();
|
|
},
|
|
"connect" => {
|
|
command_sender.send(node::NodeCommand::ConnectToSeeds).await.unwrap();
|
|
}
|
|
_ => {
|
|
log!(ERROR, "Unkown command {command}");
|
|
continue;
|
|
}
|
|
}
|
|
},
|
|
Err(_) => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|