Skip to content

Commit

Permalink
log additional details on validator voting period
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Aug 30, 2024
1 parent d4abfc3 commit 60d3d17
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
47 changes: 45 additions & 2 deletions crates/apps_lib/src/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,57 @@ pub async fn query_proposal_result<N: Namada>(
{
display_line!(context.io(), "Proposal Id: {} ", proposal_id);
if current_epoch >= proposal_query.voting_end_epoch {
display_line!(context.io(), "{:4}The voting period has ended.", "");
display_line!(context.io(), "{:4}{}", "", proposal_result);
} else {
} else if current_epoch < proposal_query.voting_start_epoch {
display_line!(
context.io(),
"{:4}The voting period has not begun yet.",
""
);
display_line!(
context.io(),
"{:4}Still voting until epoch {} begins.",
"{:4}Start epoch: {}. End epoch: {}",
"",
proposal_query.voting_start_epoch,
proposal_query.voting_end_epoch
);
if let Ok(Some(last_epoch)) =
namada_sdk::governance::utils::last_validator_voting_epoch(
proposal_query.voting_start_epoch,
proposal_query.voting_end_epoch,
)
{
display_line!(
context.io(),
"{:4}NOTE: Validators will be able to vote only until the \
end of epoch {}.",
"",
last_epoch
)
}
} else {
display_line!(
context.io(),
"{:4}The voting period is underway and will continue until \
epoch {} begins.",
"",
proposal_query.voting_end_epoch,
);
if let Ok(Some(last_epoch)) =
namada_sdk::governance::utils::last_validator_voting_epoch(
proposal_query.voting_start_epoch,
proposal_query.voting_end_epoch,
)
{
display_line!(
context.io(),
"{:4}NOTE: Validators can vote only until the end of \
epoch {}.",
"",
last_epoch
)
}
let res = format!("{}", proposal_result);
if let Some(idx) = res.find(' ') {
let slice = &res[idx..];
Expand Down
46 changes: 46 additions & 0 deletions crates/governance/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,24 @@ pub fn is_valid_validator_voting_period(
}
}

/// Returns the latest epoch in which a validator can vote, given the voting
/// start and end epochs. If the pair of start and end epoch is invalid, then
/// return `None`.
pub fn last_validator_voting_epoch(
voting_start_epoch: Epoch,
voting_end_epoch: Epoch,
) -> Result<Option<Epoch>, arith::Error> {
if voting_start_epoch >= voting_end_epoch {
Ok(None)
} else {
let latest = checked!(
voting_start_epoch.0
+ 2u64 * (voting_end_epoch.0 - voting_start_epoch.0) / 3u64
)?;
Ok(Some(Epoch(latest)))
}
}

#[cfg(test)]
mod test {
use std::ops::{Add, Sub};
Expand Down Expand Up @@ -1440,6 +1458,12 @@ mod test {
2.into(),
4.into()
));
assert_eq!(
last_validator_voting_epoch(2.into(), 4.into())
.unwrap()
.unwrap(),
3.into()
);

assert!(is_valid_validator_voting_period(
3.into(),
Expand All @@ -1456,5 +1480,27 @@ mod test {
2.into(),
5.into()
));
assert_eq!(
last_validator_voting_epoch(2.into(), 5.into())
.unwrap()
.unwrap(),
4.into()
);

for end_epoch in 1u64..=20 {
let last = last_validator_voting_epoch(0.into(), end_epoch.into())
.unwrap()
.unwrap();
assert!(is_valid_validator_voting_period(
last,
0.into(),
end_epoch.into()
));
assert!(!is_valid_validator_voting_period(
last.next(),
0.into(),
end_epoch.into()
));
}
}
}

0 comments on commit 60d3d17

Please sign in to comment.