Skip to content

Commit

Permalink
add Public key into BaseConsensusPeer
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime2 committed Sep 27, 2019
1 parent 7cc8d5d commit a174b50
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ serde_derive = "1.0.91"
libcommon-rs = { git = "https://github.com/Fantom-foundation/libcommon-rs" }
futures-preview = { version = "=0.3.0-alpha.17", features = ["async-await", "nightly"] }
libtransport = { git = "https://github.com/Fantom-foundation/libtransport" }
libsignature = { git = "https://github.com/Fantom-foundation/libsignature" }
32 changes: 28 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ extern crate serde_derive;
use crate::errors::Result;
use futures::stream::Stream;
use libcommon_rs::peer::{Peer, PeerId};
use libsignature::PublicKey;
use serde::{Deserialize, Serialize};

// Base peer structure; common for various consenus algorithms
/// A base structure for consensus peers which can be commonly used for multiple consensus algorithms.
/// The struct take in an Id type and a net address of the peer.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct BaseConsensusPeer<P> {
#[serde(rename = "PubKeyHex")]
pub struct BaseConsensusPeer<P, PK> {
/// Unique id of the node; it could be the first public key of the peer,
/// but it is not supposed to be changed, in comparison to public key,
/// which could be changed.
#[serde(rename = "ID")]
pub id: P,
#[serde(rename = "PubKeyHex")]
pub pub_key: PK,
#[serde(rename = "NetAddr")]
pub net_addr: String,
}
Expand Down Expand Up @@ -67,14 +73,19 @@ where

/// A an implementation of the Peer trait (found in libcommon repository) for the BaseConsensusPeer
/// struct (defined above).
impl<P: PeerId, Error> Peer<P, Error> for BaseConsensusPeer<P>
impl<P, Error, PK> Peer<P, Error> for BaseConsensusPeer<P, PK>
where
P: PeerId,
PK: PublicKey,
{
/// Create a new instance of the BaseConsensusPeer struct. Requires an Id type and net address
/// as inputs.
fn new(id: P, net_addr: String) -> Self {
BaseConsensusPeer { id, net_addr }
BaseConsensusPeer {
id,
net_addr,
pub_key: PK::default(),
}
}
/// Returns the Id of the peer.
fn get_id(&self) -> P {
Expand All @@ -94,6 +105,19 @@ where
}
}

impl<P, PK> BaseConsensusPeer<P, PK>
where
P: PeerId,
PK: PublicKey,
{
pub fn get_public_key(&self) -> PK {
self.pub_key.clone()
}
pub fn set_public_key(&mut self, key: PK) {
self.pub_key = key;
}
}

pub mod errors;

#[cfg(test)]
Expand Down

0 comments on commit a174b50

Please sign in to comment.