Skip to content

Conversation

@ong-jonas
Copy link
Collaborator

Changes

  1. network subscribes to Vec<Topic> from config::fullnode_topics(local_public_address) instead of being a separate argument to start

  2. Message types are determined via message.topic.as_str() which can be matched to: "consensus", "mempool" , "droppedTx" and public address string. The message data can then be deserialised according to the type of message it is:

    • hotstuff_rs::messages::Message
    • pchain_types::blockchain::TransactionV1
    • DroppedTxMessage
  3. constant inputs like protocol name and heartbeat seconds were being passed as arguments to Behaviour::new() which were subsequently passed as arguments to kad_config() and gossipsub_config. Removed these redundant arguments and used them in the lowest level functions instead.

src/behaviour.rs Outdated
.max_transmit_size(MAX_TRANSMIT_SIZE * MEGABYTES) // block size is limitted to 2 MB. Multiply by factor of safety = 2.
.message_id_fn(build_msg_id)
.heartbeat_interval(Duration::from_secs(heartbeat_secs))
.heartbeat_interval(Duration::from_secs(10))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can take this magic number out and put them with the constant up there. (Not a must, just think it might be cleaner)

src/behaviour.rs Outdated

pub(crate) const MAX_TRANSMIT_SIZE: usize = 4;
pub(crate) const MEGABYTES: usize = 1048576;
pub(crate) const PROTOCOL_NAME: &str = "/pchain_p2p/1.0.0";
Copy link
Contributor

@manngayin612 manngayin612 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protocol name is no longer a constant, it is a field in Config. That's on me, I forgot to remove it from constant.rs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both identity config and kademlia config uses protocol name, however, while kademlia accepts multiple names, identity accepts only 1. So in the event we have multiple protocol names, which one do we use for identity?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in our Config, we can change the protocol name into a single String instead of a Vec, just like what we used to do. Only put it into a vector in Kad config.

src/engine.rs Outdated
&local_keypair,
10,
&config.kademlia_protocol_names, //TODO jonas
&local_keypair
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned previously, protocol name is no longer a constant, its a configurable field in Config, so we might have to pass in the Config instance.

src/engine.rs Outdated
// 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.
match message.topic.as_str() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you couldn't do branches like Topic::Mempool.hash() in match statement. But comparing the topic using string doesn't seem a good idea.

let topic = fullnode_topics(public_addr).into_iter().find(|t| t.hash() == message.topic);

Will this way of identifying the topic work? I haven't verified, you can try.

Copy link
Contributor

@manngayin612 manngayin612 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments on the functions for deserializing Vec to pchain_network::message::Message

src/engine.rs Outdated
// 3. Subscribe to Topic
//TODO jonas
swarm.behaviour_mut().subscribe()?;
swarm.behaviour_mut().subscribe(config::fullnode_topics(local_public_address))?;
Copy link
Contributor

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.

src/engine.rs Outdated
match topic {
HotStuffRsBroadcast => {
let hotstuff_message =
hotstuff_rs::messages::Message::deserialize(&mut message.data.as_slice())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason of getting the de-serialized result of type Result<> and wrap them with Optional and check if it is Some() or None later? Can't we do something directly when the deserialisation gives out Error?

Also, you gotta return the deserialised message out after this match statement. You should be about to return pchain_network::message::Message.

You might consider getting this part out into a separate helper function to make it look cleaner.

@ong-jonas
Copy link
Collaborator Author

ong-jonas commented Oct 12, 2023

I have completed the changes according to your suggestions, but i still think we should match to the topic hash instead of topic.

The conditional function below checks whether the local peer is subscribed to the message's topic hash.

if swarm.behaviour().is_subscribed(&message)

And after checking, we repeat the check just to get the Topic type.

    let topic = config::fullnode_topics(local_public_address)
                                    .into_iter()
                                    .find(|t| t.clone().hash() == message.topic);

Additionally, config::fullnode_topics(local_public_address) limits mailbox topics to only contain HotStuffRsSend(address) of the local peer.

@manngayin612
Copy link
Contributor

I understand the problem you mentioned, but not really clear about your solution. Have you implemented your thoughts in the recent commit? If not, please do it the way you think it works, doesn't have to follow my suggestions 100%.

@ong-jonas
Copy link
Collaborator Author

the current implementation works for v0.5, will work on my solution for future updates.

Copy link
Contributor

@manngayin612 manngayin612 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve the conflicts then this branch will be ready to be merged.

@ong-jonas ong-jonas marked this pull request as ready for review October 12, 2023 02:43
@manngayin612 manngayin612 merged commit e4e4b1b into parallelchain-io:refactor Oct 12, 2023
@ong-jonas ong-jonas deleted the refactor branch October 12, 2023 02:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants