Skip to content

Commit 779690c

Browse files
refactor: reduce ibc relayer resource declarations
1 parent 17d77d1 commit 779690c

File tree

2 files changed

+150
-125
lines changed

2 files changed

+150
-125
lines changed

Tiltfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def merge_dicts(base, updates):
2424
# so that if we merge something that passes E2E tests but was not manually validated by the developer, the developer
2525
# environment is not broken for future engineers.
2626

27+
load("./tiltfiles/ibc.tilt", "default_ibc_validator_configs")
28+
2729
# Create a localnet config file from defaults, and if a default configuration doesn't exist, populate it with default values
2830
localnet_config_path = "localnet_config.yaml"
2931
localnet_config_defaults = {
@@ -96,9 +98,7 @@ localnet_config_defaults = {
9698

9799
"ibc": {
98100
"enabled": False,
99-
"relay_pairs_enabled": {
100-
"pocket-agoriclocal": False,
101-
}
101+
"validator_configs": default_ibc_validator_configs,
102102
}
103103
}
104104

tiltfiles/ibc.tilt

Lines changed: 147 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
11
load("ext://helm_resource", "helm_resource", "helm_repo")
22

3+
pocket_chain_id = "pocket"
4+
pocket_validator_resource_name = "validator"
5+
6+
default_context_path = os.path.join(".", "localnet", "kubernetes")
37
ibc_relayer_dockerfile_path = os.path.join(".", "localnet", "dockerfiles", "ibc-relayer.dockerfile")
4-
agoric_dockerfile_path = os.path.join(".", "localnet", "dockerfiles", "agoric-validator.dockerfile")
8+
ibc_relayer_config = {
9+
"dockerfile_path": ibc_relayer_dockerfile_path,
10+
}
11+
12+
default_ibc_validator_configs = {
13+
"agoric": {
14+
"enabled": False,
15+
"chain_id": "agoriclocal",
16+
"values_path": os.path.join(".", "localnet", "kubernetes", "values-agoric.yaml"),
17+
"tilt_ui_name": "Agoric Validator",
18+
"chart_name": "agoric-validator",
19+
"dockerfile_path": os.path.join(".", "localnet", "dockerfiles", "agoric-validator.dockerfile"),
20+
# TODO_IMPROVE: can we consolidate this with the chart_name?
21+
"image_name": "agoric",
22+
"port_forwards": ["46657:26657", "11090:9090", "40009:40009", ]
23+
# NOTE: this chain ID is baked into the image genesis.json and is difficult to change.
24+
}
25+
}
26+
27+
28+
# NOTE: Starlark (tilt) doesn't support multiple kwargs; otherwise,
29+
# we could simply do `dict(**default_ibc_validator_configs, **ibc_config)`.
30+
def deep_merge(a, b):
31+
result = dict(a) # shallow copy of a
32+
33+
for key, b_value in b.items():
34+
a_value = a.get(key)
35+
36+
if type(a_value) == "dict" and type(b_value) == "dict":
37+
# Recurse into nested dicts
38+
result[key] = deep_merge(a_value, b_value)
39+
else:
40+
# b overrides a
41+
result[key] = b_value
42+
43+
return result
44+
545

646
# ⚠️ IBC Disabled Resource
747
# - Creates a Tilt resource that prints a message indicating IBC is disabled
@@ -16,111 +56,74 @@ def ibc_disabled_resource(reason):
1656
# - Loads IBC resources if enabled in config
1757
# - Otherwise, creates a disabled resource with instructions
1858
def check_and_load_ibc(chart_prefix, ibc_config):
19-
if ibc_config["enabled"]:
20-
load_ibc_resources(chart_prefix, ibc_config)
59+
if ibc_config.get("enabled", False):
60+
load_ibc_resources(chart_prefix, ibc_config["validator_configs"])
2161
else:
2262
ibc_disabled_resource(
2363
"Localnet IBC resources disabled. Set `ibc.enabled` to `true` in `localnet_config.yaml` to enable them.")
2464

2565

26-
# Check and Load Agoric
27-
# - If the pocket-agoriclocal relay pair is enabled:
28-
# - Builds Agoric docker image
29-
# - Deploys Agoric via Helm/k8s
30-
# - Sets up Agoric k8s resource with port-forwards
31-
def check_and_load_agoric(chart_prefix, ibc_config):
32-
if ibc_config["relay_pairs_enabled"].get("pocket-agoriclocal", False):
33-
docker_build(
34-
"agoric",
35-
context=os.path.join(".", "localnet", "kubernetes"), # needs any folder, less context is faster
36-
dockerfile=agoric_dockerfile_path
37-
)
38-
39-
# Deploy helm chart
40-
helm_resource(
41-
"agoric-validator",
42-
chart_prefix + "agoric-validator",
43-
flags=[
44-
"--values=./localnet/kubernetes/values-agoric.yaml",
45-
],
46-
image_deps=["agoric"],
47-
image_keys=[("image.repository", "image.tag")],
48-
)
66+
# Load IBC Resources
67+
# - Loads IBC validator resources if enabled
68+
# - Builds ibc-relayer docker image if any validator configs are enabled
69+
# - Iterates through all enabled validator configs and loads relayer resources for each
70+
def load_ibc_resources(chart_prefix, validator_configs):
71+
has_validator_configs = len(validator_configs.keys()) != 0
72+
has_enabled_validator_configs = False
73+
if has_validator_configs:
74+
for validator_config in validator_configs.values():
75+
if validator_config.get("enabled", False):
76+
has_enabled_validator_configs = True
77+
break
78+
79+
if not validator_configs or not has_validator_configs or not has_enabled_validator_configs:
80+
ibc_disabled_resource(
81+
"Localnet IBC resources disabled. Set `ibc.validator_configs.<network_name>.enabled` to `true` in `localnet_config.yaml` to enable them.")
82+
return
4983

50-
# Update k8s resource with new name, lebels, and port-forwards
51-
k8s_resource(
52-
"agoric-validator",
53-
# TODO_IN_THIS_COMMIT: consider extracting to const
54-
new_name="Agoriclocal Validator",
55-
labels=["IBC"],
56-
port_forwards=[
57-
"46657:26657",
58-
"11090:9090",
59-
"40009:40009"
60-
],
61-
)
84+
# Merge default IBC validator configs with the user-provided ones; user-provided configs take precedence.
85+
merged_ibc_validator_configs = deep_merge(default_ibc_validator_configs, validator_configs)
6286

87+
docker_build(
88+
"ibc-relayer",
89+
context=os.path.join(".", "localnet", "ibc-relayer"), # needs any folder, doesn't matter
90+
dockerfile=ibc_relayer_dockerfile_path)
6391

64-
# Pair Pretty Printer
65-
# - Converts a pair string like "pocket-agoriclocal" to "Pocket->Agoriclocal"
66-
def pair_pretty(pair):
67-
chain_a_id, chain_b_id = pair.split("-")
68-
if chain_a_id == "pocket":
69-
chain_a_id = "pokt"
92+
load_ibc_relayer_server_resources(chart_prefix, merged_ibc_validator_configs)
7093

71-
if chain_b_id == "pocket":
72-
chain_b_id = "pokt"
94+
for ibc_network_name, ibc_validator_config in merged_ibc_validator_configs.items():
95+
load_ibc_relayer_setup_resources(chart_prefix, ibc_network_name, ibc_validator_config)
96+
check_and_load_ibc_validator(chart_prefix, ibc_validator_config)
7397

74-
return chain_a_id.title() + "->" + chain_b_id.title()
7598

99+
# Load IBC relayer server resources sets up the hermes relayer for
100+
# pairs of pocket with each enabled IBC validator.
101+
def load_ibc_relayer_server_resources(chart_prefix, ibc_validator_configs):
102+
resource_deps = [
103+
pocket_validator_resource_name,
104+
]
76105

77-
# TODO_IN_THIS_COMMIT: update comment...
78-
# Load IBC Relayer Server Resources
79-
# - Sets up the server-side relayer for a given pair
80-
# - Adds appropriate resource dependencies based on which chain is "pocket"
81-
def load_ibc_relayer_server_resources(chart_prefix, pairs):
82-
resource_deps = []
83106
chain_config_values_flags = []
84-
# DEV_NOTE: starlark (tilt) doesn't support python sets.
85-
chain_id_set = {}
86-
for pair in pairs:
87-
chain_a_id, chain_b_id = pair.split("-")
88-
chain_id_set[chain_a_id] = True
89-
chain_id_set[chain_b_id] = True
90-
91-
# TODO_IN_THIS_COMMIT: extract to constant
92-
if chain_a_id == "pocket":
93-
resource_deps = [
94-
"validator",
95-
chain_b_id.title() + " Validator",
96-
]
97-
elif chain_b_id == "pocket":
98-
resource_deps = [
99-
# TODO_IN_THIS_COMMIT: extract to constant
100-
"validator",
101-
chain_a_id.title() + " Validator",
102-
]
103-
else:
104-
resource_deps = [
105-
chain_a_id.title() + "Validator",
106-
chain_b_id.title() + "Validator",
107-
# TODO_IN_THIS_COMMIT: extract to constant
108-
"IBC Setup",
109-
]
110-
111-
chain_ids = chain_id_set.keys()
112-
for chain_id in chain_ids:
107+
for network_name, ibc_validator_config in ibc_validator_configs.items():
108+
chain_id = ibc_validator_config["chain_id"]
109+
tilt_ui_name = "Pokt->" + chain_id.title()
110+
ibc_setup_resource_name = "🏗️ " + tilt_ui_name
113111
chain_config_values_flags.append(
114112
"--values=" +
115113
os.path.join("localnet", "kubernetes", "values-ibc-relayer-config-" + chain_id + ".yaml"))
116114

115+
validator_resource_name = ibc_validator_config.get("tilt_ui_name", chain_id.title() + " Validator")
116+
resource_deps.append(validator_resource_name)
117+
resource_deps.append(ibc_setup_resource_name)
118+
117119
# Deploy helm chart
118120
helm_resource(
119121
"ibc-relayer",
120122
chart_prefix + "ibc-relayer",
121123
flags=[
122124
"--values=" + os.path.join("localnet", "kubernetes", "values-ibc-relayer-common.yaml"),
123125
"--values=" + os.path.join("localnet", "kubernetes", "values-ibc-relayer-daemon.yaml"),
126+
"--values=" + os.path.join("localnet", "kubernetes", "values-ibc-relayer-config-" + pocket_chain_id + ".yaml"),
124127
] + chain_config_values_flags,
125128
resource_deps=resource_deps,
126129
image_deps=["ibc-relayer"],
@@ -139,36 +142,27 @@ def load_ibc_relayer_server_resources(chart_prefix, pairs):
139142
# Load IBC Relayer Setup Resource
140143
# - Sets up the CLI/setup resource for a given pair
141144
# - Adds appropriate resource dependencies based on which chain is "pocket"
142-
def load_ibc_relayer_setup_resources(chart_prefix, pair):
143-
# TODO_IN_THIS_COMMIT: rename to chain_one_id and chain_two_id to disambiguate with a, b, c, d used elsewhere.
144-
chain_a_id, chain_b_id = pair.split("-")
145-
if chain_a_id == "pocket":
146-
resource_deps = [
147-
"validator",
148-
chain_b_id.title() + " Validator",
149-
]
150-
elif chain_b_id == "pocket":
151-
resource_deps = [
152-
"validator",
153-
chain_a_id.title() + " Validator",
154-
]
155-
else:
156-
resource_deps = [
157-
chain_a_id.title() + " Validator",
158-
chain_b_id.title() + " Validator",
159-
]
145+
def load_ibc_relayer_setup_resources(chart_prefix, network_name, validator_config):
146+
chain_id = validator_config["chain_id"]
147+
tilt_ui_name = "Pokt->" + network_name.title()
148+
validator_resource_name = validator_config.get("tilt_ui_name", chain_id.title() + " Validator")
149+
resource_deps = [
150+
pocket_validator_resource_name,
151+
validator_resource_name,
152+
]
160153

161154
# Deploy helm chart
162155
helm_resource(
163-
"ibc-relayer-setup-" + pair,
156+
"ibc-relayer-setup-" + network_name,
164157
chart_prefix + "ibc-relayer",
165158
flags=[
166159
"--values=" + os.path.join("localnet", "kubernetes", "values-ibc-relayer-common.yaml"),
167160
"--values=" + os.path.join("localnet", "kubernetes", "values-ibc-relayer-cli.yaml"),
168-
"--values=" + os.path.join("localnet", "kubernetes", "values-ibc-relayer-config-" + chain_a_id + ".yaml"),
169-
"--values=" + os.path.join("localnet", "kubernetes", "values-ibc-relayer-config-" + chain_b_id + ".yaml"),
170-
"--set=chain_a_id=" + chain_a_id,
171-
"--set=chain_b_id=" + chain_b_id
161+
"--values=" + os.path.join("localnet", "kubernetes",
162+
"values-ibc-relayer-config-" + pocket_chain_id + ".yaml"),
163+
"--values=" + os.path.join("localnet", "kubernetes", "values-ibc-relayer-config-" + chain_id + ".yaml"),
164+
"--set=chain_a_id=" + pocket_chain_id,
165+
"--set=chain_b_id=" + chain_id
172166
],
173167
resource_deps=resource_deps,
174168
image_deps=["ibc-relayer"],
@@ -179,30 +173,61 @@ def load_ibc_relayer_setup_resources(chart_prefix, pair):
179173
# port-forwards, and the manual trigger mode.
180174
k8s_resource(
181175
# workload=release_name,
182-
"ibc-relayer-setup-" + pair,
183-
new_name="🏗️ " + pair_pretty(pair),
176+
"ibc-relayer-setup-" + network_name,
177+
new_name="🏗️ " + tilt_ui_name,
184178
labels=["IBC_Pairs_Setup"],
185179
trigger_mode=TRIGGER_MODE_MANUAL,
186180
resource_deps=resource_deps,
187181
)
188182

189183

190-
# Load IBC Resources
191-
# - Loads Agoric resources if enabled
192-
# - Builds ibc-relayer docker image if any relay pairs are enabled
193-
# - Iterates through all enabled relay pairs and loads relayer resources for each
194-
def load_ibc_resources(chart_prefix, ibc_config):
195-
check_and_load_agoric(chart_prefix, ibc_config)
184+
# Check and load the given IBC validator
185+
# - If the given validator config is enabled:
186+
# - Builds validator docker image
187+
# - Deploys up validator k8s resource with port-forwards
188+
def check_and_load_ibc_validator(chart_prefix, validator_config):
189+
if not validator_config.get("enabled", False):
190+
return
191+
192+
image_deps_and_keys = {}
193+
if validator_config.get("dockerfile_path", False):
194+
image_name = validator_config["image_name"]
195+
context_path = validator_config.get("context", default_context_path)
196+
dockerfile_path = validator_config.get("dockerfile_path", None)
197+
198+
image_deps_and_keys = {
199+
"image_deps": [image_name],
200+
"image_keys": [("image.repository", "image.tag")],
201+
}
196202

197-
if len(ibc_config["relay_pairs_enabled"]) > 0:
198203
docker_build(
199-
"ibc-relayer",
200-
context=os.path.join(".", "localnet", "ibc-relayer"), # needs any folder, doesn't matter
201-
dockerfile=ibc_relayer_dockerfile_path)
204+
image_name,
205+
context=context_path,
206+
dockerfile=dockerfile_path
207+
)
202208

203-
pairs = [key for (key, val) in ibc_config["relay_pairs_enabled"].items() if val == True]
204-
load_ibc_relayer_server_resources(chart_prefix, pairs)
209+
# TODO_IMPROVE: consider computing a default chart_name.
210+
chain_id = validator_config["chain_id"]
211+
chart_name = validator_config["chart_name"]
212+
values_path = validator_config["values_path"]
213+
port_forwards = validator_config["port_forwards"]
214+
# Tilt UI name defaults to the titlized chain ID plus "Validator" (e.g. "Agoriclocal Validator")
215+
tilt_ui_name = validator_config.get("tilt_ui_name", chain_id.title() + " Validator")
205216

206-
for pair, enabled in ibc_config["relay_pairs_enabled"].items():
207-
if enabled:
208-
load_ibc_relayer_setup_resources(chart_prefix, pair)
217+
# Deploy helm chart
218+
helm_resource(
219+
chart_name,
220+
chart_prefix + chart_name,
221+
flags=[
222+
"--values=" + values_path,
223+
],
224+
**image_deps_and_keys,
225+
)
226+
227+
# Update k8s resource with new name, lebels, and port-forwards
228+
k8s_resource(
229+
chart_name,
230+
new_name=tilt_ui_name,
231+
labels=["IBC"],
232+
port_forwards=port_forwards,
233+
)

0 commit comments

Comments
 (0)