Skip to content

Commit 736c3b6

Browse files
committed
bitcoin: adds peers rpc
Adds a peers rpc that displays the peers of a given tank. e.g. warnet bitcoin peers tank-0000 manual tank-0004 manual tank-0005 manual tank-0006 manual tank-0013 manual tank-0014 manual tank-0015 manual tank-0019 manual tank-0021 inbound tank-0001 inbound tank-0003 inbound tank-0007 inbound tank-0008 inbound tank-0016
1 parent bf0046b commit 736c3b6

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/warnet/bitcoin.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ def _rpc(tank: str, method: str, params: list[str], namespace: Optional[str] = N
7373
return run_command(cmd)
7474

7575

76+
@bitcoin.command()
77+
@click.argument("tank", type=str, required=True)
78+
@click.option("--namespace", default=None, show_default=True)
79+
def peers(tank: str, namespace: Optional[str]):
80+
"""
81+
List <tank pod name>'s peers by connection type, resolving IPs to tank names
82+
"""
83+
namespace = get_default_namespace_or(namespace)
84+
85+
# Map pod IP -> tank name so non-manually connected peers show their names (instead of ips).
86+
ip_to_name = {t.status.pod_ip: t.metadata.name for t in get_mission("tank") if t.status.pod_ip}
87+
88+
try:
89+
peerinfo = json.loads(_rpc(tank, "getpeerinfo", [], namespace))
90+
except Exception as e:
91+
print(f"{e}")
92+
sys.exit(1)
93+
94+
rows = [
95+
(p["connection_type"], ip_to_name.get(p["addr"].rsplit(":", 1)[0], p["addr"]))
96+
for p in peerinfo
97+
]
98+
# manual first, then other types alphabetically
99+
rows.sort(key=lambda r: (r[0] != "manual", r[0], r[1]))
100+
101+
width = max((len(t) for t, _ in rows), default=0)
102+
for conn_type, name in rows:
103+
print(f"{conn_type:<{width}} {name}")
104+
105+
76106
@bitcoin.command()
77107
@click.argument("tank", type=str, required=True)
78108
@click.option("--namespace", default=None, show_default=True)

0 commit comments

Comments
 (0)