Last active
September 14, 2021 06:41
-
-
Save sepgh/3fb92c824eb51724be19cd8fee476570 to your computer and use it in GitHub Desktop.
Kademlia way to choose nodes using xor distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
public class KademliaNodesToReference { | |
public static void main(String[] args) { | |
// Node identifier size | |
int identifierSize = 128; | |
// Valid distances according to identifier size | |
ArrayList<Integer> distances = new ArrayList<>(); | |
for(int i = 0; i < identifierSize; i++){ | |
distances.add((int) Math.pow(2, i)); | |
} | |
// Your node id here. Must be in range of 0 -> (2 power identifierSize) | |
int nodeId = 1; | |
// Extracting nodes with specified distance | |
ArrayList<Integer> validNodes = new ArrayList<>(); | |
distances.forEach(distance -> { | |
validNodes.add(nodeId ^ distance); | |
}); | |
System.out.println("Nodes to reference for "+ nodeId +" are: " + Arrays.toString(validNodes.toArray())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment