3.0.ipv8.dht.routing

Attributes

Classes

Functions

Module Contents

3.0.ipv8.dht.routing.NODE_LIMIT_INTERVAL = 5
3.0.ipv8.dht.routing.NODE_LIMIT_QUERIES = 10
3.0.ipv8.dht.routing.NODE_STATUS_GOOD = 2
3.0.ipv8.dht.routing.NODE_STATUS_UNKNOWN = 1
3.0.ipv8.dht.routing.NODE_STATUS_BAD = 0
3.0.ipv8.dht.routing.MAX_BUCKET_SIZE = 8
3.0.ipv8.dht.routing.id_to_binary_string(node_id: bytes) str

Convert a node id to a string.

3.0.ipv8.dht.routing.distance(a: bytes, b: bytes) int

Get the XOR between two bytes strings, encoded as in int.

3.0.ipv8.dht.routing.calc_node_id(address: 3.0.ipv8.types.Address | 3.0.ipv8.messaging.interfaces.udp.endpoint.UDPv4Address | 3.0.ipv8.messaging.interfaces.udp.endpoint.UDPv6Address, mid: bytes) bytes

Loosely based on the Bittorrent DHT (https://libtorrent.org/dht_sec.html), the node id is calculated as follows for IPv4: first 3 bytes of crc32c(ip & 0x030f3fff) + first 17 bytes of sha1(public_key).

class 3.0.ipv8.dht.routing.Node(key: 3.0.ipv8.types.Key | bytes, address: 3.0.ipv8.types.Address | None = None, intro: bool = True)

Bases: 3.0.ipv8.peer.Peer

The Node class represents a peer within the DHT community.

bucket: Bucket | None = None
last_response: float = 0
last_queries: collections.deque[float]
last_ping_sent: float = 0
failed: int = 0
rtt: float = 0
property id: bytes

The id of this node.

property last_contact: float

The last timestamp (in seconds) that we interacted with this node.

property last_query: float

The last timestamp (in seconds) that we queried this node.

property blocked: bool

Whether this node is blocked.

property status: int

A good node is a node has responded to one of our queries within the last 15 minutes, or has ever responded to one of our queries and has sent us a query within the last 15 minutes. This is the same logic as used in BEP-5.

distance(other_node: Node | bytes) int

The distance to another node.

class 3.0.ipv8.dht.routing.Bucket(prefix_id: str, max_size: int = MAX_BUCKET_SIZE)

The Bucket class stores nodes that share common prefix ID.

nodes: dict[bytes, Node]
prefix_id: str
max_size: int
last_changed: float = 0
generate_id() bytes

Generate a new id.

owns(node_id: bytes) bool

Whether the given node id is in this bucket.

get(node_id: bytes) Node | None

Get the object belonging to the given node id.

add(node: Node) bool

Attempt to add the given node to this bucket.

Parameters:

node – the node to add.

Returns:

whether the addition was successful.

split() tuple[Bucket, Bucket] | None

Split this bucket in two.

Returns:

the new buckets.

class 3.0.ipv8.dht.routing.RoutingTable(my_node_id: bytes)

The RoutingTable is a binary tree that keeps track of Nodes that we have a connection to.

my_node_id
trie
lock
get_bucket(node_id: bytes) Bucket

Get the bucket that a given node id belongs to.

add(node: Node) Node | None

Add the given node to the routing table.

remove_bad_nodes() list[Node]

Remove nodes that have the BAD status.

has(node_id: bytes) bool

Check if a node id is known.

get(node_id: bytes) Node | None

Return a node belonging to an id, if it exists.

closest_nodes(node_id: bytes, max_nodes: int = 8, exclude_node: Node | None = None) list[Node]

Return the nodes closes to a node id.