libp2p / libp2p/rust-libp2p

It should be possible to create test swarms with predefined identities

Open
#6,151 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.6k
Forks
1.3k
Avg merge
8h 47m
Merged PRs (30d)
19

Description

Description

When using new_ephemeral_tokio from SwarmExt in tests, the new swarm is generated with a random identity. It should be possible to create one that has a predefined identity

Motivation

I have built session management into my application, because I need authentication on another level than PeerId. These sessions have a defined lifetime. When one peer dies, and restarts, this session is no longer present in this peer. I need to test that this doesn't lead to bugs because one peer still has the session, but the other doesn't.

To test this, I'm killing the event loop that contains the swarm, then recreate it. In order for it to look like the same peer, it needs to be created with the same identity it had before

Current Implementation

I suggest to extend the existing implementation

    #[cfg(feature = "tokio")]
    fn new_ephemeral_tokio(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self
    where
        Self: Sized,
    {
        use libp2p_core::{transport::MemoryTransport, upgrade::Version, Transport as _};
        use libp2p_identity::Keypair;

        let identity = Keypair::generate_ed25519();
        let peer_id = PeerId::from(identity.public());

        let transport = MemoryTransport::default()
            .or_transport(libp2p_tcp::tokio::Transport::default())
            .upgrade(Version::V1)
            .authenticate(libp2p_plaintext::Config::new(&identity))
            .multiplex(libp2p_yamux::Config::default())
            .timeout(Duration::from_secs(20))
            .boxed();

        Swarm::new(
            transport,
            behaviour_fn(identity),
            peer_id,
            libp2p_swarm::Config::with_tokio_executor(),
        )
    }

by

    #[cfg(feature = "tokio")]
    fn new_ephemeral_tokio_with_preexisting_keypair(identity: libp2p_identity::Keypair, behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self
    where
        Self: Sized,
    {
        use libp2p_core::{transport::MemoryTransport, upgrade::Version, Transport as _};

        let peer_id = PeerId::from(identity.public());

        let transport = MemoryTransport::default()
            .or_transport(libp2p_tcp::tokio::Transport::default())
            .upgrade(Version::V1)
            .authenticate(libp2p_plaintext::Config::new(&identity))
            .multiplex(libp2p_yamux::Config::default())
            .timeout(Duration::from_secs(20))
            .boxed();

        Swarm::new(
            transport,
            behaviour_fn(identity),
            peer_id,
            libp2p_swarm::Config::with_tokio_executor(),
        )
    }
Are you planning to do it yourself in a pull request?

Maybe

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the SwarmExt::new_ephemeral_tokio entry point and review how it generates the identity, derives the PeerId, and builds the transport. Add a way to supply a preexisting libp2p_identity::Keypair while preserving the existing generated-identity behavior. Done means tests can recreate a swarm with the same identity after its event loop is restarted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
networking
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.