Skip to content

Commit 590182c

Browse files
authored
initial block parsing (#1)
* wip * wip * wip
1 parent 7c387e9 commit 590182c

File tree

14 files changed

+657
-27
lines changed

14 files changed

+657
-27
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
id: meta
3333
uses: docker/metadata-action@v5
3434
with:
35-
images: ${{ env.REGISTRY_URL }}/anoma/namada-monitoring
35+
images: ${{ env.REGISTRY_URL }}/heliaxdev/namada-monitoring
3636
- name: Login to GHCR
3737
uses: docker/login-action@v3
3838
with:

Cargo.lock

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ tracing = { version = "0.1.40" }
2222
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
2323
chrono = "0.4.38"
2424
anyhow = "1.0.95"
25+
lru = "0.12.5"
26+
bimap = { version = "0.6.3", features = ["serde"] }
27+
prometheus_exporter = "0.8.5"
28+
subtle-encoding = "0.5.1"
2529

2630
[build-dependencies]
2731
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ use vergen::EmitBuilder;
44
fn main() -> Result<(), Box<dyn Error>> {
55
EmitBuilder::builder().all_git().emit()?;
66
Ok(())
7-
}
7+
}

justfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
devs:
2+
rustup toolchain install 1.79.0 --no-self-update --component clippy,rustfmt
3+
rustup toolchain install nightly-2024-06-14 --no-self-update --component clippy,rustfmt
4+
5+
build:
6+
cargo build
7+
8+
check:
9+
cargo check
10+
11+
fmt:
12+
cargo +nightly-2024-06-14 fmt --all
13+
14+
fmt-check:
15+
cargo +nightly-2024-06-14 fmt --all --check
16+
17+
clippy:
18+
cargo clippy
19+
20+
clippy-fix:
21+
cargo clippy --all --fix --allow-dirty --allow-staged
22+
23+
clean:
24+
cargo clean

src/apprise.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ use serde::{Deserialize, Serialize};
88
pub struct AppRiseWrapper<T: Serialize> {
99
pub urls: String,
1010
#[serde(flatten)]
11-
pub body: T
11+
pub body: T,
1212
}
1313

1414
#[derive(Debug, Clone, Serialize, Deserialize)]
1515
pub struct AppRiseSlackPayload {
16-
pub body: String
16+
pub body: String,
1717
}
1818

1919
pub struct AppRise {
2020
pub url: Url,
2121
pub client: Client,
2222
pub slack_webhook_secret: String,
23-
pub channel: String
23+
pub channel: String,
2424
}
2525

2626
impl AppRise {
@@ -29,7 +29,7 @@ impl AppRise {
2929
url: Url::from_str(&url).unwrap(),
3030
client: reqwest::Client::new(),
3131
slack_webhook_secret: slack_secret,
32-
channel: slack_channel
32+
channel: slack_channel,
3333
}
3434
}
3535

@@ -39,9 +39,14 @@ impl AppRise {
3939
urls: format!("slack:///{}/#{}", self.slack_webhook_secret, self.channel),
4040
body: payload,
4141
};
42-
43-
self.client.post(url).json(&payload).send().await.context("Should be able to send slack notification")?;
42+
43+
self.client
44+
.post(url)
45+
.json(&payload)
46+
.send()
47+
.await
48+
.context("Should be able to send slack notification")?;
4449

4550
Ok(())
4651
}
47-
}
52+
}

src/config.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ use crate::log::LogConfig;
33
#[derive(clap::Parser)]
44
pub struct AppConfig {
55
#[clap(short, long, value_parser, num_args = 1.., value_delimiter = ',')]
6-
pub tendermint_url: Vec<String>,
6+
pub cometbft_urls: Vec<String>,
7+
8+
#[clap(long, env)]
9+
pub slack_token: Option<String>,
10+
11+
#[clap(long, env)]
12+
pub slack_channel: Option<String>,
713

814
#[clap(short, long, default_value_t = String::from("http://localhost:8000"))]
915
pub apprise_url: String,
1016

17+
#[clap(long, env, default_value_t = 9184)]
18+
pub prometheus_port: u64,
19+
1120
#[clap(long, env, default_value_t = 5)]
1221
pub sleep_for: u64,
1322

1423
#[clap(flatten)]
1524
pub log: LogConfig,
16-
}
25+
}

src/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ impl LogConfig {
4545
};
4646
}
4747
}
48-
}
48+
}

src/main.rs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,61 @@
11
pub mod apprise;
22
pub mod config;
33
pub mod log;
4+
pub mod rpc;
5+
pub mod shared;
46
pub mod state;
57

6-
use std::{thread::sleep, time::Duration};
8+
use std::{net::SocketAddr, thread::sleep, time::Duration};
79

8-
use apprise::{AppRise, AppRiseSlackPayload};
10+
use anyhow::Context;
911
use clap::Parser;
1012
use config::AppConfig;
13+
use prometheus_exporter::prometheus::Registry;
14+
use rpc::Rpc;
15+
use shared::checksums::Checksums;
16+
use state::State;
1117

1218
#[tokio::main]
13-
async fn main() {
19+
async fn main() -> anyhow::Result<()> {
1420
let config = AppConfig::parse();
21+
config.log.init();
1522

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);
2124

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)?;
2638

2739
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);
3049
}
3150
}
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

Comments
 (0)