问题
I'm new in P2P networking and currently I try to understand some basic things specified by Kademlia papers. The main thing that I cannot understand is Kademlia distance metric. All papers define the distance as XOR of two IDs. The ID size is 160 bits, so the result is also has 160 bits. The question: what is a convenient way to represent this distance as integer? Some implementations, that I checked, use the following: distance = 160 - prefix length (where prefix length is number of leading zeros). Is it correct approach?
回答1:
Some implementations, that I checked, use the following: distance = 160 - prefix length (where prefix length is number of leading zeros). Is it correct approach?
That approach is based on an early revision of the kademlia paper and is insufficient to implement some of the later chapters of the final paper.
A full-fledged implementation should use a tree-like routing table that orders buckets by their absolute position in the keyspace which can be resized when bucket splitting happens.
The ID size is 160 bits, so the result is also has 160 bits. The question: what is a convenient way to represent this distance as integer?
The distance metrics are 160bit integers. You can use a big-integer class or roll your own based on arrays. To get shared prefix bit counts you just have to count the leading zeroes, which scale logarithmically with the network size and should normally fit in much smaller integers once you're done.
来源:https://stackoverflow.com/questions/48602172/how-to-represent-kademlia-distance-metric-as-integer