-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmain.rs
More file actions
73 lines (60 loc) · 2.12 KB
/
main.rs
File metadata and controls
73 lines (60 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
extern crate dotenvy;
use std::sync::Arc;
use clap::Parser;
use env_logger::Env;
use aligned_batcher::{types::errors::BatcherError, Batcher};
/// Batcher main flow:
/// There are two main tasks spawned: `listen_connections` and `listen_new_blocks`
/// * `listen_connections` waits for websocket connections and adds verification data sent by clients
/// to the batch.
/// * `listen_new_blocks` waits for new blocks and when one is received, checks if the conditions are met
/// the current batch to be submitted. In other words, this task is the one that controls when a batch
/// is to be posted.
#[derive(Parser)]
#[command(name = "Aligned Batcher")]
#[command(about = "An application with server and client subcommands", long_about = None)]
struct Cli {
#[arg(short, long)]
config: String,
#[arg(short, long)]
env_file: Option<String>,
#[arg(short, long)]
port: Option<u16>,
#[arg(short, long)]
addr: Option<String>,
}
#[tokio::main]
async fn main() -> Result<(), BatcherError> {
let cli = Cli::parse();
let port = cli.port.unwrap_or(8080);
let addr = cli.addr.unwrap_or("localhost".to_string());
match cli.env_file {
Some(env_file) => dotenvy::from_filename(env_file).ok(),
None => dotenvy::dotenv().ok(),
};
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let batcher = Batcher::new(cli.config).await;
let batcher = Arc::new(batcher);
let address = format!("{addr}:{port}");
// spawn task to listening for incoming blocks
tokio::spawn({
let app = batcher.clone();
async move {
app.listen_new_blocks()
.await
.expect("Error listening for new blocks exiting")
}
});
// spawn task to poll for BalanceUnlocked events
tokio::spawn({
let app = batcher.clone();
async move {
app.poll_balance_unlocked_events()
.await
.expect("Error polling BalanceUnlocked events")
}
});
batcher.metrics.inc_batcher_restart();
batcher.listen_connections(&address).await?;
Ok(())
}