Skip to content

Commit

Permalink
Make minResults a parameter of resolveValue
Browse files Browse the repository at this point in the history
Include any local value in results
  • Loading branch information
ianopolous committed Dec 14, 2023
1 parent 6d7c107 commit 119bc18
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/peergos/EmbeddedIpfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public CompletableFuture<Void> publishPresignedRecord(Multihash pub, byte[] pres
return dht.publishValue(pub, presignedRecord, node);
}

public CompletableFuture<byte[]> resolveValue(PubKey pub) {
public CompletableFuture<byte[]> resolveValue(PubKey pub, int minResults) {
Multihash publisher = Multihash.deserialize(PeerId.fromPubKey(pub).getBytes());
List<IpnsRecord> candidates = dht.resolveValue(publisher, 1, node);
List<IpnsRecord> candidates = dht.resolveValue(publisher, minResults, node);
List<IpnsRecord> records = candidates.stream().sorted().collect(Collectors.toList());
if (records.isEmpty())
return CompletableFuture.failedFuture(new IllegalStateException("Couldn't resolve IPNS value for " + pub));
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/peergos/protocol/dht/Kademlia.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ private Optional<GetResult> getValueFromPeer(PeerAddresses peer, Multihash publi
public List<IpnsRecord> resolveValue(Multihash publisher, int minResults, Host us) {
byte[] key = IPNS.getKey(publisher);
List<IpnsRecord> candidates = new ArrayList<>();
Optional<IpnsRecord> local = engine.getRecord(publisher);
local.ifPresent(candidates::add);

Id keyId = Id.create(Hash.sha256(key), 256);
SortedSet<RoutingEntry> toQuery = Collections.synchronizedSortedSet(new TreeSet<>((a, b) -> compareKeys(a, b, keyId)));
List<PeerAddresses> localClosest = engine.getKClosestPeers(key);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/peergos/EmbeddedIpfsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void publishValue() throws Exception {
PrivKey publisher = Ed25519Kt.generateEd25519KeyPair().getFirst();
byte[] value = "This is a test".getBytes();
node1.publishValue(publisher, value, 1, 24).join();
byte[] res = node1.resolveValue(publisher.publicKey()).join();
byte[] res = node1.resolveValue(publisher.publicKey(), 5).join();
Assert.assertTrue(Arrays.equals(res, value));

node1.stop();
Expand All @@ -73,7 +73,7 @@ public void publishPresignedValue() throws Exception {
node1.publishPresignedRecord(pub, signedRecord).join();
node1.publishPresignedRecord(pub, signedRecord).join();

byte[] res = node1.resolveValue(publisher.publicKey()).join();
byte[] res = node1.resolveValue(publisher.publicKey(), 5).join();
Assert.assertTrue(Arrays.equals(res, value));

// publish an updated value with same expiry
Expand All @@ -83,7 +83,7 @@ public void publishPresignedValue() throws Exception {
node1.publishPresignedRecord(pub, signedRecord2).join();
node1.publishPresignedRecord(pub, signedRecord2).join();

byte[] res2 = node1.resolveValue(publisher.publicKey()).join();
byte[] res2 = node1.resolveValue(publisher.publicKey(), 5).join();
Assert.assertTrue(Arrays.equals(res2, value2));

// publish an updated value with earlier expiry
Expand All @@ -93,7 +93,7 @@ public void publishPresignedValue() throws Exception {
node1.publishPresignedRecord(pub, signedRecord3).join();
node1.publishPresignedRecord(pub, signedRecord3).join();

byte[] res3 = node1.resolveValue(publisher.publicKey()).join();
byte[] res3 = node1.resolveValue(publisher.publicKey(), 5).join();
Assert.assertTrue(Arrays.equals(res3, value3));

node1.stop();
Expand Down

0 comments on commit 119bc18

Please sign in to comment.