Skip to content
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

protocols/gossipsub: Allow publishing to anything that implements Into<TopicHash> #2862

Merged
merged 6 commits into from
Sep 5, 2022
Merged
Changes from 2 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
15 changes: 12 additions & 3 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,20 +586,29 @@ where
Ok(true)
}

/// Publishes a message with multiple topics to the network.
/// Publishes a message to a topic in the network.
pub fn publish<H: Hasher>(
&mut self,
topic: Topic<H>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
topic: Topic<H>,
topic: impl Into<TopicHash>,

Would it make sense to generalise the API to this instead of duplicating the function?
This would allow you to pass either a topic or a topic hash.

Copy link
Contributor

Choose a reason for hiding this comment

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

cc @AgeManning What do you think of this API change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this approach, as it extends the existing API without overcomplicating it. Changed this PR to match this comment and added a changelog item.

data: impl Into<Vec<u8>>,
) -> Result<MessageId, PublishError> {
self.publish_to_topichash(topic.into(), data)
}

/// Publishes a message to a topic in the network using TopicHash.
pub fn publish_to_topichash(
&mut self,
topic: TopicHash,
data: impl Into<Vec<u8>>,
) -> Result<MessageId, PublishError> {
let data = data.into();

// Transform the data before building a raw_message.
let transformed_data = self
.data_transform
.outbound_transform(&topic.hash(), data.clone())?;
.outbound_transform(&topic, data.clone())?;

let raw_message = self.build_raw_message(topic.into(), transformed_data)?;
let raw_message = self.build_raw_message(topic, transformed_data)?;

// calculate the message id from the un-transformed data
let msg_id = self.config.message_id(&GossipsubMessage {
Expand Down