Skip to content

Commit

Permalink
fix: rewrite keystore path related logic
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzac committed Aug 8, 2024
1 parent 12f7e0a commit 8daed20
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
1 change: 0 additions & 1 deletion src/node/docker_mananger.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def _log_container_output(self, container, log_path):
with open(log_path, "wb+") as log_file:
for chunk in container.logs(stream=True):
log_file.write(chunk)
logger.debug(f"DOCKER LOG {chunk}")

def generate_ports(self, base_port=None, count=5):
if base_port is None:
Expand Down
16 changes: 5 additions & 11 deletions src/node/waku_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def start(self, wait_for_node_sec=20, **kwargs):

del default_args["rln-creds-id"]
del default_args["rln-creds-source"]
del default_args["rln-keystore-prefix"]

if rln_creds_set:
rln_credential_store_ready(keystore_path)
Expand Down Expand Up @@ -197,13 +198,6 @@ def register_rln(self, **kwargs):
rln_args, rln_creds_set, keystore_path = self.parse_rln_credentials(default_args, True)

if rln_creds_set:
if rln_credential_store_ready(keystore_path, True):
try:
subprocess.call(["rm", "-f", f"{keystore_path}"])
except Exception as ex:
logger.error(f"Keystore removal before RLN registration failed {ex}")
raise

self._container = self._docker_manager.start_container(
self._docker_manager.image, self._ports, rln_args, self._log_path, self._ext_ip, self._volumes
)
Expand Down Expand Up @@ -443,7 +437,7 @@ def parse_rln_credentials(self, default_args, is_registration):

eth_private_key = select_private_key(imported_creds, selected_id)

current_working_directory = os.getcwd()
cwd = os.getcwd()

if self.is_nwaku():
if is_registration:
Expand Down Expand Up @@ -479,12 +473,12 @@ def parse_rln_credentials(self, default_args, is_registration):
}
)

keystore_path = current_working_directory + "/keystore_" + selected_id + "/keystore.json"
keystore_path = cwd + "/keystore_" + default_args["rln-keystore-prefix"] + "_" + selected_id + "/keystore.json"

self._volumes.extend(
[
current_working_directory + "/rln_tree_" + selected_id + ":/etc/rln_tree",
current_working_directory + "/keystore_" + selected_id + ":/keystore",
cwd + "/rln_tree_" + default_args["rln-keystore-prefix"] + "_" + selected_id + ":/etc/rln_tree",
cwd + "/keystore_" + default_args["rln-keystore-prefix"] + "_" + selected_id + ":/keystore",
]
)

Expand Down
51 changes: 27 additions & 24 deletions src/steps/rln.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import inspect
import random
import string

import pytest
import allure

Expand All @@ -23,23 +26,23 @@ class StepsRLN(StepsCommon):
multiaddr_list = []
lightpush_nodes = []

@pytest.fixture(scope="function")
def register_main_rln_relay_nodes(self, request):
logger.debug(f"Running fixture setup: {inspect.currentframe().f_code.co_name}")
self.register_rln_single_node(rln_creds_source=RLN_CREDENTIALS, rln_creds_id="1")
self.register_rln_single_node(rln_creds_source=RLN_CREDENTIALS, rln_creds_id="2")
@allure.step
def generate_new_keystore_prefixes(self, count=2):
new_prefixes = []
for _ in range(count):
new_prefixes.append("".join(random.choices(string.ascii_lowercase, k=4)))

@pytest.fixture(scope="function")
def register_optional_rln_relay_nodes(self, request):
logger.debug(f"Running fixture setup: {inspect.currentframe().f_code.co_name}")
self.register_rln_single_node(rln_creds_source=RLN_CREDENTIALS, rln_creds_id="3")
self.register_rln_single_node(rln_creds_source=RLN_CREDENTIALS, rln_creds_id="4")
self.register_rln_single_node(rln_creds_source=RLN_CREDENTIALS, rln_creds_id="5")
return new_prefixes

@allure.step
def setup_main_rln_relay_nodes(self, **kwargs):
self.setup_first_rln_relay_node(**kwargs)
self.setup_second_rln_relay_node(**kwargs)
def register_rln_relay_nodes(self, prefixes):
for i in range(len(prefixes)):
self.register_rln_single_node(prefix=prefixes[i], rln_creds_source=RLN_CREDENTIALS, rln_creds_id=f"{i+1}")

@allure.step
def setup_main_rln_relay_nodes(self, prefixes, **kwargs):
self.setup_first_rln_relay_node(rln_keystore_prefix=prefixes[0], **kwargs)
self.setup_second_rln_relay_node(rln_keystore_prefix=prefixes[1], **kwargs)

@allure.step
def setup_first_rln_relay_node(self, **kwargs):
Expand Down Expand Up @@ -73,7 +76,7 @@ def setup_second_rln_relay_node(self, **kwargs):
self.main_nodes.extend([self.node2])

@allure.step
def setup_optional_rln_relay_nodes(self, **kwargs):
def setup_optional_rln_relay_nodes(self, prefixes, **kwargs):
if ADDITIONAL_NODES:
nodes = [node.strip() for node in ADDITIONAL_NODES.split(",")]
else:
Expand All @@ -89,13 +92,13 @@ def setup_optional_rln_relay_nodes(self, **kwargs):
rln_creds_source=RLN_CREDENTIALS,
rln_creds_id=f"{index + 3}",
rln_relay_membership_index="1",
**kwargs,
rln_keystore_prefix=prefixes[index] ** kwargs,
)
self.add_node_peer(node, [self.multiaddr_with_id])
self.optional_nodes.append(node)

@allure.step
def setup_second_rln_lightpush_node(self, relay="true", **kwargs):
def setup_second_rln_lightpush_node(self, prefixes, relay="true", **kwargs):
self.light_push_node2 = WakuNode(NODE_2, f"lightpush_node2_{self.test_id}")
self.light_push_node2.start(
relay=relay,
Expand All @@ -105,23 +108,23 @@ def setup_second_rln_lightpush_node(self, relay="true", **kwargs):
rln_creds_source=RLN_CREDENTIALS,
rln_creds_id="2",
rln_relay_membership_index="1",
**kwargs,
rln_keystore_prefix=prefixes[1] ** kwargs,
)
if relay == "true":
self.main_nodes.extend([self.light_push_node2])
self.lightpush_nodes.extend([self.light_push_node2])
self.add_node_peer(self.light_push_node2, self.multiaddr_list)

@allure.step
def register_rln_single_node(self, **kwargs):
def register_rln_single_node(self, prefix="", **kwargs):
logger.debug("Registering RLN credentials for single node")
self.node1 = WakuNode(DEFAULT_NWAKU, f"node1_{gen_step_id()}")
self.node1.register_rln(rln_creds_source=kwargs["rln_creds_source"], rln_creds_id=kwargs["rln_creds_id"])
self.node = WakuNode(DEFAULT_NWAKU, f"node_{gen_step_id()}")
self.node.register_rln(rln_keystore_prefix=prefix, rln_creds_source=kwargs["rln_creds_source"], rln_creds_id=kwargs["rln_creds_id"])

@allure.step
def check_rln_registration(self, key_id):
current_working_directory = os.getcwd()
creds_file_path = f"{current_working_directory}/keystore_{key_id}/keystore.json"
def check_rln_registration(self, prefix, key_id):
cwd = os.getcwd()
creds_file_path = f"{cwd}/keystore_{prefix}_{key_id}/keystore.json"
try:
rln_credential_store_ready(creds_file_path)
except Exception as ex:
Expand Down
20 changes: 15 additions & 5 deletions tests/relay/test_rln.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@pytest.mark.skipif("go-waku" in (NODE_1 + NODE_2), reason="Test works only with nwaku")
class TestRelayRLN(StepsRLN, StepsRelay):
SAMPLE_INPUTS_RLN = SAMPLE_INPUTS + SAMPLE_INPUTS + SAMPLE_INPUTS

keystore_prefixes = []
# @pytest.mark.usefixtures("register_main_rln_relay_nodes")
# def test_valid_payloads_lightpush_at_spam_rate(self):
# message_limit = 1
Expand Down Expand Up @@ -130,12 +130,17 @@ class TestRelayRLN(StepsRLN, StepsRelay):
# assert not failed_payloads, f"Payloads failed: {failed_payloads}"

@pytest.mark.timeout(600)
@pytest.mark.usefixtures("register_main_rln_relay_nodes")
def test_valid_payloads_dynamic_at_spam_rate(self):
message_limit = 100
epoch_sec = 600
self.keystore_prefixes = self.generate_new_keystore_prefixes()
self.register_rln_relay_nodes(self.keystore_prefixes)
self.setup_main_rln_relay_nodes(
rln_relay_user_message_limit=message_limit, rln_relay_epoch_sec=epoch_sec, rln_relay_dynamic="true", wait_for_node_sec=600
prefixes=self.keystore_prefixes,
rln_relay_user_message_limit=message_limit,
rln_relay_epoch_sec=epoch_sec,
rln_relay_dynamic="true",
wait_for_node_sec=600,
)
self.subscribe_main_relay_nodes()
start = math.trunc(time())
Expand All @@ -152,11 +157,16 @@ def test_valid_payloads_dynamic_at_spam_rate(self):
assert "RLN validation failed" or "NonceLimitReached" in str(e)

@pytest.mark.timeout(600)
@pytest.mark.usefixtures("register_main_rln_relay_nodes")
def test_valid_payloads_dynamic_at_slow_rate(self):
message_limit = 100
self.keystore_prefixes = self.generate_new_keystore_prefixes()
self.register_rln_relay_nodes(self.keystore_prefixes)
self.setup_main_rln_relay_nodes(
rln_relay_user_message_limit=message_limit, rln_relay_epoch_sec=600, rln_relay_dynamic="true", wait_for_node_sec=600
prefixes=self.keystore_prefixes,
rln_relay_user_message_limit=message_limit,
rln_relay_epoch_sec=600,
rln_relay_dynamic="true",
wait_for_node_sec=600,
)
self.subscribe_main_relay_nodes()
failed_payloads = []
Expand Down

0 comments on commit 8daed20

Please sign in to comment.