Skip to content

Commit

Permalink
Introduce Rust VM option (0xSpaceShard#372)
Browse files Browse the repository at this point in the history
* [skip ci] - a release commit follows which will run the CI/CD workflow

Co-authored-by: fmoletta <[email protected]>
Co-authored-by: Federica <[email protected]>
Co-authored-by: jrigada <[email protected]>
Co-authored-by: Juan Rigada <[email protected]>
  • Loading branch information
5 people committed Dec 23, 2022
1 parent 50c38b9 commit 62be088
Show file tree
Hide file tree
Showing 16 changed files with 745 additions and 32 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ jobs:
<<: *test_template
docker:
- image: cimg/python:3.9-node

test_py_min_rust:
<<: *test_template
environment:
STARKNET_DEVNET_CAIRO_VM: "rust"

package_build_and_publish:
docker:
Expand Down Expand Up @@ -120,6 +125,7 @@ workflows:
jobs:
- test_py_min
- test_py_max
- test_py_min_rust
- image_build:
<<: *on_master
- image_build_arm:
Expand Down
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ Dockerfile
test/
.pylintrc
page/

# Hardhat specific
node_modules/
hardhat-cache/
11 changes: 5 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ RUN apk add gmp-dev g++ gcc libffi-dev

RUN pip3 install poetry

RUN poetry build -f wheel

RUN poetry export -f requirements.txt --without-hashes > requirements.txt
# install rustc+cargo
RUN wget https://sh.rustup.rs -O - | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

RUN poetry build -f wheel
RUN poetry export -f requirements.txt --without-hashes --with vm > requirements.txt
RUN pip3 wheel --no-cache-dir --no-deps --wheel-dir /wheels -r requirements.txt


FROM python:3.9.13-alpine3.16

RUN apk add --no-cache libgmpxx

COPY --from=builder /dist/*.whl /wheels/

COPY --from=builder /wheels /wheels

RUN pip3 install --no-cache /wheels/*

RUN rm -rf /wheels

ENV PYTHONUNBUFFERED=1
Expand Down
4 changes: 2 additions & 2 deletions page/docs/guide/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ sidebar_position: 18

# Development

If you're a developer willing to contribute, be sure to have installed [**Poetry**](https://pypi.org/project/poetry/) and all the dependency packages by running the following script. You are expected to have [**npm**](https://www.npmjs.com/).
If you're a developer willing to contribute, be sure to have installed [**Poetry**](https://pypi.org/project/poetry/) and all the dependency packages by running the installation script. Prerequisites for running the script: `gcc`, `g++`, `gmp`, `npm`.

```text
```bash
./scripts/install_dev_tools.sh
```

Expand Down
36 changes: 36 additions & 0 deletions page/docs/guide/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,39 @@ docker run -p 127.0.0.1:5050:5050 shardlabs/starknet-devnet
You may ignore any address-related output logged on container startup (e.g. `Running on all addresses` or `Running on http:https://172.17.0.2:5050`). What you will use is what you specified with the `-p` argument.

If you don't specify the `HOST` part, the server will indeed be available on all of your host machine's addresses (localhost, local network IP, etc.), which may present a security issue if you don't want anyone from the local network to access your Devnet instance.

## Run with the Rust implementation of Cairo VM

By default, Devnet uses the [Python implementation](https://github.com/starkware-libs/cairo-lang/) of Cairo VM.

Using the Rust implementation brings improvement for Cairo-VM-intensive operations, but introduces its own overhead, so it may not be useful for simple contracts.

You can enable it by following these steps:

1. Install compilers

Make sure you have `gcc`, `g++` and [Rust](https://www.rust-lang.org/tools/install).

2. Install [cairo-rs-py](https://github.com/lambdaclass/cairo-rs-py) in the [**same environment**](https://docs.python.org/3/library/venv.html) as Devnet:

```bash
$ pip install cairo-rs-py
```

3. Set `STARKNET_DEVNET_CAIRO_VM=rust`

```bash
$ STARKNET_DEVNET_CAIRO_VM=rust starknet-devnet
```

With Docker, use `-e`:

```bash
$ docker run -it [OPTIONS] -e STARKNET_DEVNET_CAIRO_VM=rust shardlabs/starknet-devnet [ARGS]
```

To use the Python VM, **unset** the variable or set it to `python`

```bash
$ STARKNET_DEVNET_CAIRO_VM=python starknet-devnet
```
18 changes: 14 additions & 4 deletions page/docs/intro.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
---
sidebar_position: 1
---

# Getting Started

Let's discover **[starknet-devnet](https://github.com/Shard-Labs/starknet-devnet)**.
:::danger Take care

## ⚠️ Disclaimer ⚠️

:::

- Devnet should not be used as a replacement for Alpha testnet. After testing on Devnet, be sure to test on testnet (alpha-goerli)!
Expand All @@ -20,23 +23,30 @@ Works with Python versions >=3.8 and <3.10.

On Ubuntu/Debian, first run:


```bash
sudo apt install -y libgmp3-dev
$ sudo apt install -y libgmp3-dev
```

On Mac, you can use `brew`:

```bash
brew install gmp
$ brew install gmp
```

## Install

```bash
pip install starknet-devnet
$ pip install starknet-devnet
```

## Run

```
$ starknet-devnet
```

For more running possibilities, see [this](https://shard-labs.github.io/starknet-devnet/docs/guide/run).

### Windows installation

Follow this guide: https://www.spaceshard.io/blog/starknet-devnet-windows-tutorial
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ black = "~22.6"
requests = "~2.28"
isort = "^5.10.1"

[tool.poetry.group.vm.dependencies]
cairo-rs-py = "~0.1.0"

[tool.isort]
profile = "black"
skip_gitignore = true
Expand Down
9 changes: 9 additions & 0 deletions scripts/install_dev_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ echo "python3: $(python3 --version)"
pip3 install -U poetry==1.2.1
echo "poetry: $(poetry --version)"

# https://www.rust-lang.org/tools/install
# need rust to install cairo-rs-py
if rustc --version; then
echo "rustc installed"
else
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source "$HOME/.cargo/env"
fi

# install dependencies
poetry install
poetry lock --check
Expand Down
21 changes: 21 additions & 0 deletions starknet_devnet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Contains the server implementation and its utility classes and functions.
"""

import os
import sys
from copy import copy

Expand All @@ -9,6 +11,8 @@
from starkware.crypto.signature.fast_pedersen_hash import pedersen_hash
from starkware.starknet.services.api.contract_class import ContractClass

from .util import warn

__version__ = "0.4.2"


Expand Down Expand Up @@ -43,3 +47,20 @@ def simpler_copy(self, memo): # pylint: disable=unused-argument


setattr(ContractClass, "__deepcopy__", simpler_copy)


# Optionally apply cairo-rs-py monkey patch
_VM_VAR = "STARKNET_DEVNET_CAIRO_VM"
_cairo_vm = os.environ.get(_VM_VAR)
if _cairo_vm == "rust":
from starknet_devnet.cairo_rs_py_patch import cairo_rs_py_monkeypatch

cairo_rs_py_monkeypatch()
warn("Using Cairo VM: Rust")

elif not _cairo_vm or _cairo_vm == "python":
# python VM set by default
pass

else:
sys.exit(f"Error: Invalid value of environment variable {_VM_VAR}: '{_cairo_vm}'")
Loading

0 comments on commit 62be088

Please sign in to comment.