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

Commit 6634a4e

Browse files
authored
Merge pull request #85 from heliaxdev/yuji/fix-inflation-check
fix inflation check
2 parents 3849335 + 480360c commit 6634a4e

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

check/src/checks/inflation.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1+
use namada_sdk::governance::utils::{ProposalStatus, TallyResult};
12
use namada_sdk::rpc;
3+
use namada_sdk::token::Amount;
24

35
use crate::sdk::namada::Sdk;
6+
use crate::state::State;
47

58
use super::DoCheck;
69

10+
const PROPOSAL_DEPOSIT: u64 = 50 * namada_sdk::token::NATIVE_SCALE;
11+
712
#[derive(Clone, Debug, Default)]
813
pub struct InflationCheck;
914

1015
impl DoCheck for InflationCheck {
11-
async fn check(&self, sdk: &Sdk, state: &mut crate::state::State) -> Result<(), String> {
16+
async fn check(&self, sdk: &Sdk, state: &mut State) -> Result<(), String> {
1217
let native_token = rpc::query_native_token(&sdk.namada.client)
1318
.await
1419
.map_err(|e| e.to_string())?;
1520
let current_total_supply = rpc::get_token_total_supply(&sdk.namada.client, &native_token)
1621
.await
1722
.map_err(|e| format!("Failed to query total supply: {e}"))?;
1823

19-
if state.last_total_supply <= current_total_supply {
24+
let rejected = count_rejected_proposals(sdk, state).await?;
25+
let burned_amount = Amount::from_u64(rejected * PROPOSAL_DEPOSIT);
26+
let last_total_supply = state
27+
.last_total_supply
28+
.checked_sub(burned_amount)
29+
.unwrap_or_default();
30+
31+
if last_total_supply <= current_total_supply {
2032
state.last_total_supply = current_total_supply;
2133
tracing::info!("Total supply ok");
2234
Ok(())
2335
} else {
2436
Err(format!(
2537
"Total supply decreases: before: {} -> after {}",
26-
state.last_total_supply, current_total_supply
38+
last_total_supply, current_total_supply
2739
))
2840
}
2941
}
@@ -36,3 +48,37 @@ impl DoCheck for InflationCheck {
3648
"InflationCheck".to_string()
3749
}
3850
}
51+
52+
async fn count_rejected_proposals(sdk: &Sdk, state: &mut State) -> Result<u64, String> {
53+
let client = &sdk.namada.client;
54+
let epoch = rpc::query_epoch(client).await.map_err(|e| e.to_string())?;
55+
56+
let mut rejected = 0;
57+
let mut proposal_id = state.last_end_proposal_id.map_or(0, |last_id| last_id + 1);
58+
loop {
59+
let proposal = rpc::query_proposal_by_id(client, proposal_id)
60+
.await
61+
.map_err(|e| e.to_string())?;
62+
let Some(proposal) = proposal else {
63+
return Ok(0);
64+
};
65+
66+
if matches!(proposal.get_status(epoch), ProposalStatus::Ended) {
67+
state.last_end_proposal_id = Some(proposal_id);
68+
69+
let result = rpc::query_proposal_result(client, proposal_id)
70+
.await
71+
.map_err(|e| e.to_string())?
72+
.expect("Proposal should exist");
73+
if matches!(result.result, TallyResult::Rejected) {
74+
rejected += 1;
75+
}
76+
} else {
77+
break;
78+
}
79+
80+
proposal_id += 1;
81+
}
82+
83+
Ok(rejected)
84+
}

check/src/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct State {
77
pub last_epoch: u64,
88
pub last_total_supply: token::Amount,
99
pub two_nodes_have_two_third: bool,
10+
pub last_end_proposal_id: Option<u64>,
1011
}
1112

1213
impl State {
@@ -17,6 +18,7 @@ impl State {
1718
last_epoch: 0,
1819
last_total_supply: token::Amount::default(),
1920
two_nodes_have_two_third: true,
21+
last_end_proposal_id: None,
2022
}
2123
}
2224
}

0 commit comments

Comments
 (0)