Skip to content

Commit

Permalink
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 ec19310 commit d166215
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
42 changes: 39 additions & 3 deletions crates/governance/src/finalize_block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! 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 @@ -41,7 +43,7 @@ pub fn finalize_block<S, Token, PoS, FnTx, VpCache, TxCache, FnIbcTransfer>(
is_new_epoch: bool,
dispatch_tx: FnTx,
transfer_over_ibc: FnIbcTransfer,
) -> Result<()>
) -> Result<Option<ProposalsResult>>
where
S: StateRead + State,
Token: token::Read<S> + token::Write<S> + token::Events<S>,
Expand All @@ -50,7 +52,7 @@ where
FnIbcTransfer: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> Result<()>,
{
if is_new_epoch {
load_and_execute_governance_proposals::<
let result = load_and_execute_governance_proposals::<
S,
Token,
PoS,
Expand All @@ -67,8 +69,12 @@ where
dispatch_tx,
transfer_over_ibc,
)?;
if result.passed.is_empty() && result.rejected.is_empty() {
return Ok(None);
}
return Ok(Some(result));
}
Ok(())
Ok(None)
}

/// Count of passed and rejected proposals.
Expand All @@ -78,6 +84,36 @@ pub struct ProposalsResult {
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>()
}

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

0 comments on commit d166215

Please sign in to comment.