Skip to content

Commit 1d90aea

Browse files
committed
fixups to container naming and service classes
1 parent 4bf4822 commit 1d90aea

File tree

5 files changed

+65
-23
lines changed

5 files changed

+65
-23
lines changed

src/services/dns_seed.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44

55
PORT = 15353
6+
DNS_SEED_NAME = "dns-seed"
7+
ZONE_FILE_NAME = "dns-seed.zone"
8+
NAMED_CONF_NAME = "named.conf.local"
69

710

811
class DnsSeed(BaseService):
@@ -11,7 +14,7 @@ def __init__(self, docker_network, templates, config_dir):
1114
self.docker_network = docker_network
1215
self.templates = templates
1316
self.service = {
14-
"container_name": "dns-seed",
17+
"container_name": DNS_SEED_NAME,
1518
"ports": [f"{PORT}:53/udp", f"{PORT}:53/tcp"],
1619
"build": {
1720
"context": ".",
@@ -20,5 +23,5 @@ def __init__(self, docker_network, templates, config_dir):
2023
"networks": [self.docker_network],
2124
}
2225
# Copy files for dockerfile
23-
shutil.copy(str(self.templates / "dns-seed.zone"), config_dir)
24-
shutil.copy(str(self.templates / "named.conf.local"), config_dir)
26+
shutil.copy(str(self.templates / ZONE_FILE_NAME), config_dir)
27+
shutil.copy(str(self.templates / NAMED_CONF_NAME), config_dir)

src/warnet/tank.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self):
4040
self._container = None
4141
self._suffix = None
4242
self._ipv4 = None
43-
self._bitcoind_name = None
43+
self._container_name = None
4444
self._exporter_name = None
4545
self.config_dir = Path()
4646

@@ -118,10 +118,12 @@ def ipv4(self):
118118
return self._ipv4
119119

120120
@property
121-
def bitcoind_name(self):
122-
if self._bitcoind_name is None:
123-
self._bitcoind_name = f"{CONTAINER_PREFIX_BITCOIND}_{self.suffix}"
124-
return self._bitcoind_name
121+
def container_name(self):
122+
if self._container_name is None:
123+
self._container_name = (
124+
f"{self.docker_network}_{CONTAINER_PREFIX_BITCOIND}_{self.suffix}"
125+
)
126+
return self._container_name
125127

126128
@property
127129
def exporter_name(self):
@@ -132,7 +134,7 @@ def exporter_name(self):
132134
@property
133135
def container(self) -> Container:
134136
if self._container is None:
135-
self._container = docker.from_env().containers.get(self.bitcoind_name)
137+
self._container = docker.from_env().containers.get(self.container_name)
136138
return self._container
137139

138140
@exponential_backoff()
@@ -150,19 +152,19 @@ def apply_network_conditions(self):
150152

151153
if not sanitize_tc_netem_command(self.netem):
152154
logger.warning(
153-
f"Not applying unsafe tc-netem conditions to container {self.bitcoind_name}: `{self.netem}`"
155+
f"Not applying unsafe tc-netem conditions to container {self.container_name}: `{self.netem}`"
154156
)
155157
return
156158

157159
# Apply the network condition to the container
158160
rcode, result = self.exec(self.netem)
159161
if rcode == 0:
160162
logger.info(
161-
f"Successfully applied network conditions to {self.bitcoind_name}: `{self.netem}`"
163+
f"Successfully applied network conditions to {self.container_name}: `{self.netem}`"
162164
)
163165
else:
164166
logger.error(
165-
f"Error applying network conditions to {self.bitcoind_name}: `{self.netem}` ({result})"
167+
f"Error applying network conditions to {self.container_name}: `{self.netem}` ({result})"
166168
)
167169

168170
def write_bitcoin_conf(self, base_bitcoin_conf):
@@ -198,7 +200,7 @@ def write_torrc(self):
198200
def add_services(self, services):
199201
assert self.index is not None
200202
assert self.conf_file is not None
201-
services[self.bitcoind_name] = {}
203+
services[self.container_name] = {}
202204

203205
# Setup bitcoind, either release binary or build from source
204206
if "/" and "#" in self.version:
@@ -219,12 +221,14 @@ def add_services(self, services):
219221
"dockerfile": str(TEMPLATES / f"Dockerfile_{self.version}"),
220222
}
221223
# Use entrypoint for derived build, but not for compiled build
222-
services[self.bitcoind_name].update({"entrypoint": "/warnet_entrypoint.sh"})
224+
services[self.container_name].update(
225+
{"entrypoint": "/warnet_entrypoint.sh"}
226+
)
223227

224228
# Add the bitcoind service
225-
services[self.bitcoind_name].update(
229+
services[self.container_name].update(
226230
{
227-
"container_name": self.bitcoind_name,
231+
"container_name": self.container_name,
228232
"build": build,
229233
"volumes": [
230234
f"{self.conf_file}:/home/bitcoin/.bitcoin/bitcoin.conf",
@@ -257,7 +261,7 @@ def add_services(self, services):
257261
def add_scrapers(self, scrapers):
258262
scrapers.append(
259263
{
260-
"job_name": self.bitcoind_name,
264+
"job_name": self.container_name,
261265
"scrape_interval": "5s",
262266
"static_configs": [{"targets": [f"{self.exporter_name}:9332"]}],
263267
}

src/warnet/test_framework_bridge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def setup(self):
3333
warnet = Warnet.from_docker_env(self.options.network)
3434
for i, tank in enumerate(warnet.tanks):
3535
ip = tank.ipv4
36-
logger.info(f"Adding TestNode {i} from {tank.bitcoind_name} with IP {ip}")
36+
logger.info(f"Adding TestNode {i} from {tank.container_name} with IP {ip}")
3737
node = TestNode(
3838
i,
3939
"", # datadir path

src/warnet/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import inspect
23
import ipaddress
34
import logging
45
import os
@@ -326,3 +327,22 @@ def gen_config_dir(network: str) -> Path:
326327
xdg_config = os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
327328
config_dir = Path(xdg_config) / "warnet" / network
328329
return config_dir
330+
331+
332+
def bubble_exception_str(func):
333+
@functools.wraps(func)
334+
def wrapper(*args, **kwargs):
335+
try:
336+
return func(*args, **kwargs)
337+
except Exception as e:
338+
func_name = inspect.currentframe().f_code.co_name
339+
local_vars = inspect.currentframe().f_locals
340+
# Filter out the 'self' variable from the local_vars
341+
context_str = ", ".join(
342+
f"{k}={v}" for k, v in local_vars.items() if k != "self"
343+
)
344+
raise Exception(
345+
f"Exception in function '{func_name}' with context ({context_str}): {str(e)}"
346+
)
347+
348+
return wrapper

src/warnet/warnet.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
from services.grafana import Grafana
1818
from services.tor import Tor
1919
from services.fork_observer import ForkObserver
20-
from services.dns_seed import DnsSeed
20+
from services.dns_seed import DnsSeed, ZONE_FILE_NAME, DNS_SEED_NAME
2121
from warnet.tank import Tank
22-
from warnet.utils import parse_bitcoin_conf, gen_config_dir
22+
from warnet.utils import parse_bitcoin_conf, gen_config_dir, bubble_exception_str
2323

24-
logger = logging.getLogger("Warnet")
24+
logger = logging.getLogger("warnet")
2525
FO_CONF_NAME = "fork_observer_config.toml"
26-
ZONE_FILE_NAME = "dns-seed.zone"
2726
logging.getLogger("docker.utils.config").setLevel(logging.WARNING)
2827
logging.getLogger("docker.auth").setLevel(logging.WARNING)
2928

@@ -60,6 +59,7 @@ def __str__(self) -> str:
6059
)
6160

6261
@classmethod
62+
@bubble_exception_str
6363
def from_graph_file(
6464
cls, graph_file: str, config_dir: Path, network: str = "warnet"
6565
):
@@ -74,6 +74,7 @@ def from_graph_file(
7474
return self
7575

7676
@classmethod
77+
@bubble_exception_str
7778
def from_graph(cls, graph):
7879
self = cls(Path())
7980
self.graph = graph
@@ -82,6 +83,7 @@ def from_graph(cls, graph):
8283
return self
8384

8485
@classmethod
86+
@bubble_exception_str
8587
def from_network(
8688
cls, config_dir: Path = Path(), network: str = "warnet", tanks=True
8789
):
@@ -95,6 +97,7 @@ def from_network(
9597
return self
9698

9799
@classmethod
100+
@bubble_exception_str
98101
def from_docker_env(cls, network_name):
99102
config_dir = gen_config_dir(network_name)
100103
self = cls(config_dir)
@@ -113,9 +116,11 @@ def from_docker_env(cls, network_name):
113116
return self
114117

115118
@property
119+
@bubble_exception_str
116120
def zone_file_path(self):
117121
return self.config_dir / ZONE_FILE_NAME
118122

123+
@bubble_exception_str
119124
def tanks_from_graph(self):
120125
for node_id in self.graph.nodes():
121126
if int(node_id) != len(self.tanks):
@@ -125,17 +130,20 @@ def tanks_from_graph(self):
125130
self.tanks.append(Tank.from_graph_node(node_id, self))
126131
logger.info(f"Imported {len(self.tanks)} tanks from graph")
127132

133+
@bubble_exception_str
128134
def write_bitcoin_confs(self):
129135
with open(TEMPLATES / "bitcoin.conf", "r") as file:
130136
text = file.read()
131137
base_bitcoin_conf = parse_bitcoin_conf(text)
132138
for tank in self.tanks:
133139
tank.write_bitcoin_conf(base_bitcoin_conf)
134140

141+
@bubble_exception_str
135142
def apply_network_conditions(self):
136143
for tank in self.tanks:
137144
tank.apply_network_conditions()
138145

146+
@bubble_exception_str
139147
def generate_zone_file_from_tanks(self):
140148
records_list = [
141149
f"seed.dns-seed. 300 IN A {tank.ipv4}" for tank in self.tanks
@@ -152,11 +160,12 @@ def generate_zone_file_from_tanks(self):
152160
with open(self.config_dir / ZONE_FILE_NAME, "w") as f:
153161
f.write(content_str)
154162

163+
@bubble_exception_str
155164
def apply_zone_file(self):
156165
"""
157166
Sync the dns seed list served by dns-seed with currently active Tanks.
158167
"""
159-
seeder = self.docker.containers.get("dns-seed")
168+
seeder = self.docker.containers.get(f"{self.docker_network}_{DNS_SEED_NAME}")
160169

161170
# Read the content from the generated zone file
162171
with open(self.config_dir / ZONE_FILE_NAME, "r") as f:
@@ -171,6 +180,7 @@ def apply_zone_file(self):
171180
# Reload that single zone only
172181
seeder.exec_run("rndc reload dns-seed")
173182

183+
@bubble_exception_str
174184
def connect_edges(self):
175185
for edge in self.graph.edges():
176186
(src, dst) = edge
@@ -180,6 +190,7 @@ def connect_edges(self):
180190
cmd = f"bitcoin-cli addpeeraddress {dst_ip} 18444"
181191
src_tank.exec(cmd=cmd, user="bitcoin")
182192

193+
@bubble_exception_str
183194
def docker_compose_build_up(self):
184195
command = ["docker-compose", "-p", self.docker_network, "up", "-d", "--build"]
185196
try:
@@ -196,6 +207,7 @@ def docker_compose_build_up(self):
196207
f"An error occurred while executing `{' '.join(command)}` in {self.config_dir}: {e}"
197208
)
198209

210+
@bubble_exception_str
199211
def docker_compose_up(self):
200212
command = ["docker-compose", "-p", self.docker_network, "up", "-d"]
201213
try:
@@ -212,6 +224,7 @@ def docker_compose_up(self):
212224
f"An error occurred while executing `{' '.join(command)}` in {self.config_dir}: {e}"
213225
)
214226

227+
@bubble_exception_str
215228
def docker_compose_down(self):
216229
command = ["docker-compose", "down"]
217230
try:
@@ -228,6 +241,7 @@ def docker_compose_down(self):
228241
f"An error occurred while executing `{' '.join(command)}` in {self.config_dir}: {e}"
229242
)
230243

244+
@bubble_exception_str
231245
def write_docker_compose(self, dns=True):
232246
compose = {
233247
"version": "3.8",
@@ -270,6 +284,7 @@ def write_docker_compose(self, dns=True):
270284
f"An error occurred while writing to {docker_compose_path}: {e}"
271285
)
272286

287+
@bubble_exception_str
273288
def write_prometheus_config(self):
274289
config = {
275290
"global": {"scrape_interval": "15s"},

0 commit comments

Comments
 (0)