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

Implement MDNS based discovery for local networks #92

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Currently implemented properties:
* bloom/[infini filtered](https://www.rasmuspagh.net/papers/infinifilter.pdf) blockstore
* connect bitswap to kademlia for discovery, with a faster version with supplied peerids
* configurable cid publishing function
* mDNS peer discovery
* Android compatibility
* example serverless chat app using p2p http proxy for Android
* interop tests with other implementations - https://github.com/libp2p/test-plans/
Expand All @@ -37,7 +38,6 @@ In the future we will add:
* circuit-relay
* dcutr (direct connection upgrade through relay)
* AutoRelay
* mDNS peer discovery
* example iOS chat app
* QUIC transport (and encryption and multiplexing)

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/peergos/EmbeddedIpfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.libp2p.core.crypto.*;
import io.libp2p.core.multiformats.*;
import io.libp2p.core.multistream.*;
import io.libp2p.discovery.*;
import io.libp2p.protocol.*;
import org.peergos.blockstore.*;
import org.peergos.blockstore.metadatadb.BlockMetadataStore;
Expand All @@ -26,6 +27,7 @@
import org.peergos.protocol.ipns.*;
import org.peergos.util.Logging;

import java.net.*;
import java.nio.file.*;
import java.sql.Connection;
import java.sql.DriverManager;
Expand Down Expand Up @@ -152,6 +154,18 @@ public void start() {
LOG.info("Bootstrapping IPFS kademlia");
dht.bootstrap(node);
dht.startBootstrapThread(node);
InetAddress address = null;
MDnsDiscovery mdns = new MDnsDiscovery(node, "_ipfs-discovery._udp.local.", 60, address);
mdns.getNewPeerFoundListeners().add(peerInfo -> {
PeerId remote = PeerId.fromBase58(peerInfo.getPeerId().toBase58().substring(1)); // Not what's wrong with peerInfo, but this works
if (! remote.equals(node.getPeerId())) {
LOG.info(node.getPeerId() + " found local peer: " + peerInfo.getPeerId().toBase58() + ", addrs: " + peerInfo.getAddresses());
KademliaController ctr = dht.dial(node, remote, peerInfo.getAddresses().toArray(new Multiaddr[0])).getController().join();
ctr.closerPeers(node.getPeerId().getBytes()).join();
}
return null;
});
mdns.start();

blockProvider.ifPresent(p -> p.start());
}
Expand Down
116 changes: 0 additions & 116 deletions src/main/java/org/peergos/client/PerfTest.java

This file was deleted.

12 changes: 0 additions & 12 deletions src/main/java/org/peergos/protocol/perf/Correlation.java

This file was deleted.

156 changes: 0 additions & 156 deletions src/main/java/org/peergos/protocol/perf/Perf.kt

This file was deleted.

19 changes: 19 additions & 0 deletions src/test/java/org/peergos/EmbeddedIpfsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ public void largeBlock() throws Exception {
node2.stop();
}

@Test
public void mdnsDiscovery() throws Exception {
EmbeddedIpfs node1 = build(Collections.emptyList(), List.of(new MultiAddress("/ip4/127.0.0.1/tcp/" + TestPorts.getPort())));
node1.start();
EmbeddedIpfs node2 = build(Collections.emptyList(), List.of(new MultiAddress("/ip4/127.0.0.1/tcp/" + TestPorts.getPort())));
node2.start();

Thread.sleep(5_000);
Cid block = node2.blockstore.put(new byte[1024], Cid.Codec.Raw).join();
PeerId peerId2 = node2.node.getPeerId();
List<HashedBlock> retrieved = ForkJoinPool.commonPool().submit(
() -> node1.getBlocks(List.of(new Want(block)), Set.of(peerId2), false))
.get(5, TimeUnit.SECONDS);
Assert.assertTrue(retrieved.size() == 1);

node1.stop();
node2.stop();
}

@Test
public void publishValue() throws Exception {
EmbeddedIpfs node1 = build(BootstrapTest.BOOTSTRAP_NODES, List.of(new MultiAddress("/ip4/127.0.0.1/tcp/" + TestPorts.getPort())));
Expand Down
Loading