-
Notifications
You must be signed in to change notification settings - Fork 0
WIP: Refactor pchain network engine #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
47046ea
8359198
89af337
06d7fd1
34dd150
309a0d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| //! Upon receiving commands from application, gossipsub message will be delivered to a | ||
| //! Gossipsub topic. | ||
|
|
||
| use borsh::BorshDeserialize; | ||
| use futures::StreamExt; | ||
| use libp2p::{ | ||
| core::{muxing::StreamMuxerBox, transport::Boxed}, | ||
|
|
@@ -40,12 +41,13 @@ use std::time::Duration; | |
|
|
||
| use crate::{ | ||
| behaviour::{Behaviour, PeerNetworkEvent}, | ||
| messages::{Envelope, Topic}, | ||
| peer::{EngineCommand, PeerBuilder, Peer}, conversions, | ||
| config, | ||
| conversions, | ||
| messages::{DroppedTxMessage, Envelope, Topic}, | ||
| messages::Topic::{DroppedTxns, HotStuffRsBroadcast, HotStuffRsSend, Mempool}, | ||
| peer::{EngineCommand, PeerBuilder, Peer}, | ||
| }; | ||
|
|
||
| const KADEMLIA_PROTOCOL_NAME: &str = "/pchain_p2p/1.0.0"; | ||
|
|
||
| /// [start] p2p networking peer and return the handle [NetworkHandle] of this process. | ||
| pub(crate) async fn start( | ||
| peer: PeerBuilder, | ||
|
|
@@ -70,8 +72,7 @@ pub(crate) async fn start( | |
| let behaviour = Behaviour::new( | ||
| local_public_address, | ||
| &local_keypair, | ||
| 10, | ||
| &config.kademlia_protocol_names, //TODO jonas | ||
| config.protocol_name | ||
| ); | ||
|
|
||
| let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build(); | ||
|
|
@@ -88,8 +89,7 @@ pub(crate) async fn start( | |
| } | ||
|
|
||
| // 3. Subscribe to Topic | ||
| //TODO jonas | ||
| swarm.behaviour_mut().subscribe()?; | ||
| swarm.behaviour_mut().subscribe(config::fullnode_topics(local_public_address))?; | ||
|
|
||
| // 4. Start p2p networking | ||
| let (sender, mut receiver) = | ||
|
|
@@ -169,12 +169,52 @@ pub(crate) async fn start( | |
| // So we need to convert Vec<u8> to pchain_network::Message. Instead of implementing | ||
| // TryFrom trait for Vec<u8> to Message, implement a function that takes in the Message Topic to help | ||
| // converting Vec<u8> to Message. You can refer to fullnode/mempool messagegate to see how to | ||
| // deserialise each Message type. | ||
| // deserialise each Message type. | ||
|
|
||
| let topic = config::fullnode_topics(local_public_address) | ||
| .into_iter() | ||
| .find(|t| t.clone().hash() == message.topic); | ||
|
|
||
| if let Some(topic) = topic { | ||
| match topic { | ||
| HotStuffRsBroadcast => { | ||
| let hotstuff_message = | ||
| hotstuff_rs::messages::Message::deserialize(&mut message.data.as_slice()) | ||
|
||
| .map_or(None, |msg| { | ||
| Some(msg) | ||
| }); | ||
| }, | ||
| Mempool => { | ||
| let mempool_message = | ||
| pchain_types::blockchain::TransactionV1::deserialize(&mut message.data.as_slice()) | ||
| .map_or(None, |msg| { | ||
| Some(msg) | ||
| }); | ||
| }, | ||
| DroppedTxns => { | ||
| let droppedtx_message = | ||
| DroppedTxMessage::deserialize(&mut message.data.as_slice()) | ||
| .map_or(None, |msg| { | ||
| Some(msg) | ||
| }); | ||
| }, | ||
| HotStuffRsSend(local_public_address) => { | ||
| let hotstuff_message = | ||
| hotstuff_rs::messages::Message::deserialize(&mut message.data.as_slice()) | ||
| .map_or(None, |msg| { | ||
| Some(msg) | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| } else { | ||
| log::debug!("Invalid message topic"); | ||
| } | ||
| } else { | ||
| log::debug!("Receive unknown gossip message"); | ||
| } | ||
| } else { | ||
| log::debug!("Received message from invalid PeerId.") | ||
| log::debug!("Received message from invalid PeerId."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think all applications that uses pchain_network will always subscribe to all topics. For example, fullnode doesn't subscribe to droppedTx. So the topics to be subscribed should be based on the Config we passed in.