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

Remove total_voting_power parameter from validator::Set::new #740

Merged
merged 6 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `[tendermint]` (Since v0.17.0-rc3) Bech32 encoding fix ([#690])
- `[tendermint, light-client]` Specify the proposer in the validator set of fetched light blocks ([#705])
- `[tendermint-proto]` (Since v0.17.0-rc3) Upgrade protobuf definitions to Tendermint Go v0.34.0 ([#737])
- `[tendermint]` Remove `total_voting_power` parameter from `validator::Set::new` ([#739])

[#425]: https://github.com/informalsystems/tendermint-rs/issues/425
[#646]: https://github.com/informalsystems/tendermint-rs/pull/646
Expand All @@ -20,6 +21,7 @@
[#717]: https://github.com/informalsystems/tendermint-rs/issues/717
[#705]: https://github.com/informalsystems/tendermint-rs/issues/705
[#737]: https://github.com/informalsystems/tendermint-rs/pull/737
[#739]: https://github.com/informalsystems/tendermint-rs/issues/739

## v0.17.0-rc3

Expand Down
10 changes: 10 additions & 0 deletions tendermint/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anomaly::{BoxError, Context};
use thiserror::Error;

use crate::account;
use crate::vote;

/// Error type
pub type Error = BoxError;
Expand Down Expand Up @@ -163,6 +164,15 @@ pub enum Kind {
#[error("negative power")]
NegativePower,

/// Mismatch between raw voting power and computed one in validator set
#[error("mismatch between raw voting power ({raw}) and computed one ({computed})")]
RawVotingPowerMismatch {
/// raw voting power
raw: vote::Power,
/// computed voting power
computed: vote::Power,
},

/// Missing Public Key
#[error("missing public key")]
MissingPublicKey,
Expand Down
49 changes: 26 additions & 23 deletions tendermint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@ impl TryFrom<RawValidatorSet> for Set {
type Error = Error;

fn try_from(value: RawValidatorSet) -> Result<Self, Self::Error> {
let unsorted_validators_result: Result<Vec<Info>, Error> = value
let validators = value
.validators
.into_iter()
.map(TryInto::try_into)
.collect();
Ok(Self::new(
unsorted_validators_result?,
value.proposer.map(TryInto::try_into).transpose()?,
Some(value.total_voting_power.try_into()?),
))
.collect::<Result<Vec<_>, _>>()?;

let proposer = value.proposer.map(TryInto::try_into).transpose()?;
let validator_set = Self::new(validators, proposer);

// Ensure that the raw voting power matches the computed one
let raw_voting_power = value.total_voting_power.try_into()?;
if raw_voting_power != validator_set.total_voting_power() {
return Err(Kind::RawVotingPowerMismatch {
raw: raw_voting_power,
computed: validator_set.total_voting_power(),
}
.into());
}

Ok(validator_set)
}
}

Expand All @@ -50,23 +60,16 @@ impl From<Set> for RawValidatorSet {

impl Set {
/// Constructor
pub fn new(
validators: Vec<Info>,
proposer: Option<Info>,
total_voting_power: Option<vote::Power>,
) -> Set {
let mut validators = validators;
pub fn new(mut validators: Vec<Info>, proposer: Option<Info>) -> Set {
Self::sort_validators(&mut validators);

// Compute the total voting power
let total_voting_power = total_voting_power.unwrap_or_else(|| {
validators
.iter()
.map(|v| v.voting_power.value())
.sum::<u64>()
.try_into()
.unwrap()
});
let total_voting_power = validators
.iter()
.map(|v| v.voting_power.value())
.sum::<u64>()
.try_into()
.unwrap();

Set {
validators,
Expand All @@ -77,7 +80,7 @@ impl Set {

/// Convenience constructor for cases where there is no proposer
pub fn without_proposer(validators: Vec<Info>) -> Set {
Self::new(validators, None, None)
Self::new(validators, None)
}

/// Convenience constructor for cases where there is a proposer
Expand All @@ -94,7 +97,7 @@ impl Set {

// Create the validator set with the given proposer.
// This is required by IBC on-chain validation.
Ok(Self::new(validators, Some(proposer), None))
Ok(Self::new(validators, Some(proposer)))
}

/// Get Info of the underlying validators.
Expand Down
10 changes: 9 additions & 1 deletion tendermint/src/vote/power.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
//! Voting power

use std::convert::{TryFrom, TryInto};
use std::fmt;

use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};

use crate::{Error, Kind};
use std::convert::{TryFrom, TryInto};

/// Voting power
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Default)]
pub struct Power(u64);

impl fmt::Display for Power {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.value())
}
}

impl TryFrom<i64> for Power {
type Error = Error;

Expand Down