ghost-eye/src/main.rs
Uncle Stretch 73db8ca32a
integration of subxt dependency
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
2024-11-24 19:01:41 +03:00

107 lines
2.9 KiB
Rust

use clap::Parser;
use color_eyre::Result;
use subxt::{
OnlineClient,
backend::{legacy::LegacyRpcMethods, rpc::RpcClient},
};
mod action;
mod app;
mod cli;
mod components;
mod config;
mod errors;
mod logging;
mod tui;
mod network;
mod widgets;
mod types;
mod palette;
mod casper;
use casper::{CasperAccountId, CasperConfig};
#[subxt::subxt(runtime_metadata_path = "./artifacts/casper.scale")]
pub mod casper_network {}
#[tokio::main]
async fn start_tokio_action_loop(
io_rx: std::sync::mpsc::Receiver<action::Action>,
network: &mut network::Network,
) {
while let Ok(io_event) = io_rx.recv() {
let _ = network.handle_network_event(io_event).await;
}
}
#[tokio::main]
async fn start_tokio_finalized_subscription(sub: &mut network::FinalizedSubscription) {
let _ = sub.subscribe_finalized_blocks().await;
}
#[tokio::main]
async fn start_tokio_best_subscription(sub: &mut network::BestSubscription) {
let _ = sub.subscribe_best_blocks().await;
}
#[tokio::main]
async fn main() -> Result<()> {
crate::errors::init()?;
crate::logging::init()?;
let args = cli::Cli::parse();
let (sync_io_tx, sync_io_rx) = std::sync::mpsc::channel();
let (action_tx, action_rx) = tokio::sync::mpsc::unbounded_channel();
let rpc_client = RpcClient::from_url(args.rpc_endpoint).await?;
let legacy_client_api = LegacyRpcMethods::<CasperConfig>::new(rpc_client.clone());
let online_client =
OnlineClient::<CasperConfig>::from_rpc_client(rpc_client.clone()).await?;
let finalized_blocks_sub = online_client.blocks().subscribe_finalized().await?;
let best_blocks_sub = online_client.blocks().subscribe_best().await?;
let cloned_action_tx = action_tx.clone();
std::thread::spawn(move || {
let mut network = network::Network::new(
cloned_action_tx,
online_client,
legacy_client_api,
rpc_client,
);
start_tokio_action_loop(sync_io_rx, &mut network);
});
let cloned_action_tx = action_tx.clone();
let cloned_sync_tx = sync_io_tx.clone();
std::thread::spawn(move || {
let mut subscription = network::FinalizedSubscription::new(
cloned_action_tx,
cloned_sync_tx,
finalized_blocks_sub,
);
start_tokio_finalized_subscription(&mut subscription);
});
let cloned_action_tx = action_tx.clone();
let cloned_sync_tx = sync_io_tx.clone();
std::thread::spawn(move || {
let mut subscription = network::BestSubscription::new(
cloned_action_tx,
cloned_sync_tx,
best_blocks_sub,
);
start_tokio_best_subscription(&mut subscription);
});
app::App::new(sync_io_tx, action_tx, action_rx)?
.with_frame_rate(args.frame_rate)
.with_tick_rate(args.tick_rate)
.with_mouse(args.mouse_needed)
.with_paste(args.paste_needed)
.run()
.await?;
Ok(())
}