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

Add jcli command to tally all proposals in a vote plan #2980

Merged
merged 12 commits into from
Feb 9, 2021
8 changes: 4 additions & 4 deletions jcli/src/jcli_app/utils/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct TallyDecryptShare(#[serde(with = "serde_base64_bytes")] Vec<u8>);

// Set of shares (belonging to a single committee member) for the decryption of a vote plan
#[derive(Debug, Serialize, Deserialize)]
pub struct SingleMemberVotePlanShares(Vec<TallyDecryptShare>);
pub struct MemberVotePlanShares(Vec<TallyDecryptShare>);

// Set of decrypt shares (belonging to different committee members)
// that decrypts a vote plan
Expand All @@ -92,7 +92,7 @@ impl TryFrom<TallyDecryptShare> for chain_vote::TallyDecryptShare {
}
}

impl From<Vec<chain_vote::TallyDecryptShare>> for SingleMemberVotePlanShares {
impl From<Vec<chain_vote::TallyDecryptShare>> for MemberVotePlanShares {
fn from(shares: Vec<chain_vote::TallyDecryptShare>) -> Self {
Self(
shares
Expand All @@ -103,9 +103,9 @@ impl From<Vec<chain_vote::TallyDecryptShare>> for SingleMemberVotePlanShares {
}
}

impl TryFrom<Vec<SingleMemberVotePlanShares>> for VotePlanDecryptShares {
impl TryFrom<Vec<MemberVotePlanShares>> for VotePlanDecryptShares {
type Error = SharesError;
fn try_from(shares: Vec<SingleMemberVotePlanShares>) -> Result<Self, Self::Error> {
fn try_from(shares: Vec<MemberVotePlanShares>) -> Result<Self, Self::Error> {
let shares = shares.into_iter().map(|s| s.0).collect::<Vec<_>>();
if shares.is_empty() {
return Err(SharesError::Empty);
Expand Down
5 changes: 2 additions & 3 deletions jcli/src/jcli_app/vote/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::jcli_app::utils::output_file::{self, OutputFile};
use crate::jcli_app::utils::vote::{SharesError, VotePlanError};
use jormungandr_lib::interfaces::Tally;

pub mod bech32_constants;
mod committee;
Expand Down Expand Up @@ -44,8 +43,8 @@ pub enum Error {
EncryptedTallyRead,
#[error("failed to read decryption key bytes")]
DecryptionKeyRead,
#[error("expected encrypted private tally, found {found:?}")]
PrivateTallyExpected { found: Option<Tally> },
#[error("expected encrypted private tally, found {found}")]
PrivateTallyExpected { found: &'static str },
#[error(transparent)]
TallyError(#[from] chain_vote::TallyError),
#[error(transparent)]
Expand Down
14 changes: 11 additions & 3 deletions jcli/src/jcli_app/vote/tally/decrypt_shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ pub struct TallyVotePlanWithAllShares {
/// The minimum number of shares needed for decryption
#[structopt(long, default_value = "3")]
threshold: usize,
/// The path to json-encoded necessary base64 shares. If this parameter is not
/// specified, the shares will be read from the standard input.
/// The path to a JSON file containing decryption shares necessary to decrypt
/// the vote plan. If this parameter is not specified, the shares will be read
/// from the standard input.
#[structopt(long)]
shares: Option<PathBuf>,
#[structopt(flatten)]
Expand Down Expand Up @@ -130,7 +131,14 @@ impl TallyVotePlanWithAllShares {
max_stake = std::cmp::max(total_stake.into(), max_stake);
encrypted_tallies.push(encrypted_tally.into_bytes());
}
other => return Err(Error::PrivateTallyExpected { found: other }),
other => {
let found = match other {
Some(Tally::Public { .. }) => "public tally",
Some(Tally::Private { .. }) => "private decrypted tally",
None => "none",
};
return Err(Error::PrivateTallyExpected { found });
}
}
}
let table = chain_vote::TallyOptimizationTable::generate(max_stake);
Expand Down
6 changes: 3 additions & 3 deletions jcli/src/jcli_app/vote/tally/decryption_tally.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Error;
use crate::jcli_app::utils::io;
use crate::jcli_app::utils::vote::{self, SingleMemberVotePlanShares, VotePlanDecryptShares};
use crate::jcli_app::utils::vote::{self, MemberVotePlanShares, VotePlanDecryptShares};
use bech32::FromBase32;
use chain_vote::{EncryptedTally, OpeningVoteKey};
use jormungandr_lib::crypto::hash::Hash;
Expand Down Expand Up @@ -112,7 +112,7 @@ impl TallyGenerateVotePlanDecryptionShares {
.collect::<Vec<_>>();
println!(
"{}",
serde_json::to_value(SingleMemberVotePlanShares::from(shares))?
serde_json::to_value(MemberVotePlanShares::from(shares))?
);
Ok(())
}
Expand All @@ -124,7 +124,7 @@ impl MergeShares {
.shares
.iter()
.map(|path| Ok(serde_json::from_reader(io::open_file_read(&Some(path))?)?))
.collect::<Result<Vec<SingleMemberVotePlanShares>, Error>>()?;
.collect::<Result<Vec<MemberVotePlanShares>, Error>>()?;
let vote_plan_shares = VotePlanDecryptShares::try_from(shares)?;
println!("{}", serde_json::to_string(&vote_plan_shares)?);
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions jcli/src/jcli_app/vote/tally/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Tally {
///
/// The decryption share data will be printed in hexadecimal encoding
/// on standard output.
VotePlanDecryptionShares(decryption_tally::TallyGenerateVotePlanDecryptionShares),
DecryptionShares(decryption_tally::TallyGenerateVotePlanDecryptionShares),
/// Merge multiple sets of shares in a single object to be used in the
/// decryption of a vote plan.
MergeShares(decryption_tally::MergeShares),
Expand All @@ -29,16 +29,16 @@ pub enum Tally {
///
/// The decrypted tally data will be printed in hexadecimal encoding
/// on standard output.
DecryptVotePlan(decrypt_shares::TallyVotePlanWithAllShares),
DecryptResults(decrypt_shares::TallyVotePlanWithAllShares),
}

impl Tally {
pub fn exec(self) -> Result<(), Error> {
match self {
Tally::DecryptionShare(cmd) => cmd.exec(),
Tally::VotePlanDecryptionShares(cmd) => cmd.exec(),
Tally::DecryptionShares(cmd) => cmd.exec(),
Tally::Decrypt(cmd) => cmd.exec(),
Tally::DecryptVotePlan(cmd) => cmd.exec(),
Tally::DecryptResults(cmd) => cmd.exec(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could hide the deprecated commands from help, but still have them supported in the CLI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove them altogether once we have tested the new ones? I do not see much value in keeping those, and we would have to maintain more functionalities.

Tally::MergeShares(cmd) => cmd.exec(),
}
}
Expand Down