I've read the paper-Kademlia: A Peer-to-peer Information System Based on the XOR Metric. but i still have 2 questions.
Q1:How does a node get its node id?
BEP5 says: Each node has a globally unique identifier known as the "node ID." Node IDs are chosen at random from the same 160-bit space as BitTorrent infohashes .
Then if you choose at random,how do you choose and how to make sure the id you choose is globally unique? I mean somebody may choose the same id as yours.
Q2: How does the node storing the key-value know if some peer leave the swarm?
The paper I mentioned in the first line says:To compensate for nodes leaving the network, Kademlia republishes each key-value pair once an hour.
But if I were the node, i want to update who is still downloading one certain file, i still have no idea who have left the swarm.
Then if you choose at random,how do you choose
With a random number generator.
how to make sure the id you choose is globally unique?
It is exceedingly unlikely that someone will choose the same node ID at random. The keyspace is 160bits in size. That's 2^160 possibilities. That's slightly fewer than Earth has atoms.
Q2: How does the node storing the key-value know if some peer leave the swarm?
It doesn't. It simply times out values that are not refreshed.
But if I were the node, i want to update who is still downloading one certain file
The goal of the DHT is not to have an accurate reflection of who's downloading a torrent, it's only there to provide initial contacts to join a swarm. Slight inaccuracies do not matter.