Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mismatch of key name in archive format json file when exporting and importing(exporting: compilation_units, importing: compilations) #518

Open
acorn421 opened this issue Nov 14, 2023 · 1 comment

Comments

@acorn421
Copy link

A function for exporting CryticCompile object as an archive format to JSON file.

def export(self, **kwargs: str) -> List[str]:
"""Export to json.
The json format can be crytic-compile, solc or truffle.
The type must be specified in the kwargs with "export_format"
Args:
**kwargs: optional arguments. Used: "export_format"
Raises:
ValueError: Incorrect type
Returns:
List[str]: List of the filenames generated
"""
export_format = kwargs.get("export_format", None)
if export_format is None:
return export_to_standard(self, **kwargs)
if export_format not in PLATFORMS_EXPORT:
raise ValueError("Export format unknown")
return PLATFORMS_EXPORT[export_format](self, **kwargs)

The above function calls the function that constructs a standard (archive) format object.

output = {
"compilation_units": compilation_units,
"package": crytic_compile.package,
"working_dir": str(crytic_compile.working_dir),
"type": int(crytic_compile.platform.platform_type_used),
"unit_tests": crytic_compile.platform.guessed_tests(),
"crytic_version": "0.0.2",
}

We use the "compilation_units" as a key for a dictionary object.

However, when you look at a function importing CryticCompile object from a JSON file.

def import_archive_compilations(compiled_archive: Union[str, Dict]) -> List["CryticCompile"]:
"""Import from an archive. compiled_archive is either a json file or the loaded dictionary
The dictionary myst contain the "compilations" keyword
Args:
compiled_archive: Union[str, Dict]: list of archive to import
Raises:
ValueError: The import did not worked
Returns:
[CryticCompile]: List of crytic compile object
"""
# If the argument is a string, it is likely a filepath, load the archive.
if isinstance(compiled_archive, str):
with open(compiled_archive, encoding="utf8") as file:
compiled_archive = json.load(file)
# Verify the compiled archive is of the correct form
if not isinstance(compiled_archive, dict) or "compilations" not in compiled_archive:
raise ValueError("Cannot import compiled archive, invalid format.")
return [CryticCompile(archive) for archive in compiled_archive["compilations"]]

That function has a check routine as below.

if not isinstance(compiled_archive, dict) or "compilations" not in compiled_archive:
raise ValueError("Cannot import compiled archive, invalid format.")
return [CryticCompile(archive) for archive in compiled_archive["compilations"]]

It checks whether the dictionary object has a "compilations" key, and we can't pass this check.

@acorn421
Copy link
Author

My guess is that it would be easily solved by unifying with one of the keys.
When I tested it locally, I found the import feature works fine.

If I'm not mistaken, I'll request a pull request for this issue.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant