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

Reusable/util Swarm #6

Open
rvalle opened this issue Mar 22, 2024 · 3 comments
Open

Reusable/util Swarm #6

rvalle opened this issue Mar 22, 2024 · 3 comments

Comments

@rvalle
Copy link
Contributor

rvalle commented Mar 22, 2024

Projects depending on subp2p explorer will need to build a swarm too.

It would be beneficial to move the swarm building code to utility including the typical sets of behaviors used, while still allowing for a custom set of behaviors.

This would also facilitate managing the later #3 migration.

@rvalle
Copy link
Contributor Author

rvalle commented Mar 27, 2024

@lexnv I am looking into it.
This seems not obvious, by the way behaviors are created and initialized after/become swarm is created.

I currently need the behavior PeerId+Discovery and you use also the PeerId+Discovery+Notification, are you planning on putting together other sets?

Perhaps we could provide utility swarm creation for the typical ones.

@lexnv
Copy link
Owner

lexnv commented Mar 28, 2024

One way to approach this would be to make the default-behavior exposed by the subp2p explorer have an optional notifications::behavior::Notifications. This way users can enable it or disable it depending on their workflow.

In the meanwhile, I think you could use the following approach:

let mut swarm: Swarm<Behaviour> = {
let transport = TransportBuilder::new()
.yamux_maximum_buffer_size(256 * MIB)
.build(local_key.clone());
let discovery = DiscoveryBuilder::new()
.record_ttl(Some(Duration::from_secs(0)))
.provider_ttl(Some(Duration::from_secs(0)))
.query_timeout(Duration::from_secs(5 * 60))
.build(local_peer_id, genesis);
let peer_info = PeerBehaviour::new(local_key.public());
let notifications = Notifications::new(protocol_data);
let behavior = Behaviour {
notifications,
peer_info,
discovery,
};
SwarmBuilder::with_tokio_executor(transport, behavior, local_peer_id).build()
};

And define your own custom Behavior protocol, similar to:

pub struct Behaviour {
/// Notification protocols.
pub notifications: notifications::behavior::Notifications,
/// Implements Ping and Identity and stores peer information in a cache.
pub peer_info: peer_behavior::PeerBehaviour,
/// Discovers nodes of the network.
pub discovery: discovery::Discovery,
}

@rvalle
Copy link
Contributor Author

rvalle commented Mar 28, 2024

Having an optional notifications, or a behavior with optional sub-behaviors sounds ideal. I will investigate.

Yes, perhaps we can start by the default behaviour.

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

No branches or pull requests

2 participants