Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updated conversion errors
  • Loading branch information
ong-jonas committed Oct 23, 2023
commit d2f1d7256c728b5a1572fd1575af0980fa5739b3
23 changes: 12 additions & 11 deletions src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ impl From<PublicAddress> for pchain_types::cryptography::PublicAddress {
}

impl TryFrom<PeerId> for PublicAddress {
type Error = DecodingError;
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 = PeerIdTryFromPublicAddressError;
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 @@ -49,20 +50,20 @@ impl TryFrom<PublicAddress> for PeerId {
}

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

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

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

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

// Convert it back to PeerId
let result: Result<PeerId, PeerIdTryFromPublicAddressError> = 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
8 changes: 3 additions & 5 deletions src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ pub struct Peer {
/// Network handle for the [tokio::task] which is the main thread for the p2p network
pub(crate) handle: JoinHandle<()>,

/// mpsc sender for delivering [PeerCommand] to the p2p network.
/// mpsc sender for delivering [PeerCommand] to the internal thread, commands are used to
/// publish [Topic] specific messages to the p2p network.
pub(crate) sender: tokio::sync::mpsc::Sender<PeerCommand>,
}

impl Peer {
/// Constructs a [Peer] from the given configuration and handlers and start the thread for the p2p network \
/// Constructs a [Peer] from the given configuration and handlers and start the thread for the p2p network
/// 1. Load network configuration to set up transport for the P2P network.
/// 2. Establishes connection to the network by adding bootnodes and subscribing to message [Topic].
/// 3. Spawns an asynchronous [tokio] task and enters the main event loop, returning a mpsc Sender used for sending
/// [PeerCommand] to the internal thread.
///
pub async fn start(config: Config, handlers: Vec<Box<dyn Fn(PublicAddress, Message) + Send>>) -> Result<Peer, PeerStartError> {
let swarm = set_up_transport(&config).await?;
let swarm = establish_network_connections(swarm, &config)?;
Expand Down Expand Up @@ -158,8 +158,6 @@ async fn set_up_transport(config: &Config) -> Result<libp2p::Swarm<Behaviour>,Pe
);
swarm.listen_on(multiaddr)?;



Ok(swarm)
}

Expand Down