Simplified the use of generics on identity keypair by using output types

This commit is contained in:
Jedrzej Stuczynski
2020-01-29 15:30:20 +00:00
parent b9f3000dcc
commit a73585e13a
7 changed files with 39 additions and 84 deletions
+10 -10
View File
@@ -6,14 +6,13 @@ use bs58;
use curve25519_dalek::scalar::Scalar;
use sphinx::route::DestinationAddressBytes;
pub trait MixnetIdentityKeyPair<Priv, Pub>: Clone
where
Priv: MixnetIdentityPrivateKey,
Pub: MixnetIdentityPublicKey,
{
pub trait MixnetIdentityKeyPair: Clone {
type PublicKeyMaterial: MixnetIdentityPublicKey;
type PrivateKeyMaterial: MixnetIdentityPrivateKey;
fn new() -> Self;
fn private_key(&self) -> &Priv;
fn public_key(&self) -> &Pub;
fn private_key(&self) -> &Self::PrivateKeyMaterial;
fn public_key(&self) -> &Self::PublicKeyMaterial;
fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self;
// TODO: signing related methods
@@ -56,9 +55,10 @@ pub struct DummyMixIdentityKeyPair {
pub public_key: DummyMixIdentityPublicKey,
}
impl MixnetIdentityKeyPair<DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey>
for DummyMixIdentityKeyPair
{
impl MixnetIdentityKeyPair for DummyMixIdentityKeyPair {
type PublicKeyMaterial = DummyMixIdentityPublicKey;
type PrivateKeyMaterial = DummyMixIdentityPrivateKey;
fn new() -> Self {
let keypair = encryption::x25519::KeyPair::new();
DummyMixIdentityKeyPair {
+4 -23
View File
@@ -1,10 +1,7 @@
use crate::result::HealthCheckResult;
use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey};
use directory_client::requests::presence_topology_get::PresenceTopologyGetRequester;
use directory_client::DirectoryClient;
use log::{debug, error, info, trace};
use crypto::identity::MixnetIdentityKeyPair;
use log::trace;
use std::fmt::{Error, Formatter};
use std::marker::PhantomData;
use std::time::Duration;
use topology::{NymTopology, NymTopologyError};
@@ -36,26 +33,13 @@ impl From<topology::NymTopologyError> for HealthCheckerError {
}
}
pub struct HealthChecker<IDPair, Priv, Pub>
where
IDPair: MixnetIdentityKeyPair<Priv, Pub>,
Priv: MixnetIdentityPrivateKey,
Pub: MixnetIdentityPublicKey,
{
pub struct HealthChecker<IDPair: MixnetIdentityKeyPair> {
num_test_packets: usize,
resolution_timeout: Duration,
identity_keypair: IDPair,
_phantom_private: PhantomData<Priv>,
_phantom_public: PhantomData<Pub>,
}
impl<IDPair, Priv, Pub> HealthChecker<IDPair, Priv, Pub>
where
IDPair: crypto::identity::MixnetIdentityKeyPair<Priv, Pub>,
Priv: crypto::identity::MixnetIdentityPrivateKey,
Pub: crypto::identity::MixnetIdentityPublicKey,
{
impl<IDPair: MixnetIdentityKeyPair> HealthChecker<IDPair> {
pub fn new(
resolution_timeout_f64: f64,
num_test_packets: usize,
@@ -65,9 +49,6 @@ where
resolution_timeout: Duration::from_secs_f64(resolution_timeout_f64),
num_test_packets,
identity_keypair,
_phantom_private: PhantomData,
_phantom_public: PhantomData,
}
}
+3 -8
View File
@@ -1,4 +1,4 @@
use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey};
use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey};
use itertools::Itertools;
use log::{debug, error, info, trace, warn};
use mix_client::MixClient;
@@ -27,16 +27,11 @@ pub(crate) struct PathChecker {
}
impl PathChecker {
pub(crate) async fn new<IDPair, Priv, Pub>(
pub(crate) async fn new<IDPair: MixnetIdentityKeyPair>(
providers: Vec<provider::Node>,
identity_keys: &IDPair,
check_id: [u8; 16],
) -> Self
where
IDPair: MixnetIdentityKeyPair<Priv, Pub>,
Priv: MixnetIdentityPrivateKey,
Pub: MixnetIdentityPublicKey,
{
) -> Self {
let mut provider_clients = HashMap::new();
let address = identity_keys.public_key().derive_address();
+3 -5
View File
@@ -1,6 +1,6 @@
use crate::path_check::{PathChecker, PathStatus};
use crate::score::NodeScore;
use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey};
use crypto::identity::MixnetIdentityKeyPair;
use log::{debug, error, info, warn};
use rand_os::rand_core::RngCore;
use sphinx::route::NodeAddressBytes;
@@ -102,7 +102,7 @@ impl HealthCheckResult {
id
}
pub async fn calculate<T, IDPair, Priv, Pub>(
pub async fn calculate<T, IDPair>(
topology: &T,
iterations: usize,
resolution_timeout: Duration,
@@ -110,9 +110,7 @@ impl HealthCheckResult {
) -> Self
where
T: NymTopology,
IDPair: MixnetIdentityKeyPair<Priv, Pub>,
Priv: MixnetIdentityPrivateKey,
Pub: MixnetIdentityPublicKey,
IDPair: MixnetIdentityKeyPair,
{
// currently healthchecker supports only up to 255 iterations - if we somehow
// find we need more, it's relatively easy change
+7 -12
View File
@@ -1,4 +1,6 @@
use crate::pathfinder::PathFinder;
use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey};
use crypto::PemStorable;
use pem::{encode, parse, Pem};
use std::fs::File;
use std::io;
@@ -25,12 +27,7 @@ impl PemStore {
}
}
pub fn read_identity<IDPair, Priv, Pub>(&self) -> io::Result<IDPair>
where
IDPair: crypto::identity::MixnetIdentityKeyPair<Priv, Pub>,
Priv: crypto::identity::MixnetIdentityPrivateKey,
Pub: crypto::identity::MixnetIdentityPublicKey,
{
pub fn read_identity<IDPair: MixnetIdentityKeyPair>(&self) -> io::Result<IDPair> {
let private_pem = self.read_pem_file(self.private_mix_key.clone())?;
let public_pem = self.read_pem_file(self.public_mix_key.clone())?;
@@ -62,12 +59,10 @@ impl PemStore {
// This should be refactored and made more generic for when we have other kinds of
// KeyPairs that we want to persist (e.g. validator keypairs, or keys for
// signing vs encryption). However, for the moment, it does the job.
pub fn write_identity<IDPair, Priv, Pub>(&self, key_pair: IDPair) -> io::Result<()>
where
IDPair: crypto::identity::MixnetIdentityKeyPair<Priv, Pub>,
Priv: crypto::identity::MixnetIdentityPrivateKey,
Pub: crypto::identity::MixnetIdentityPublicKey,
{
pub fn write_identity<IDPair: MixnetIdentityKeyPair>(
&self,
key_pair: IDPair,
) -> io::Result<()> {
std::fs::create_dir_all(self.config_dir.clone())?;
let private_key = key_pair.private_key();
+6 -16
View File
@@ -3,14 +3,13 @@ use crate::client::received_buffer::ReceivedMessagesBuffer;
use crate::client::topology_control::TopologyInnerRef;
use crate::sockets::tcp;
use crate::sockets::ws;
use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey};
use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey};
use directory_client::presence::Topology;
use futures::channel::mpsc;
use futures::join;
use log::*;
use sfw_provider_requests::AuthToken;
use sphinx::route::Destination;
use std::marker::PhantomData;
use std::net::SocketAddr;
use tokio::runtime::Runtime;
use topology::NymTopology;
@@ -37,11 +36,9 @@ pub enum SocketType {
None,
}
pub struct NymClient<IDPair, Priv, Pub>
pub struct NymClient<IDPair>
where
IDPair: MixnetIdentityKeyPair<Priv, Pub> + Send + Sync,
Priv: MixnetIdentityPrivateKey + Send + Sync,
Pub: MixnetIdentityPublicKey + Send + Sync,
IDPair: MixnetIdentityKeyPair + Send + Sync,
{
keypair: IDPair,
@@ -53,19 +50,14 @@ where
directory: String,
auth_token: Option<AuthToken>,
socket_type: SocketType,
_phantom_private: PhantomData<Priv>,
_phantom_public: PhantomData<Pub>,
}
#[derive(Debug)]
pub struct InputMessage(pub Destination, pub Vec<u8>);
impl<IDPair: 'static, Priv: 'static, Pub: 'static> NymClient<IDPair, Priv, Pub>
impl<IDPair> NymClient<IDPair>
where
IDPair: MixnetIdentityKeyPair<Priv, Pub> + Send + Sync,
Priv: MixnetIdentityPrivateKey + Send + Sync,
Pub: MixnetIdentityPublicKey + Send + Sync,
IDPair: 'static + MixnetIdentityKeyPair + Send + Sync,
{
pub fn new(
keypair: IDPair,
@@ -84,8 +76,6 @@ where
directory,
auth_token,
socket_type,
_phantom_private: PhantomData,
_phantom_public: PhantomData,
}
}
@@ -129,7 +119,7 @@ where
// TODO: when we switch to our graph topology, we need to remember to change 'Topology' type
let topology_controller =
rt.block_on(topology_control::TopologyControl::<Topology, _, _, _>::new(
rt.block_on(topology_control::TopologyControl::<Topology, _>::new(
self.directory.clone(),
TOPOLOGY_REFRESH_RATE,
healthcheck_keys,
+6 -10
View File
@@ -1,5 +1,5 @@
use crate::built_info;
use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey};
use crypto::identity::MixnetIdentityKeyPair;
use healthcheck::HealthChecker;
use log::{error, info, trace, warn};
use std::sync::Arc;
@@ -12,16 +12,14 @@ const NODE_HEALTH_THRESHOLD: f64 = 0.0;
// auxiliary type for ease of use
pub type TopologyInnerRef<T> = Arc<FRwLock<Inner<T>>>;
pub(crate) struct TopologyControl<T, IDPair, Priv, Pub>
pub(crate) struct TopologyControl<T, IDPair>
where
T: NymTopology,
IDPair: MixnetIdentityKeyPair<Priv, Pub>,
Priv: MixnetIdentityPrivateKey,
Pub: MixnetIdentityPublicKey,
IDPair: MixnetIdentityKeyPair,
{
directory_server: String,
inner: Arc<FRwLock<Inner<T>>>,
health_checker: HealthChecker<IDPair, Priv, Pub>,
health_checker: HealthChecker<IDPair>,
refresh_rate: f64,
}
@@ -31,12 +29,10 @@ enum TopologyError {
NoValidPathsError,
}
impl<T, IDPair, Priv, Pub> TopologyControl<T, IDPair, Priv, Pub>
impl<T, IDPair> TopologyControl<T, IDPair>
where
T: NymTopology,
IDPair: MixnetIdentityKeyPair<Priv, Pub>,
Priv: MixnetIdentityPrivateKey,
Pub: MixnetIdentityPublicKey,
IDPair: MixnetIdentityKeyPair,
{
pub(crate) async fn new(
directory_server: String,