|
| 1 | +#! /usr/bin/env python3 |
| 2 | +""" |
| 3 | +Starts 2 near validators and 2 mpc nodes. |
| 4 | +Deploys mpc contract. |
| 5 | +Deploys a test contract with a function that makes parallel sign calls. |
| 6 | +Calls the test function and ensures a successful response. |
| 7 | +""" |
| 8 | + |
| 9 | +import sys |
| 10 | +import base64 |
| 11 | +import pytest |
| 12 | +import pathlib |
| 13 | +import time |
| 14 | +from utils import load_binary_file |
| 15 | +from dataclasses import dataclass |
| 16 | + |
| 17 | +sys.path.append(str(pathlib.Path(__file__).resolve().parents[1])) |
| 18 | +from common_lib import shared, contracts, constants |
| 19 | +from common_lib.shared import metrics |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class NodeMetrics: |
| 24 | + queue_size: int |
| 25 | + requests_indexed: int |
| 26 | + responses_indexed: int |
| 27 | + matching_responses_indexed: int |
| 28 | + |
| 29 | + def __sub__(self, other): |
| 30 | + if isinstance(other, NodeMetrics): |
| 31 | + res = NodeMetrics(0, 0, 0, 0) |
| 32 | + res.queue_size = self.queue_size - other.queue_size |
| 33 | + res.requests_indexed = self.requests_indexed - other.requests_indexed |
| 34 | + res.responses_indexed = self.responses_indexed - other.responses_indexed |
| 35 | + res.matching_responses_indexed = ( |
| 36 | + self.matching_responses_indexed - other.matching_responses_indexed |
| 37 | + ) |
| 38 | + return res |
| 39 | + |
| 40 | + |
| 41 | +def load_parallel_sign_contract() -> bytearray: |
| 42 | + """ |
| 43 | + Returns test contract for parallel sign |
| 44 | + """ |
| 45 | + return load_binary_file(contracts.PARALLEL_CONTRACT_BINARY_PATH) |
| 46 | + |
| 47 | + |
| 48 | +def get_metric_value_for_node(cluster, metric_name: str, node_id: int): |
| 49 | + result = cluster.get_int_metric_value_for_node(metric_name, node_id) |
| 50 | + return result if result is not None else 0 |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("num_parallel_requests", [6]) |
| 54 | +@pytest.mark.no_atexit_cleanup |
| 55 | +def test_parallel_sign_calls( |
| 56 | + compile_parallel_contract, num_parallel_requests, shared_cluster: shared.MpcCluster |
| 57 | +): |
| 58 | + assert num_parallel_requests % 3 == 0, "expected number multiple of 3" |
| 59 | + # start cluster and deploy mpc contract |
| 60 | + contract = load_parallel_sign_contract() |
| 61 | + |
| 62 | + print("Deploying parallel contract") |
| 63 | + shared_cluster.deploy_secondary_contract(contract) |
| 64 | + |
| 65 | + started = time.time() |
| 66 | + while True: |
| 67 | + assert time.time() - started < constants.SHORT_TIMEOUT, "Waiting for metrics" |
| 68 | + initial_node_metrics = get_node_metrics_all_nodes(shared_cluster) |
| 69 | + initial_queue_attempts = get_queue_attemps_generated(shared_cluster) |
| 70 | + if sum(node_metric.queue_size for node_metric in initial_node_metrics) == 0: |
| 71 | + break |
| 72 | + time.sleep(1) |
| 73 | + |
| 74 | + print("Making parallel request calls") |
| 75 | + # call `parallel_sign` and verify that it returns successfully |
| 76 | + res = shared_cluster.make_function_call_on_secondary_contract( |
| 77 | + function_name="make_parallel_sign_calls", |
| 78 | + args={ |
| 79 | + "target_contract": shared_cluster.mpc_contract_account(), |
| 80 | + "ecdsa_calls_by_domain": {0: num_parallel_requests // 3}, |
| 81 | + "eddsa_calls_by_domain": {1: num_parallel_requests // 3}, |
| 82 | + "ckd_calls_by_domain": {2: num_parallel_requests // 3}, |
| 83 | + "seed": 23, |
| 84 | + }, |
| 85 | + ) |
| 86 | + |
| 87 | + # check the return value |
| 88 | + assert ( |
| 89 | + "result" in res |
| 90 | + and "status" in res["result"] |
| 91 | + and "SuccessValue" in res["result"]["status"] |
| 92 | + ), res |
| 93 | + encoded_value = res["result"]["status"]["SuccessValue"] |
| 94 | + decoded_value = base64.b64decode(encoded_value).decode("utf-8") |
| 95 | + assert int(decoded_value) == num_parallel_requests |
| 96 | + |
| 97 | + target_metrics = NodeMetrics(0, *[num_parallel_requests] * 3) |
| 98 | + # check metrics to make sure signature requests are handled properly. |
| 99 | + started = time.time() |
| 100 | + while True: |
| 101 | + assert time.time() - started < constants.SHORT_TIMEOUT, "Waiting for metrics" |
| 102 | + metrics_good = True |
| 103 | + current_metrics = get_node_metrics_all_nodes(shared_cluster) |
| 104 | + for i in range(len(shared_cluster.mpc_nodes)): |
| 105 | + if current_metrics[i] - initial_node_metrics[i] != target_metrics: |
| 106 | + metrics_good = False |
| 107 | + led_requests = ( |
| 108 | + get_queue_attemps_generated(shared_cluster) - initial_queue_attempts |
| 109 | + ) |
| 110 | + |
| 111 | + print(f"led_signatures={led_requests}") |
| 112 | + if led_requests != num_parallel_requests: |
| 113 | + metrics_good = False |
| 114 | + if metrics_good: |
| 115 | + break |
| 116 | + time.sleep(1) |
| 117 | + print( |
| 118 | + "All requests and responses indexed, all requests had exactly one leader, and signature/ckd queue is empty on all nodes. All Done." |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +def get_node_metrics_all_nodes(cluster: shared.MpcCluster): |
| 123 | + number_nodes = len(cluster.mpc_nodes) |
| 124 | + |
| 125 | + network_metrics = [NodeMetrics(0, 0, 0, 0) for _ in range(number_nodes)] |
| 126 | + for i in range(len(cluster.mpc_nodes)): |
| 127 | + network_metrics[i].queue_size = get_metric_value_for_node( |
| 128 | + cluster, "mpc_pending_signatures_queue_size", i |
| 129 | + ) |
| 130 | + network_metrics[i].requests_indexed = get_metric_value_for_node( |
| 131 | + cluster, "mpc_pending_signatures_queue_requests_indexed", i |
| 132 | + ) |
| 133 | + network_metrics[i].responses_indexed = get_metric_value_for_node( |
| 134 | + cluster, "mpc_pending_signatures_queue_responses_indexed", i |
| 135 | + ) |
| 136 | + network_metrics[i].matching_responses_indexed = get_metric_value_for_node( |
| 137 | + cluster, "mpc_pending_signatures_queue_matching_responses_indexed", i |
| 138 | + ) |
| 139 | + |
| 140 | + network_metrics[i].queue_size += get_metric_value_for_node( |
| 141 | + cluster, "mpc_pending_ckds_queue_size", i |
| 142 | + ) |
| 143 | + network_metrics[i].requests_indexed += get_metric_value_for_node( |
| 144 | + cluster, "mpc_pending_ckds_queue_requests_indexed", i |
| 145 | + ) |
| 146 | + network_metrics[i].responses_indexed += get_metric_value_for_node( |
| 147 | + cluster, "mpc_pending_ckds_queue_responses_indexed", i |
| 148 | + ) |
| 149 | + network_metrics[i].matching_responses_indexed += get_metric_value_for_node( |
| 150 | + cluster, "mpc_pending_ckds_queue_matching_responses_indexed", i |
| 151 | + ) |
| 152 | + print( |
| 153 | + f"Node {i}: queue_size={network_metrics[i].queue_size}, requests_indexed={network_metrics[i].requests_indexed}, responses_indexed={network_metrics[i].responses_indexed}, matching_responses_indexed={network_metrics[i].matching_responses_indexed}" |
| 154 | + ) |
| 155 | + return network_metrics |
| 156 | + |
| 157 | + |
| 158 | +def get_queue_attemps_generated(cluster: shared.MpcCluster): |
| 159 | + led_requests = cluster.get_int_metric_value( |
| 160 | + metrics.IntMetricName.MPC_PENDING_SIGNATURES_QUEUE_ATTEMPTS_GENERATED |
| 161 | + ) + cluster.get_int_metric_value( |
| 162 | + metrics.IntMetricName.MPC_PENDING_CKDS_QUEUE_ATTEMPTS_GENERATED |
| 163 | + ) |
| 164 | + return sum(a for a in led_requests if a is not None) |
0 commit comments