From eaf9a4730a4c1c6999e82b23025923434681f39d Mon Sep 17 00:00:00 2001 From: Mathieu <60658558+enitrat@users.noreply.github.com> Date: Fri, 19 May 2023 22:11:04 +0200 Subject: [PATCH] fix: ci runner (#158) * fix: add --available-gas option to cairo-runner * fix: CI --- .gitignore | 1 + .../scripts/cairo_programs_verifier.sh | 2 +- po/es.po | 12 +-- po/fr.po | 8 +- po/messages.pot | 6 +- src/ch01-01-installation.md | 102 +++++++++--------- src/ch07-01-generic-data-types.md | 32 ++++-- src/ch07-02-traits-in-cairo.md | 19 +++- 8 files changed, 108 insertions(+), 74 deletions(-) diff --git a/.gitignore b/.gitignore index 84d1253f5..f1797da84 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ book target +output # Editors tmp files. *~ diff --git a/mdbook-cairo/scripts/cairo_programs_verifier.sh b/mdbook-cairo/scripts/cairo_programs_verifier.sh index 40337c1fb..631ec0cd1 100644 --- a/mdbook-cairo/scripts/cairo_programs_verifier.sh +++ b/mdbook-cairo/scripts/cairo_programs_verifier.sh @@ -23,7 +23,7 @@ fi has_error=false for prog in *.cairo; do - cairo-run "$prog" > output/"$prog".out 2> output/"$prog".err + cairo-run --available-gas=20000000 "$prog" > output/"$prog".out 2> output/"$prog".err compile_code="$?" diff --git a/po/es.po b/po/es.po index 19dff19fc..375b1ebd6 100644 --- a/po/es.po +++ b/po/es.po @@ -217,11 +217,11 @@ msgstr "" #: src/title-page.md:5 msgid "" -"This version of the text assumes you’re using Cairo v1.0.0-alpha.7 (released " +"This version of the text assumes you’re using Cairo v1.0.0 (released " "2023-04-13). See the “Installation” section of Chapter 1 to install or " "update Cairo." msgstr "" -"Esta versión del texto asume que estás usando Cairo v1.0.0-alpha.7 " +"Esta versión del texto asume que estás usando Cairo v1.0.0 " "(publicado el 2023-04-13). Consulte la sección \"Instalación\" del Capítulo " "1 para instalar o actualizar Cairo." @@ -522,11 +522,11 @@ msgstr "### Instalación" msgid "" "If you wish to install a specific release of Cairo rather than the latest " "head, set the `CAIRO_GIT_TAG` environment variable (e.g. `export " -"CAIRO_GIT_TAG=v1.0.0-alpha.6`)." +"CAIRO_GIT_TAG=v1.0.0`)." msgstr "" "Si deseas instalar una versión específica de Cairo en lugar de la última " "versión, debes establecer la variable de entorno `CAIRO_GIT_TAG` (por " -"ejemplo,`export CAIRO_GIT_TAG=v1.0.0-alpha.6`)." +"ejemplo,`export CAIRO_GIT_TAG=v1.0.0`)." #: src/ch01-01-installation.md:22 msgid "" @@ -855,7 +855,7 @@ msgid "" "# View tags (you can also do this in the cairo compiler repository)\n" "git describe --tags `git rev-list --tags`\n" "# Checkout the version you want\n" -"git checkout tags/v1.0.0-alpha.6\n" +"git checkout tags/v1.0.0\n" "\n" "# Generate release binaries\n" "cargo build --all --release\n" @@ -878,7 +878,7 @@ msgstr "" "# View tags (you can also do this in the cairo compiler repository)\n" "git describe --tags `git rev-list --tags`\n" "# Checkout the version you want\n" -"git checkout tags/v1.0.0-alpha.6\n" +"git checkout tags/v1.0.0\n" "\n" "# Generate release binaries\n" "cargo build --all --release\n" diff --git a/po/fr.po b/po/fr.po index c9654a9fb..e04dbcf78 100644 --- a/po/fr.po +++ b/po/fr.po @@ -204,10 +204,10 @@ msgstr "par la communauté de Cairo et ses [contributeurs](https://github.com/ca #: src/title-page.md:5 msgid "" -"This version of the text assumes you’re using Cairo v1.0.0-alpha.7 (released " +"This version of the text assumes you’re using Cairo v1.0.0 (released " "2023-04-13). See the “Installation” section of Chapter 1 to install or " "update Cairo." -msgstr "La version actuelle considère que vous utilisez Cairo v1.0.0-alpha.7 (version 2023-04-13). " +msgstr "La version actuelle considère que vous utilisez Cairo v1.0.0 (version 2023-04-13). " "Se référer à la section “Installation” du chapitre 1 pour installer ou actualiser Cairo." #: src/ch00-01-foreword.md:1 @@ -427,7 +427,7 @@ msgstr "" msgid "" "If you wish to install a specific release of Cairo rather than the latest " "head, set the `CAIRO_GIT_TAG` environment variable (e.g. `export " -"CAIRO_GIT_TAG=v1.0.0-alpha.6`)." +"CAIRO_GIT_TAG=v1.0.0`)." msgstr "" #: src/ch01-01-installation.md:22 @@ -645,7 +645,7 @@ msgid "" "# View tags (you can also do this in the cairo compiler repository)\n" "git describe --tags `git rev-list --tags`\n" "# Checkout the version you want\n" -"git checkout tags/v1.0.0-alpha.6\n" +"git checkout tags/v1.0.0\n" "\n" "# Generate release binaries\n" "cargo build --all --release\n" diff --git a/po/messages.pot b/po/messages.pot index 3c1910fe0..b91a4ac61 100644 --- a/po/messages.pot +++ b/po/messages.pot @@ -211,7 +211,7 @@ msgstr "" #: src/title-page.md:5 msgid "" -"This version of the text assumes you’re using Cairo v1.0.0-alpha.7 (released " +"This version of the text assumes you’re using Cairo v1.0.0 (released " "2023-04-13). See the “Installation” section of Chapter 1 to install or " "update Cairo." msgstr "" @@ -416,7 +416,7 @@ msgstr "" msgid "" "If you wish to install a specific release of Cairo rather than the latest " "head, set the `CAIRO_GIT_TAG` environment variable (e.g. `export " -"CAIRO_GIT_TAG=v1.0.0-alpha.6`)." +"CAIRO_GIT_TAG=v1.0.0`)." msgstr "" #: src/ch01-01-installation.md:22 @@ -638,7 +638,7 @@ msgid "" "# View tags (you can also do this in the cairo compiler repository)\n" "git describe --tags `git rev-list --tags`\n" "# Checkout the version you want\n" -"git checkout tags/v1.0.0-alpha.6\n" +"git checkout tags/v1.0.0\n" "\n" "# Generate release binaries\n" "cargo build --all --release\n" diff --git a/src/ch01-01-installation.md b/src/ch01-01-installation.md index 55239c088..704be36a5 100644 --- a/src/ch01-01-installation.md +++ b/src/ch01-01-installation.md @@ -19,7 +19,7 @@ Install [Git](https://git-scm.com/). If you wish to install a specific release of Cairo rather than the latest head, set the `CAIRO_GIT_TAG` environment variable (e.g. `export CAIRO_GIT_TAG=v1.0.0`). -```bash +```bash curl -L https://github.com/franalgaba/cairo-installer/raw/main/bin/cairo-installer | bash ``` @@ -54,69 +54,71 @@ exec $SHELL ### Set up your shell environment for Cairo -* Define environment variable `CAIRO_ROOT` to point to the path where +- Define environment variable `CAIRO_ROOT` to point to the path where Cairo will store its data. `$HOME/.cairo` is the default. If you installed Cairo via Git checkout, we recommend to set it to the same location as where you cloned it. -* Add the `cairo-*` executables to your `PATH` if it's not already there +- Add the `cairo-*` executables to your `PATH` if it's not already there The below setup should work for the vast majority of users for common use cases. - - For **bash**: +- For **bash**: - Stock Bash startup files vary widely between distributions in which of them source - which, under what circumstances, in what order and what additional configuration they perform. - As such, the most reliable way to get Cairo in all environments is to append Cairo - configuration commands to both `.bashrc` (for interactive shells) - and the profile file that Bash would use (for login shells). + Stock Bash startup files vary widely between distributions in which of them source + which, under what circumstances, in what order and what additional configuration they perform. + As such, the most reliable way to get Cairo in all environments is to append Cairo + configuration commands to both `.bashrc` (for interactive shells) + and the profile file that Bash would use (for login shells). - First, add the commands to `~/.bashrc` by running the following in your terminal: + First, add the commands to `~/.bashrc` by running the following in your terminal: - ~~~ bash - echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.bashrc - echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.bashrc - ~~~ + ```bash + echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.bashrc + echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.bashrc + ``` - Then, if you have `~/.profile`, `~/.bash_profile` or `~/.bash_login`, add the commands there as well. - If you have none of these, add them to `~/.profile`. + Then, if you have `~/.profile`, `~/.bash_profile` or `~/.bash_login`, add the commands there as well. + If you have none of these, add them to `~/.profile`. - * to add to `~/.profile`: - ~~~ bash - echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.profile - echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.profile - ~~~ + - to add to `~/.profile`: - * to add to `~/.bash_profile`: - ~~~ bash - echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.bash_profile - echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.bash_profile - ~~~ + ```bash + echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.profile + echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.profile + ``` - - For **Zsh**: - ~~~ zsh - echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.zshrc - echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.zshrc - ~~~ + - to add to `~/.bash_profile`: + ```bash + echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.bash_profile + echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.bash_profile + ``` - If you wish to get Cairo in non-interactive login shells as well, also add the commands to `~/.zprofile` or `~/.zlogin`. +- For **Zsh**: - - For **Fish shell**: + ```zsh + echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.zshrc + echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.zshrc + ``` - If you have Fish 3.2.0 or newer, execute this interactively: + If you wish to get Cairo in non-interactive login shells as well, also add the commands to `~/.zprofile` or `~/.zlogin`. - ~~~ fish - set -Ux CAIRO_ROOT $HOME/.cairo - fish_add_path $CAIRO_ROOT/target/release - ~~~ +- For **Fish shell**: - Otherwise, execute the snippet below: + If you have Fish 3.2.0 or newer, execute this interactively: + + ```fish + set -Ux CAIRO_ROOT $HOME/.cairo + fish_add_path $CAIRO_ROOT/target/release + ``` - ~~~ fish - set -Ux CAIRO_ROOT $HOME/.cairo - set -U fish_user_paths $CAIRO_ROOT/target/release $fish_user_paths - ~~~ + Otherwise, execute the snippet below: - In MacOS, you might also want to install [Fig](https://fig.io/) which + ```fish + set -Ux CAIRO_ROOT $HOME/.cairo + set -U fish_user_paths $CAIRO_ROOT/target/release $fish_user_paths + ``` + +In MacOS, you might also want to install [Fig](https://fig.io/) which provides alternative shell completions for many command line tools with an IDE-like popup interface in the terminal window. (Note that their completions are independent from Cairo's codebase @@ -124,11 +126,11 @@ so they might be slightly out of sync for bleeding-edge interface changes.) ### Restart your shell - for the `PATH` changes to take effect. +for the `PATH` changes to take effect. - ```sh - exec "$SHELL" - ``` +```sh +exec "$SHELL" +``` ## Installing Cairo Manually ([Guide](https://github.com/auditless/cairo-template) by [Abdel](https://github.com/abdelhamidbakhta)) @@ -160,7 +162,7 @@ git checkout tags/v1.0.0 cargo build --all --release ``` -. +. **NOTE: Keeping Cairo up to date** @@ -185,7 +187,7 @@ export PATH="$CAIRO_ROOT/target/release:$PATH" - Disable previous Cairo 0.x extension - Install the Cairo 1 extension for proper syntax highlighting and code navigation. -Just follow the steps indicated [here](https://github.com/starkware-libs/cairo/blob/main/vscode-cairo/README.md). + Just follow the steps indicated [here](https://github.com/starkware-libs/cairo/blob/main/vscode-cairo/README.md). #### Cairo Language Server diff --git a/src/ch07-01-generic-data-types.md b/src/ch07-01-generic-data-types.md index abc09f47a..ba5c0e00b 100644 --- a/src/ch07-01-generic-data-types.md +++ b/src/ch07-01-generic-data-types.md @@ -136,6 +136,8 @@ fn main() { Compiling the above code would error due to the `derive` macro not working well with generics. When using generic types is best to directly write the traits you want to use: + + ```rust struct Wallet { balance: T, @@ -202,14 +204,15 @@ struct Wallet { } impl WalletDrop> of Drop>; +impl WalletCopy> of Copy>; trait WalletTrait { - fn balance(self: @Wallet) -> @T; + fn balance(self: @Wallet) -> T; } -impl WalletImpl of WalletTrait { - fn balance(self: @Wallet) -> @T { - return self.balance; +impl WalletImpl> of WalletTrait { + fn balance(self: @Wallet) -> T { + return *self.balance; } } @@ -224,6 +227,23 @@ We first define `WalletTrait` trait using a generic type `T` which defines a We can also specify constraints on generic types when defining methods on the type. We could, for example, implement methods only for `Wallet` instances rather than `Wallet`. In the code example we define an implementation for wallets which have a concrete type of `u128` for the `balance` field. ```rust +struct Wallet { + balance: T, +} +impl WalletDrop> of Drop>; +impl WalletCopy> of Copy>; +/// Generic trait for wallets +trait WalletTrait { + fn balance(self: @Wallet) -> T; +} + +impl WalletImpl> of WalletTrait { + fn balance(self: @Wallet) -> T { + return *self.balance; + } +} + +/// Trait for wallets of type u128 trait WalletReceiveTrait { fn receive(ref self: Wallet, value: u128); } @@ -238,7 +258,7 @@ fn main() { let mut w = Wallet { balance: 50_u128 }; assert(w.balance() == 50_u128, 0); - w.receive(100_u128) + w.receive(100_u128); assert(w.balance() == 150_u128, 0); } ``` @@ -289,7 +309,7 @@ impl WalletMixImpl, U1, impl U1Drop: Drop> of Wall We add the requirements for `T1` and `U1` to be droppable on `WalletMixImpl` declaration. Then we do the same for `T2` and `U2`, this time as part of `mixup` signature. We can now try the `mixup` function: -```rs +```rs, does_not_compile fn main() { let w1 = Wallet { balance: true, address: 10_u128 }; let w2 = Wallet { balance: 32, address: 100_u8 }; diff --git a/src/ch07-02-traits-in-cairo.md b/src/ch07-02-traits-in-cairo.md index 7cba458d3..04a1a40f5 100644 --- a/src/ch07-02-traits-in-cairo.md +++ b/src/ch07-02-traits-in-cairo.md @@ -63,6 +63,17 @@ In the example below, we use generic type `T` and our method signatures can use ```rust use debug::PrintTrait; +#[derive(Copy,Drop)] +struct Rectangle{ + height: u64, + width: u64, +} + +#[derive(Copy,Drop)] +struct Circle{ + radius: u64, +} + // Here T is an alias type which will be provided buring implementation trait ShapeGeometry { fn boundary(self: T) -> u64; @@ -92,11 +103,11 @@ impl CircleGeometry of ShapeGeometry { } fn main() { - let rect = Rectangle { height: 5_u128, width: 7_u128 }; + let rect = Rectangle { height: 5_u64, width: 7_u64 }; rect.area().print(); // 35 rect.boundary().print(); // 24 - let circ = Circle { radius: 5_u128 }; + let circ = Circle { radius: 5_u64 }; circ.area().print(); // 78 circ.boundary().print(); // 31 } @@ -104,14 +115,14 @@ fn main() { ## Managing and using external trait implementations -To use traits methods, you need to make sure the correct traits/implementation(s) are imported. In the code above we imported `PrintTrait` from `debug` with `use debug::PrintTrait;` to use `print()` methods. +To use traits methods, you need to make sure the correct traits/implementation(s) are imported. In the code above we imported `PrintTrait` from `debug` with `use debug::PrintTrait;` to use the `print()` methods on supported types. In some cases you might need to import not only the trait but also the implementation if they are declared in separate modules. If `CircleGeometry` was in a separate module/file `circle` then to use `boundary` on `circ: Circle`, we'd need to import `CircleGeometry` in addition to `ShapeGeometry`. If the code was organised into modules like this, -```rust +```rust, does_not_compile use debug::PrintTrait; // struct Circle { ... } and struct Rectangle { ... }