Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lookupNode by nodeId in DiscoverySystem #181

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading