Skip to content

Commit

Permalink
Add bulk get links call which can be used to efficiently mirror a who…
Browse files Browse the repository at this point in the history
…le DAG
  • Loading branch information
ianopolous committed Apr 11, 2024
1 parent 96d35a9 commit e5ea311
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/org/peergos/AggregatedMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private static Counter build(String name, String help) {
public static final Counter API_BLOCK_RM = build("api_block_rm", "Total calls to block/rm.");
public static final Counter API_BLOCK_RM_BULK = build("api_block_rm_bulk", "Total calls to block/rm/bulk.");
public static final Counter API_BLOCK_STAT = build("api_block_stat", "Total calls to block/stat.");
public static final Counter API_BLOCK_STAT_BULK = build("api_block_stat_bulk", "Total calls to block/stat/bulk.");
public static final Counter API_REFS_LOCAL = build("api_refs_local", "Total calls to refs/local.");
public static final Counter API_BLOCK_HAS = build("api_block_has", "Total calls to block/has.");
public static final Counter API_BLOOM_ADD = build("api_bloom_add", "Total calls to bloom/add.");
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/peergos/Want.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.peergos;

import io.ipfs.cid.Cid;
import org.peergos.config.*;

import java.util.*;

public class Want {
public class Want implements Jsonable {
public final Cid cid;
public final Optional<String> authHex;
public Want(Cid cid, Optional<String> authHex) {
Expand All @@ -24,6 +25,17 @@ public boolean equals(Object o) {
return cid.equals(want.cid) && authHex.equals(want.authHex);
}

public Map<String, Object> toJson() {
Map<String, Object> m = new LinkedHashMap<>();
m.put("c", cid.toString());
authHex.ifPresent(h -> m.put("a", h));
return m;
}

public static Want fromJson(Map<String, String> m) {
return new Want(Cid.decode(m.get("c")), Optional.ofNullable(m.get("a")));
}

@Override
public int hashCode() {
return Objects.hash(cid, authHex);
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/peergos/net/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.libp2p.core.PeerId;
import io.libp2p.crypto.keys.*;
import org.peergos.*;
import org.peergos.cbor.*;
import org.peergos.protocol.ipns.*;
import org.peergos.protocol.ipns.pb.*;
import org.peergos.util.*;
Expand All @@ -30,6 +31,7 @@ public class APIHandler extends Handler {
public static final String RM = "block/rm";
public static final String RM_BULK = "block/rm/bulk";
public static final String STAT = "block/stat";
public static final String BULK_STAT = "block/stat/bulk";
public static final String REFS_LOCAL = "refs/local";
public static final String BLOOM_ADD = "bloom/add";
public static final String HAS = "block/has";
Expand Down Expand Up @@ -99,6 +101,31 @@ public void handleCallToAPI(HttpExchange httpExchange) {
}
break;
}
case BULK_STAT: {
AggregatedMetrics.API_BLOCK_STAT_BULK.inc();
Map<String, Object> json = (Map<String, Object>) JSONParser.parse(new String(readFully(httpExchange.getRequestBody())));
List<Want> wants = ((List<Map<String, String>>)json.get("wants"))
.stream()
.map(Want::fromJson)
.collect(Collectors.toList());
Set<PeerId> peers = Optional.ofNullable(params.get("peers"))
.map(p -> p.stream().map(PeerId::fromBase58).collect(Collectors.toSet()))
.orElse(Collections.emptySet());
List<HashedBlock> blocks = ipfs.getBlocks(wants, peers, true);
if (wants.size() == blocks.size()) {
List<List<String>> links = blocks.stream()
.map(b -> CborObject.getLinks(b.hash, b.block).stream().map(Cid::toString).collect(Collectors.toList()))
.collect(Collectors.toList());
replyJson(httpExchange, JSONParser.toString(links));
} else {
try {
httpExchange.sendResponseHeaders(400, 0);
} catch (IOException ioe) {
HttpUtil.replyError(httpExchange, ioe);
}
}
break;
}
case PUT: { // https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-block-put
AggregatedMetrics.API_BLOCK_PUT.inc();
List<String> format = params.get("format");
Expand Down

0 comments on commit e5ea311

Please sign in to comment.