The Codex DHT is a modified clone of the DiscV5 DHT. The upstream library is https://github.com/status-im/nim-eth/tree/master/eth/p2p/discoveryv5 which is based on Ethereum's discv5 specification: https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md. It has been modified in order to store Codex's signed-peer-record (SPR) entries, as well as content identifiers (CID) for each host. This allows users of the Codex DHT to publish their connection information formatted as an SPR, as well as information regarding the datasets they are storing. CIDs are converted into NodeId values, which are used as keys in the DHT. The value of each DHT entry is a variable sized array of SPRs.
The Codex DHT is limited to DHT specific operations: publishing and looking up of DHT records. DHT records will automatically expire and disappear from the network. It is the responsibility of the user to periodically re-publish relevant records.
## 2. Interfaces
| Interface | Description | Input | Output |
|-----------|-------------|-------|--------|
| `newProtocol` | Creates new instance of the Codex DHT | Private key, connection information, bootstrap node records, configuration options. | New Codex DHT instance object. |
| `open` | First initialization step. | - | - |
| `start` | Second initialization step. | - | - |
| `closeWait` | Decommissioning step. Blocks until network resources are closed. | - | - |
| `updateRecord` | Sets a new SPR as the local node's record. | SPR, which can be constructed from a private key, peerId, and an array of multiaddresses. | - |
| `removeProvidersLocal` | Removes an SPR from the local instance of the Codex DHT. | PeerId of the record to be removed. | - |
| `addProvider` | Adds the SPR of the local node to a new or existing DHT entry. | NodeId to be added to, which can be constructed from address information or CID. | - |
| `getProviders` | Looks up the DHT entries for a NodeId. | NodeId (again, from address or CID). | An array of SPRs. |
| `resolve` | Looks up a peer record given a NodeId. | NodeId. | PeerRecord if found. Contains PeerId, sequence-number, and an array of multiaddresses. |
## 3. Functional requirements
List of features or behaviors the component must provide.
### General
- The component provides logging information.
- The component raises exceptions in case of errors.
- The component returns nullable or optional values where appropriate.
### Network maintenance
- The component requires bootstrap node information at initialization.
- The component requires connection information describing its own network accessability at initialization.
- The component enables the user to update its own network accessability information during operation.
- The component uses the bootstrap node information to contact other nodes in the network.
- When contacting other nodes, the component will:
- Provide its own connection information
- Query other nodes for their connection information
- Maintain a list of other nodes adhering to the following constraints:
- The entries of nodes in this list will contain:
- Connection information
- Timing information
- Reliability information
- When the number of entries in the list is too small as defined by configuration parameters, the component will actively query other nodes for previously undiscovered connection information.
- When the number of entries in the list is too large as defined by configuration parameters, the component will remove the least relevant entries as determined by entry information.
- The component will perform a random lookup every 5 minutes.
### Publishing records
- The component enables the user to publish DHT records containing its own connection information for a given NodeId.
- The component will contact other nodes in the network to disseminate new and updated records.
### Querying records
- The component enables the user to query DHT records for a given NodeId.
- The component will contact other nodes in the network to perform queries.
## 4. Non-Functional Requirements
Performance, security, scalability, reliability, etc.
### Performance
- The component will respond to messages from other nodes within 1 second.
- The component will respond to user interaction within 1 second.
- The component will produce a timeout when other nodes do not respond to its messages within 1 second.
### Security
- The component will disregard unsigned records.
- The component will verify record signatures.
- The component will sign its records before transmitting them.
### Scalability
- The component will discard stale records as defined by configuration parameters.
- The component will deprioritize the distribution of connection information for nodes in the network that appear unresponsive.
- When performing queries, the component will prioritize communication with other nodes most likely to contain the desired records.
## 5. Internal Behavior
Description of algorithms, workflows, or state machines.
Include diagrams if needed.
### Distances, routing-tables, storage, and retrieval