Skip to content

Commit

Permalink
wip: move actions to governance
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Sep 20, 2023
1 parent a0b0126 commit 97f1c4f
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 154 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/core/component/governance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ component = [
"penumbra-chain/component",
"penumbra-sct/component",
"penumbra-stake/component",
"tokio",
]
proving-keys = ["penumbra-proof-params/proving-keys"]
default = ["std", "component", "proving-keys"]
Expand Down Expand Up @@ -60,6 +61,7 @@ rand = "0.8"
im = "15.1"
regex = "1.5"
futures = "0.3"
tokio = { version = "1.21.1", features = ["full", "tracing"], optional = true }

[dev-dependencies]
proptest = "1"
Expand Down
11 changes: 11 additions & 0 deletions crates/core/component/governance/src/action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod delegator_vote;
mod proposal_deposit_claim;
mod proposal_submit;
mod proposal_withdraw;
mod validator_vote;

pub use delegator_vote::{DelegatorVote, DelegatorVoteBody};
pub use proposal_deposit_claim::ProposalDepositClaim;
pub use proposal_submit::ProposalSubmit;
pub use proposal_withdraw::ProposalWithdraw;
pub use validator_vote::{ValidatorVote, ValidatorVoteBody};
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
use anyhow::Context;
use ark_ff::Zero;
use decaf377::Fr;

use crate::{vote::Vote, DelegatorVoteProof};
use decaf377_rdsa::{Signature, SpendAuth, VerificationKey};
use penumbra_asset::{balance, Value};
use penumbra_governance::{DelegatorVoteProof, VotingReceiptToken};
use penumbra_asset::Value;
use penumbra_num::Amount;
use penumbra_proto::{core::governance::v1alpha1 as pb, DomainType, TypeUrl};
use penumbra_sct::Nullifier;
use penumbra_tct as tct;

use crate::{
view::action_view::DelegatorVoteView, vote::Vote, Action, ActionView, IsAction,
TransactionPerspective,
};

#[derive(Debug, Clone)]
pub struct DelegatorVote {
pub body: DelegatorVoteBody,
pub auth_sig: Signature<SpendAuth>,
pub proof: DelegatorVoteProof,
}

impl IsAction for DelegatorVote {
fn balance_commitment(&self) -> balance::Commitment {
Value {
amount: self.body.unbonded_amount,
asset_id: VotingReceiptToken::new(self.body.proposal).id(),
}
.commit(Fr::zero())
}

fn view_from_perspective(&self, txp: &TransactionPerspective) -> ActionView {
let delegator_vote_view = match txp.spend_nullifiers.get(&self.body.nullifier) {
Some(note) => DelegatorVoteView::Visible {
delegator_vote: self.to_owned(),
note: txp.view_note(note.to_owned()),
},
None => DelegatorVoteView::Opaque {
delegator_vote: self.to_owned(),
},
};

ActionView::DelegatorVote(delegator_vote_view)
}
}

/// The body of a delegator vote.
#[derive(Debug, Clone)]
pub struct DelegatorVoteBody {
Expand Down Expand Up @@ -155,9 +125,3 @@ impl TryFrom<pb::DelegatorVote> for DelegatorVote {
})
}
}

impl From<DelegatorVote> for Action {
fn from(value: DelegatorVote) -> Self {
Action::DelegatorVote(value)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use ark_ff::Zero;
use serde::{Deserialize, Serialize};

use decaf377::Fr;
use penumbra_asset::{
asset::{self, DenomMetadata},
balance, Balance, Value, STAKING_TOKEN_ASSET_ID,
Balance, Value, STAKING_TOKEN_ASSET_ID,
};
use penumbra_governance::ProposalNft;
use penumbra_num::Amount;
use penumbra_proto::{core::governance::v1alpha1 as pb, TypeUrl};

use crate::{
proposal::{Outcome, Withdrawn},
ActionView, IsAction, TransactionPerspective,
};
use crate::proposal::{Outcome, Withdrawn};

use crate::ProposalNft;

/// A claim for the initial submission deposit for a proposal.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -62,16 +58,6 @@ impl TypeUrl for ProposalDepositClaim {
const TYPE_URL: &'static str = "/penumbra.core.governance.v1alpha1.ProposalDepositClaim";
}

impl IsAction for ProposalDepositClaim {
fn balance_commitment(&self) -> balance::Commitment {
self.balance().commit(Fr::zero())
}

fn view_from_perspective(&self, _txp: &TransactionPerspective) -> ActionView {
ActionView::ProposalDepositClaim(self.clone())
}
}

impl ProposalDepositClaim {
/// Compute the balance contributed to the transaction by this proposal deposit claim.
pub fn balance(&self) -> Balance {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use ark_ff::Zero;
use serde::{Deserialize, Serialize};

use decaf377::Fr;
use penumbra_asset::{balance, Balance, Value, STAKING_TOKEN_ASSET_ID};
use penumbra_governance::ProposalNft;
use penumbra_asset::{Balance, Value, STAKING_TOKEN_ASSET_ID};
use penumbra_num::Amount;
use penumbra_proto::{core::governance::v1alpha1 as pb, DomainType, TypeUrl};

use crate::{proposal::Proposal, ActionView, IsAction, TransactionPerspective};
use crate::proposal::Proposal;

use crate::ProposalNft;

/// A proposal submission describes the proposal to propose, and the (transparent, ephemeral) refund
/// address for the proposal deposit, along with a key to be used to verify the signature for a
Expand All @@ -21,16 +20,6 @@ pub struct ProposalSubmit {
pub deposit_amount: Amount,
}

impl IsAction for ProposalSubmit {
fn balance_commitment(&self) -> balance::Commitment {
self.balance().commit(Fr::zero())
}

fn view_from_perspective(&self, _txp: &TransactionPerspective) -> ActionView {
ActionView::ProposalSubmit(self.to_owned())
}
}

impl ProposalSubmit {
/// Compute a commitment to the value contributed to a transaction by this proposal submission.
pub fn balance(&self) -> Balance {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use ark_ff::Zero;
use serde::{Deserialize, Serialize};

use decaf377::Fr;
use penumbra_asset::{balance, Balance, Value};
use penumbra_governance::ProposalNft;
use penumbra_asset::{Balance, Value};
use penumbra_num::Amount;
use penumbra_proto::{core::governance::v1alpha1 as pb, DomainType, TypeUrl};

use crate::{ActionView, IsAction, TransactionPerspective};
use crate::ProposalNft;

/// A withdrawal of a proposal.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -19,16 +16,6 @@ pub struct ProposalWithdraw {
pub reason: String,
}

impl IsAction for ProposalWithdraw {
fn balance_commitment(&self) -> balance::Commitment {
self.balance().commit(Fr::zero())
}

fn view_from_perspective(&self, _txp: &TransactionPerspective) -> ActionView {
ActionView::ProposalWithdraw(self.to_owned())
}
}

impl From<ProposalWithdraw> for pb::ProposalWithdraw {
fn from(value: ProposalWithdraw) -> pb::ProposalWithdraw {
pb::ProposalWithdraw {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use decaf377_rdsa::{Signature, SpendAuth};
use penumbra_asset::balance;
use penumbra_proto::{core::governance::v1alpha1 as pb, DomainType, TypeUrl};
use penumbra_stake::{GovernanceKey, IdentityKey};
use serde::{Deserialize, Serialize};

use crate::{vote::Vote, ActionView, IsAction, TransactionPerspective};
use crate::vote::Vote;

/// A vote by a validator.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -16,16 +15,6 @@ pub struct ValidatorVote {
pub auth_sig: Signature<SpendAuth>,
}

impl IsAction for ValidatorVote {
fn balance_commitment(&self) -> balance::Commitment {
Default::default()
}

fn view_from_perspective(&self, _txp: &TransactionPerspective) -> ActionView {
ActionView::ValidatorVote(self.to_owned())
}
}

impl From<ValidatorVote> for pb::ValidatorVote {
fn from(msg: ValidatorVote) -> Self {
Self {
Expand Down
7 changes: 5 additions & 2 deletions crates/core/component/governance/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::Arc;

mod view;

use anyhow::{Context, Result};
use async_trait::async_trait;
use penumbra_chain::component::StateReadExt;
use penumbra_storage::StateWrite;
use tendermint::v0_34::abci;
use tracing::instrument;
Expand All @@ -14,9 +15,11 @@ use crate::{
Outcome as ProposalOutcome, State as ProposalState, Withdrawn as ProposalWithdrawn,
},
tally,
view::{StateReadExt as _, StateWriteExt},
};

use self::view::{StateReadExt as _, StateWriteExt};
pub use penumbra_chain::component::StateReadExt;

pub struct Governance {}

#[async_trait]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use penumbra_stake::{DelegationToken, GovernanceKey, IdentityKey};
use penumbra_storage::{StateRead, StateWrite};
use penumbra_tct as tct;
use penumbra_transaction::{
action::Vote,
proposal::{self, Proposal, ProposalPayload},
Transaction,
};
Expand All @@ -28,7 +27,7 @@ use tracing::instrument;

use penumbra_stake::{rate::RateData, validator, StateReadExt as _};

use crate::proposal_state::State::{Claimed, Finished, Withdrawn};
use crate::{proposal_state::State as ProposalState, vote::Vote};
use crate::{state_key, tally::Tally};

#[async_trait]
Expand Down Expand Up @@ -56,9 +55,9 @@ pub trait StateReadExt: StateRead + penumbra_stake::StateReadExt {
}

/// Get the state of a proposal.
async fn proposal_state(&self, proposal_id: u64) -> Result<Option<proposal::State>> {
async fn proposal_state(&self, proposal_id: u64) -> Result<Option<ProposalState>> {
Ok(self
.get::<proposal::State>(&state_key::proposal_state(proposal_id))
.get::<ProposalState>(&state_key::proposal_state(proposal_id))
.await?)
}

Expand Down Expand Up @@ -333,21 +332,20 @@ pub trait StateReadExt: StateRead + penumbra_stake::StateReadExt {
/// Check that a deposit claim could be made on the proposal.
async fn check_proposal_claimable(&self, proposal_id: u64) -> Result<()> {
if let Some(proposal_state) = self.proposal_state(proposal_id).await? {
use proposal::State::*;
match proposal_state {
Voting => {
anyhow::bail!("proposal {} is still voting", proposal_id)
}
Withdrawn { .. } => {
ProposalState::Withdrawn { .. } => {
anyhow::bail!(
"proposal {} has been withdrawn but voting has not concluded",
proposal_id
)
}
Finished { .. } => {
ProposalState::Finished { .. } => {
// This is when you can claim a proposal
}
Claimed { .. } => {
ProposalState::Claimed { .. } => {
anyhow::bail!(
"the deposit for proposal {} has already been claimed",
proposal_id
Expand Down
5 changes: 3 additions & 2 deletions crates/core/component/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ pub mod voting_receipt_token;
pub use proposal_nft::ProposalNft;
pub use voting_receipt_token::VotingReceiptToken;

// TODO: De-duplicate with action_handler/actions
pub mod action;

mod metrics;
mod state_key;
mod tally;
mod view;

#[cfg_attr(docsrs, doc(cfg(feature = "component")))]
#[cfg(feature = "component")]
Expand All @@ -26,5 +28,4 @@ mod action_handler;
#[cfg(feature = "component")]
pub use component::StateReadExt;

pub mod proposal;
pub mod vote;
5 changes: 0 additions & 5 deletions crates/core/transaction/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ use crate::{ActionView, IsAction, TransactionPerspective};

pub use crate::proposal::{Proposal, ProposalKind, ProposalPayload};
pub use crate::vote::Vote;
pub use delegator_vote::{DelegatorVote, DelegatorVoteBody};
pub use proposal_deposit_claim::ProposalDepositClaim;
pub use proposal_submit::ProposalSubmit;
pub use proposal_withdraw::ProposalWithdraw;
pub use validator_vote::{ValidatorVote, ValidatorVoteBody};

/// An action performed by a Penumbra transaction.
#[derive(Clone, Debug)]
Expand Down
Loading

0 comments on commit 97f1c4f

Please sign in to comment.