Skip to content

Commit

Permalink
Augment missing events for governance (#4769)
Browse files Browse the repository at this point in the history
This takes the event related changes from
#4708 and pulls them out
for independent review and merging.

## Checklist before requesting a review

- [x] If this code contains consensus-breaking changes, I have added the
"consensus-breaking" label. Otherwise, I declare my belief that there
are not consensus-breaking changes, for the following reason:

  > event emission only.

---------

Co-authored-by: plaidfinch <[email protected]>
  • Loading branch information
cronokirby and plaidfinch authored Jul 25, 2024
1 parent acbfe7f commit 3b21ac8
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 65 deletions.
2 changes: 1 addition & 1 deletion crates/core/app/src/action_handler/actions/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl AppActionHandler for ProposalSubmit {
// state needed to vote as delegators
state.mark_proposal_started();

state.record_proto(event::proposal_submit(self));
state.record_proto(event::proposal_submit(self, current_block, voting_end));

tracing::debug!(proposal = %proposal_id, "created proposal");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl ActionHandler for DelegatorVote {
.cast_delegator_vote(*proposal, identity_key, *vote, nullifier, *unbonded_amount)
.await?;

state.record_proto(event::delegator_vote(self));
state.record_proto(event::delegator_vote(self, &identity_key));

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ impl ActionHandler for ValidatorVote {
}
}

state.record_proto(event::validator_vote(self));
let voting_power = state
.specific_validator_voting_power_at_proposal_start(*proposal, *identity_key)
.await?;
state.record_proto(event::validator_vote(self, voting_power));

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/component/governance/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub async fn enact_all_passed_proposals<S: StateWrite>(mut state: S) -> Result<(
.ok_or_else(|| {
anyhow::anyhow!("proposal {} does not exist", proposal_id)
})?;
state.record_proto(event::enact_proposal(&proposal));
state.record_proto(event::proposal_passed(&proposal));
}
tally::Outcome::Fail => {
tracing::info!(proposal = %proposal_id, "proposal failed");
Expand Down
14 changes: 14 additions & 0 deletions crates/core/component/governance/src/component/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,20 @@ pub trait StateReadExt: StateRead + penumbra_stake::StateReadExt {
Ok(())
}

/// Get a specific validator's voting power for a proposal.
async fn specific_validator_voting_power_at_proposal_start(
&self,
proposal_id: u64,
identity_key: IdentityKey,
) -> Result<u64> {
self.get_proto(&state_key::voting_power_at_proposal_start(
proposal_id,
identity_key,
))
.await
.map(Option::unwrap_or_default)
}

/// Get all the active validator voting power for the proposal.
async fn validator_voting_power_at_proposal_start(
&self,
Expand Down
22 changes: 17 additions & 5 deletions crates/core/component/governance/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use penumbra_proto::penumbra::core::component::governance::v1 as pb;
use penumbra_stake::IdentityKey;

use crate::{
DelegatorVote, Proposal, ProposalDepositClaim, ProposalSubmit, ProposalWithdraw, ValidatorVote,
};

pub fn delegator_vote(delegator_vote: &DelegatorVote) -> pb::EventDelegatorVote {
pub fn delegator_vote(
delegator_vote: &DelegatorVote,
validator_identity_key: &IdentityKey,
) -> pb::EventDelegatorVote {
pb::EventDelegatorVote {
vote: Some(pb::DelegatorVote::from(*delegator_vote)),
validator_identity_key: Some(validator_identity_key.clone().into()),
}
}

Expand All @@ -18,9 +23,10 @@ pub fn proposal_deposit_claim(
}
}

pub fn validator_vote(validator_vote: &ValidatorVote) -> pb::EventValidatorVote {
pub fn validator_vote(validator_vote: &ValidatorVote, voting_power: u64) -> pb::EventValidatorVote {
pb::EventValidatorVote {
vote: Some(pb::ValidatorVote::from(validator_vote.clone())),
voting_power,
}
}

Expand All @@ -30,14 +36,20 @@ pub fn proposal_withdraw(withdraw: &ProposalWithdraw) -> pb::EventProposalWithdr
}
}

pub fn proposal_submit(submit: &ProposalSubmit) -> pb::EventProposalSubmit {
pub fn proposal_submit(
submit: &ProposalSubmit,
start_height: u64,
end_height: u64,
) -> pb::EventProposalSubmit {
pb::EventProposalSubmit {
submit: Some(pb::ProposalSubmit::from(submit.clone())),
start_height,
end_height,
}
}

pub fn enact_proposal(proposal: &Proposal) -> pb::EventEnactProposal {
pb::EventEnactProposal {
pub fn proposal_passed(proposal: &Proposal) -> pb::EventProposalPassed {
pb::EventProposalPassed {
proposal: Some(pb::Proposal::from(proposal.clone())),
}
}
Expand Down
35 changes: 34 additions & 1 deletion crates/core/component/governance/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ impl TryFrom<ProposalToml> for Proposal {
}

/// The specific kind of a proposal.
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "clap", derive(clap::Subcommand))]
#[serde(try_from = "pb::ProposalKind", into = "pb::ProposalKind")]
pub enum ProposalKind {
/// A signaling proposal.
#[cfg_attr(feature = "clap", clap(display_order = 100))]
Expand All @@ -227,6 +228,38 @@ pub enum ProposalKind {
UnfreezeIbcClient,
}

impl From<ProposalKind> for pb::ProposalKind {
fn from(kind: ProposalKind) -> pb::ProposalKind {
match kind {
ProposalKind::Signaling => pb::ProposalKind::Signaling,
ProposalKind::Emergency => pb::ProposalKind::Emergency,
ProposalKind::ParameterChange => pb::ProposalKind::ParameterChange,
ProposalKind::CommunityPoolSpend => pb::ProposalKind::CommunityPoolSpend,
ProposalKind::UpgradePlan => pb::ProposalKind::UpgradePlan,
ProposalKind::FreezeIbcClient => pb::ProposalKind::FreezeIbcClient,
ProposalKind::UnfreezeIbcClient => pb::ProposalKind::UnfreezeIbcClient,
}
}
}

impl TryFrom<pb::ProposalKind> for ProposalKind {
type Error = anyhow::Error;

fn try_from(kind: pb::ProposalKind) -> anyhow::Result<ProposalKind> {
let kind = match kind {
pb::ProposalKind::Unspecified => anyhow::bail!("unspecified proposal kind"),
pb::ProposalKind::Signaling => ProposalKind::Signaling,
pb::ProposalKind::Emergency => ProposalKind::Emergency,
pb::ProposalKind::ParameterChange => ProposalKind::ParameterChange,
pb::ProposalKind::CommunityPoolSpend => ProposalKind::CommunityPoolSpend,
pb::ProposalKind::UpgradePlan => ProposalKind::UpgradePlan,
pb::ProposalKind::FreezeIbcClient => ProposalKind::FreezeIbcClient,
pb::ProposalKind::UnfreezeIbcClient => ProposalKind::UnfreezeIbcClient,
};
Ok(kind)
}
}

impl FromStr for ProposalKind {
type Err = anyhow::Error;

Expand Down
68 changes: 64 additions & 4 deletions crates/proto/src/gen/penumbra.core.component.governance.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,11 @@ pub struct EventDelegatorVote {
/// The delegator vote.
#[prost(message, optional, tag = "1")]
pub vote: ::core::option::Option<DelegatorVote>,
/// The corresponding validator's identity key.
#[prost(message, optional, tag = "2")]
pub validator_identity_key: ::core::option::Option<
super::super::super::keys::v1::IdentityKey,
>,
}
impl ::prost::Name for EventDelegatorVote {
const NAME: &'static str = "EventDelegatorVote";
Expand Down Expand Up @@ -1252,6 +1257,9 @@ pub struct EventValidatorVote {
/// The validator vote.
#[prost(message, optional, tag = "1")]
pub vote: ::core::option::Option<ValidatorVote>,
/// The validator's voting power at the time of the proposal's start.
#[prost(uint64, tag = "2")]
pub voting_power: u64,
}
impl ::prost::Name for EventValidatorVote {
const NAME: &'static str = "EventValidatorVote";
Expand Down Expand Up @@ -1280,6 +1288,12 @@ pub struct EventProposalSubmit {
/// Details on the submitted proposal.
#[prost(message, optional, tag = "1")]
pub submit: ::core::option::Option<ProposalSubmit>,
/// The start height for the proposal.
#[prost(uint64, tag = "2")]
pub start_height: u64,
/// The end height for the proposal.
#[prost(uint64, tag = "3")]
pub end_height: u64,
}
impl ::prost::Name for EventProposalSubmit {
const NAME: &'static str = "EventProposalSubmit";
Expand All @@ -1290,13 +1304,13 @@ impl ::prost::Name for EventProposalSubmit {
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EventEnactProposal {
/// The enacted proposal.
pub struct EventProposalPassed {
/// The passed proposal.
#[prost(message, optional, tag = "1")]
pub proposal: ::core::option::Option<Proposal>,
}
impl ::prost::Name for EventEnactProposal {
const NAME: &'static str = "EventEnactProposal";
impl ::prost::Name for EventProposalPassed {
const NAME: &'static str = "EventProposalPassed";
const PACKAGE: &'static str = "penumbra.core.component.governance.v1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!("penumbra.core.component.governance.v1.{}", Self::NAME)
Expand Down Expand Up @@ -1330,6 +1344,52 @@ impl ::prost::Name for EventProposalSlashed {
::prost::alloc::format!("penumbra.core.component.governance.v1.{}", Self::NAME)
}
}
/// All the different kinds of proposals.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum ProposalKind {
/// To make the linter happy
Unspecified = 0,
Signaling = 1,
Emergency = 2,
ParameterChange = 3,
CommunityPoolSpend = 4,
UpgradePlan = 5,
FreezeIbcClient = 6,
UnfreezeIbcClient = 7,
}
impl ProposalKind {
/// String value of the enum field names used in the ProtoBuf definition.
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
ProposalKind::Unspecified => "PROPOSAL_KIND_UNSPECIFIED",
ProposalKind::Signaling => "PROPOSAL_KIND_SIGNALING",
ProposalKind::Emergency => "PROPOSAL_KIND_EMERGENCY",
ProposalKind::ParameterChange => "PROPOSAL_KIND_PARAMETER_CHANGE",
ProposalKind::CommunityPoolSpend => "PROPOSAL_KIND_COMMUNITY_POOL_SPEND",
ProposalKind::UpgradePlan => "PROPOSAL_KIND_UPGRADE_PLAN",
ProposalKind::FreezeIbcClient => "PROPOSAL_KIND_FREEZE_IBC_CLIENT",
ProposalKind::UnfreezeIbcClient => "PROPOSAL_KIND_UNFREEZE_IBC_CLIENT",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"PROPOSAL_KIND_UNSPECIFIED" => Some(Self::Unspecified),
"PROPOSAL_KIND_SIGNALING" => Some(Self::Signaling),
"PROPOSAL_KIND_EMERGENCY" => Some(Self::Emergency),
"PROPOSAL_KIND_PARAMETER_CHANGE" => Some(Self::ParameterChange),
"PROPOSAL_KIND_COMMUNITY_POOL_SPEND" => Some(Self::CommunityPoolSpend),
"PROPOSAL_KIND_UPGRADE_PLAN" => Some(Self::UpgradePlan),
"PROPOSAL_KIND_FREEZE_IBC_CLIENT" => Some(Self::FreezeIbcClient),
"PROPOSAL_KIND_UNFREEZE_IBC_CLIENT" => Some(Self::UnfreezeIbcClient),
_ => None,
}
}
}
/// Generated client implementations.
#[cfg(feature = "rpc")]
pub mod query_service_client {
Expand Down
Loading

0 comments on commit 3b21ac8

Please sign in to comment.