Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit a90e3ff

Browse files
authored
Refactoring check (#47)
* WIP: checker * add checker * map_err
1 parent 106ae75 commit a90e3ff

File tree

9 files changed

+231
-269
lines changed

9 files changed

+231
-269
lines changed

check/Cargo.lock

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

check/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ antithesis_sdk = "0.2.1"
1111
async-trait = "0.1.74"
1212
chrono = "0.4.38"
1313
clap = { version = "4.4.2", features = ["derive", "env"] }
14+
enum_dispatch = "0.3.13"
1415
futures = "0.3.30"
1516
namada_sdk = { version = "0.47.1", default-features = false, features = ["std", "async-send", "download-params"] }
1617
reqwest = { version = "0.11.22", features = ["json"] }

check/src/checks/epoch.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,32 @@ use crate::sdk::namada::Sdk;
55
use super::DoCheck;
66

77
#[derive(Clone, Debug, Default)]
8-
pub struct EpochCheck {}
8+
pub struct EpochCheck;
99

1010
impl DoCheck for EpochCheck {
11-
async fn check(sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
12-
let client = sdk.namada.clone_client();
13-
let last_epoch = rpc::query_epoch(&client).await;
11+
async fn check(&self, sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
12+
let current_epoch = rpc::query_epoch(&sdk.namada.client)
13+
.await
14+
.map_err(|e| format!("Failed to query last epoch: {e}"))?
15+
.into();
1416

15-
match last_epoch {
16-
Ok(epoch) => {
17-
let current_epoch = epoch.0;
18-
if state.last_epoch <= current_epoch {
19-
state.last_epoch = current_epoch;
20-
tracing::info!("Epoch ok");
21-
Ok(())
22-
} else {
23-
Err(format!(
24-
"Epoch decreased: before: {} -> after {}",
25-
state.last_epoch, epoch.0
26-
))
27-
}
28-
}
29-
Err(e) => Err(format!("Failed to query last epoch: {}", e)),
17+
if state.last_epoch <= current_epoch {
18+
state.last_epoch = current_epoch;
19+
tracing::info!("Epoch ok");
20+
Ok(())
21+
} else {
22+
Err(format!(
23+
"Epoch decreased: before: {} -> after {}",
24+
state.last_epoch, current_epoch
25+
))
3026
}
3127
}
3228

33-
fn timing() -> u32 {
29+
fn timing(&self) -> u32 {
3430
15
3531
}
3632

37-
fn to_string() -> String {
33+
fn name(&self) -> String {
3834
"EpochCheck".to_string()
3935
}
4036
}

check/src/checks/height.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
1-
use tendermint_rpc::Client;
1+
use namada_sdk::rpc;
22

33
use crate::sdk::namada::Sdk;
44

55
use super::DoCheck;
66

77
#[derive(Clone, Debug, Default)]
8-
pub struct HeightCheck {}
8+
pub struct HeightCheck;
99

1010
impl DoCheck for HeightCheck {
11-
async fn check(sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
12-
let client = sdk.namada.clone_client();
13-
let last_block = client.latest_block().await;
11+
async fn check(&self, sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
12+
let last_block = rpc::query_block(&sdk.namada.client)
13+
.await
14+
.map_err(|e| format!("Failed to query last block: {e}"))?
15+
.ok_or("No block found".to_string())?;
1416

15-
match last_block {
16-
Ok(block) => {
17-
let current_block_height = u64::from(block.block.header.height);
18-
if state.last_block_height <= current_block_height {
19-
tracing::info!(
20-
"Block height ok ({} -> {})",
21-
state.last_block_height,
22-
current_block_height
23-
);
24-
state.last_block_height = current_block_height;
25-
Ok(())
26-
} else {
27-
Err(format!(
28-
"Block height didnt increase: before: {} -> after {}",
29-
state.last_block_height, current_block_height
30-
))
31-
}
32-
}
33-
Err(e) => Err(format!("Failed to query last block: {}", e)),
17+
let current_block_height = last_block.height.into();
18+
if state.last_block_height <= current_block_height {
19+
tracing::info!(
20+
"Block height ok ({} -> {})",
21+
state.last_block_height,
22+
current_block_height
23+
);
24+
state.last_block_height = current_block_height;
25+
Ok(())
26+
} else {
27+
Err(format!(
28+
"Block height didnt increase: before: {} -> after {}",
29+
state.last_block_height, current_block_height
30+
))
3431
}
3532
}
3633

37-
fn timing() -> u32 {
34+
fn timing(&self) -> u32 {
3835
6
3936
}
4037

41-
fn to_string() -> String {
38+
fn name(&self) -> String {
4239
"HeightCheck".to_string()
4340
}
4441
}

check/src/checks/inflation.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,34 @@ use crate::sdk::namada::Sdk;
55
use super::DoCheck;
66

77
#[derive(Clone, Debug, Default)]
8-
pub struct InflationCheck {}
8+
pub struct InflationCheck;
99

1010
impl DoCheck for InflationCheck {
11-
async fn check(sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
12-
let client = sdk.namada.clone_client();
13-
let native_token = match rpc::query_native_token(&client).await {
14-
Ok(address) => address,
15-
Err(e) => {
16-
return Err(e.to_string());
17-
}
18-
};
19-
20-
let total_supply = rpc::get_token_total_supply(&client, &native_token).await;
21-
22-
match total_supply {
23-
Ok(current_total_supply) => {
24-
if state.last_total_supply <= current_total_supply {
25-
state.last_total_supply = current_total_supply;
26-
tracing::info!("Total supply ok");
27-
Ok(())
28-
} else {
29-
Err(format!(
30-
"Total supply decreases: before: {} -> after {}",
31-
state.last_total_supply, current_total_supply
32-
))
33-
}
34-
}
35-
Err(e) => Err(format!("Failed to query total supply: {}", e)),
11+
async fn check(&self, sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
12+
let native_token = rpc::query_native_token(&sdk.namada.client)
13+
.await
14+
.map_err(|e| e.to_string())?;
15+
let current_total_supply = rpc::get_token_total_supply(&sdk.namada.client, &native_token)
16+
.await
17+
.map_err(|e| format!("Failed to query total supply: {e}"))?;
18+
19+
if state.last_total_supply <= current_total_supply {
20+
state.last_total_supply = current_total_supply;
21+
tracing::info!("Total supply ok");
22+
Ok(())
23+
} else {
24+
Err(format!(
25+
"Total supply decreases: before: {} -> after {}",
26+
state.last_total_supply, current_total_supply
27+
))
3628
}
3729
}
3830

39-
fn timing() -> u32 {
31+
fn timing(&self) -> u32 {
4032
20
4133
}
4234

43-
fn to_string() -> String {
35+
fn name(&self) -> String {
4436
"InflationCheck".to_string()
4537
}
4638
}

check/src/checks/masp_indexer.rs

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,49 @@ pub struct LatestHeightResponse {
1010
}
1111

1212
#[derive(Clone, Debug, Default)]
13-
pub struct MaspIndexerHeightCheck {}
13+
pub struct MaspIndexerHeightCheck;
1414

1515
impl DoCheck for MaspIndexerHeightCheck {
16-
async fn check(sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
16+
async fn check(&self, sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
1717
let url = format!("{}/api/v1/height", sdk.masp_indexer_url);
18-
let masp_indexer_block_height = reqwest::get(&url).await;
19-
20-
match masp_indexer_block_height {
21-
Ok(response) => match response.status() {
22-
reqwest::StatusCode::OK => match response.json::<LatestHeightResponse>().await {
23-
Ok(parsed) => {
24-
let current_block_height = parsed.block_height;
25-
if state.last_block_height_masp_indexer <= current_block_height {
26-
tracing::info!(
27-
"Masp indexer block height ok ({} -> {})",
28-
state.last_block_height_masp_indexer,
29-
current_block_height
30-
);
31-
state.last_block_height_masp_indexer = current_block_height;
32-
Ok(())
33-
} else {
34-
Err(format!(
35-
"Masp indexer height didnt increase: before: {} -> after {}",
36-
state.last_block_height_masp_indexer, current_block_height
37-
))
38-
}
39-
}
40-
Err(e) => Err(format!(
41-
"Error while requesting height from masp indexer: {e}",
42-
)),
43-
},
44-
_ => Err(format!(
45-
"Error while requesting height from masp indexer: status code was {}",
46-
response.status()
47-
)),
48-
},
49-
Err(e) => Err(format!(
50-
"Error while requesting height from masp indexer: {e}",
51-
)),
18+
let response = reqwest::get(&url)
19+
.await
20+
.map_err(|e| format!("Error while requesting height from masp indexer: {e}"))?;
21+
22+
if response.status() != reqwest::StatusCode::OK {
23+
return Err(format!(
24+
"Error while requesting height from masp indexer: status code was {}",
25+
response.status()
26+
));
27+
}
28+
29+
let parsed = response
30+
.json::<LatestHeightResponse>()
31+
.await
32+
.map_err(|e| format!("Error while parsing height from masp indexer: {e}"))?;
33+
34+
let current_block_height = parsed.block_height;
35+
if state.last_block_height_masp_indexer <= current_block_height {
36+
tracing::info!(
37+
"Masp indexer block height ok ({} -> {})",
38+
state.last_block_height_masp_indexer,
39+
current_block_height
40+
);
41+
state.last_block_height_masp_indexer = current_block_height;
42+
Ok(())
43+
} else {
44+
Err(format!(
45+
"Masp indexer height didn't increase: before: {} -> after {}",
46+
state.last_block_height_masp_indexer, current_block_height
47+
))
5248
}
5349
}
5450

55-
fn timing() -> u32 {
51+
fn timing(&self) -> u32 {
5652
12
5753
}
5854

59-
fn to_string() -> String {
55+
fn name(&self) -> String {
6056
"MaspIndexerHeightCheck".to_string()
6157
}
6258
}

0 commit comments

Comments
 (0)