Skip to content

It should be possible to create test swarms with predefined identities #6151

@MatthiasvB

Description

@MatthiasvB

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions