latest.ipv8.dht.routing

Module Contents

Classes

Node

The Node class represents a peer within the DHT community.

Bucket

The Bucket class stores nodes that share common prefix ID.

RoutingTable

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

Functions

id_to_binary_string(→ str)

Convert a node id to a string.

distance(→ int)

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

calc_node_id(→ bytes)

Loosely based on the Bittorrent DHT (https://libtorrent.org/dht_sec.html), the node id is calculated as

Attributes

NODE_LIMIT_INTERVAL

NODE_LIMIT_QUERIES

NODE_STATUS_GOOD

NODE_STATUS_UNKNOWN

NODE_STATUS_BAD

MAX_BUCKET_SIZE

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

Convert a node id to a string.

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

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

latest.ipv8.dht.routing.calc_node_id(address: latest.ipv8.types.Address | latest.ipv8.messaging.interfaces.udp.endpoint.UDPv4Address | latest.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 latest.ipv8.dht.routing.Node(key: latest.ipv8.types.Key | bytes, address: latest.ipv8.types.Address | None = None, intro: bool = True)

Bases: latest.ipv8.peer.Peer

The Node class represents a peer within the DHT community.

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 latest.ipv8.dht.routing.Bucket(prefix_id: str, max_size: int = MAX_BUCKET_SIZE)

The Bucket class stores nodes that share common prefix ID.

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 latest.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.

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.