Skip to content

Commit

Permalink
Cairo v0.11.0 (pre3).
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Mar 19, 2023
1 parent 706ae5f commit d34583c
Show file tree
Hide file tree
Showing 22 changed files with 707 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/starkware/cairo/lang/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.0a1
0.11.0a2
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def _execute(
storage=syscall_handler.storage,
result=get_call_result(runner=runner, initial_gas=self.initial_gas),
events=syscall_handler.events,
l2_to_l1_messages=[],
l2_to_l1_messages=syscall_handler.l2_to_l1_messages,
internal_calls=syscall_handler.internal_calls,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"range_check_builtin": 18
},
"n_memory_holes": 0,
"n_steps": 926
"n_steps": 920
},
"emit_event": {
"builtin_instance_counter": {},
Expand Down Expand Up @@ -124,7 +124,7 @@
"range_check_builtin": 83
},
"n_memory_holes": 0,
"n_steps": 3577
"n_steps": 3571
},
"INVOKE_FUNCTION": {
"builtin_instance_counter": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python_lib(starknet_transaction_objects_lib
LIBS
everest_business_logic_lib
everest_business_logic_state_api_lib
everest_definitions_lib
everest_internal_transaction_lib
everest_transaction_lib
starknet_abi_lib
Expand Down
17 changes: 17 additions & 0 deletions src/starkware/starknet/business_logic/transaction/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from services.everest.api.gateway.transaction import EverestTransaction
from services.everest.business_logic.internal_transaction import EverestInternalTransaction
from services.everest.business_logic.state_api import StateProxy
from services.everest.definitions.fields import format_felt_list
from starkware.python.utils import as_non_optional, from_bytes, to_bytes
from starkware.starknet.business_logic.execution.execute_entry_point import ExecuteEntryPoint
from starkware.starknet.business_logic.execution.objects import (
Expand Down Expand Up @@ -348,6 +349,22 @@ def run_validate_entrypoint(
n_steps=general_config.validate_max_n_steps
),
)

class_hash = state.get_class_hash_at(contract_address=self.sender_address)
compiled_class_hash = state.get_compiled_class_hash(class_hash=class_hash)
if compiled_class_hash != 0:
# The account contract class is a Cairo 1.0 contract; the 'validate' entry point
# should return 'VALID'.
stark_assert(
call_info.retdata == constants.VALIDATE_RETDATA,
code=StarknetErrorCode.INVALID_RETURN_DATA,
message=(
"Invalid 'validate' return values. "
f"Expected: {format_felt_list(constants.VALIDATE_RETDATA)}, "
f"got: {format_felt_list(call_info.retdata)}."
),
)

remaining_gas -= call_info.gas_consumed
verify_no_calls_to_other_contracts(call_info=call_info, function_name="'validate'")

Expand Down
39 changes: 32 additions & 7 deletions src/starkware/starknet/cli/starknet_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ async def declare(args: argparse.Namespace, command_args: List[str]):
await simulate_or_estimate_fee(args=args, tx=declare_tx_for_simulate)
return

max_fee = await compute_max_fee(args=args, tx=declare_tx_for_simulate, has_wallet=has_wallet)
max_fee = await compute_max_fee(
args=args,
tx=declare_tx_for_simulate,
has_wallet=has_wallet,
skip_validate=args.skip_validate,
)

tx = await create_declare_tx(
args=args,
Expand Down Expand Up @@ -154,7 +159,12 @@ async def deprecated_declare(args: argparse.Namespace):
await simulate_or_estimate_fee(args=args, tx=declare_tx_for_simulate)
return

max_fee = await compute_max_fee(args=args, tx=declare_tx_for_simulate, has_wallet=has_wallet)
max_fee = await compute_max_fee(
args=args,
tx=declare_tx_for_simulate,
has_wallet=has_wallet,
skip_validate=args.skip_validate,
)

tx = await create_deprecated_declare_tx(
args=args,
Expand Down Expand Up @@ -234,7 +244,9 @@ async def deploy_with_invoke(args: argparse.Namespace):
max_fee=0,
call=True,
)
max_fee = await compute_max_fee(args=args, tx=invoke_tx_for_fee_estimation, has_wallet=True)
max_fee = await compute_max_fee(
args=args, tx=invoke_tx_for_fee_estimation, has_wallet=True, skip_validate=False
)
tx, contract_address = await create_invoke_tx_for_deploy(
args=args,
salt=salt,
Expand Down Expand Up @@ -289,7 +301,12 @@ async def deploy_account(args: argparse.Namespace, command_args: List[str]):
await simulate_or_estimate_fee(args=args, tx=deploy_account_tx_for_simulate)
return

max_fee = await compute_max_fee(args=args, tx=deploy_account_tx_for_simulate, has_wallet=True)
max_fee = await compute_max_fee(
args=args,
tx=deploy_account_tx_for_simulate,
has_wallet=True,
skip_validate=args.skip_validate,
)

tx, contract_address = await create_deploy_account_tx(
args=args,
Expand Down Expand Up @@ -360,7 +377,12 @@ async def invoke(args: argparse.Namespace, command_args: List[str]):
if args.dry_run:
assert has_wallet, "--dry_run can only be used for invocation through an account contract."

max_fee = await compute_max_fee(args=args, tx=invoke_tx_for_simulate, has_wallet=has_wallet)
max_fee = await compute_max_fee(
args=args,
tx=invoke_tx_for_simulate,
has_wallet=has_wallet,
skip_validate=args.skip_validate,
)

tx = await create_invoke_tx(
args=args,
Expand Down Expand Up @@ -847,7 +869,10 @@ def validate_max_fee(max_fee: Optional[int]):


async def compute_max_fee(
args: argparse.Namespace, tx: Optional[AccountTransaction], has_wallet: bool
args: argparse.Namespace,
tx: Optional[AccountTransaction],
has_wallet: bool,
skip_validate: bool,
) -> int:
"""
Returns max_fee argument if passed, and estimates and returns the max fee otherwise.
Expand All @@ -860,7 +885,7 @@ async def compute_max_fee(
max_fee = await compute_max_fee_for_tx(
feeder_client=get_feeder_gateway_client(args),
tx=as_non_optional(tx),
skip_validate=args.skip_validate,
skip_validate=skip_validate,
)
max_fee_eth = float(Web3.fromWei(max_fee, "ether"))

Expand Down
5 changes: 4 additions & 1 deletion src/starkware/starknet/compiler/v1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ set(CAIRO_COMPILER_ARTIFACTS

add_custom_command(
OUTPUT "${CAIRO_COMPILER_DUMMY_FILE}" "${CAIRO_COMPILER_FILES}"
COMMAND curl -Lo release-x86_64-unknown-linux-musl.tar.gz https://github.com/starkware-libs/cairo/releases/download/v1.0.0-alpha.5/release-x86_64-unknown-linux-musl.tar.gz
COMMAND curl -Lo release-x86_64-unknown-linux-musl.tar.gz https://github.com/starkware-libs/cairo/releases/download/v1.0.0-alpha.6/release-x86_64-unknown-linux-musl.tar.gz
COMMAND tar -xf release-x86_64-unknown-linux-musl.tar.gz
COMMAND touch "${CAIRO_COMPILER_DUMMY_FILE}"
COMMENT "Downloading cairo compiler."
Expand All @@ -96,6 +96,9 @@ python_lib(starknet_compile_v1_lib

FILES
compile.py
mainnet_libfuncs.json
testnet_libfuncs.json
testnet2_libfuncs.json

ARTIFACTS
"${CAIRO_COMPILER_ARTIFACTS}"
Expand Down
67 changes: 53 additions & 14 deletions src/starkware/starknet/compiler/v1/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

DEFAULT_ALLOWED_LIBFUNCS_ARG: List[str] = []


def get_allowed_libfuncs_list_file(file_name: str) -> str:
main_dir_path = os.path.dirname(__file__)
file_path = os.path.join(main_dir_path, file_name + ".json")

return file_path


if "RUNFILES_DIR" in os.environ:
from bazel_tools.tools.python.runfiles import runfiles

Expand All @@ -25,51 +33,82 @@
STARKNET_COMPILE = os.path.join(os.path.dirname(__file__), "bin", "starknet-compile")


def build_allowed_libfuncs_args(
allowed_libfuncs_list_name: Optional[str] = None,
allowed_libfuncs_list_file: Optional[str] = None,
) -> List[str]:
if allowed_libfuncs_list_name is None and allowed_libfuncs_list_file is None:
return DEFAULT_ALLOWED_LIBFUNCS_ARG

assert allowed_libfuncs_list_name is None or allowed_libfuncs_list_file is None, (
"Received too many libfuncs list parameters."
f"allowed_libfuncs_list_name = {allowed_libfuncs_list_name},"
f"allowed_libfuncs_list_file = {allowed_libfuncs_list_file}."
)

if allowed_libfuncs_list_name is not None:
return ["--allowed-libfuncs-list-name", allowed_libfuncs_list_name]

assert allowed_libfuncs_list_file is not None
return [
"--allowed-libfuncs-list-file",
get_allowed_libfuncs_list_file(allowed_libfuncs_list_file),
]


def compile_cairo_to_sierra(
cairo_path: str, allowed_libfuncs_list_name: Optional[str] = None
cairo_path: str,
allowed_libfuncs_list_name: Optional[str] = None,
allowed_libfuncs_list_file: Optional[str] = None,
) -> JsonObject:
"""
Compiles a Starknet Cairo 1.0 contract; returns the resulting Sierra as json.
"""
additional_args = (
DEFAULT_ALLOWED_LIBFUNCS_ARG
if allowed_libfuncs_list_name is None
else ["--allowed-libfuncs-list-name", allowed_libfuncs_list_name]
additional_args = build_allowed_libfuncs_args(
allowed_libfuncs_list_name=allowed_libfuncs_list_name,
allowed_libfuncs_list_file=allowed_libfuncs_list_file,
)
return run_compile_command(command=[STARKNET_COMPILE, cairo_path, *additional_args])


def compile_sierra_to_casm(
sierra_path: str, allowed_libfuncs_list_name: Optional[str] = None
sierra_path: str,
allowed_libfuncs_list_name: Optional[str] = None,
allowed_libfuncs_list_file: Optional[str] = None,
) -> JsonObject:
"""
Compiles a Starknet Sierra contract; returns the resulting Casm as json.
"""
additional_args = (
DEFAULT_ALLOWED_LIBFUNCS_ARG
if allowed_libfuncs_list_name is None
else ["--allowed-libfuncs-list-name", allowed_libfuncs_list_name]
additional_args = build_allowed_libfuncs_args(
allowed_libfuncs_list_name=allowed_libfuncs_list_name,
allowed_libfuncs_list_file=allowed_libfuncs_list_file,
)
return run_compile_command(
command=[STARKNET_SIERRA_COMPILE, sierra_path, "--add-pythonic-hints", *additional_args]
)


def compile_cairo_to_casm(
cairo_path: str, allowed_libfuncs_list_name: Optional[str] = None
cairo_path: str,
allowed_libfuncs_list_name: Optional[str] = None,
allowed_libfuncs_list_file: Optional[str] = None,
) -> JsonObject:
"""
Compiles a Starknet Cairo 1.0 contract to Casm; returns the resulting Casm as json.
"""
raw_sierra = compile_cairo_to_sierra(
cairo_path=cairo_path, allowed_libfuncs_list_name=allowed_libfuncs_list_name
cairo_path=cairo_path,
allowed_libfuncs_list_name=allowed_libfuncs_list_name,
allowed_libfuncs_list_file=allowed_libfuncs_list_file,
)
with tempfile.NamedTemporaryFile(mode="w") as sierra_file:
json.dump(obj=raw_sierra, fp=sierra_file, indent=2)
sierra_file.flush()

return compile_sierra_to_casm(
sierra_path=sierra_file.name, allowed_libfuncs_list_name=allowed_libfuncs_list_name
sierra_path=sierra_file.name,
allowed_libfuncs_list_name=allowed_libfuncs_list_name,
allowed_libfuncs_list_file=allowed_libfuncs_list_file,
)


Expand All @@ -93,7 +132,7 @@ def run_compile_command(command: List[str]) -> JsonObject:
if result.returncode != 0:
raise StarkException(
code=StarknetErrorCode.COMPILATION_FAILED,
message=f"Compilation failed. Error: {result.stderr.decode()}",
message=f"Compilation failed. {result.stderr.decode()}",
)

# Read and return the compilation result from the output.
Expand Down
3 changes: 3 additions & 0 deletions src/starkware/starknet/compiler/v1/mainnet_libfuncs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"allowed_libfuncs": []
}
Loading

0 comments on commit d34583c

Please sign in to comment.