|
1 | 1 | pub mod apprise;
|
2 | 2 | pub mod config;
|
3 | 3 | pub mod log;
|
| 4 | +pub mod rpc; |
| 5 | +pub mod shared; |
4 | 6 | pub mod state;
|
5 | 7 |
|
6 |
| -use std::{thread::sleep, time::Duration}; |
| 8 | +use std::{net::SocketAddr, thread::sleep, time::Duration}; |
7 | 9 |
|
8 |
| -use apprise::{AppRise, AppRiseSlackPayload}; |
| 10 | +use anyhow::Context; |
9 | 11 | use clap::Parser;
|
10 | 12 | use config::AppConfig;
|
| 13 | +use prometheus_exporter::prometheus::Registry; |
| 14 | +use rpc::Rpc; |
| 15 | +use shared::checksums::Checksums; |
| 16 | +use state::State; |
11 | 17 |
|
12 | 18 | #[tokio::main]
|
13 |
| -async fn main() { |
| 19 | +async fn main() -> anyhow::Result<()> { |
14 | 20 | let config = AppConfig::parse();
|
| 21 | + config.log.init(); |
15 | 22 |
|
16 |
| - // let apprise = AppRise::new( |
17 |
| - // config.apprise_url, |
18 |
| - // "T01J3DMLWUW/B086DJEEQKD/pNDFKSQIiiEkMmqrCtKOuzTr".to_string(), |
19 |
| - // "namada-alerts".to_string(), |
20 |
| - // ); |
| 23 | + let rpc = Rpc::new(config.cometbft_urls); |
21 | 24 |
|
22 |
| - // let slack_payload = AppRiseSlackPayload { |
23 |
| - // body: "test".to_string(), |
24 |
| - // }; |
25 |
| - // apprise.send_to_slack(&slack_payload).await.unwrap(); |
| 25 | + let mut checksums = Checksums::default(); |
| 26 | + for code_path in Checksums::code_paths() { |
| 27 | + let code = rpc |
| 28 | + .query_tx_code_hash(&code_path) |
| 29 | + .await? |
| 30 | + .unwrap_or_else(|| panic!("{} must be defined in namada storage.", code_path)); |
| 31 | + checksums.add(code_path, code); |
| 32 | + } |
| 33 | + |
| 34 | + let mut state = State::new(checksums); |
| 35 | + let registry = state.prometheus_registry(); |
| 36 | + |
| 37 | + start_prometheus_exporter(registry, config.prometheus_port)?; |
26 | 38 |
|
27 | 39 | loop {
|
28 |
| - println!("Test..."); |
29 |
| - sleep(Duration::from_secs(3)); |
| 40 | + let block_height = state.next_block_height(); |
| 41 | + let epoch = rpc.query_current_epoch(block_height).await?.unwrap_or(0); |
| 42 | + let block = rpc |
| 43 | + .query_block(block_height, &state.checksums, epoch) |
| 44 | + .await?; |
| 45 | + |
| 46 | + state.update(block); |
| 47 | + |
| 48 | + tracing::info!("Done block {}", block_height); |
30 | 49 | }
|
31 | 50 | }
|
| 51 | + |
| 52 | +fn start_prometheus_exporter(registry: Registry, port: u64) -> anyhow::Result<()> { |
| 53 | + let addr_raw = format!("0.0.0.0:{}", port); |
| 54 | + let addr: SocketAddr = addr_raw.parse().context("can not parse listen addr")?; |
| 55 | + |
| 56 | + let mut builder = prometheus_exporter::Builder::new(addr); |
| 57 | + builder.with_registry(registry); |
| 58 | + builder.start().context("can not start exporter")?; |
| 59 | + |
| 60 | + Ok(()) |
| 61 | +} |
0 commit comments