Skip to content

Commit

Permalink
Encode hex auth as bytes in bitswap
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed Jul 12, 2023
1 parent a8a2ac7 commit cd7dcd5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/peergos/protocol/bitswap/Bitswap.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.libp2p.core.multistream.*;
import org.peergos.*;
import org.peergos.protocol.bitswap.pb.*;
import org.peergos.util.*;

import java.util.*;
import java.util.concurrent.*;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void sendWants(Host us, Set<PeerId> peers) {
MessageOuterClass.Message.Wantlist.WantType.Block :
MessageOuterClass.Message.Wantlist.WantType.Have)
.setBlock(ByteString.copyFrom(want.cid.toBytes()))
.setAuth(ByteString.copyFrom(want.auth.orElse("").getBytes()))
.setAuth(ByteString.copyFrom(ArrayOps.hexToBytes(want.auth.orElse(""))))
.build())
.collect(Collectors.toList());
engine.buildAndSendMessages(wantsProto, Collections.emptyList(), Collections.emptyList(),
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/peergos/protocol/bitswap/BitswapEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.peergos.*;
import org.peergos.blockstore.*;
import org.peergos.protocol.bitswap.pb.*;
import org.peergos.util.*;

import java.io.*;
import java.nio.charset.*;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void receiveMessage(MessageOuterClass.Message msg, Stream source) {
if (msg.hasWantlist()) {
for (MessageOuterClass.Message.Wantlist.Entry e : msg.getWantlist().getEntriesList()) {
Cid c = Cid.cast(e.getBlock().toByteArray());
Optional<String> auth = e.getAuth().isEmpty() ? Optional.empty() : Optional.of(e.getAuth().toStringUtf8());
Optional<String> auth = e.getAuth().isEmpty() ? Optional.empty() : Optional.of(ArrayOps.bytesToHex(e.getAuth().toByteArray()));
boolean isCancel = e.getCancel();
boolean sendDontHave = e.getSendDontHave();
boolean wantBlock = e.getWantType().getNumber() == 0;
Expand Down Expand Up @@ -155,7 +156,7 @@ public void receiveMessage(MessageOuterClass.Message msg, Stream source) {
byte[] cidPrefix = block.getPrefix().toByteArray();
Optional<String> auth = block.getAuth().isEmpty() ?
Optional.empty() :
Optional.of(block.getAuth().toStringUtf8());
Optional.of(ArrayOps.bytesToHex(block.getAuth().toByteArray()));
byte[] data = block.getData().toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(cidPrefix);
try {
Expand Down Expand Up @@ -188,7 +189,9 @@ public void receiveMessage(MessageOuterClass.Message msg, Stream source) {
LOG.info("Remaining: " + localWants.size());
for (MessageOuterClass.Message.BlockPresence blockPresence : msg.getBlockPresencesList()) {
Cid c = Cid.cast(blockPresence.getCid().toByteArray());
Optional<String> auth = blockPresence.getAuth().isEmpty() ? Optional.empty() : Optional.of(blockPresence.getAuth().toStringUtf8());
Optional<String> auth = blockPresence.getAuth().isEmpty() ?
Optional.empty() :
Optional.of(ArrayOps.bytesToHex(blockPresence.getAuth().toByteArray()));
Want w = new Want(c, auth);
boolean have = blockPresence.getType().getNumber() == 0;
if (have && localWants.containsKey(w)) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/peergos/util/ArrayOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ public static String bytesToHex(byte[] data)
return s.toString();
}

public static byte[] hexToBytes(String hex) {
byte[] res = new byte[hex.length()/2];
for (int i=0; i < res.length; i++)
res[i] = (byte) Integer.parseInt(hex.substring(2*i, 2*i+2), 16);
return res;
}
}

0 comments on commit cd7dcd5

Please sign in to comment.