Nodes
In the Polykey network, nodes represent the foundational elements of connectivity and interaction.
Node Structure
The structure of nodes in Polykey encompasses several key elements, each contributing to the node's identity, connectivity, and functionality within the network:
-
NodeId: The primary identifier for a node within the Polykey network. It uniquely identifies each node, playing a critical role in node-to-node interactions and communications.
-
NodeAddress: Comprises a host or hostname and a port. This combination allows for the location and accessibility of nodes within the network, facilitating direct connections and communications.
-
NodeContact: A record mapping NodeContactAddress to NodeContactAddressData. It contains detailed information about how and when a node was last connected, including the connection mode and last active timestamp.
-
NodeBucket: An array of node IDs and their corresponding contacts. Node buckets are used in distributed hash tables (DHTs) to organize nodes based on their distance in the network.
-
NodeGraphSpace: Indicates the space within the NodeGraph where a node resides. It is used to classify and manage nodes in different contexts or domains within the network.
-
ConnectionErrorCode and ConnectionErrorReason: Enums that define various error codes and reasons related to node connections, aiding in error handling and network stability.
-
SeedNodes: A record of initial nodes used for bootstrapping the network. Seed nodes provide the entry points for new nodes to join and integrate into the network.
type NodeAddressScope = 'local' | 'global';
type NodeAddress = [Host | Hostname, Port];
type NodeBucketIndex = number;
type NodeBucket = Array<[NodeId, NodeContact]>;
Specification
NodeManager
type NodeId
- A unique identifier for a node within the Polykey network, essential for node identification and interaction.
type NodeAddress
- A tuple comprising a host (or hostname) and a port, representing the network address of a node for connectivity.
type NodeBucket
- An array structure used in a distributed hash table (DHT) to organize nodes
based on their network distance, containing pairs of
NodeId
andNodeContact
.
type NodeBucketIndex
- A numerical index used to categorize nodes into different buckets within the DHT based on their proximity or distance.
type NodeContactAddressData
- Detailed information about a node's last connection, including the connection mode (direct, signal, relay), last connected time, and address scopes.
type NodeIdEncoded
- An encoded form of
NodeId
, facilitating the storage and transmission of node identifiers in a compact, serialized format.
NodeManager Class Specification
constructor(...)
Initializes an instance of NodeManager
with necessary dependencies and
configuration.
Parameters:
- An object containing:
db
: Database instance for data storage.keyRing
: Key management system.sigchain
: Signature chain for cryptographic operations.gestaltGraph
: GestaltGraph instance for managing node identities.taskManager
: TaskManager for handling asynchronous tasks.nodeGraph
: NodeGraph for managing node network topology.nodeConnectionManager
: Manages connections between nodes.mdnsOptions
: Optional multicast DNS options for local network discovery.connectionConnectTimeoutTime
: Timeout for establishingNodeConnection
.refreshBucketDelayTime
: Interval for refreshing buckets in the DHT.refreshBucketDelayJitter
: Jitter applied to the bucket refresh interval.retryConnectionsDelayTime
: Interval for retrying connections to maintain network health.nodesConnectionFindLocalTimeoutTime
: Timeout for finding local connections via MDNS.logger
: Logger instance.
Usage Example:
const nodeManager = new NodeManager({
db: databaseInstance,
keyRing: keyRingInstance,
sigchain: sigchainInstance,
gestaltGraph: gestaltGraphInstance,
taskManager: taskManagerInstance,
nodeGraph: nodeGraphInstance,
nodeConnectionManager: nodeConnectionManagerInstance,
mdnsOptions: { groups: [hostInstance], port: portNumber },
connectionConnectTimeoutTime: 10000,
refreshBucketDelayTime: 30000,
refreshBucketDelayJitter: 5000,
retryConnectionsDelayTime: 45000,
nodesConnectionFindLocalTimeoutTime: 5000,
logger: new Logger('NodeManager'),
});
public async start(): Promise<void>
Starts the NodeManager, initializing network services, task handlers, and MDNS for node discovery.
Functionality:
- Registers task handlers for various operations like refreshing buckets, garbage collection, connection checks, and node graph synchronization.
- Sets up initial refresh bucket tasks and schedules tasks for checking connections.
- Initializes multicast DNS (MDNS) for local network discovery and service registration.
- Adds event listeners for handling node connections.
Usage Example:
await nodeManager.start();
public async stop(): Promise<void>
Stops the NodeManager, halting network services, task handlers, and MDNS operations.
Functionality:
- Removes event listeners for node connections.
- Stops the MDNS service.
- Cancels all scheduled tasks and deregisters task handlers.
Usage Example:
await nodeManager.stop();
public acquireConnection(...): ResourceAcquire<NodeConnection>
Acquires a connection to a specified node, establishing a new connection if necessary.
Parameters:
nodeId
: Identifier of the target node (NodeId
).ctx?
: Optional context for timed operations.
Functionality:
- Validates the target node ID and checks for an existing connection.
- If no connection exists, initiates a
findNode
operation to establish a connection. - Returns a resource acquisition function for the established node connection.
Returns:
ResourceAcquire<NodeConnection>
: A function to acquire the connection resource.
Usage Example:
const connectionAcquire = nodeManager.acquireConnection(targetNodeId);
const connection = await connectionAcquire();
public withConnF(...): PromiseCancellable<T>
Facilitates communication with another node over the network using an existing or new connection.
Parameters:
nodeId
: Identifier of the target node (NodeId
).f
: A function that handles communication with the node connection.ctx?
: Optional context for timed operations.