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

refactor: move examples to common location #3509

Merged
merged 23 commits into from
Mar 8, 2023

Conversation

yellowred
Copy link
Contributor

@yellowred yellowred commented Feb 27, 2023

Description

Refactor examples into separate binary crates.

Fixes #3111.

Links to any relevant issues

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Excellent start. I am pleased that Git recognizes these as "moved". That should make this fairly easy to review and merge.

examples/chat/Cargo.toml Outdated Show resolved Hide resolved
examples/chat/Cargo.toml Outdated Show resolved Hide resolved
examples/README.md Outdated Show resolved Hide resolved
examples/README.md Show resolved Hide resolved
@yellowred yellowred marked this pull request as ready for review March 2, 2023 22:03
@yellowred yellowred changed the title Draft: Refactor examples Refactor examples Mar 2, 2023
@yellowred yellowred changed the title Refactor examples refactor: examples Mar 2, 2023
@yellowred
Copy link
Contributor Author

yellowred commented Mar 2, 2023

Hi @thomaseizinger, I have moved all the examples to their crates. chat-tokio, gossipsub-chat and mdns-passive-discovery are excluded since they duplicate functionality from other examples. Let me know if you want them back and also if you would like to add other examples. I would add QUIC as OrTransport somewhere, but as you said we need a model example that has all the transports showcased.

EDIT: Still have to resolve some naming collisions in binaries.

@thomaseizinger
Copy link
Contributor

Awesome, thanks a lot for this!

Ping me once you've got CI passing or need help with it :)

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thanks for pushing this forward!

I made some comments, most of them a pretty minor. I really like the direction of this :)

Comment on lines -183 to -217
[[example]]
name = "chat"
required-features = ["full"]

[[example]]
name = "chat-tokio"
required-features = ["full"]

[[example]]
name = "file-sharing"
required-features = ["full"]

[[example]]
name = "gossipsub-chat"
required-features = ["full"]

[[example]]
name = "ipfs-private"
required-features = ["full"]

[[example]]
name = "ipfs-kad"
required-features = ["full"]

[[example]]
name = "ping"
required-features = ["full"]

[[example]]
name = "mdns-passive-discovery"
required-features = ["full"]

[[example]]
name = "distributed-key-value-store"
required-features = ["full"]
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

env_logger = "0.10.0"
futures = "0.3.26"
futures-timer = "3.0"
libp2p = { path = "../../", features=["async-std", "dns", "dcutr", "identify", "macros", "mplex", "noise", "ping", "relay", "rendezvous", "tcp", "tokio", "yamux"] }
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
libp2p = { path = "../../", features=["async-std", "dns", "dcutr", "identify", "macros", "mplex", "noise", "ping", "relay", "rendezvous", "tcp", "tokio", "yamux"] }
libp2p = { path = "../../", features = ["async-std", "dns", "dcutr", "identify", "macros", "mplex", "noise", "ping", "relay", "rendezvous", "tcp", "tokio", "yamux"] }

Formatting nit.

event_process = false,
prelude = "libp2p_swarm::derive_prelude"
)]
#[behaviour(out_event = "Event", event_process = false, prelude = "derive_prelude")]
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
#[behaviour(out_event = "Event", event_process = false, prelude = "derive_prelude")]
#[behaviour(out_event = "Event", event_process = false)]

When depending on the libp2p crate, this is not needed!

env_logger = "0.10"
futures = "0.3.26"
libp2p = { path = "../../", features=["async-std", "dns", "kad", "mplex", "noise", "tcp", "websocket", "yamux"] }
multiaddr = { version = "0.17.0" }
Copy link
Contributor

Choose a reason for hiding this comment

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

We should be able to import multiaddr through libp2p::core::multiaddr. Why is the separate dependency needed?

Comment on lines 18 to 19
name = "rzv-discover"
path = "src/bin/discover.rs"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be in favor of just renaming the files and rely on auto-discover of the binaries. Convention over configuration.

@@ -0,0 +1,12 @@
[package]
name = "chat"
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to append example to these like chat-example? Then it would show up as "Building chat-example" in the cargo logs which I'd mildly prefer.

Comment on lines 42 to 45
libp2p::tcp::tokio::Transport::default()
.upgrade(Version::V1)
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&identity).unwrap())
.multiplex(libp2p_yamux::YamuxConfig::default())
.authenticate(libp2p::noise::NoiseAuthenticated::xx(&key_pair).unwrap())
.multiplex(libp2p::yamux::YamuxConfig::default())
Copy link
Contributor

Choose a reason for hiding this comment

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

We are in the process of apply more consistent naming to all our crates to make usage easier and clearer. Can we import the submodules here please like use libp2p::tcp; and then say tcp::tokio::Transport?

void::unreachable(event)
}
}

#[derive(NetworkBehaviour)]
#[behaviour(
out_event = "MyEvent",
Copy link
Contributor

Choose a reason for hiding this comment

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

In a recent version of the NetworkBehaviour derive, we add the ability to auto-generate this enum. I think our examples should use that as we consider it the recommended way of using the derive. Can you adapt the examples please? The only thing that will needed changing is that the generated enum will be called MyBehaviourEvent.

@@ -0,0 +1,12 @@
[package]
name = "pingpong"
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
name = "pingpong"
name = "ping-example"

examples/file-sharing/src/main.rs Show resolved Hide resolved
@thomaseizinger thomaseizinger changed the title refactor: examples refactor: move examples to common location Mar 3, 2023
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Nice, thank you for that! A few more minor comments.

examples/rendezvous/Cargo.toml Outdated Show resolved Hide resolved
let mut swarm = Swarm::with_tokio_executor(
libp2p_tcp::tokio::Transport::default()
libp2p::tcp::tokio::Transport::default()
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
libp2p::tcp::tokio::Transport::default()
tcp::tokio::Transport::default()

Nit: Could we also align these please? i.e. add use libp2p::tcp.

examples/rendezvous/src/bin/rzv-identify.rs Show resolved Hide resolved
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Two more small requests, sorry for the back and forth!

@@ -0,0 +1,14 @@
[package]
name = "rendezvous"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we consistently apply the naming of appending -example here? Thanks!

Comment on lines 43 to 44
.authenticate(libp2p::noise::NoiseAuthenticated::xx(&key_pair).unwrap())
.multiplex(libp2p::yamux::YamuxConfig::default())
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with this topic here. I didn't explicitly comment on all lines but let's do this consistently across all examples and use yamux:: etc in the code and use libp2p::yamux at the top. (For all libp2p modules: noise, yamux, etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, missed it

@mergify
Copy link
Contributor

mergify bot commented Mar 6, 2023

This pull request has merge conflicts. Could you please resolve them @yellowred? 🙏

@mergify
Copy link
Contributor

mergify bot commented Mar 7, 2023

This pull request has merge conflicts. Could you please resolve them @yellowred? 🙏

@yellowred
Copy link
Contributor Author

@thomaseizinger there is one tiny check fails, not sure why, otherwise should be ok?

@thomaseizinger
Copy link
Contributor

@thomaseizinger there is one tiny check fails, not sure why, otherwise should be ok?

A quick investigation leads to this: clap-rs/clap#4733

Seems like we need to update clap for this to be resolved fully. We do however don't require beta clippy to pass so this doesn't block this PR.

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thank you for tackling this, I am convinced this makes our repository a bit more approachable :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move all examples to a common location
2 participants