-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a CLI for hivemind.DHT (#465)
* Implement a CLI for hivemind.DHT * Fix log message in README * Update examples/albert/README.md * Add a basic test for hivemind-dht * Move log_visible_maddrs to hivemind.utils.networking Co-authored-by: Michael Diskin <[email protected]> Co-authored-by: Alexander Borzunov <[email protected]>
- Loading branch information
1 parent
724cdfe
commit c49802a
Showing
12 changed files
with
177 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import time | ||
from argparse import ArgumentParser | ||
|
||
from hivemind.dht import DHT, DHTNode | ||
from hivemind.utils.logging import get_logger, use_hivemind_log_handler | ||
from hivemind.utils.networking import log_visible_maddrs | ||
|
||
use_hivemind_log_handler("in_root_logger") | ||
logger = get_logger(__name__) | ||
|
||
|
||
async def report_status(dht: DHT, node: DHTNode): | ||
logger.info( | ||
f"{len(node.protocol.routing_table.uid_to_peer_id) + 1} DHT nodes (including this one) " | ||
f"are in the local routing table " | ||
) | ||
logger.debug(f"Routing table contents: {node.protocol.routing_table}") | ||
logger.info(f"Local storage contains {len(node.protocol.storage)} keys") | ||
logger.debug(f"Local storage contents: {node.protocol.storage}") | ||
|
||
|
||
def main(): | ||
parser = ArgumentParser() | ||
parser.add_argument( | ||
"--initial_peers", | ||
nargs="*", | ||
help="Multiaddrs of the peers that will welcome you into the existing DHT. " | ||
"Example: /ip4/203.0.113.1/tcp/31337/p2p/XXXX /ip4/203.0.113.2/tcp/7777/p2p/YYYY", | ||
) | ||
parser.add_argument( | ||
"--host_maddrs", | ||
nargs="*", | ||
default=["/ip4/0.0.0.0/tcp/0"], | ||
help="Multiaddrs to listen for external connections from other DHT instances. " | ||
"Defaults to all IPv4 interfaces and the TCP protocol: /ip4/0.0.0.0/tcp/0", | ||
) | ||
parser.add_argument( | ||
"--announce_maddrs", | ||
nargs="*", | ||
help="Visible multiaddrs the host announces for external connections from other DHT instances", | ||
) | ||
parser.add_argument( | ||
"--use_ipfs", | ||
action="store_true", | ||
help='Use IPFS to find initial_peers. If enabled, you only need to provide the "/p2p/XXXX" ' | ||
"part of the multiaddrs for the initial_peers " | ||
"(no need to specify a particular IPv4/IPv6 host and port)", | ||
) | ||
parser.add_argument( | ||
"--identity_path", | ||
help="Path to a private key file. If defined, makes the peer ID deterministic. " | ||
"If the file does not exist, writes a new private key to this file.", | ||
) | ||
parser.add_argument( | ||
"--refresh_period", type=int, default=30, help="Period (in seconds) for fetching the keys from DHT" | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
dht = DHT( | ||
start=True, | ||
initial_peers=args.initial_peers, | ||
host_maddrs=args.host_maddrs, | ||
announce_maddrs=args.announce_maddrs, | ||
use_ipfs=args.use_ipfs, | ||
identity_path=args.identity_path, | ||
) | ||
log_visible_maddrs(dht.get_visible_maddrs(), only_p2p=args.use_ipfs) | ||
|
||
while True: | ||
dht.run_coroutine(report_status, return_future=False) | ||
time.sleep(args.refresh_period) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import re | ||
from subprocess import PIPE, Popen | ||
from time import sleep | ||
|
||
DHT_START_PATTERN = re.compile(r"Running a DHT instance. To connect other peers to this one, use (.+)$") | ||
|
||
|
||
def test_dht_connection_successful(): | ||
dht_refresh_period = 1 | ||
|
||
dht_proc = Popen( | ||
["hivemind-dht", "--host_maddrs", "/ip4/127.0.0.1/tcp/0", "--refresh_period", str(dht_refresh_period)], | ||
stderr=PIPE, | ||
text=True, | ||
encoding="utf-8", | ||
) | ||
|
||
first_line = dht_proc.stderr.readline() | ||
second_line = dht_proc.stderr.readline() | ||
dht_pattern_match = DHT_START_PATTERN.search(first_line) | ||
assert dht_pattern_match is not None, first_line | ||
assert "Full list of visible multiaddresses:" in second_line, second_line | ||
|
||
initial_peers = dht_pattern_match.group(1).split(" ") | ||
|
||
dht_client_proc = Popen( | ||
["hivemind-dht", *initial_peers, "--host_maddrs", "/ip4/127.0.0.1/tcp/0"], | ||
stderr=PIPE, | ||
text=True, | ||
encoding="utf-8", | ||
) | ||
|
||
# skip first two lines with connectivity info | ||
for _ in range(2): | ||
dht_client_proc.stderr.readline() | ||
first_report_msg = dht_client_proc.stderr.readline() | ||
|
||
assert "2 DHT nodes (including this one) are in the local routing table" in first_report_msg | ||
|
||
# ensure we get the output of dht_proc after the start of dht_client_proc | ||
sleep(dht_refresh_period) | ||
|
||
# expect that one of the next logging outputs from the first peer shows a new connection | ||
for _ in range(5): | ||
first_report_msg = dht_proc.stderr.readline() | ||
second_report_msg = dht_proc.stderr.readline() | ||
|
||
if ( | ||
"2 DHT nodes (including this one) are in the local routing table" in first_report_msg | ||
and "Local storage contains 0 keys" in second_report_msg | ||
): | ||
break | ||
else: | ||
assert ( | ||
"2 DHT nodes (including this one) are in the local routing table" in first_report_msg | ||
and "Local storage contains 0 keys" in second_report_msg | ||
) | ||
|
||
dht_proc.terminate() | ||
dht_client_proc.terminate() | ||
|
||
dht_proc.wait() | ||
dht_client_proc.wait() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters