-
Notifications
You must be signed in to change notification settings - Fork 2k
Adding chia peer command #10760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Adding chia peer command #10760
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
e3a016c
Adding chia peer command
wallentx f3ba37f
Cleaning up peer and show commands
wallentx b8f68a5
Formatting to please flake8
wallentx 87cb68e
Update chia/cmds/peer.py
wallentx 84e6970
Making suggested changes to peer.py
wallentx 98adcae
Merge branch 'wallentx/chia-peer-command' of github.com:Chia-Network/…
wallentx ab5a3aa
Merge branch 'main' into wallentx/chia-peer-command
jack60612 df333b1
finalize pr
jack60612 d398434
fix lint, damn isort
jack60612 ece1178
allow passthrough of root_dir
jack60612 9623f06
yikes
jack60612 36fbfd3
Update show.py
jack60612 3579824
fix
jack60612 34f6adf
happiness is a thing
jack60612 93cb6f9
Update show.py
jack60612 bda20cb
re add rpc, oops
jack60612 18267e7
allow any node type, per request
jack60612 dad21be
make separate funcs files to align with rest of codebase
jack60612 5415873
Revert "make separate funcs files to align with rest of codebase"
jack60612 4c7db68
Merge branch 'main' into wallentx/chia-peer-command
jack60612 cf9b240
remake changes
jack60612 785ff3d
lint & finish chia peer
jack60612 7917dd9
isort
jack60612 4b389b9
lint
jack60612 038cf75
tad more lint never hurt
jack60612 b76034b
cleaner code is fun
jack60612 43f1b11
final change
jack60612 bf31816
Update cmds_util.py
jack60612 9cda285
Merge branch 'main' into wallentx/chia-peer-command
jack60612 d5bb5d3
Update chia.py
jack60612 e8dfcd9
remove accidental change
jack60612 0425770
rename function
jack60612 08732ee
Merge branch 'main' into wallentx/chia-peer-command
jack60612 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,49 @@ | ||
from typing import Optional | ||
|
||
import click | ||
|
||
from chia.cmds.cmds_util import NODE_TYPES, execute_with_any_node | ||
from chia.cmds.peer_funcs import peer_async | ||
|
||
|
||
@click.command("peer", short_help="Show, or modify peering connections", no_args_is_help=True) | ||
@click.option( | ||
"-p", | ||
"--rpc-port", | ||
help=( | ||
"Set the port where the farmer, wallet, full node or harvester " | ||
"is hosting the RPC interface. See the rpc_port in config.yaml" | ||
), | ||
type=int, | ||
default=None, | ||
) | ||
@click.option( | ||
"-c", "--connections", help="List nodes connected to this Full Node", is_flag=True, type=bool, default=False | ||
) | ||
@click.option("-a", "--add-connection", help="Connect to another Full Node by ip:port", type=str, default="") | ||
@click.option( | ||
"-r", "--remove-connection", help="Remove a Node by the first 8 characters of NodeID", type=str, default="" | ||
) | ||
@click.argument("node_type", type=click.Choice(list(NODE_TYPES.keys())), nargs=1, required=True) | ||
@click.pass_context | ||
def peer_cmd( | ||
ctx: click.Context, | ||
rpc_port: Optional[int], | ||
connections: bool, | ||
add_connection: str, | ||
remove_connection: str, | ||
node_type: str, | ||
) -> None: | ||
import asyncio | ||
|
||
asyncio.run( | ||
execute_with_any_node( | ||
node_type, | ||
rpc_port, | ||
peer_async, | ||
ctx.obj["root_path"], | ||
connections, | ||
add_connection, | ||
remove_connection, | ||
) | ||
) |
This file contains hidden or 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,117 @@ | ||
from typing import Any, Dict | ||
|
||
from chia.rpc.rpc_client import RpcClient | ||
|
||
|
||
async def add_node_connection(rpc_client: RpcClient, add_connection: str) -> None: | ||
if ":" not in add_connection: | ||
print("Enter a valid IP and port in the following format: 10.5.4.3:8000") | ||
else: | ||
ip, port = ( | ||
":".join(add_connection.split(":")[:-1]), | ||
add_connection.split(":")[-1], | ||
) | ||
print(f"Connecting to {ip}, {port}") | ||
try: | ||
await rpc_client.open_connection(ip, int(port)) | ||
except Exception: | ||
print(f"Failed to connect to {ip}:{port}") | ||
|
||
|
||
async def remove_node_connection(rpc_client: RpcClient, remove_connection: str) -> None: | ||
from chia.server.outbound_message import NodeType | ||
|
||
result_txt = "" | ||
if len(remove_connection) != 8: | ||
result_txt = "Invalid NodeID. Do not include '.'" | ||
else: | ||
connections = await rpc_client.get_connections() | ||
for con in connections: | ||
if remove_connection == con["node_id"].hex()[:8]: | ||
print("Attempting to disconnect", "NodeID", remove_connection) | ||
try: | ||
await rpc_client.close_connection(con["node_id"]) | ||
except Exception: | ||
result_txt = f"Failed to disconnect NodeID {remove_connection}" | ||
else: | ||
result_txt = ( | ||
f"NodeID {remove_connection}... {NodeType(con['type']).name} {con['peer_host']} disconnected" | ||
) | ||
elif result_txt == "": | ||
result_txt = f"NodeID {remove_connection}... not found" | ||
print(result_txt) | ||
|
||
|
||
async def print_connections(rpc_client: RpcClient, trusted_peers: Dict[str, Any]) -> None: | ||
import time | ||
|
||
from chia.server.outbound_message import NodeType | ||
from chia.util.network import is_trusted_inner | ||
|
||
connections = await rpc_client.get_connections() | ||
print("Connections:") | ||
print("Type IP Ports NodeID Last Connect" + " MiB Up|Dwn") | ||
for con in connections: | ||
last_connect_tuple = time.struct_time(time.localtime(con["last_message_time"])) | ||
last_connect = time.strftime("%b %d %T", last_connect_tuple) | ||
mb_down = con["bytes_read"] / (1024 * 1024) | ||
mb_up = con["bytes_written"] / (1024 * 1024) | ||
|
||
host = con["peer_host"] | ||
# Strip IPv6 brackets | ||
host = host.strip("[]") | ||
|
||
trusted: bool = is_trusted_inner(host, con["node_id"], trusted_peers, False) | ||
# Nodetype length is 9 because INTRODUCER will be deprecated | ||
if NodeType(con["type"]) is NodeType.FULL_NODE: | ||
peak_height = con.get("peak_height", None) | ||
connection_peak_hash = con.get("peak_hash", None) | ||
if connection_peak_hash is None: | ||
connection_peak_hash = "No Info" | ||
else: | ||
if connection_peak_hash.startswith(("0x", "0X")): | ||
connection_peak_hash = connection_peak_hash[2:] | ||
connection_peak_hash = f"{connection_peak_hash[:8]}..." | ||
con_str = ( | ||
f"{NodeType(con['type']).name:9} {host:38} " | ||
f"{con['peer_port']:5}/{con['peer_server_port']:<5}" | ||
f" {con['node_id'].hex()[:8]}... " | ||
f"{last_connect} " | ||
f"{mb_up:7.1f}|{mb_down:<7.1f}" | ||
f"\n " | ||
) | ||
if peak_height is not None: | ||
con_str += f"-Height: {peak_height:8.0f} -Hash: {connection_peak_hash}" | ||
else: | ||
con_str += f"-Height: No Info -Hash: {connection_peak_hash}" | ||
# Only show when Trusted is True | ||
if trusted: | ||
con_str += f" -Trusted: {trusted}" | ||
else: | ||
con_str = ( | ||
f"{NodeType(con['type']).name:9} {host:38} " | ||
f"{con['peer_port']:5}/{con['peer_server_port']:<5}" | ||
f" {con['node_id'].hex()[:8]}... " | ||
f"{last_connect} " | ||
f"{mb_up:7.1f}|{mb_down:<7.1f}" | ||
) | ||
print(con_str) | ||
Check failureCode scanning / CodeQL Clear-text logging of sensitive information
[Sensitive data (secret)](1) is logged here.
|
||
|
||
|
||
async def peer_async( | ||
rpc_client: RpcClient, | ||
config: Dict[str, Any], | ||
show_connections: bool, | ||
add_connection: str, | ||
remove_connection: str, | ||
# trusted_peers: Dict[str, Any], | ||
) -> None: | ||
# Check or edit node connections | ||
if show_connections: | ||
trusted_peers: Dict[str, Any] = config["full_node"].get("trusted_peers", {}) | ||
await print_connections(rpc_client, trusted_peers) | ||
# if called together with state, leave a blank line | ||
if add_connection: | ||
await add_node_connection(rpc_client, add_connection) | ||
if remove_connection: | ||
await remove_node_connection(rpc_client, remove_connection) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.