Skip to content

Commit

Permalink
Removes duplicated proposal vote enum
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Jan 2, 2024
1 parent 691fc8b commit c2fa529
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 127 deletions.
3 changes: 2 additions & 1 deletion apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use namada::core::ledger::governance::cli::offline::{
OfflineProposal, OfflineSignedProposal, OfflineVote,
};
use namada::core::ledger::governance::cli::onchain::{
DefaultProposal, PgfFundingProposal, PgfStewardProposal, ProposalVote,
DefaultProposal, PgfFundingProposal, PgfStewardProposal,
};
use namada::core::ledger::governance::storage::vote::ProposalVote;
use namada::core::ledger::storage::EPOCH_SWITCH_BLOCKS_DELAY;
use namada::ibc::apps::transfer::types::Memo;
use namada::proto::{CompressedSignature, Section, Signer, Tx};
Expand Down
6 changes: 3 additions & 3 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ mod test_finalize_block {
use namada::core::ledger::eth_bridge::storage::wrapped_erc20s;
use namada::core::ledger::governance::storage::keys::get_proposal_execution_key;
use namada::core::ledger::governance::storage::proposal::ProposalType;
use namada::core::ledger::governance::storage::vote::StorageProposalVote;
use namada::core::ledger::governance::storage::vote::ProposalVote;
use namada::core::ledger::replay_protection;
use namada::core::types::storage::KeySeg;
use namada::eth_bridge::storage::bridge_pool::{
Expand Down Expand Up @@ -1642,8 +1642,8 @@ mod test_finalize_block {
};

// Add a proposal to be accepted and one to be rejected.
add_proposal(0, StorageProposalVote::Yay);
add_proposal(1, StorageProposalVote::Nay);
add_proposal(0, ProposalVote::Yay);
add_proposal(1, ProposalVote::Nay);

// Commit the genesis state
shell.wl_storage.commit_block().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions benches/native_vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
use masp_primitives::bls12_381;
use masp_primitives::sapling::Node;
use namada::core::ledger::governance::storage::proposal::ProposalType;
use namada::core::ledger::governance::storage::vote::StorageProposalVote;
use namada::core::ledger::governance::storage::vote::ProposalVote;
use namada::core::ledger::ibc::{IbcActions, TransferModule};
use namada::core::ledger::pgf::storage::steward::StewardDetail;
use namada::core::ledger::storage_api::{StorageRead, StorageWrite};
Expand Down Expand Up @@ -85,7 +85,7 @@ fn governance(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay,
vote: ProposalVote::Yay,
voter: defaults::albert_address(),
delegations: vec![defaults::validator_address()],
},
Expand All @@ -101,7 +101,7 @@ fn governance(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Nay,
vote: ProposalVote::Nay,
voter: defaults::validator_address(),
delegations: vec![],
},
Expand Down
6 changes: 3 additions & 3 deletions benches/txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;

use criterion::{criterion_group, criterion_main, Criterion};
use namada::core::ledger::governance::storage::proposal::ProposalType;
use namada::core::ledger::governance::storage::vote::StorageProposalVote;
use namada::core::ledger::governance::storage::vote::ProposalVote;
use namada::core::ledger::pgf::storage::steward::StewardDetail;
use namada::core::types::key::{
common, SecretKey as SecretKeyInterface, SigScheme,
Expand Down Expand Up @@ -548,7 +548,7 @@ fn vote_proposal(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay,
vote: ProposalVote::Yay,
voter: defaults::albert_address(),
delegations: vec![defaults::validator_address()],
},
Expand All @@ -561,7 +561,7 @@ fn vote_proposal(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Nay,
vote: ProposalVote::Nay,
voter: defaults::validator_address(),
delegations: vec![],
},
Expand Down
8 changes: 4 additions & 4 deletions benches/vps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeSet;

use criterion::{criterion_group, criterion_main, Criterion};
use namada::core::ledger::governance::storage::vote::StorageProposalVote;
use namada::core::ledger::governance::storage::vote::ProposalVote;
use namada::core::types::address::{self, Address};
use namada::core::types::key::{
common, SecretKey as SecretKeyInterface, SigScheme,
Expand Down Expand Up @@ -97,7 +97,7 @@ fn vp_user(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay,
vote: ProposalVote::Yay,
voter: defaults::albert_address(),
delegations: vec![defaults::validator_address()],
},
Expand Down Expand Up @@ -237,7 +237,7 @@ fn vp_implicit(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay,
vote: ProposalVote::Yay,
voter: Address::from(&implicit_account.to_public()),
delegations: vec![], /* NOTE: no need to bond tokens because the
* implicit vp doesn't check that */
Expand Down Expand Up @@ -394,7 +394,7 @@ fn vp_validator(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay,
vote: ProposalVote::Yay,
voter: defaults::validator_address(),
delegations: vec![],
},
Expand Down
2 changes: 1 addition & 1 deletion core/src/ledger/governance/cli/offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use borsh::{BorshDeserialize, BorshSerialize};
use borsh_ext::BorshSerializeExt;
use serde::{Deserialize, Serialize};

use super::onchain::ProposalVote;
use super::validation::{is_valid_tally_epoch, ProposalValidation};
use crate::ledger::governance::storage::vote::ProposalVote;
use crate::proto::SignatureIndex;
use crate::types::account::AccountPublicKeysMap;
use crate::types::address::Address;
Expand Down
49 changes: 0 additions & 49 deletions core/src/ledger/governance/cli/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,52 +312,3 @@ pub struct PgfFundingTarget {
/// Target address
pub address: Address,
}

/// Represent an proposal vote
#[derive(
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
PartialEq,
)]
pub enum ProposalVote {
/// Represent an yay proposal vote
Yay,
/// Represent an nay proposal vote
Nay,
/// Represent an abstain proposal vote
Abstain,
}

impl TryFrom<String> for ProposalVote {
type Error = String;

fn try_from(value: String) -> Result<Self, Self::Error> {
match value.trim().to_lowercase().as_str() {
"yay" => Ok(ProposalVote::Yay),
"nay" => Ok(ProposalVote::Nay),
"abstain" => Ok(ProposalVote::Abstain),
_ => Err("invalid vote".to_string()),
}
}
}

impl ProposalVote {
/// Check if the vote type is yay
pub fn is_yay(&self) -> bool {
matches!(self, ProposalVote::Yay)
}

/// Check if the vote type is nay
pub fn is_nay(&self) -> bool {
matches!(self, ProposalVote::Nay)
}

/// Check if the vote type is abstain
pub fn is_abstain(&self) -> bool {
matches!(self, ProposalVote::Abstain)
}
}
61 changes: 24 additions & 37 deletions core/src/ledger/governance/storage/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::fmt::Display;
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

use super::super::cli::onchain::ProposalVote;

#[derive(
Debug,
Clone,
Expand All @@ -16,7 +14,7 @@ use super::super::cli::onchain::ProposalVote;
Deserialize,
)]
/// The vote for a proposal
pub enum StorageProposalVote {
pub enum ProposalVote {
/// Yes
Yay,
/// No
Expand All @@ -25,39 +23,42 @@ pub enum StorageProposalVote {
Abstain,
}

impl StorageProposalVote {
impl ProposalVote {
/// Check if a vote is yay
pub fn is_yay(&self) -> bool {
matches!(self, StorageProposalVote::Yay)
matches!(self, ProposalVote::Yay)
}

/// Check if a vote is nay
pub fn is_nay(&self) -> bool {
matches!(self, StorageProposalVote::Nay)
matches!(self, ProposalVote::Nay)
}

/// Check if a vote is abstain
pub fn is_abstain(&self) -> bool {
matches!(self, StorageProposalVote::Abstain)
matches!(self, ProposalVote::Abstain)
}
}

impl From<&ProposalVote> for StorageProposalVote {
fn from(value: &ProposalVote) -> Self {
match value {
ProposalVote::Yay => StorageProposalVote::Yay,
ProposalVote::Nay => StorageProposalVote::Nay,
ProposalVote::Abstain => StorageProposalVote::Abstain,
impl Display for ProposalVote {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ProposalVote::Yay => write!(f, "yay"),
ProposalVote::Nay => write!(f, "nay"),
ProposalVote::Abstain => write!(f, "abstain"),
}
}
}

impl Display for StorageProposalVote {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StorageProposalVote::Yay => write!(f, "yay"),
StorageProposalVote::Nay => write!(f, "nay"),
StorageProposalVote::Abstain => write!(f, "abstain"),
impl TryFrom<String> for ProposalVote {
type Error = String;

fn try_from(value: String) -> Result<Self, Self::Error> {
match value.trim().to_lowercase().as_str() {
"yay" => Ok(ProposalVote::Yay),
"nay" => Ok(ProposalVote::Nay),
"abstain" => Ok(ProposalVote::Abstain),
_ => Err("invalid vote".to_string()),
}
}
}
Expand All @@ -66,28 +67,14 @@ impl Display for StorageProposalVote {
/// Testing helpers and strategies for governance votes
pub mod testing {
use proptest::prelude::{Just, Strategy};
use proptest::prop_compose;

use super::*;

prop_compose! {
/// Geerate an arbitrary vote type
pub fn arb_vote_type()(discriminant in 0..3) -> VoteType {
match discriminant {
0 => VoteType::Default,
1 => VoteType::PGFSteward,
2 => VoteType::PGFPayment,
_ => unreachable!(),
}
}
}

/// Generate an arbitrary proposal vote
pub fn arb_proposal_vote() -> impl Strategy<Value = StorageProposalVote> {
arb_vote_type()
.prop_map(StorageProposalVote::Yay)
pub fn arb_proposal_vote() -> impl Strategy<Value = ProposalVote> {
Just(ProposalVote::Yay)
.boxed()
.prop_union(Just(StorageProposalVote::Nay).boxed())
.or(Just(StorageProposalVote::Abstain).boxed())
.prop_union(Just(ProposalVote::Nay).boxed())
.or(Just(ProposalVote::Abstain).boxed())
}
}
10 changes: 5 additions & 5 deletions core/src/ledger/governance/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use borsh::{BorshDeserialize, BorshSerialize};

use super::cli::offline::OfflineVote;
use super::storage::proposal::ProposalType;
use super::storage::vote::StorageProposalVote;
use super::storage::vote::ProposalVote;
use crate::types::address::Address;
use crate::types::storage::Epoch;
use crate::types::token;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct Vote {
/// Field holding the address of the delegator
pub delegator: Address,
/// Field holding vote data
pub data: StorageProposalVote,
pub data: ProposalVote,
}

impl Display for Vote {
Expand Down Expand Up @@ -206,13 +206,13 @@ impl Display for ProposalResult {
#[derive(Debug)]
pub enum TallyVote {
/// Rappresent a vote for a proposal onchain
OnChain(StorageProposalVote),
OnChain(ProposalVote),
/// Rappresent a vote for a proposal offline
Offline(OfflineVote),
}

impl From<StorageProposalVote> for TallyVote {
fn from(vote: StorageProposalVote) -> Self {
impl From<ProposalVote> for TallyVote {
fn from(vote: ProposalVote) -> Self {
Self::OnChain(vote)
}
}
Expand Down
8 changes: 3 additions & 5 deletions core/src/ledger/storage_api/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::ledger::governance::storage::keys as governance_keys;
use crate::ledger::governance::storage::proposal::{
ProposalType, StorageProposal,
};
use crate::ledger::governance::storage::vote::StorageProposalVote;
use crate::ledger::governance::storage::vote::ProposalVote;
use crate::ledger::governance::utils::Vote;
use crate::ledger::governance::ADDRESS as governance_address;
use crate::ledger::storage_api::{self, StorageRead, StorageWrite};
Expand Down Expand Up @@ -168,10 +168,8 @@ where
{
let vote_prefix_key =
governance_keys::get_proposal_vote_prefix_key(proposal_id);
let vote_iter = storage_api::iter_prefix::<StorageProposalVote>(
storage,
&vote_prefix_key,
)?;
let vote_iter =
storage_api::iter_prefix::<ProposalVote>(storage, &vote_prefix_key)?;

let votes = vote_iter
.filter_map(|vote_result| {
Expand Down
4 changes: 2 additions & 2 deletions core/src/types/transaction/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::ledger::governance::cli::onchain::{
use crate::ledger::governance::storage::proposal::{
AddRemove, PGFAction, PGFTarget, ProposalType,
};
use crate::ledger::governance::storage::vote::StorageProposalVote;
use crate::ledger::governance::storage::vote::ProposalVote;
use crate::types::address::Address;
use crate::types::hash::Hash;
use crate::types::storage::Epoch;
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct VoteProposalData {
/// The proposal id
pub id: u64,
/// The proposal vote
pub vote: StorageProposalVote,
pub vote: ProposalVote,
/// The proposal author address
pub voter: Address,
/// Delegator addresses
Expand Down
4 changes: 2 additions & 2 deletions light_sdk/src/transaction/governance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use namada_core::ledger::governance::storage::proposal::ProposalType;
use namada_core::ledger::governance::storage::vote::StorageProposalVote;
use namada_core::ledger::governance::storage::vote::ProposalVote;
use namada_core::proto::Tx;
use namada_core::types::address::Address;
use namada_core::types::hash::Hash;
Expand Down Expand Up @@ -75,7 +75,7 @@ impl VoteProposal {
/// Build a raw VoteProposal transaction from the given parameters
pub fn new(
id: u64,
vote: StorageProposalVote,
vote: ProposalVote,
voter: Address,
delegations: Vec<Address>,
args: GlobalArgs,
Expand Down
Loading

0 comments on commit c2fa529

Please sign in to comment.