pcli/command/query/governance.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
use std::{
collections::BTreeMap,
io::{stdout, Write},
};
use anyhow::{Context, Result};
use futures::TryStreamExt;
use penumbra_governance::Vote;
use penumbra_proto::core::component::governance::v1::{
query_service_client::QueryServiceClient as GovernanceQueryServiceClient,
AllTalliedDelegatorVotesForProposalRequest, ProposalDataRequest, ProposalListRequest,
ProposalListResponse, ValidatorVotesRequest, ValidatorVotesResponse,
VotingPowerAtProposalStartRequest,
};
use penumbra_stake::IdentityKey;
use serde::Serialize;
use serde_json::json;
use crate::App;
#[derive(Debug, clap::Subcommand)]
pub enum GovernanceCmd {
/// List all governance proposals by number.
ListProposals {
/// Whether to include proposals which have already finished voting.
#[clap(short, long)]
inactive: bool,
},
/// Query for information about a particular proposal.
Proposal {
/// The proposal id to query.
proposal_id: u64,
/// The query to ask of it.
#[clap(subcommand)]
query: PerProposalCmd,
},
}
#[derive(Debug, clap::Subcommand)]
pub enum PerProposalCmd {
/// Fetch the details of a proposal, as submitted to the chain.
Definition,
/// Display the current state of a proposal.
State,
/// Display the voting period of a proposal.
Period,
/// Display the most recent tally of votes on the proposal.
Tally,
}
impl GovernanceCmd {
pub async fn exec(&self, app: &mut App) -> Result<()> {
// use PerProposalCmd::*;
let mut client = GovernanceQueryServiceClient::new(app.pd_channel().await?);
match self {
GovernanceCmd::ListProposals { inactive } => {
let proposals: Vec<ProposalListResponse> = client
.proposal_list(ProposalListRequest {
inactive: *inactive,
..Default::default()
})
.await?
.into_inner()
.try_collect::<Vec<_>>()
.await
.context("cannot process proposal list data")?;
let mut writer = stdout();
for proposal_response in proposals {
let proposal = proposal_response
.proposal
.expect("proposal should always be set");
let proposal_title = proposal.title;
let proposal_state = proposal_response
.state
.expect("proposal state should always be set");
let proposal_id = proposal.id;
writeln!(
writer,
"#{proposal_id} {proposal_state:?} {proposal_title}"
)?;
}
Ok(())
}
GovernanceCmd::Proposal { proposal_id, query } => {
match query {
&PerProposalCmd::Definition => {
let proposal = client
.proposal_data(ProposalDataRequest {
proposal_id: *proposal_id,
..Default::default()
})
.await?
.into_inner();
toml(
&proposal
.proposal
.expect("proposal should always be populated"),
)?;
}
PerProposalCmd::State => {
let proposal = client
.proposal_data(ProposalDataRequest {
proposal_id: *proposal_id,
..Default::default()
})
.await?
.into_inner();
json(
&proposal
.state
.expect("proposal state should always be populated"),
)?;
}
PerProposalCmd::Period => {
let proposal = client
.proposal_data(ProposalDataRequest {
proposal_id: *proposal_id,
..Default::default()
})
.await?
.into_inner();
let start: u64 = proposal.start_block_height;
let end: u64 = proposal.end_block_height;
let period = json!({
"voting_start_block": start,
"voting_end_block": end,
});
json(&period)?;
}
PerProposalCmd::Tally => {
let validator_votes: Vec<ValidatorVotesResponse> = client
.validator_votes(ValidatorVotesRequest {
proposal_id: *proposal_id,
..Default::default()
})
.await?
.into_inner()
.try_collect::<Vec<_>>()
.await?;
let mut validator_votes_and_power: BTreeMap<IdentityKey, (Vote, u64)> =
BTreeMap::new();
for vote_response in validator_votes {
let identity_key: IdentityKey = vote_response
.identity_key
.expect("identity key must be set for vote response")
.try_into()?;
let vote: Vote = vote_response
.vote
.expect("vote must be set for vote response")
.try_into()?;
let power: u64 = client
.voting_power_at_proposal_start(VotingPowerAtProposalStartRequest {
proposal_id: *proposal_id,
identity_key: Some(identity_key.into()),
..Default::default()
})
.await
.context("Error looking for validator power")?
.into_inner()
.voting_power;
validator_votes_and_power.insert(identity_key, (vote, power));
}
let mut delegator_tallies: BTreeMap<
IdentityKey,
penumbra_governance::Tally,
> = client
.all_tallied_delegator_votes_for_proposal(
AllTalliedDelegatorVotesForProposalRequest {
proposal_id: *proposal_id,
..Default::default()
},
)
.await?
.into_inner()
.map_ok(|response| {
let identity_key: IdentityKey = response
.identity_key
.expect("identity key must be set for vote response")
.try_into()?;
let tally: penumbra_governance::Tally = response
.tally
.expect("tally must be set for vote response")
.try_into()?;
Ok::<(IdentityKey, penumbra_governance::Tally), anyhow::Error>((
identity_key,
tally,
))
})
// TODO: double iterator here is suboptimal but trying to collect
// `Result<Vec<_>>` was annoying
.try_collect::<Vec<_>>()
.await?
.into_iter()
.collect::<Result<BTreeMap<_, _>>>()?;
// Combine the two mappings
let mut total = penumbra_governance::Tally::default();
let mut all_votes_and_power: BTreeMap<String, serde_json::Value> =
BTreeMap::new();
for (identity_key, (vote, power)) in validator_votes_and_power.into_iter() {
all_votes_and_power.insert(identity_key.to_string(), {
let mut map = serde_json::Map::new();
map.insert(
"validator".to_string(),
json!({
vote.to_string(): power,
}),
);
let delegator_tally =
if let Some(tally) = delegator_tallies.remove(&identity_key) {
map.insert("delegators".to_string(), json_tally(&tally));
tally
} else {
Default::default()
};
// Subtract delegator total from validator power, then add delegator
// tally in to get the total tally for this validator:
let sub_total = penumbra_governance::Tally::from((
vote,
power - delegator_tally.total(),
)) + delegator_tally;
map.insert("sub_total".to_string(), json_tally(&sub_total));
total += sub_total;
map.into()
});
}
for (identity_key, tally) in delegator_tallies.into_iter() {
all_votes_and_power.insert(identity_key.to_string(), {
let mut map = serde_json::Map::new();
let sub_total = tally;
map.insert("delegators".to_string(), json_tally(&tally));
map.insert("sub_total".to_string(), json_tally(&sub_total));
total += sub_total;
map.into()
});
}
json(&json!({
"total": json_tally(&total),
"details": all_votes_and_power,
}))?;
}
};
Ok(())
}
}
}
}
fn json<T: Serialize>(value: &T) -> Result<()> {
let mut writer = stdout();
serde_json::to_writer_pretty(&mut writer, value)?;
writer.write_all(b"\n")?;
Ok(())
}
fn json_tally(tally: &penumbra_governance::Tally) -> serde_json::Value {
let mut map = serde_json::Map::new();
if tally.yes() > 0 {
map.insert("yes".to_string(), tally.yes().into());
}
if tally.no() > 0 {
map.insert("no".to_string(), tally.no().into());
}
if tally.abstain() > 0 {
map.insert("abstain".to_string(), tally.abstain().into());
}
map.into()
}
fn toml<T: Serialize>(value: &T) -> Result<()> {
let mut writer = stdout();
let string = toml::to_string_pretty(value)?;
writer.write_all(string.as_bytes())?;
Ok(())
}