Skip to content

Commit

Permalink
Improved the error handling in parsing_utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanMadzharov committed Oct 17, 2024
1 parent 0f996c1 commit 7e86caf
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions hydra/garaga/starknet/groth16_contract_generator/parsing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
)


class KeyPatternNotFound(Exception):
def __init__(self, key_patterns):
super().__init__(f"No key found with patterns {key_patterns}")
self.key_patterns = key_patterns


def iterate_nested_dict(d):
for key, value in d.items():
if isinstance(value, dict):
Expand All @@ -49,7 +55,7 @@ def find_item_from_key_patterns(data: dict, key_patterns: List[str]) -> Any:
if best_match is not None:
return best_match
else:
raise ValueError(f"No key found with patterns {key_patterns}")
raise KeyPatternNotFound(key_patterns)


def try_parse_g1_point_from_key(
Expand Down Expand Up @@ -151,7 +157,7 @@ def try_parse_g2_point(point: Any, curve_id: CurveID = None) -> G2Point:
def try_guessing_curve_id_from_json(data: dict) -> CurveID:
try:
curve_id = CurveID.from_str(find_item_from_key_patterns(data, ["curve"]))
except (ValueError, KeyError):
except (ValueError, KeyError, KeyPatternNotFound):
# Try guessing the curve id from the bit size of the first found integer in the json.
x = None
for value in iterate_nested_dict(data):
Expand Down Expand Up @@ -220,7 +226,7 @@ def from_dict(data: dict) -> "Groth16VerifyingKey":
for point in find_item_from_key_patterns(verifying_key, ["ic"])
],
)
except ValueError:
except (ValueError, KeyPatternNotFound):
# Gnark case.
g1_points = find_item_from_key_patterns(verifying_key, ["g1"])
g2_points = find_item_from_key_patterns(verifying_key, ["g2"])
Expand Down Expand Up @@ -318,7 +324,7 @@ def from_dict(
curve_id = try_guessing_curve_id_from_json(data)
try:
proof = find_item_from_key_patterns(data, ["proof"])
except ValueError:
except KeyPatternNotFound:
proof = data

try:
Expand All @@ -335,6 +341,8 @@ def from_dict(
pass
except KeyError:
pass
except KeyPatternNotFound:
pass
except Exception as e:
print(f"Error: {e}")
raise e
Expand All @@ -347,7 +355,11 @@ def from_dict(
else:
raise ValueError(f"Invalid public inputs format: {public_inputs}")
else:
public_inputs = find_item_from_key_patterns(data, ["public"])
try:
public_inputs = find_item_from_key_patterns(data, ["public"])
except KeyPatternNotFound as e:
print(f"Error: {e}")
raise e
return Groth16Proof(
a=try_parse_g1_point_from_key(proof, ["a"], curve_id),
b=try_parse_g2_point_from_key(proof, ["b"], curve_id),
Expand Down

0 comments on commit 7e86caf

Please sign in to comment.