Skip to content

Commit

Permalink
Merge pull request #97 from Peergos/feat/configureable-announce-addre…
Browse files Browse the repository at this point in the history
…sses

Configureable announce addresses
  • Loading branch information
ianopolous authored Sep 16, 2024
2 parents 7cce49c + b0adef5 commit 11fda4d
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 37 deletions.
25 changes: 15 additions & 10 deletions src/main/java/org/peergos/EmbeddedIpfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.peergos.protocol.ipns.*;
import org.peergos.util.Logging;

import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.sql.Connection;
import java.sql.DriverManager;
Expand All @@ -52,6 +50,7 @@ public class EmbeddedIpfs {
public final Optional<HttpProtocol.Binding> p2pHttp;
private final List<MultiAddress> bootstrap;
private final Optional<PeriodicBlockProvider> blockProvider;
private final List<MultiAddress> announce;
private final List<MDnsDiscovery> mdns = new ArrayList<>();

public EmbeddedIpfs(Host node,
Expand All @@ -61,7 +60,8 @@ public EmbeddedIpfs(Host node,
Bitswap bitswap,
Optional<HttpProtocol.Binding> p2pHttp,
List<MultiAddress> bootstrap,
Optional<BlockingDeque<Cid>> newBlockProvider) {
Optional<BlockingDeque<Cid>> newBlockProvider,
List<MultiAddress> announce) {
this.node = node;
this.blockstore = blockstore;
this.records = records;
Expand All @@ -72,6 +72,7 @@ public EmbeddedIpfs(Host node,
this.blocks = new BitswapBlockService(node, bitswap, dht);
this.blockProvider = newBlockProvider.map(q -> new PeriodicBlockProvider(22 * 3600_000L,
() -> blockstore.refs(false).join().stream(), node, dht, q));
this.announce = announce;
}

public int maxBlockSize() {
Expand Down Expand Up @@ -142,12 +143,15 @@ public void start() {
try {
this.stop().join();
} catch (Exception ex) {
ex.printStackTrace();
LOG.info(ex.getMessage());
}
});
Runtime.getRuntime().addShutdownHook(shutdownHook);
node.start().join();
IdentifyBuilder.addIdentifyProtocol(node);
IdentifyBuilder.addIdentifyProtocol(node, announce.stream()
.map(MultiAddress::toString)
.map(Multiaddr::new)
.collect(Collectors.toList()));
LOG.info("Node started and listening on " + node.listenAddresses());
LOG.info("Bootstrapping IPFS routing table");
if (bootstrap.isEmpty())
Expand All @@ -173,14 +177,14 @@ public void start() {
});
mdns.start();

blockProvider.ifPresent(p -> p.start());
blockProvider.ifPresent(PeriodicBlockProvider::start);
}

public CompletableFuture<Void> stop() throws Exception {
if (records != null) {
records.close();
}
blockProvider.ifPresent(b -> b.stop());
blockProvider.ifPresent(PeriodicBlockProvider::stop);
dht.stopBootstrapThread();
for (MDnsDiscovery m : mdns) {
m.stop();
Expand Down Expand Up @@ -246,8 +250,8 @@ public static EmbeddedIpfs build(RecordStore records,
IdentitySection identity,
BlockRequestAuthoriser authoriser,
Optional<HttpProtocol.HttpRequestProcessor> handler) {
return build(records, blocks, provideBlocks, swarmAddresses, bootstrap, identity, authoriser, handler,
Optional.empty(), Optional.empty());
return build(records, blocks, provideBlocks, swarmAddresses, bootstrap, identity, Collections.emptyList(),
authoriser, handler, Optional.empty(), Optional.empty());
}

public static EmbeddedIpfs build(RecordStore records,
Expand All @@ -256,6 +260,7 @@ public static EmbeddedIpfs build(RecordStore records,
List<MultiAddress> swarmAddresses,
List<MultiAddress> bootstrap,
IdentitySection identity,
List<MultiAddress> announce,
BlockRequestAuthoriser authoriser,
Optional<HttpProtocol.HttpRequestProcessor> handler,
Optional<String> bitswapProtocolId,
Expand Down Expand Up @@ -291,7 +296,7 @@ public static EmbeddedIpfs build(RecordStore records,
Optional<BlockingDeque<Cid>> newBlockProvider = provideBlocks ?
Optional.of(((ProvidingBlockstore)blockstore).toPublish) :
Optional.empty();
return new EmbeddedIpfs(node, blockstore, records, dht, bitswap, httpHandler, bootstrap, newBlockProvider);
return new EmbeddedIpfs(node, blockstore, records, dht, bitswap, httpHandler, bootstrap, newBlockProvider, announce);
}

public static Multiaddr[] getAddresses(Host node, Kademlia dht, Multihash targetNodeId) throws ConnectionException {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/peergos/protocol/IdentifyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

public class IdentifyBuilder {

public static void addIdentifyProtocol(Host node) {
public static void addIdentifyProtocol(Host node, List<Multiaddr> announceAddresses) {
IdentifyOuterClass.Identify.Builder identifyBuilder = IdentifyOuterClass.Identify.newBuilder()
.setProtocolVersion("ipfs/0.1.0")
.setAgentVersion("nabu/v0.1.0")
.setPublicKey(ByteArrayExtKt.toProtobuf(node.getPrivKey().publicKey().bytes()))
.addAllListenAddrs(node.listenAddresses().stream()
.addAllListenAddrs(Stream.concat(node.listenAddresses().stream(), announceAddresses.stream())
.flatMap(a -> expandWildcardAddresses(a).stream())
.map(Multiaddr::serialize)
.map(ByteArrayExtKt::toProtobuf)
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/peergos/APIServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void runAPIServiceWithFileStorageTest() {
@Test
public void bulkGetTest() {
EmbeddedIpfs ipfs = new EmbeddedIpfs(null, new ProvidingBlockstore(new RamBlockstore()), null,
null, null, Optional.empty(), Collections.emptyList(), Optional.empty());
null, null, Optional.empty(), Collections.emptyList(), Optional.empty(), Collections.emptyList());
Cid cid1 = ipfs.blockstore.put("Hello".getBytes(), Cid.Codec.Raw).join();
Cid cid2= ipfs.blockstore.put("world!".getBytes(), Cid.Codec.Raw).join();
List<Want> wants = new ArrayList<>();
Expand All @@ -57,7 +57,7 @@ public void bulkGetTest() {

public static void runAPIServiceTest(Blockstore blocks) {
EmbeddedIpfs ipfs = new EmbeddedIpfs(null, new ProvidingBlockstore(blocks), null,
null, null, Optional.empty(), Collections.emptyList(), Optional.empty());
null, null, Optional.empty(), Collections.emptyList(), Optional.empty(), Collections.emptyList());
Cid cid = Cid.decode("zdpuAwfJrGYtiGFDcSV3rDpaUrqCtQZRxMjdC6Eq9PNqLqTGg");
Assert.assertFalse("cid found", ipfs.blockstore.has(cid).join());
String text = "Hello world!";
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/peergos/BitswapMirrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void mirrorTree() throws IOException {
new RamProviderStore(1000), new RamRecordStore(), new RamBlockstore(), (c, p, a) -> CompletableFuture.completedFuture(true));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
IPFS kubo = new IPFS("localhost", 5001);
Multiaddr kuboAddress = Multiaddr.fromString("/ip4/127.0.0.1/tcp/4001/p2p/" + kubo.id().get("ID"));
preloadBlocksToKubo(kubo);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/peergos/BootstrapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void bootstrap() {
new RamProviderStore(1000), new RamRecordStore(), new RamBlockstore(), (c, p, a) -> CompletableFuture.completedFuture(true));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
Multihash node1Id = Multihash.deserialize(node1.getPeerId().getBytes());

try {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/peergos/FindPeerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void findLongRunningNode() {
new RamProviderStore(1000), new RamRecordStore(), blockstore1, (c, p, a) -> CompletableFuture.completedFuture(true));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());

try {
// bootstrap node 1
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/peergos/HandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void codecTest() {
apiServer = HttpServer.create(localAPIAddress, 500);
Blockstore blocks = new TypeLimitedBlockstore(new RamBlockstore(), Set.of(Cid.Codec.Raw));
EmbeddedIpfs ipfs = new EmbeddedIpfs(null, new ProvidingBlockstore(blocks), null, null,
new Bitswap(new BitswapEngine(null, null, Bitswap.MAX_MESSAGE_SIZE)), Optional.empty(), Collections.emptyList(), Optional.empty());
new Bitswap(new BitswapEngine(null, null, Bitswap.MAX_MESSAGE_SIZE)), Optional.empty(), Collections.emptyList(), Optional.empty(), Collections.emptyList());
apiServer.createContext(APIHandler.API_URL, new APIHandler(ipfs));
apiServer.setExecutor(Executors.newFixedThreadPool(50));
apiServer.start();
Expand Down Expand Up @@ -100,7 +100,7 @@ public void findBlockProviderTest() throws IOException {

apiServer = HttpServer.create(localAPIAddress, 500);
EmbeddedIpfs ipfs = new EmbeddedIpfs(node1, new ProvidingBlockstore(new RamBlockstore()), null, dht,
null, Optional.empty(), Collections.emptyList(), Optional.empty());
null, Optional.empty(), Collections.emptyList(), Optional.empty(), Collections.emptyList());
apiServer.createContext(APIHandler.API_URL, new APIHandler(ipfs));
apiServer.setExecutor(Executors.newFixedThreadPool(50));
apiServer.start();
Expand All @@ -126,7 +126,7 @@ public void blockMethodsTest() {

apiServer = HttpServer.create(localAPIAddress, 500);
EmbeddedIpfs ipfs = new EmbeddedIpfs(null, new ProvidingBlockstore(new RamBlockstore()), null,
null, new Bitswap(new BitswapEngine(null, null, Bitswap.MAX_MESSAGE_SIZE)), Optional.empty(), Collections.emptyList(), Optional.empty());
null, new Bitswap(new BitswapEngine(null, null, Bitswap.MAX_MESSAGE_SIZE)), Optional.empty(), Collections.emptyList(), Optional.empty(), Collections.emptyList());
apiServer.createContext(APIHandler.API_URL, new APIHandler(ipfs));
apiServer.setExecutor(Executors.newFixedThreadPool(50));
apiServer.start();
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/org/peergos/HttpProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import org.peergos.protocol.dht.*;
import org.peergos.protocol.http.*;
import org.peergos.util.*;
import org.peergos.util.HttpUtil;

import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.zip.*;
Expand Down Expand Up @@ -79,7 +77,7 @@ public void p2pProxyRequest() throws IOException {

ByteArrayOutputStream bout = new ByteArrayOutputStream();
resp.content().readBytes(bout, resp.headers().getInt("content-length"));
Assert.assertTrue(resp.headers().get(headerName).equals(headerValue));
Assert.assertEquals(resp.headers().get(headerName), headerValue);
resp.release();
byte[] replyBody = bout.toByteArray();
equal(replyBody, httpReply);
Expand All @@ -99,7 +97,7 @@ public void p2pProxyClientTest() throws Exception {
.addProtocol(new HttpProtocol.Binding(unusedProxyTarget));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
Kademlia dht = builder1.getWanDht().get();
dht.bootstrapRoutingTable(node1, BootstrapTest.BOOTSTRAP_NODES, a -> true);
System.out.println("Bootstrapping node...");
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/peergos/IpnsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void publishIPNSRecordToKubo() throws IOException {
new RamProviderStore(1000), new RamRecordStore(), blockstore1, (c, p, a) -> CompletableFuture.completedFuture(true));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
Multihash node1Id = Multihash.deserialize(node1.getPeerId().getBytes());

try {
Expand Down Expand Up @@ -73,7 +73,7 @@ public void retrieveKuboPublishedIPNS() throws IOException {
new RamProviderStore(1000), new RamRecordStore(), blockstore1, (c, p, a) -> CompletableFuture.completedFuture(true));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());

try {
IPFS kubo = new IPFS("localhost", 5001);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/peergos/KademliaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public void findOtherNode() throws Exception {
new RamProviderStore(1000), new RamRecordStore(), blockstore1, (c, p, a) -> CompletableFuture.completedFuture(true));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());

HostBuilder builder2 = HostBuilder.create(TestPorts.getPort(),
new RamProviderStore(1000), new RamRecordStore(), new RamBlockstore(), (c, p, a) -> CompletableFuture.completedFuture(true));
Host node2 = builder2.build();
node2.start().join();
IdentifyBuilder.addIdentifyProtocol(node2);
IdentifyBuilder.addIdentifyProtocol(node2, Collections.emptyList());

try {
// bootstrap node 2
Expand Down Expand Up @@ -64,13 +64,13 @@ public void ipnsBenchmark() throws Exception {
new RamProviderStore(1000), new RamRecordStore(), blockstore1, (c, p, a) -> CompletableFuture.completedFuture(true));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());

HostBuilder builder2 = HostBuilder.create(TestPorts.getPort(),
new RamProviderStore(1000), new RamRecordStore(), new RamBlockstore(), (c, p, a) -> CompletableFuture.completedFuture(true));
Host node2 = builder2.build();
node2.start().join();
IdentifyBuilder.addIdentifyProtocol(node2);
IdentifyBuilder.addIdentifyProtocol(node2, Collections.emptyList());

Cid value = blockstore1.put("Publish me.".getBytes(), Cid.Codec.Raw).join();

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/peergos/KuboFindProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void findProviderOverYamux() throws IOException {
.addMuxers(List.of(StreamMuxerProtocol.getYamux()));
Host node1 = builder1.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
try {
IPFS kubo = new IPFS("localhost", 5001);
Multiaddr address2 = Multiaddr.fromString("/ip4/127.0.0.1/tcp/4001/p2p/" + kubo.id().get("ID"));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/peergos/KuboPingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void runPingOverYamux() throws IOException {
.addMuxers(List.of(StreamMuxerProtocol.getYamux()))
.build();
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
try {
IPFS kubo = new IPFS("localhost", 5001);
Multiaddr address2 = Multiaddr.fromString("/ip4/127.0.0.1/tcp/4001/p2p/" + kubo.id().get("ID"));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/peergos/KuboTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void getBlock() throws IOException {
Bitswap bitswap1 = new Bitswap(new BitswapEngine(new RamBlockstore(), (c, p, a) -> CompletableFuture.completedFuture(true), Bitswap.MAX_MESSAGE_SIZE));
Host node1 = HostBuilder.build(TestPorts.getPort(), List.of(bitswap1));
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
try {
IPFS kubo = new IPFS("localhost", 5001);
Multiaddr address2 = Multiaddr.fromString("/ip4/127.0.0.1/tcp/4001/p2p/" + kubo.id().get("ID"));
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/peergos/PingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void runPingEd25519ToRSA() {
new Bitswap(new BitswapEngine(new RamBlockstore(), (c, p, a) -> CompletableFuture.completedFuture(true), Bitswap.MAX_MESSAGE_SIZE))));
node1.start().join();
node2.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node2);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
IdentifyBuilder.addIdentifyProtocol(node2, Collections.emptyList());

Assert.assertTrue(new Multihash(Multihash.Type.id, node1Keys.publicKey().bytes()).toString().equals(node1.getPeerId().toString()));
Assert.assertTrue(new Multihash(Multihash.Type.sha2_256, Hash.sha256(node2Keys.publicKey().bytes())).toString().equals(node2.getPeerId().toString()));
Expand All @@ -87,9 +87,9 @@ public void replyIdentifyOnNewDial() {
Host node1 = HostBuilder.build(TestPorts.getPort(), List.of(new Ping(), new Bitswap(new BitswapEngine(new RamBlockstore(), (c, p, a) -> CompletableFuture.completedFuture(true), Bitswap.MAX_MESSAGE_SIZE))));
Host node2 = HostBuilder.build(TestPorts.getPort(), List.of(new Ping(), new Bitswap(new BitswapEngine(new RamBlockstore(), (c, p, a) -> CompletableFuture.completedFuture(true), Bitswap.MAX_MESSAGE_SIZE))));
node1.start().join();
IdentifyBuilder.addIdentifyProtocol(node1);
IdentifyBuilder.addIdentifyProtocol(node1, Collections.emptyList());
node2.start().join();
IdentifyBuilder.addIdentifyProtocol(node2);
IdentifyBuilder.addIdentifyProtocol(node2, Collections.emptyList());
try {
// ping from 1 to 2
Multiaddr address2 = node2.listenAddresses().get(0);
Expand Down

0 comments on commit 11fda4d

Please sign in to comment.