Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make governance proposal id a required field #2365

Merged
merged 11 commits into from
Jan 13, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- When constructing a governance proposal the id is now a required field.
([\#2365](https://github.com/anoma/namada/pull/2365))
2 changes: 1 addition & 1 deletion apps/src/lib/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Default for BenchShell {
let signed_tx = bench_shell.generate_tx(
TX_INIT_PROPOSAL_WASM,
InitProposalData {
id: None,
id: 0,
content: content_section.get_hash(),
author: defaults::albert_address(),
r#type: ProposalType::Default(None),
Expand Down
58 changes: 58 additions & 0 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub mod cmds {
.subcommand(QueryResult::def().display_order(5))
.subcommand(QueryRawBytes::def().display_order(5))
.subcommand(QueryProposal::def().display_order(5))
.subcommand(QueryProposalVotes::def().display_order(5))
.subcommand(QueryProposalResult::def().display_order(5))
.subcommand(QueryProtocolParameters::def().display_order(5))
.subcommand(QueryPgf::def().display_order(5))
Expand Down Expand Up @@ -330,6 +331,8 @@ pub mod cmds {
let query_result = Self::parse_with_ctx(matches, QueryResult);
let query_raw_bytes = Self::parse_with_ctx(matches, QueryRawBytes);
let query_proposal = Self::parse_with_ctx(matches, QueryProposal);
let query_proposal_votes =
Self::parse_with_ctx(matches, QueryProposalVotes);
let query_proposal_result =
Self::parse_with_ctx(matches, QueryProposalResult);
let query_protocol_parameters =
Expand Down Expand Up @@ -385,6 +388,7 @@ pub mod cmds {
.or(query_result)
.or(query_raw_bytes)
.or(query_proposal)
.or(query_proposal_votes)
.or(query_proposal_result)
.or(query_protocol_parameters)
.or(query_pgf)
Expand Down Expand Up @@ -472,6 +476,7 @@ pub mod cmds {
QueryFindValidator(QueryFindValidator),
QueryRawBytes(QueryRawBytes),
QueryProposal(QueryProposal),
QueryProposalVotes(QueryProposalVotes),
QueryProposalResult(QueryProposalResult),
QueryProtocolParameters(QueryProtocolParameters),
QueryPgf(QueryPgf),
Expand Down Expand Up @@ -1011,6 +1016,28 @@ pub mod cmds {
}
}

#[derive(Debug, Clone)]
pub struct QueryProposalVotes(pub args::QueryProposalVotes<args::CliTypes>);

impl SubCmd for QueryProposalVotes {
const CMD: &'static str = "query-proposal-votes";

fn parse(matches: &ArgMatches) -> Option<Self>
where
Self: Sized,
{
matches.subcommand_matches(Self::CMD).map(|matches| {
QueryProposalVotes(args::QueryProposalVotes::parse(matches))
})
}

fn def() -> App {
App::new(Self::CMD)
.about("Query votes for the proposal.")
.add_args::<args::QueryProposalVotes<args::CliTypes>>()
}
}

#[derive(Clone, Debug)]
pub struct QueryProposal(pub args::QueryProposal<args::CliTypes>);

Expand Down Expand Up @@ -3058,6 +3085,7 @@ pub mod args {
pub const VALIDATOR_ETH_HOT_KEY: ArgOpt<WalletPublicKey> =
arg_opt("eth-hot-key");
pub const VALUE: Arg<String> = arg("value");
pub const VOTER_OPT: ArgOpt<WalletAddress> = arg_opt("voter");
pub const VIEWING_KEY: Arg<WalletViewingKey> = arg("key");
pub const VP: ArgOpt<String> = arg_opt("vp");
pub const WALLET_ALIAS_FORCE: ArgFlag = flag("wallet-alias-force");
Expand Down Expand Up @@ -4788,6 +4816,36 @@ pub mod args {
}
}

impl CliToSdk<QueryProposalVotes<SdkTypes>> for QueryProposalVotes<CliTypes> {
fn to_sdk(self, ctx: &mut Context) -> QueryProposalVotes<SdkTypes> {
QueryProposalVotes::<SdkTypes> {
query: self.query.to_sdk(ctx),
proposal_id: self.proposal_id,
voter: self.voter.map(|x| ctx.borrow_chain_or_exit().get(&x)),
}
}
}

impl Args for QueryProposalVotes<CliTypes> {
fn parse(matches: &ArgMatches) -> Self {
let query = Query::parse(matches);
let proposal_id = PROPOSAL_ID.parse(matches);
let voter = VOTER_OPT.parse(matches);

Self {
query,
proposal_id,
voter,
}
}

fn def(app: App) -> App {
app.add_args::<Query<CliTypes>>()
.arg(PROPOSAL_ID_OPT.def().help("The proposal identifier."))
.arg(VOTER_OPT.def().help("The address of the proposal voter."))
}
}

#[derive(Clone, Debug)]
pub struct QueryProposalResult<C: NamadaTypes = SdkTypes> {
/// Common query args
Expand Down
11 changes: 11 additions & 0 deletions apps/src/lib/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,17 @@ impl CliApi {
let namada = ctx.to_sdk(client, io);
rpc::query_proposal_result(&namada, args).await;
}
Sub::QueryProposalVotes(QueryProposalVotes(mut args)) => {
let client = client.unwrap_or_else(|| {
C::from_tendermint_address(
&mut args.query.ledger_address,
)
});
client.wait_until_node_is_synced(&io).await?;
let args = args.to_sdk(&mut ctx);
let namada = ctx.to_sdk(client, io);
rpc::query_proposal_votes(&namada, args).await;
}
Sub::QueryProtocolParameters(QueryProtocolParameters(
mut args,
)) => {
Expand Down
37 changes: 37 additions & 0 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,43 @@ async fn get_ibc_denom_alias(
.unwrap_or(ibc_denom.as_ref().to_string())
}

/// Query votes for the given proposal
pub async fn query_proposal_votes(
context: &impl Namada,
args: args::QueryProposalVotes,
) {
let result = namada_sdk::rpc::query_proposal_votes(
context.client(),
args.proposal_id,
)
.await
.unwrap();

match args.voter {
Some(voter) => {
match result.into_iter().find(|vote| vote.delegator == voter) {
Some(vote) => display_line!(context.io(), "{}", vote,),
None => display_line!(
context.io(),
"The address {} has not voted on proposal {}",
voter,
args.proposal_id
),
}
}
None => {
display_line!(
context.io(),
"Votes for proposal id {}\n",
args.proposal_id
);
for vote in result {
display_line!(context.io(), "{}\n", vote);
}
}
}
}

/// Query Proposals
pub async fn query_proposal(context: &impl Namada, args: args::QueryProposal) {
let current_epoch = query_and_print_epoch(context).await;
Expand Down
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
10 changes: 4 additions & 6 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +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, VoteType,
};
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 @@ -1614,7 +1612,7 @@ mod test_finalize_block {
shell.proposal_data.insert(proposal_id);

let proposal = InitProposalData {
id: Some(proposal_id),
id: proposal_id,
content: Hash::default(),
author: validator.clone(),
voting_start_epoch: Epoch::default(),
Expand Down Expand Up @@ -1644,8 +1642,8 @@ mod test_finalize_block {
};

// Add a proposal to be accepted and one to be rejected.
add_proposal(0, StorageProposalVote::Yay(VoteType::Default));
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
14 changes: 6 additions & 8 deletions benches/native_vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +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, VoteType,
};
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 @@ -87,7 +85,7 @@ fn governance(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay(VoteType::Default),
vote: ProposalVote::Yay,
voter: defaults::albert_address(),
delegations: vec![defaults::validator_address()],
},
Expand All @@ -103,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 All @@ -128,7 +126,7 @@ fn governance(c: &mut Criterion) {
shell.generate_tx(
TX_INIT_PROPOSAL_WASM,
InitProposalData {
id: None,
id: 0,
content: content_section.get_hash(),
author: defaults::albert_address(),
r#type: ProposalType::Default(None),
Expand Down Expand Up @@ -180,7 +178,7 @@ fn governance(c: &mut Criterion) {
shell.generate_tx(
TX_INIT_PROPOSAL_WASM,
InitProposalData {
id: Some(1),
id: 1,
content: content_section.get_hash(),
author: defaults::albert_address(),
r#type: ProposalType::Default(Some(
Expand Down Expand Up @@ -252,7 +250,7 @@ fn governance(c: &mut Criterion) {
// let governance_proposal = shell.generate_tx(
// TX_INIT_PROPOSAL_WASM,
// InitProposalData {
// id: None,
// id: 0,
// content: content_section.get_hash(),
// author: defaults::albert_address(),
// r#type: ProposalType::Default(None),
Expand Down
12 changes: 5 additions & 7 deletions benches/txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +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, VoteType,
};
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 @@ -464,7 +462,7 @@ fn init_proposal(c: &mut Criterion) {
shell.generate_tx(
TX_INIT_PROPOSAL_WASM,
InitProposalData {
id: None,
id: 0,
content: content_section.get_hash(),
author: defaults::albert_address(),
r#type: ProposalType::Default(None),
Expand Down Expand Up @@ -514,7 +512,7 @@ fn init_proposal(c: &mut Criterion) {
shell.generate_tx(
TX_INIT_PROPOSAL_WASM,
InitProposalData {
id: Some(1),
id: 1,
content: content_section.get_hash(),
author: defaults::albert_address(),
r#type: ProposalType::Default(Some(
Expand Down Expand Up @@ -550,7 +548,7 @@ fn vote_proposal(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay(VoteType::Default),
vote: ProposalVote::Yay,
voter: defaults::albert_address(),
delegations: vec![defaults::validator_address()],
},
Expand All @@ -563,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
10 changes: 4 additions & 6 deletions benches/vps.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::collections::BTreeSet;

use criterion::{criterion_group, criterion_main, Criterion};
use namada::core::ledger::governance::storage::vote::{
StorageProposalVote, VoteType,
};
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 @@ -99,7 +97,7 @@ fn vp_user(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay(VoteType::Default),
vote: ProposalVote::Yay,
voter: defaults::albert_address(),
delegations: vec![defaults::validator_address()],
},
Expand Down Expand Up @@ -239,7 +237,7 @@ fn vp_implicit(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay(VoteType::Default),
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 @@ -396,7 +394,7 @@ fn vp_validator(c: &mut Criterion) {
TX_VOTE_PROPOSAL_WASM,
VoteProposalData {
id: 0,
vote: StorageProposalVote::Yay(VoteType::Default),
vote: ProposalVote::Yay,
voter: defaults::validator_address(),
delegations: vec![],
},
Expand Down
7 changes: 1 addition & 6 deletions 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 Expand Up @@ -251,11 +251,6 @@ impl OfflineVote {
self.vote.is_abstain()
}

/// Check if two votes are equal
pub fn is_same_side(&self, other: &Self) -> bool {
self.vote.is_same_side(&other.vote)
}

/// compute the hash of a proposal
pub fn compute_hash(&self) -> Hash {
let proposal_hash_data = self.proposal_hash.serialize_to_vec();
Expand Down
Loading
Loading