Skip to content

Commit

Permalink
remove ValidatorSet
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jan 20, 2023
1 parent 668184c commit 4f62c70
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 76 deletions.
27 changes: 2 additions & 25 deletions ferveo-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use std::cmp::Ordering;
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
/// Represents a tendermint validator
pub struct TendermintValidator<E: PairingEngine> {
/// Total voting power in tendermint consensus
pub power: u64,
/// The established address of the validator
pub address: String,
/// The Public key
Expand All @@ -21,15 +19,15 @@ pub struct TendermintValidator<E: PairingEngine> {

impl<E: PairingEngine> PartialEq for TendermintValidator<E> {
fn eq(&self, other: &Self) -> bool {
(self.power, &self.address) == (other.power, &other.address)
(&self.address) == (&other.address)
}
}

impl<E: PairingEngine> Eq for TendermintValidator<E> {}

impl<E: PairingEngine> PartialOrd for TendermintValidator<E> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some((self.power, &self.address).cmp(&(other.power, &other.address)))
Some(self.address.cmp(&other.address))
}
}

Expand All @@ -39,27 +37,6 @@ impl<E: PairingEngine> Ord for TendermintValidator<E> {
}
}

#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
/// The set of tendermint validators for a dkg instance
pub struct ValidatorSet<E: PairingEngine> {
pub validators: Vec<TendermintValidator<E>>,
}

impl<E: PairingEngine> ValidatorSet<E> {
/// Sorts the validators from highest to lowest. This ordering
/// first considers staking weight and breaks ties on established
/// address
pub fn new(validators: Vec<TendermintValidator<E>>) -> Self {
Self { validators }
}

/// Get the total voting power of the validator set
// TODO: Remove this
pub fn total_voting_power(&self) -> u64 {
self.validators.iter().map(|v| v.power).sum()
}
}

#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Validator<E: PairingEngine> {
pub validator: TendermintValidator<E>,
Expand Down
23 changes: 10 additions & 13 deletions ferveo/benches/benchmarks/pvdkg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use ark_bls12_381::Bls12_381 as EllipticCurve;
use criterion::{criterion_group, criterion_main, Criterion};
use ferveo_common::{TendermintValidator, ValidatorSet};
use ferveo_common::TendermintValidator;
use pprof::criterion::{Output, PProfProfiler};

use ferveo::*;
Expand Down Expand Up @@ -47,16 +47,13 @@ pub fn gen_keypairs(num: u64) -> Vec<ferveo_common::Keypair<EllipticCurve>> {
/// Generate a few validators
pub fn gen_validators(
keypairs: &[ferveo_common::Keypair<EllipticCurve>],
) -> ValidatorSet<EllipticCurve> {
ValidatorSet::new(
(0..keypairs.len())
.map(|i| TendermintValidator {
power: 1, // TODO: Remove it. //i as u64,
address: format!("validator_{}", i),
public_key: keypairs[i].public(),
})
.collect(),
)
) -> Vec<TendermintValidator<EllipticCurve>> {
(0..keypairs.len())
.map(|i| TendermintValidator {
address: format!("validator_{}", i),
public_key: keypairs[i].public(),
})
.collect()
}

/// Create a test dkg in state [`DkgState::Init`]
Expand All @@ -66,13 +63,13 @@ pub fn setup_dkg(
) -> PubliclyVerifiableDkg<EllipticCurve> {
let keypairs = gen_keypairs(num);
let validators = gen_validators(&keypairs);
let me = validators.validators[validator].clone();
let me = validators[validator].clone();
PubliclyVerifiableDkg::new(
validators,
Params {
tau: 0,
security_threshold: 300 / 3,
total_weight: 300,
shares_num: 4,
retry_after: 2,
},
me,
Expand Down
21 changes: 9 additions & 12 deletions ferveo/examples/pvdkg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use ark_bls12_381::Bls12_381 as EllipticCurve;
use ferveo::*;
use ferveo_common::{TendermintValidator, ValidatorSet};
use ferveo_common::TendermintValidator;
use measure_time::print_time;

pub fn main() {
Expand All @@ -21,16 +21,13 @@ pub fn gen_keypairs(num: u64) -> Vec<ferveo_common::Keypair<EllipticCurve>> {
/// Generate a few validators
pub fn gen_validators(
keypairs: &[ferveo_common::Keypair<EllipticCurve>],
) -> ValidatorSet<EllipticCurve> {
ValidatorSet::new(
(0..keypairs.len())
.map(|i| TendermintValidator {
power: i as u64,
address: format!("validator_{}", i),
public_key: keypairs[i].public(),
})
.collect(),
)
) -> Vec<TendermintValidator<EllipticCurve>> {
(0..keypairs.len())
.map(|i| TendermintValidator {
address: format!("validator_{}", i),
public_key: keypairs[i].public(),
})
.collect()
}

/// Create a test dkg in state [`DkgState::Init`]
Expand All @@ -41,7 +38,7 @@ pub fn setup_dkg(
) -> PubliclyVerifiableDkg<EllipticCurve> {
let keypairs = gen_keypairs(num);
let validators = gen_validators(&keypairs);
let me = validators.validators[validator].clone();
let me = validators[validator].clone();
PubliclyVerifiableDkg::new(
validators,
Params {
Expand Down
7 changes: 3 additions & 4 deletions ferveo/src/dkg/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::*;
use ferveo_common::ValidatorSet;
use ferveo_common::TendermintValidator;
use itertools::izip;

pub fn make_validators<E: PairingEngine>(
validator_set: ValidatorSet<E>,
validators: Vec<TendermintValidator<E>>,
) -> Vec<ferveo_common::Validator<E>> {
validator_set
.validators
validators
.iter()
.enumerate()
.map(|(index, validator)| ferveo_common::Validator::<E> {
Expand Down
34 changes: 14 additions & 20 deletions ferveo/src/dkg/pv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ark_ec::PairingEngine;
use ark_ff::Field;
use ark_serialize::*;
use ark_std::{end_timer, start_timer};
use ferveo_common::{PublicKey, TendermintValidator, ValidatorSet};
use ferveo_common::{PublicKey, TendermintValidator};
use std::collections::BTreeMap;

/// The DKG context that holds all of the local state for participating in the DKG
Expand All @@ -31,7 +31,7 @@ impl<E: PairingEngine> PubliclyVerifiableDkg<E> {
/// `me` the validator creating this instance
/// `session_keypair` the keypair for `me`
pub fn new(
validator_set: ValidatorSet<E>,
validators: Vec<TendermintValidator<E>>,
params: Params,
me: TendermintValidator<E>,
session_keypair: ferveo_common::Keypair<E>,
Expand All @@ -43,15 +43,14 @@ impl<E: PairingEngine> PubliclyVerifiableDkg<E> {
.ok_or_else(|| anyhow!("unable to construct domain"))?;

// keep track of the owner of this instance in the validator set
let me = validator_set
.validators
let me = validators
.iter()
.position(|probe| me.address == probe.address)
.context(
"could not find this validator in the provided validator set",
)?;

let validators = make_validators(validator_set);
let validators = make_validators(validators);

// TODO: Remove my_partition
let my_partition =
Expand Down Expand Up @@ -277,22 +276,19 @@ pub(crate) mod test_common {
pub fn gen_n_validators(
keypairs: &[ferveo_common::Keypair<EllipticCurve>],
n: u32,
) -> ValidatorSet<EllipticCurve> {
ValidatorSet::new(
(0..n)
.map(|i| TendermintValidator {
power: 1, // TODO: Should set to 1 in order to force partitioning to give one share to each validator. Replace with 1 by reworking how partitioning works.
address: format!("validator_{}", i),
public_key: keypairs[i as usize].public(),
})
.collect(),
)
) -> Vec<TendermintValidator<EllipticCurve>> {
(0..n)
.map(|i| TendermintValidator {
address: format!("validator_{}", i),
public_key: keypairs[i as usize].public(),
})
.collect()
}

/// Generate a few validators
pub fn gen_validators(
keypairs: &[ferveo_common::Keypair<EllipticCurve>],
) -> ValidatorSet<EllipticCurve> {
) -> Vec<TendermintValidator<EllipticCurve>> {
gen_n_validators(keypairs, 4)
}

Expand All @@ -305,7 +301,7 @@ pub(crate) mod test_common {
let keypairs = gen_n_keypairs(n_validators);
for _keypair in &keypairs {}
let validators = gen_n_validators(&keypairs, n_validators);
let me = validators.validators[my_index].clone();
let me = validators[my_index].clone();
PubliclyVerifiableDkg::new(
validators,
Params {
Expand Down Expand Up @@ -400,7 +396,6 @@ mod test_dkg_init {
retry_after: 2,
},
TendermintValidator::<EllipticCurve> {
power: 9001,
address: "non-existant-validator".into(),
public_key: keypair.public(),
},
Expand Down Expand Up @@ -493,8 +488,7 @@ mod test_dealing {
));
let pvss = dkg.share(rng).expect("Test failed");
let sender = TendermintValidator::<EllipticCurve> {
power: 9001,
address: "Goku".into(),
address: "fake-address".into(),
public_key: ferveo_common::Keypair::<EllipticCurve>::new(rng)
.public(),
};
Expand Down
2 changes: 1 addition & 1 deletion ferveo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod test_dkg_full {
use ark_bls12_381::{Bls12_381 as EllipticCurve, Bls12_381, G2Projective};
use ark_ec::bls12::G2Affine;
use ark_ff::{Fp12, UniformRand};
use ferveo_common::{Keypair, TendermintValidator, ValidatorSet};
use ferveo_common::{Keypair, TendermintValidator};
use group_threshold_cryptography as tpke;
use group_threshold_cryptography::Ciphertext;
use itertools::{zip_eq, Itertools};
Expand Down
2 changes: 1 addition & 1 deletion ferveo/src/vss/pvss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ mod test_pvss {
use crate::dkg::pv::test_common::*;
use ark_bls12_381::Bls12_381 as EllipticCurve;
use ark_ff::UniformRand;
use ferveo_common::{TendermintValidator, ValidatorSet};
use ferveo_common::TendermintValidator;

type Fr = <EllipticCurve as PairingEngine>::Fr;
type G1 = <EllipticCurve as PairingEngine>::G1Affine;
Expand Down

0 comments on commit 4f62c70

Please sign in to comment.