Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
27 changes: 17 additions & 10 deletions src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//! - TryFrom<[PublicAddress]> for [PeerId]

use libp2p::identity::{self, ed25519, DecodingError, OtherVariantError, PeerId};
use libp2p::Multiaddr;
use std::net::Ipv4Addr;

/// PublicAddress(PublicAddress) is wrapper around [PublicAddress](pchain_types::cryptography::PublicAddress).
pub struct PublicAddress(pchain_types::cryptography::PublicAddress);
Expand All @@ -28,7 +30,7 @@ impl From<PublicAddress> for pchain_types::cryptography::PublicAddress {
}

impl TryFrom<PeerId> for PublicAddress {
type Error = ConversionError;
type Error = DecodingError;

fn try_from(peer_id: PeerId) -> Result<Self, Self::Error> {
let kp = identity::PublicKey::try_decode_protobuf(&peer_id.to_bytes())?;
Expand All @@ -37,7 +39,7 @@ impl TryFrom<PeerId> for PublicAddress {
}

impl TryFrom<PublicAddress> for PeerId {
type Error = ConversionError;
type Error = PeerIdTryFromPublicAddressError;

fn try_from(public_addr: PublicAddress) -> Result<Self, Self::Error> {
let kp = ed25519::PublicKey::try_from_bytes(&public_addr.0)?;
Expand All @@ -47,23 +49,28 @@ impl TryFrom<PublicAddress> for PeerId {
}

#[derive(Debug)]
pub enum ConversionError {
pub enum PeerIdTryFromPublicAddressError {
OtherVariantError(OtherVariantError),
DecodingError(DecodingError),
}

impl From<OtherVariantError> for ConversionError {
fn from(error: OtherVariantError) -> ConversionError {
ConversionError::OtherVariantError(error)
impl From<OtherVariantError> for PeerIdTryFromPublicAddressError {
fn from(error: OtherVariantError) -> PeerIdTryFromPublicAddressError {
PeerIdTryFromPublicAddressError::OtherVariantError(error)
}
}

impl From<DecodingError> for ConversionError {
fn from(error: DecodingError) -> ConversionError {
ConversionError::DecodingError(error)
impl From<DecodingError> for PeerIdTryFromPublicAddressError {
fn from(error: DecodingError) -> PeerIdTryFromPublicAddressError {
PeerIdTryFromPublicAddressError::DecodingError(error)
}
}

/// Convert ip address [std::net::Ipv4Addr] and port [u16] into MultiAddr [libp2p::Multiaddr] type
pub fn multi_addr(ip_address: Ipv4Addr, port: u16) -> Multiaddr {
format!("/ip4/{}/tcp/{}", ip_address, port).parse().unwrap()
}

#[cfg(test)]

mod test {
Expand All @@ -81,7 +88,7 @@ mod test {
assert!(result.is_ok());

// Convert it back to PeerId
let result: Result<PeerId, ConversionError> = result.unwrap().try_into();
let result: Result<PeerId, PeerIdTryFromPublicAddressError> = result.unwrap().try_into();
assert!(result.is_ok());
assert_eq!(result.unwrap(), test_peerid);
}
Expand Down
250 changes: 0 additions & 250 deletions src/engine.rs

This file was deleted.

10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
//! kademlia_protocol_name // "/pchain_p2p/1.0.0"
//! };
//!
//! // 2. Create a message handler
//! // 2. Create message handlers
//! let (tx, rx) = mpsc::channel();
//! let message_sender = tx.clone();
//! let message_handler = move |msg_orgin: [u8;32], msg: Message| {
//! // processing the message...
//! let _ = message_sender.send((msg_origin, msg));
//! };
//! let mut message_handlers: Vec<Box<dyn Fn(PublicAddress, Message) + Send>> = vec![];
//! message_handlers.push(Box::new(message_handler));
//!
//! // 3. Start P2P network.
//! let peer = PeerBuilder::new(config)
//! .on_receive_msg(message_handler)
//! .build()
//! let peer = Peer::start(config, message_handlers)
//! .await
//! .unwrap();
//!
Expand All @@ -53,8 +53,6 @@ pub mod config;

pub mod conversions;

pub mod engine;

pub mod messages;

pub mod peer;
Loading