Skip to content

Commit

Permalink
Deprecate cairo_project.toml in favour of Scarb.toml (#1922)
Browse files Browse the repository at this point in the history
Issues for docs, error messages and tests:
#1954 
#1957

### Checklist
- [x] Relevant issue is linked
- [x] Docs updated/issue for docs created
- [ ] Added relevant tests
- [x] checklist from #1737

---------

Co-authored-by: Wojciech Szymczyk <wojciech.szymczyk@swmansion.com>
  • Loading branch information
piotmag769 and THenry14 authored May 22, 2023
1 parent 1b6c874 commit ceed1b6
Show file tree
Hide file tree
Showing 139 changed files with 309 additions and 368 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ parameters:

scarb_version:
type: string
default: 0.1.0
default: 0.2.0

node_version:
type: string
Expand Down
30 changes: 23 additions & 7 deletions protostar/cairo/bindings/cairo_bindings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from pathlib import Path
from typing import Optional
from typing import Optional, Tuple
from dataclasses import dataclass
from contextlib import contextmanager

Expand All @@ -16,6 +16,7 @@ def ensure_output_path(output_path: Optional[Path]):


AvailableGas = Optional[int]
PackageName = str


@dataclass
Expand All @@ -27,28 +28,38 @@ class TestCollectorOutput:
def compile_starknet_contract_to_casm_from_path(
input_path: Path,
output_path: Optional[Path] = None,
cairo_path: Optional[list[Path]] = None,
linked_libraries: Optional[list[Tuple[Path, PackageName]]] = None,
) -> str:
ensure_output_path(output_path=output_path)
with handle_bindings_errors("compile_starknet_contract_to_casm_from_path"):
return cairo_python_bindings.compile_starknet_contract_to_casm_from_path( # pyright: ignore
str(input_path),
str(output_path) if output_path else None,
[str(path) for path in cairo_path] if cairo_path else None,
[
(str(package_path), package_name)
for package_path, package_name in linked_libraries
]
if linked_libraries
else None,
)


def compile_starknet_contract_to_sierra_from_path(
input_path: Path,
output_path: Optional[Path] = None,
cairo_path: Optional[list[Path]] = None,
linked_libraries: Optional[list[Tuple[Path, PackageName]]] = None,
) -> str:
ensure_output_path(output_path=output_path)
with handle_bindings_errors("compile_starknet_contract_to_sierra_from_path"):
return cairo_python_bindings.compile_starknet_contract_to_sierra_from_path( # pyright: ignore
str(input_path),
str(output_path) if output_path else None,
[str(path) for path in cairo_path] if cairo_path else None,
[
(str(package_path), package_name)
for package_path, package_name in linked_libraries
]
if linked_libraries
else None,
)


Expand Down Expand Up @@ -79,14 +90,19 @@ def compile_starknet_contract_sierra_to_casm_from_sierra_code(
def collect_tests(
input_path: Path,
output_path: Optional[Path] = None,
cairo_path: Optional[list[Path]] = None,
linked_libraries: Optional[list[Tuple[Path, PackageName]]] = None,
) -> TestCollectorOutput:
ensure_output_path(output_path=output_path)
with handle_bindings_errors("collect_tests"):
output = cairo_python_bindings.collect_tests( # pyright: ignore
str(input_path),
str(output_path) if output_path else None,
[str(path) for path in cairo_path] if cairo_path else None,
[
(str(package_path), package_name)
for package_path, package_name in linked_libraries
]
if linked_libraries
else None,
RUNNER_BUILTINS_TITLE_CASE + ["GasBuiltin"],
)
return TestCollectorOutput(sierra_output=output[0], collected_tests=output[1])
Expand Down
8 changes: 4 additions & 4 deletions protostar/cairo_testing/cairo1_test_collector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import List, Iterable
from typing import List, Iterable, Tuple

from starkware.cairo.lang.compiler.preprocessor.preprocessor_error import (
PreprocessorError,
Expand All @@ -16,11 +16,11 @@ class Cairo1TestCollectionException(Exception):


class Cairo1TestCollector(TestCollector):
def __init__(self, cairo_path: list[str]):
def __init__(self, linked_libraries: list[Tuple[Path, cairo1.PackageName]]):
super().__init__(
get_suite_function_names=self.collect_cairo1_tests_and_cache_outputs
)
self._cairo_path = cairo_path
self.linked_libraries = linked_libraries
self._cairo_1_test_path_to_sierra_output: dict[Path, str] = {}

def collect_cairo1_tests_and_cache_outputs(
Expand All @@ -30,7 +30,7 @@ def collect_cairo1_tests_and_cache_outputs(
try:
collector_output = cairo1.collect_tests(
file_path,
cairo_path=[Path(cp) for cp in self._cairo_path],
linked_libraries=self.linked_libraries,
)
except RuntimeError as rt_err:
raise PreprocessorError(str(rt_err)) from rt_err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
ContractsController,
DeclaredSierraClass,
)
from protostar.commands.cairo1_commands.fetch_from_scarb import (
fetch_linked_libraries_from_scarb,
)
from protostar.cairo.contract_class import make_contract_class, make_compiled_class
from protostar.compiler.cairo1_contract_compiler import (
Cairo1ContractCompiler,
SierraCompilationException,
CasmCompilationException,
)

from protostar.contract_path_resolver import ContractPathResolver
from protostar.configuration_file.configuration_file import (
ContractNameNotFoundException,
Expand Down Expand Up @@ -77,7 +79,11 @@ def _get_contract_classes(
sierra_compiled,
casm_compiled,
) = Cairo1ContractCompiler.compile_contract(
contract_name, contract_path
contract_name,
contract_path,
linked_libraries=fetch_linked_libraries_from_scarb(
self._contract_path_resolver.project_root_path
),
)
except SierraCompilationException as ex:
raise CheatcodeException(
Expand Down
8 changes: 0 additions & 8 deletions protostar/cli/common_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,3 @@
type="str",
default="",
)

LINKED_LIBRARIES = ProtostarArgument(
name="linked-libraries",
value_parser="list",
description="Paths to cairo1 modules. "
"Should not be explicitly provided when managing dependencies using Scarb.",
type="path",
)
20 changes: 4 additions & 16 deletions protostar/commands/cairo1_commands/build_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from protostar.cli import ProtostarCommand, MessengerFactory
from protostar.cli.common_arguments import (
COMPILED_CONTRACTS_DIR_ARG,
LINKED_LIBRARIES,
CONTRACT_NAME,
)
from protostar.cairo.contract_class import (
Expand All @@ -19,7 +18,7 @@
from protostar.io import StructuredMessage, LogColorProvider, Messenger

from protostar.commands.cairo1_commands.fetch_from_scarb import (
maybe_fetch_linked_libraries_from_scarb,
fetch_linked_libraries_from_scarb,
)


Expand Down Expand Up @@ -76,7 +75,6 @@ def description(self) -> str:
def arguments(self):
return [
*MessengerFactory.OUTPUT_ARGUMENTS,
LINKED_LIBRARIES,
COMPILED_CONTRACTS_DIR_ARG,
CONTRACT_NAME,
]
Expand All @@ -86,7 +84,6 @@ async def run(self, args: Any):
messenger = self._messenger_factory.from_args(args)
await self.build(
output_dir=args.compiled_contracts_dir,
relative_cairo_path=args.linked_libraries,
target_contract_name=args.contract_name,
messenger=messenger,
)
Expand All @@ -98,7 +95,6 @@ async def _build_contract(
self,
contract_name: str,
output_dir: Path,
linked_libraries: list[Path],
messenger: Messenger,
):
contract_paths = self._configuration_file.get_contract_source_paths(
Expand All @@ -111,15 +107,12 @@ async def _build_contract(
f"only one file per contract is supported in cairo1!"
)

cairo_path = linked_libraries + maybe_fetch_linked_libraries_from_scarb(
package_root_path=contract_paths[0],
linked_libraries=linked_libraries,
)

sierra_compiled, casm_compiled = Cairo1ContractCompiler.compile_contract(
contract_name=contract_name,
contract_path=contract_paths[0],
cairo_path=cairo_path,
linked_libraries=fetch_linked_libraries_from_scarb(
package_root_path=self._project_root_path,
),
output_dir=output_dir,
)

Expand All @@ -146,26 +139,21 @@ async def build(
self,
output_dir: Path,
messenger: Messenger,
relative_cairo_path: Optional[list[Path]] = None,
target_contract_name: str = "",
) -> None:
linked_libraries = relative_cairo_path or []

if not output_dir.is_absolute():
output_dir = self._project_root_path / output_dir

if target_contract_name:
await self._build_contract(
contract_name=target_contract_name,
output_dir=output_dir,
linked_libraries=linked_libraries,
messenger=messenger,
)
return
for contract_name in self._configuration_file.get_contract_names():
await self._build_contract(
contract_name=contract_name,
output_dir=output_dir,
linked_libraries=linked_libraries,
messenger=messenger,
)
9 changes: 8 additions & 1 deletion protostar/commands/cairo1_commands/declare_cairo1_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
MAX_FEE_ARG,
WAIT_FOR_ACCEPTANCE_ARG,
)
from protostar.commands.cairo1_commands.fetch_from_scarb import (
fetch_linked_libraries_from_scarb,
)

from protostar.commands.declare.declare_messages import SuccessfulDeclareMessage
from protostar.compiler.cairo1_contract_compiler import Cairo1ContractCompiler
Expand Down Expand Up @@ -103,7 +106,11 @@ async def run(self, args: Namespace) -> Any:
)

contract_sierra, contract_casm = Cairo1ContractCompiler.compile_contract(
contract_name=contract_name, contract_path=contract_path
contract_name=contract_name,
contract_path=contract_path,
linked_libraries=fetch_linked_libraries_from_scarb(
package_root_path=self._contract_path_resolver.project_root_path,
),
)

response = await self.declare(
Expand Down
Loading

0 comments on commit ceed1b6

Please sign in to comment.