Skip to content

Commit

Permalink
fixup! governance: display proposals result
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Aug 30, 2024
1 parent 77743c8 commit ae95058
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 64 deletions.
65 changes: 8 additions & 57 deletions crates/governance/src/finalize_block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Governance logic applied on an end of a block.

use std::borrow::Cow;
use std::collections::BTreeSet;
use std::fmt::Display;
use std::str::FromStr;

use borsh::BorshDeserialize;
Expand Down Expand Up @@ -43,7 +41,7 @@ pub fn finalize_block<S, Token, PoS, FnTx, VpCache, TxCache, FnIbcTransfer>(
is_new_epoch: bool,
dispatch_tx: FnTx,
transfer_over_ibc: FnIbcTransfer,
) -> Result<Option<ProposalsResult>>
) -> Result<()>
where
S: StateRead + State,
Token: token::Read<S> + token::Write<S> + token::Events<S>,
Expand All @@ -52,7 +50,7 @@ where
FnIbcTransfer: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> Result<()>,
{
if is_new_epoch {
let result = load_and_execute_governance_proposals::<
load_and_execute_governance_proposals::<
S,
Token,
PoS,
Expand All @@ -69,49 +67,8 @@ where
dispatch_tx,
transfer_over_ibc,
)?;
if result.passed.is_empty() && result.rejected.is_empty() {
return Ok(None);
}
return Ok(Some(result));
}
Ok(None)
}

/// Count of passed and rejected proposals.
#[derive(Default)]
pub struct ProposalsResult {
passed: Vec<u64>,
rejected: Vec<u64>,
}

impl Display for ProposalsResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}{}",
if self.passed.is_empty() {
Cow::Owned(format!("Passed IDs: {}. ", fmt_ids(&self.passed)))
} else {
Cow::Borrowed("")
},
if self.rejected.is_empty() {
Cow::Owned(format!(
"Rejected IDs: {}.",
fmt_ids(&self.rejected)
))
} else {
Cow::Borrowed("")
}
)
}
}

fn fmt_ids(ids: &[u64]) -> String {
itertools::intersperse(
ids.iter().map(|s| Cow::Owned(s.to_string())),
Cow::Borrowed(", "),
)
.collect::<String>()
Ok(())
}

fn load_and_execute_governance_proposals<
Expand All @@ -130,7 +87,7 @@ fn load_and_execute_governance_proposals<
current_epoch: Epoch,
dispatch_tx: FnTx,
transfer_over_ibc: FnIbcTransfer,
) -> Result<ProposalsResult>
) -> Result<()>
where
S: StateRead + State,
Token: token::Read<S> + token::Write<S> + token::Events<S>,
Expand All @@ -140,7 +97,7 @@ where
{
let proposal_ids = load_proposals(state, current_epoch)?;

let proposals_result = execute_governance_proposals::<
execute_governance_proposals::<
S,
Token,
PoS,
Expand All @@ -156,9 +113,7 @@ where
proposal_ids,
dispatch_tx,
transfer_over_ibc,
)?;

Ok(proposals_result)
)
}

fn execute_governance_proposals<
Expand All @@ -177,16 +132,14 @@ fn execute_governance_proposals<
proposal_ids: BTreeSet<u64>,
mut dispatch_tx: FnTx,
mut transfer_over_ibc: FnIbcTransfer,
) -> Result<ProposalsResult>
) -> Result<()>
where
S: StateRead + State,
Token: token::Read<S> + token::Write<S> + token::Events<S>,
PoS: proof_of_stake::Read<S>,
FnTx: FnMut(&Tx, &mut S, &mut VpCache, &mut TxCache) -> Result<bool>,
FnIbcTransfer: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> Result<()>,
{
let mut proposals_result = ProposalsResult::default();

for id in proposal_ids {
let proposal_funds_key = keys::get_funds_key(id);
let proposal_end_epoch_key = keys::get_voting_end_epoch_key(id);
Expand Down Expand Up @@ -285,7 +238,6 @@ where
}
};
events.emit(proposal_event);
proposals_result.passed.push(id);

// Take events that could have been emitted by PGF
// over IBC, governance proposal execution, etc
Expand Down Expand Up @@ -322,7 +274,6 @@ where
matches!(proposal_type, ProposalType::DefaultWithWasm(_)),
);
events.emit(proposal_event);
proposals_result.rejected.push(id);

tracing::info!(
"Governance proposal {} has been executed and rejected.",
Expand Down Expand Up @@ -369,7 +320,7 @@ where
}
}

Ok(proposals_result)
Ok(())
}

fn compute_proposal_votes<S, PoS>(
Expand Down
2 changes: 1 addition & 1 deletion crates/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub mod storage;
pub mod utils;
pub mod vp;

pub use finalize_block::{finalize_block, ProposalsResult};
pub use finalize_block::finalize_block;
use namada_state::{StorageRead, StorageWrite};
pub use namada_systems::governance::*;
use parameters::GovernanceParameters;
Expand Down
8 changes: 2 additions & 6 deletions crates/node/src/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ where
// Sub-system updates:
// - Governance - applied first in case a proposal changes any of the
// other syb-systems
if let Some(proposals_result) =
gov_finalize_block(self, emit_events, current_epoch, new_epoch)?
{
tracing::info!("Governance proposals result: {proposals_result}");
}
gov_finalize_block(self, emit_events, current_epoch, new_epoch)?;
// - Token
token_finalize_block(&mut self.state, emit_events, is_masp_new_epoch)?;
// - PoS
Expand Down Expand Up @@ -1138,7 +1134,7 @@ fn gov_finalize_block<D, H>(
emit_events: &mut Vec<Event>,
current_epoch: Epoch,
is_new_epoch: bool,
) -> Result<Option<governance::ProposalsResult>>
) -> Result<()>
where
D: DB + for<'iter> DBIter<'iter> + Sync,
H: StorageHasher + Sync,
Expand Down

0 comments on commit ae95058

Please sign in to comment.