Skip to content

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

Open
@acorn421

Description

@acorn421

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions