Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
14 changes: 7 additions & 7 deletions src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
messages::{Message, Topic},
};
use libp2p::{
gossipsub::{self, ConfigBuilder, MessageAuthenticity, MessageId, PublishError},
gossipsub::{self, ConfigBuilder, MessageAuthenticity, MessageId, PublishError, TopicHash},
identify,
identity::{
ed25519::{Keypair, PublicKey},
Expand Down Expand Up @@ -150,9 +150,9 @@ impl Behaviour {
self.gossip.publish(topic.hash(), msg)
}

/// Check if the [gossipsub::Message] is subscribed by this peer
pub fn is_subscribed(&self, message: &gossipsub::Message) -> bool {
self.gossip.topics().any(|topic| message.topic.eq(topic))
/// Check if the [gossipsub::topic] is subscribed by this peer
pub fn is_subscribed(&self, topic_hash: &TopicHash) -> bool {
self.gossip.topics().any(|topic| topic_hash.eq(topic))
}
}

Expand Down Expand Up @@ -293,7 +293,7 @@ mod test {
sequence_number: None,
topic: mailbox_topic_hash,
};
assert!(peer.behaviour.is_subscribed(&mailbox_topic_msg));
assert!(peer.behaviour.is_subscribed(&mailbox_topic_msg.topic));

let hotstuff_rs_msg = gossipsub::Message {
source: None,
Expand All @@ -313,7 +313,7 @@ mod test {
let subscribed_topics: Vec<&MessageTopicHash> = peer.behaviour.gossip.topics().collect();
assert_eq!(subscribed_topics.len(), 2); //including the initial subscribed topic

assert!(peer.behaviour.is_subscribed(&hotstuff_rs_msg));
assert!(!peer.behaviour.is_subscribed(&unsubscribed_msg));
assert!(peer.behaviour.is_subscribed(&hotstuff_rs_msg.topic));
assert!(!peer.behaviour.is_subscribed(&unsubscribed_msg.topic));
}
}
30 changes: 19 additions & 11 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,16 +30,17 @@ impl From<PublicAddress> for pchain_types::cryptography::PublicAddress {
}

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

fn try_from(peer_id: PeerId) -> Result<Self, Self::Error> {
let kp = identity::PublicKey::try_decode_protobuf(&peer_id.to_bytes())?;
Ok(PublicAddress(kp.try_into_ed25519().unwrap().to_bytes()))
let ed25519_key = kp.try_into_ed25519()?;
Ok(PublicAddress(ed25519_key.to_bytes()))
}
}

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

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

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

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

impl From<DecodingError> for ConversionError {
fn from(error: DecodingError) -> ConversionError {
ConversionError::DecodingError(error)
impl From<DecodingError> for PublicAddressTryFromPeerIdError {
fn from(error: DecodingError) -> PublicAddressTryFromPeerIdError {
PublicAddressTryFromPeerIdError::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 +89,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, DecodingError> = 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.

Loading