Skip to content

Commit

Permalink
Add lookupNode by nodeId in DiscoverySystem (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 authored Sep 24, 2024
1 parent c66f3d7 commit 7f6aac8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/org/ethereum/beacon/discovery/DiscoverySystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -114,4 +115,20 @@ public Stream<NodeRecord> streamLiveNodes() {
public CompletableFuture<Collection<NodeRecord>> searchForNewPeers() {
return taskManager.searchForNewPeers();
}

/**
* Lookup node in locally stored KBuckets by its nodeId. Allows lookup of local node record.
*
* @param nodeId NodeId, big endian UInt256 Node ID in bytes
* @return NodeRecord if any found
*/
public Optional<NodeRecord> lookupNode(final Bytes nodeId) {
if (nodeId.equals(getLocalNodeRecord().getNodeId())) {
return Optional.of(getLocalNodeRecord());
}
return buckets
.streamClosestNodes(nodeId)
.findFirst()
.filter(node -> node.getNodeId().equals(nodeId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,38 @@ public NodeRecord fromRlp(RLPReader reader) {
assertFalse(bootnodePingResult.isCompletedExceptionally());
}

@Test
public void shouldLookupNodes() throws Exception {
final DiscoverySystem bootnode = createDiscoveryClient();
final DiscoverySystem node1 = createDiscoveryClient(bootnode.getLocalNodeRecord());
final DiscoverySystem node2 = createDiscoveryClient(bootnode.getLocalNodeRecord());

waitFor(
() -> {
waitFor(node1.searchForNewPeers(), 10);
waitFor(node2.searchForNewPeers(), 10);
assertThat(bootnode.lookupNode(bootnode.getLocalNodeRecord().getNodeId()))
.contains(bootnode.getLocalNodeRecord());
assertThat(bootnode.lookupNode(node1.getLocalNodeRecord().getNodeId()))
.contains(node1.getLocalNodeRecord());
assertThat(bootnode.lookupNode(node2.getLocalNodeRecord().getNodeId()))
.contains(node2.getLocalNodeRecord());
assertThat(bootnode.lookupNode(node1.getLocalNodeRecord().getNodeId().not())).isEmpty();

assertThat(node1.lookupNode(bootnode.getLocalNodeRecord().getNodeId()))
.contains(bootnode.getLocalNodeRecord());
assertThat(node1.lookupNode(node2.getLocalNodeRecord().getNodeId()))
.contains(node2.getLocalNodeRecord());
assertThat(node1.lookupNode(node1.getLocalNodeRecord().getNodeId().not())).isEmpty();

assertThat(node2.lookupNode(node1.getLocalNodeRecord().getNodeId()))
.contains(node1.getLocalNodeRecord());
assertThat(node2.lookupNode(bootnode.getLocalNodeRecord().getNodeId()))
.contains(bootnode.getLocalNodeRecord());
assertThat(node2.lookupNode(node1.getLocalNodeRecord().getNodeId().not())).isEmpty();
});
}

private DiscoverySystem createDiscoveryClient(final NodeRecord... bootnodes) throws Exception {
return createDiscoveryClient(true, bootnodes);
}
Expand Down

0 comments on commit 7f6aac8

Please sign in to comment.