Skip to content

Commit

Permalink
Merge branch 'dev' into #17-substreams
Browse files Browse the repository at this point in the history
  • Loading branch information
vikinatora authored Apr 6, 2023
2 parents f0eca7a + 9bba258 commit 7acb15b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Binary file modified libs/nabu-v0.0.1-SNAPSHOT-jar-with-dependencies.jar
Binary file not shown.
30 changes: 30 additions & 0 deletions src/main/java/com/limechain/network/kad/KademliaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.peergos.protocol.dht.RamProviderStore;
import org.peergos.protocol.dht.RamRecordStore;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
Expand Down Expand Up @@ -52,6 +54,7 @@ private void initialize(String protocolId, Multihash hostId, boolean localEnable
*/
public void connectBootNodes(String[] bootNodes) {
var bootstrapMultiAddress = List.of(bootNodes).stream()
.map(KademliaService::dnsNodeToIp4)
.map(MultiAddress::new)
.collect(Collectors.toList());
int successfulBootNodes = dht.bootstrapRoutingTable(host, bootstrapMultiAddress, addr -> !addr.contains("wss"));
Expand All @@ -67,4 +70,31 @@ public void findNewPeers() {
Multihash randomPeerId = new Multihash(Multihash.Type.sha2_256, hash);
dht.findClosestPeers(randomPeerId, REPLICATION, host);
}

/**
* Makes a dns lookup and changes the address to an equal ip4 address
* Implementation is necessary due to a bug in jvm-libp2p that involves resolving dns addresses
* https://github.com/Peergos/nabu/issues/22#issuecomment-1495687079
*
* @param bootNode
* @return bootNode in ip4 format
*/
public static String dnsNodeToIp4(String bootNode) {
int prefixEnd = bootNode.indexOf('/', 1) + 1;
String prefix = bootNode.substring(0, prefixEnd);

if (prefix.equals("/dns/")) {
int domainEnd = bootNode.indexOf('/', prefixEnd);
String domain = bootNode.substring(prefixEnd, domainEnd);
String postfix = bootNode.substring(domainEnd);

try {
InetAddress address = InetAddress.getByName(domain);
bootNode = "/ip4/" + address.getHostAddress() + postfix;
} catch (UnknownHostException e) {
log.log(Level.WARNING, "Unknown domain for bootstrap node address", e);
}
}
return bootNode;
}
}
42 changes: 42 additions & 0 deletions src/test/java/com/limechain/network/kad/KademliaServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.limechain.network.kad;

import org.junit.jupiter.api.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

import static com.limechain.network.kad.KademliaService.dnsNodeToIp4;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class KademliaServiceTest {

/**
* Test might throw UnknownHostException and fail if domain is no longer accessible
*
* @throws UnknownHostException
*/
@Test
public void dnsNodeToIp4_TransformDnsNode() throws UnknownHostException {
//CHECKSTYLE.OFF
String bootNode = "/dns/p2p.0.polkadot.network/tcp/30333/p2p/12D3KooWHsvEicXjWWraktbZ4MQBizuyADQtuEGr3NbDvtm5rFA5";
InetAddress address = InetAddress.getByName("p2p.0.polkadot.network");
String expected = "/ip4/" + address.getHostAddress() + "/tcp/30333/p2p/12D3KooWHsvEicXjWWraktbZ4MQBizuyADQtuEGr3NbDvtm5rFA5";
//CHECKSTYLE.ON

assertEquals(expected, dnsNodeToIp4(bootNode));
}

@Test
public void dnsNodeToIp4_UnchangedAddress_ifInvalidDomain() {
String bootNode2 = "/dns/invalid.domain.limechain/tcp/12345";

assertEquals(bootNode2, dnsNodeToIp4(bootNode2));
}

@Test
public void dnsNodeToIp4_UnchangedAddress_IfNotDns() {
String bootNode3 = "/ip4/192.168.1.1/tcp/12345";

assertEquals(bootNode3, dnsNodeToIp4(bootNode3));
}
}

0 comments on commit 7acb15b

Please sign in to comment.