-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python bindings for https://github.com/software-mansion-labs/cairo/
- Loading branch information
1 parent
cb109e8
commit 62ffecc
Showing
17 changed files
with
1,929 additions
and
1,471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import cairo_python_bindings | ||
|
||
|
||
def call_cairo_to_sierra_compiler( | ||
input_path: Path, output_path: Optional[Path] = None | ||
) -> Optional[str]: | ||
return cairo_python_bindings.call_cairo_to_sierra_compiler( # pyright: ignore | ||
str(input_path), str(output_path) if output_path else None | ||
) | ||
|
||
|
||
def call_sierra_to_casm_compiler( | ||
input_path: Path, output_path: Optional[Path] = None | ||
) -> Optional[str]: | ||
return cairo_python_bindings.call_sierra_to_casm_compiler( # pyright: ignore | ||
str(input_path), str(output_path) if output_path else None | ||
) | ||
|
||
|
||
def call_cairo_to_casm_compiler( | ||
input_path: Path, output_path: Optional[Path] = None | ||
) -> Optional[str]: | ||
return cairo_python_bindings.call_cairo_to_casm_compiler( # pyright: ignore | ||
str(input_path), str(output_path) if output_path else None | ||
) | ||
|
||
|
||
def call_starknet_contract_compiler( | ||
input_path: Path, output_path: Optional[Path] = None | ||
) -> Optional[str]: | ||
return cairo_python_bindings.call_starknet_contract_compiler( # pyright: ignore | ||
str(input_path), str(output_path) if output_path else None | ||
) | ||
|
||
|
||
def call_test_collector( | ||
input_path: Path, output_path: Optional[Path] = None | ||
) -> tuple[Optional[str], list[str]]: | ||
return cairo_python_bindings.call_test_collector( # pyright: ignore | ||
str(input_path), str(output_path) if output_path else None | ||
) | ||
|
||
|
||
def call_protostar_sierra_to_casm( | ||
named_tests: list[str], input_path: Path, output_path: Optional[Path] = None | ||
) -> Optional[str]: | ||
return cairo_python_bindings.call_protostar_sierra_to_casm( # pyright: ignore | ||
named_tests, str(input_path), str(output_path) if output_path else None | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
# clean up | ||
if [ "$1" == "--cleanup" ]; then | ||
poetry env info -p | xargs rm -rf | ||
if [[ $(uname -m) == 'arm64' ]]; then | ||
CFLAGS=-I/opt/homebrew/opt/gmp/include LDFLAGS=-L/opt/homebrew/opt/gmp/lib poetry install | ||
else | ||
poetry install | ||
fi | ||
fi | ||
|
||
function install() { | ||
pushd "${1}" | ||
git clone https://github.com/software-mansion-labs/cairo.git | ||
pushd cairo | ||
# currrent master works ok, in case it doesn't, uncomment the line below | ||
# git checkout 5608ce7e052df79da11485689cb5f1459d3e5d18 # working commit | ||
pushd crates/cairo-lang-python-bindings | ||
rustup override set nightly | ||
maturin develop --release || return 1; | ||
popd # cairo | ||
popd # cairo/crates/cairo_python_bindings | ||
popd # "${1}" | ||
} | ||
|
||
DIR=$(mktemp -d) | ||
install $DIR && echo "DONE" || echo "installation failed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/integration/cairo_compiler/contracts/basic_starknet_contract.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#[contract] | ||
mod HelloStarknet { | ||
struct Storage { balance: felt, } | ||
|
||
// Increases the balance by the given amount. | ||
#[external] | ||
fn increase_balance(amount: felt) { | ||
balance::write(balance::read() + amount); | ||
} | ||
|
||
// Returns the current balance. | ||
#[view] | ||
fn get_balance() -> felt { | ||
balance::read(); | ||
0 | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/integration/cairo_compiler/contracts/basic_starknet_test.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#[abi] | ||
trait IAnotherContract { | ||
fn foo(); } | ||
|
||
|
||
#[contract] | ||
mod TestContract { | ||
struct Storage { my_storage_var: felt, } | ||
|
||
fn internal_func() -> felt { | ||
1 | ||
} | ||
|
||
#[external] | ||
fn test(ref arg: felt, arg1: felt, arg2: felt) -> felt { | ||
let x = my_storage_var::read(); | ||
my_storage_var::write(x + 1); | ||
x + internal_func() | ||
} | ||
|
||
#[external] | ||
fn empty() { | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/integration/cairo_compiler/contracts/enum_contract.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
enum MyEnumShort { | ||
a: felt, | ||
b: felt | ||
} | ||
enum MyEnumLong { | ||
a: felt, | ||
b: felt, | ||
c: felt | ||
} | ||
enum MyEnumGeneric<S, T> { | ||
a: T, | ||
b: S, | ||
c: T | ||
} | ||
|
||
impl MyEnumGenericDrop of Drop::<MyEnumGeneric::<(), felt>>; | ||
|
||
fn main() -> felt { | ||
let es0 = MyEnumShort::a(10); | ||
match_short(es0); | ||
let es1 = MyEnumShort::b(11); | ||
match_short(es1); | ||
let el0 = MyEnumLong::a(20); | ||
match_long(el0); | ||
let el1 = MyEnumLong::b(21); | ||
match_long(el1); | ||
let el2 = MyEnumLong::c(22); | ||
match_long(el2); | ||
let eg1: MyEnumGeneric::<(), felt> = MyEnumGeneric::<(), felt>::a(30); | ||
let eg2: MyEnumGeneric::<(), felt> = MyEnumGeneric::<(), felt>::b(()); | ||
let eg3: MyEnumGeneric::<(), felt> = MyEnumGeneric::<(), felt>::c(32); | ||
300 | ||
} | ||
|
||
fn match_short(e: MyEnumShort) -> felt { | ||
match e { | ||
MyEnumShort::a(x) => { | ||
x | ||
}, | ||
MyEnumShort::b(x) => { | ||
x | ||
}, | ||
} | ||
} | ||
|
||
fn match_long(e: MyEnumLong) -> felt { | ||
match e { | ||
MyEnumLong::a(x) => { | ||
x | ||
}, | ||
MyEnumLong::b(x) => { | ||
x | ||
}, | ||
MyEnumLong::c(x) => { | ||
x | ||
}, | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/integration/cairo_compiler/contracts/roll_test.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[test] | ||
fn test_cheatcode_caller() { | ||
roll(1, 2) | ||
} | ||
#[test] | ||
fn test_cheatcode_caller_twice() { | ||
roll(1, 2); | ||
roll(1, 2) | ||
} | ||
#[test] | ||
fn test_cheatcode_caller_three() { | ||
roll(1, 2); | ||
roll(1, 2); | ||
roll(1, 2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Callable, NamedTuple | ||
from pathlib import Path | ||
from enum import Enum | ||
|
||
from tests.integration._conftest import ProtostarFixture | ||
|
||
TEST_CONTRACTS_PATH = Path(__file__).parent / "contracts" | ||
|
||
|
||
class RequestedFile(str, Enum): | ||
input_enum_contract_cairo = "enum_contract.cairo" | ||
input_basic_starknet_contract_cairo = "basic_starknet_contract.cairo" | ||
input_basic_starknet_test_cairo = "basic_starknet_test.cairo" | ||
input_roll_test_cairo = "roll_test.cairo" | ||
output_sierra = "output.sierra" | ||
output_casm = "output.casm" | ||
|
||
|
||
class PreparedFile(NamedTuple): | ||
path: Path | ||
contents: str | ||
|
||
|
||
class PrepareFilesFixture: | ||
def __init__(self, protostar: ProtostarFixture): | ||
self.protostar = protostar | ||
|
||
def prepare_files( | ||
self, requested_files: list[RequestedFile] | ||
) -> dict[str, PreparedFile]: | ||
files: dict[str, PreparedFile] = {} | ||
for file_item in requested_files: | ||
file_path = Path(TEST_CONTRACTS_PATH / file_item.value) | ||
contents = file_path.read_text() if file_path.exists() else "" | ||
file_with_ext = ".".join(file_item.name.rsplit("_", 1)) | ||
files[file_item.name] = PreparedFile( | ||
path=Path(f"./src/{file_with_ext}"), contents=contents | ||
) | ||
|
||
self.protostar.create_files( | ||
{str(path): contents for path, contents in files.values()} | ||
) | ||
files = { | ||
label: PreparedFile( | ||
path=Path(self.protostar.project_root_path / path), contents=contents | ||
) | ||
for label, (path, contents) in files.items() | ||
} | ||
for path, _ in files.values(): | ||
assert path.exists() | ||
return files | ||
|
||
|
||
def check_compiler_function( | ||
compiler_function_to_test: Callable, input_path: Path, output_path: Path | ||
): | ||
compiler_function_to_test(input_path, output_path) | ||
assert output_path.exists() and output_path.stat().st_size | ||
contents = compiler_function_to_test(input_path) | ||
assert contents | ||
assert contents == "".join(output_path.read_text()) |
Oops, something went wrong.