How do I periodically remove inactive nodes and flush the routing table with the use of kademlia python library?

41 Views Asked by At

I've installed kademlia DHT with pip install kademlia. I have 3 nodes. 1 initial node that other nodes can bootstrap with and 2 other nodes. When 1 node goes offline (not the initial node), the other node should remove that inactive node and flush the routing table. How do i do that?

Below is my initial node code:

import logging
import asyncio

from kademlia.network import Server

handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
log.setLevel(logging.DEBUG)


loop = asyncio.get_event_loop()
loop.set_debug(True)

server = Server()
loop.run_until_complete(server.listen(8468))

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    server.stop()
    loop.close()

Node 2 code:

import logging
import asyncio
import sys

from kademlia.network import Server

if len(sys.argv) != 5:
    print("Usage: python set.py <bootstrap node> <bootstrap port> <key> <value>")
    sys.exit(1)

handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
log.setLevel(logging.DEBUG)

async def run():
    server = Server()
    await server.listen(8469)
    bootstrap_node = (sys.argv[1], int(sys.argv[2]))
    await server.bootstrap([bootstrap_node])
    await server.set(sys.argv[3], sys.argv[4])

    # Query the DHT for the key on node2
    retrieved_value = await server.get(sys.argv[3])
    print(f"Node 2 retrieved value from DHT: {retrieved_value}")
    #server.stop()

asyncio.run(run())

Node 3 code:

import logging
import asyncio
import sys

from kademlia.network import Server

if len(sys.argv) != 4:
    print("Usage: python get.py <bootstrap node> <bootstrap port> <key>")
    sys.exit(1)

handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
log.setLevel(logging.DEBUG)

async def run():
    server = Server()
    await server.listen(8470)
    bootstrap_node = (sys.argv[1], int(sys.argv[2]))
    await server.bootstrap([bootstrap_node])

    result = await server.get(sys.argv[3])
    print("Get result:", result)
    server.stop()

asyncio.run(run())

In the kademlia python package, seems like there are 3 useful methods that can be used. Those are lonely_buckets(), remove_contact(node) and flush(). Here is the documentation: API. How should one node remove inactive nodes and flush the routing table. Or should you even flush every time a node is removed? I don't have expert knowledge on how kademlia work, hence that is the reason for this question.

1

There are 1 best solutions below

1
aicoder69 On

The library does this automatically if the nodes are running infinitely.