Skip to content

Commit

Permalink
Merge pull request bitcoin-dev-project#277 from willcl-ark/get-msgs-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark authored Feb 21, 2024
2 parents 3095326 + 49281a1 commit b58c8e9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/backends/backend_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ def get_file(self, tank_index: int, service: ServiceType, file_path: str):
raise NotImplementedError("This method should be overridden by child class")

@abstractmethod
def get_messages(self, tank_index: int, b_ipv4: str, bitcoin_network: str = "regtest"):
def get_messages(self, a_index: int, b_index: int, bitcoin_network: str = "regtest"):
"""
Get bitcoin messages between containers [tank_index] and [b_ipv4] on [bitcoin_network]
Get bitcoin messages between containers [a_index] and [b_index] on [bitcoin_network]
"""
raise NotImplementedError("This method should be overridden by child class")

Expand Down
20 changes: 15 additions & 5 deletions src/backends/compose/compose_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,26 @@ def get_file(self, tank_index: int, service: ServiceType, file_path: str):
out = out[: stat["size"]]
return out

def get_messages(self, tank_index: int, b_ipv4: str, bitcoin_network: str = "regtest"):
# start with the IP of the peer
def get_messages(self, a_index: int, b_index: int, bitcoin_network: str = "regtest"):
# Find the ip of peer B
b_ipv4 = self.get_ipv4_address(self.get_container(b_index, ServiceType.BITCOIN))

# find the corresponding message capture folder
# (which may include the internal port if connection is inbound)
subdir = "/" if bitcoin_network == "main" else f"{bitcoin_network}/"
base_dir = f"/root/.bitcoin/{subdir}message_capture"
dirs = self.exec_run(
tank_index, ServiceType.BITCOIN, f"ls /home/bitcoin/.bitcoin/{subdir}message_capture"
a_index, ServiceType.BITCOIN, f"ls {base_dir}"
)
dirs = dirs.splitlines()
messages = []
for dir_name in dirs:
if b_ipv4 in dir_name:
for file, outbound in [["msgs_recv.dat", False], ["msgs_sent.dat", True]]:
blob = self.get_file(
tank_index,
a_index,
ServiceType.BITCOIN,
f"/home/bitcoin/.bitcoin/{subdir}message_capture/{dir_name}/{file}",
f"{base_dir}/{dir_name}/{file}",
)
json = parse_raw_messages(blob, outbound)
messages = messages + json
Expand Down Expand Up @@ -501,3 +504,10 @@ def tank_from_deployment(self, service, warnet):
tank.lnnode = LNNode(warnet, tank, labels["lnnode_impl"], self)
tank.lnnode.ipv4 = labels.get("lnnode_ipv4_address")
return tank

def get_ipv4_address(self, container: Container) -> str:
"""
Fetches the IPv4 address of a given container.
"""
container_inspect = self.client.containers.get(container.id).attrs
return container_inspect['NetworkSettings']['Networks'][self.network_name]['IPAddress']
39 changes: 21 additions & 18 deletions src/backends/kubernetes/kubernetes_backend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import io
import base64
import logging
import re
import time
Expand Down Expand Up @@ -78,7 +78,7 @@ def get_file(self, tank_index: int, service: ServiceType, file_path: str):
Read a file from inside a container
"""
pod_name = self.get_pod_name(tank_index, service)
exec_command = ["cat", file_path]
exec_command = ['sh', '-c', f'cat "{file_path}" | base64']

resp = stream(
self.client.connect_get_namespaced_pod_exec,
Expand All @@ -95,17 +95,18 @@ def get_file(self, tank_index: int, service: ServiceType, file_path: str):
else LN_CONTAINER_NAME,
)

file = io.BytesIO()
base64_encoded_data = ""
while resp.is_open():
resp.update(timeout=1)
if resp.peek_stdout():
file.write(resp.read_stdout())
base64_encoded_data += resp.read_stdout()
if resp.peek_stderr():
raise Exception(
"Problem copying file from pod" + resp.read_stderr().decode("utf-8")
)
stderr_output = resp.read_stderr()
logger.error(f"STDERR: {stderr_output}")
raise Exception(f"Problem copying file from pod: {stderr_output}")

return file.getvalue()
decoded_bytes = base64.b64decode(base64_encoded_data)
return decoded_bytes

def get_pod_name(self, tank_index: int, type: ServiceType) -> str:
if type == ServiceType.LIGHTNING:
Expand Down Expand Up @@ -232,30 +233,32 @@ def get_bitcoin_cli(self, tank: Tank, method: str, params=None):

def get_messages(
self,
tank_index: int,
b_ipv4: str,
a_index: int,
b_index: int,
bitcoin_network: str = "regtest",
):
b_pod = self.get_pod(self.get_pod_name(b_index, ServiceType.BITCOIN))
subdir = "/" if bitcoin_network == "main" else f"{bitcoin_network}/"
cmd = f"ls /home/bitcoin/.bitcoin/{subdir}message_capture"
self.log.debug(f"Running {cmd=:} on {tank_index=:}")
base_dir = f"/root/.bitcoin/{subdir}message_capture"
cmd = f"ls {base_dir}"
self.log.debug(f"Running {cmd=:} on {a_index=:}")
dirs = self.exec_run(
tank_index,
a_index,
ServiceType.BITCOIN,
cmd,
)
dirs = dirs.splitlines()
self.log.debug(f"Got dirs: {dirs}")
messages = []

for dir_name in dirs:
if b_ipv4 in dir_name:
if b_pod.status.pod_ip in dir_name:
for file, outbound in [["msgs_recv.dat", False], ["msgs_sent.dat", True]]:
# Fetch the file contents from the container
file_path = f"/home/bitcoin/.bitcoin/{subdir}message_capture/{dir_name}/{file}"
blob = self.exec_run(
tank_index, ServiceType.BITCOIN, f"cat {file_path}", self.namespace
file_path = f"{base_dir}/{dir_name}/{file}"
blob = self.get_file(
a_index, ServiceType.BITCOIN, f"{file_path}"
)

# Parse the blob
json = parse_raw_messages(blob, outbound)
messages = messages + json
Expand Down
2 changes: 1 addition & 1 deletion src/warnet/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def tank_messages(self, network: str, node_a: int, node_b: int) -> str:
messages = [
msg
for msg in wn.container_interface.get_messages(
wn.tanks[node_a].index, wn.tanks[node_b].ipv4, wn.bitcoin_network
wn.tanks[node_a].index, wn.tanks[node_b].index, wn.bitcoin_network
)
if msg is not None
]
Expand Down
3 changes: 3 additions & 0 deletions test/rpc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@
# should be at least more than one node
assert count > 1

msgs = base.warcli("messages 0 1")
assert "verack" in msgs

base.stop_server()

0 comments on commit b58c8e9

Please sign in to comment.