Skip to content

Commit

Permalink
Cairo v0.3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Jul 18, 2021
1 parent 5445235 commit e882321
Show file tree
Hide file tree
Showing 173 changed files with 5,745 additions and 892 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ endif()
find_program(PYTHON "python3")

include("src/cmake_utils/cmake_rules.cmake")
include("src/starkware/cairo/lang/cairo_cmake_rules.cmake")

# Repos needs to be first as it defines macros that are needed by src.
add_subdirectory(src)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ We recommend starting from [Setting up the environment](https://cairo-lang.org/d
# Installation instructions

You should be able to download the python package zip file directly from
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.2.0)
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.3.0)
and install it using ``pip``.
See [Setting up the environment](https://cairo-lang.org/docs/quickstart.html).

Expand Down Expand Up @@ -54,7 +54,7 @@ Once the docker image is built, you can fetch the python package zip file using:

```bash
> container_id=$(docker create cairo)
> docker cp ${container_id}:/app/cairo-lang-0.2.0.zip .
> docker cp ${container_id}:/app/cairo-lang-0.3.0.zip .
> docker rm -v ${container_id}
```

35 changes: 33 additions & 2 deletions scripts/requirements-deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
"package_name": "bitarray"
}
},
{
"dependencies": [],
"package": {
"installed_version": "4.2.2",
"key": "cachetools",
"package_name": "cachetools"
}
},
{
"dependencies": [],
"package": {
Expand Down Expand Up @@ -632,15 +640,15 @@
{
"dependencies": [],
"package": {
"installed_version": "21.1.2",
"installed_version": "20.2.3",
"key": "pip",
"package_name": "pip"
}
},
{
"dependencies": [
{
"installed_version": "21.1.2",
"installed_version": "20.2.3",
"key": "pip",
"package_name": "pip",
"required_version": ">=6.0.0"
Expand All @@ -667,6 +675,14 @@
"package_name": "pluggy"
}
},
{
"dependencies": [],
"package": {
"installed_version": "0.11.0",
"key": "prometheus-client",
"package_name": "prometheus-client"
}
},
{
"dependencies": [
{
Expand Down Expand Up @@ -765,6 +781,21 @@
"package_name": "pytest"
}
},
{
"dependencies": [
{
"installed_version": "6.2.4",
"key": "pytest",
"package_name": "pytest",
"required_version": ">=5.4.0"
}
],
"package": {
"installed_version": "0.15.1",
"key": "pytest-asyncio",
"package_name": "pytest-asyncio"
}
},
{
"dependencies": [
{
Expand Down
3 changes: 3 additions & 0 deletions scripts/requirements-gen.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
aiohttp
cachetools
ecdsa
eth-hash[pycryptodome]==0.2.0
fastecdsa
Expand All @@ -11,7 +12,9 @@ marshmallow>=3.2.1
mpmath
numpy
pipdeptree
prometheus-client
pytest
pytest-asyncio
sympy
typeguard
Web3
3 changes: 3 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ async-timeout==3.0.1
attrs==21.2.0
base58==2.1.0
bitarray==1.2.2
cachetools==4.2.2
certifi==2020.12.5
chardet==4.0.0
cytoolz==0.11.0
Expand Down Expand Up @@ -41,12 +42,14 @@ packaging==20.9
parsimonious==0.8.1
pipdeptree==2.0.0
pluggy==0.13.1
prometheus-client==0.11.0
protobuf==3.17.1
py==1.10.0
pycryptodome==3.10.1
pyparsing==2.4.7
pyrsistent==0.17.3
pytest==6.2.4
pytest-asyncio==0.15.1
requests==2.25.1
rlp==2.0.1
six==1.16.0
Expand Down
2 changes: 1 addition & 1 deletion src/demo/amm_demo/amm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Account:
end

# The maximum amount of each token that belongs to the AMM.
const MAX_BALANCE = %[ 2**64 - 1 %]
const MAX_BALANCE = 2 ** 64 - 1

struct AmmState:
# A dictionary that tracks the accounts' state.
Expand Down
1 change: 1 addition & 0 deletions src/services/everest/api/gateway/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ python_lib(everest_transaction_lib

LIBS
starkware_utils_lib
pip_marshmallow_enum
pip_marshmallow_oneofschema
)

Expand Down
2 changes: 2 additions & 0 deletions src/services/everest/definitions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ python_lib(everest_definitions_lib
PREFIX services/everest/definitions

FILES
constants.py
fields.py

LIBS
starkware_utils_lib
pip_marshmallow
pip_web3
)
3 changes: 3 additions & 0 deletions src/services/everest/definitions/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ETH_ADDRESS_BITS = 160
ETH_ADDRESS_LOWER_BOUND = 0
ETH_ADDRESS_UPPER_BOUND = 1 << ETH_ADDRESS_BITS
79 changes: 79 additions & 0 deletions src/services/everest/definitions/fields.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,88 @@
import random
import string
from typing import List, Optional

import marshmallow.fields as mfields
from eth_typing import ChecksumAddress
from web3 import Web3

from services.everest.definitions import constants
from starkware.python.utils import initialize_random
from starkware.starkware_utils.error_handling import ErrorCode, StarkErrorCode
from starkware.starkware_utils.field_validators import validate_non_negative
from starkware.starkware_utils.validated_fields import Field, RangeValidatedField

# Fields data: validation data, dataclass metadata.
tx_id_marshmallow_field = mfields.Integer(
strict=True, required=True, validate=validate_non_negative('tx_id'))

tx_id_field_metadata = dict(marshmallow_field=tx_id_marshmallow_field)

# Fact Registry Address.


class EthAddressTypeField(Field[str]):
"""
A field representation of an Ethereum address.
"""

def __init__(self, name, error_code):
self._name = name
self._error_code = error_code

@property
def name(self) -> str:
return self._name

# Randomization.
def get_random_value(self, random_object: Optional[random.Random] = None) -> str:
r = initialize_random(random_object=random_object)
raw_address = ''.join(r.choices(population=string.hexdigits, k=40))
return Web3.toChecksumAddress(value=f'0x{raw_address}')

# Validation.
def is_valid(self, value: str) -> bool:
return Web3.isChecksumAddress(value)

def get_invalid_values(self) -> List[str]:
return [
'0x0Fa81Ec60fe5422d49174F1abdfdC06a9F1c52F2', # Not checksummed.
self.get_random_value()[:-1], # Too short address.
self.get_random_value() + '0' # type: ignore # Too long address.
]

@property
def error_code(self) -> ErrorCode:
return self._error_code

def format_invalid_value_error_message(self, value: str, name: Optional[str] = None) -> str:
name = self.name if name is None else name
return f'{name} {value} is out of range / not checksummed.'

# Serialization.
def get_marshmallow_field(self) -> mfields.Field:
return mfields.String(required=True)

def convert_valid_to_checksum(self, value: str) -> ChecksumAddress:
self.validate(value=value)
# This won't change value. It will only allow the function to return value as return
# ChecksumAddress.
return Web3.toChecksumAddress(value=value)

def format(self, value: str) -> str:
return value


FactRegistryField = EthAddressTypeField(
name='Address of fact registry',
error_code=StarkErrorCode.INVALID_CONTRACT_ADDRESS)

EthAddressField = EthAddressTypeField(
name='Ethereum address',
error_code=StarkErrorCode.INVALID_ETH_ADDRESS)

EthAddressIntField = RangeValidatedField(
lower_bound=constants.ETH_ADDRESS_LOWER_BOUND,
upper_bound=constants.ETH_ADDRESS_UPPER_BOUND,
name_in_error_message='Ethereum address',
out_of_range_error_code=StarkErrorCode.OUT_OF_RANGE_ETH_ADDRESS)
5 changes: 4 additions & 1 deletion src/services/external_api/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ async def _send_request(
exception.status_code not in self.retry_config.retry_codes):
raise

logger.error(f'BadRequest with code {exception.status_code}, retrying...')
logger.error(
f'Got BadRequest while trying to access {url}. '
f'status_code: {exception.status_code}. text: {exception.text}, '
'retrying...')

await asyncio.sleep(1)

Expand Down
1 change: 1 addition & 0 deletions src/starkware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ add_subdirectory(crypto)
add_subdirectory(python)
add_subdirectory(starknet)
add_subdirectory(starkware_utils)
add_subdirectory(storage)
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func merkle_multi_update_inner{hash_ptr : HashBuiltin*, update_ptr : DictAccess*

update_right:
let hash_ptr = hash_ptr + 2 * HashBuiltin.SIZE
prev_root = hash0.result
new_root = hash1.result
assert hash0.result = prev_root
assert hash1.result = new_root

# Make sure the same authentication path is used.
assert hash0.x = hash1.x
Expand All @@ -60,8 +60,8 @@ func merkle_multi_update_inner{hash_ptr : HashBuiltin*, update_ptr : DictAccess*

update_left:
let hash_ptr = hash_ptr + 2 * HashBuiltin.SIZE
prev_root = hash0.result
new_root = hash1.result
assert hash0.result = prev_root
assert hash1.result = new_root

# Make sure the same authentication path is used.
assert hash0.y = hash1.y
Expand All @@ -83,8 +83,8 @@ func merkle_multi_update_inner{hash_ptr : HashBuiltin*, update_ptr : DictAccess*
# Write the update.
%{ assert case == 'leaf' %}
index = update_ptr.key
prev_root = update_ptr.prev_value
new_root = update_ptr.new_value
assert update_ptr.prev_value = prev_root
assert update_ptr.new_value = new_root
let update_ptr = update_ptr + DictAccess.SIZE
return ()

Expand All @@ -95,8 +95,8 @@ func merkle_multi_update_inner{hash_ptr : HashBuiltin*, update_ptr : DictAccess*
local_left_index = index * 2; ap++

let hash_ptr = hash_ptr + 2 * HashBuiltin.SIZE
prev_root = hash0.result
new_root = hash1.result
assert hash0.result = prev_root
assert hash1.result = new_root

# Update left.
%{ vm_enter_scope(dict(node=left_child, preimage=preimage)) %}
Expand Down
14 changes: 7 additions & 7 deletions src/starkware/cairo/apps/starkex2_0/dex_constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
const ZERO_VAULT_HASH = 3051532127692517571387022095821932649971160144101372951378323654799587621206

# Balance should be in the range [0, 2**63).
const BALANCE_BOUND = %[ 2 ** 63 %]
const BALANCE_BOUND = 2 ** 63

# Nonce should be in the range [0, 2**31).
const NONCE_BOUND = %[ 2 ** 31 %]
const NONCE_BOUND = 2 ** 31

# Expiration timestamp should be in the range [0, 2**22).
const EXPIRATION_TIMESTAMP_BOUND = %[ 2 ** 22 %]
const EXPIRATION_TIMESTAMP_BOUND = 2 ** 22

# Order id should be in the range [0, 2**63).
const ORDER_ID_BOUND = %[ 2 ** 63 %]
const ORDER_ID_BOUND = 2 ** 63

# The result of a hash builtin should be in the range [0, 2**251).
const HASH_MESSAGE_BOUND = %[ 2 ** 251 %]
const HASH_MESSAGE_BOUND = 2 ** 251

# The range-check builtin enables verifying that a value is within the range [0, 2**128).
const RANGE_CHECK_BOUND = %[ 2 ** 128 %]
const RANGE_CHECK_BOUND = 2 ** 128

namespace PackedOrderMsg:
const SETTLEMENT_ORDER_TYPE = 0
const TRANSFER_ORDER_TYPE = 1
const CONDITIONAL_TRANSFER_ORDER_TYPE = 2
# Vault shift in packed order message is 2**31, regardless of the actual vault tree height.
const VAULT_SHIFT = %[ 2 ** 31 %]
const VAULT_SHIFT = 2 ** 31
const AMOUNT_SHIFT = BALANCE_BOUND
const NONCE_SHIFT = NONCE_BOUND
const EXPIRATION_TIMESTAMP_SHIFT = EXPIRATION_TIMESTAMP_BOUND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ from starkware.cairo.apps.starkex2_0.dex_context import DexContext
from starkware.cairo.apps.starkex2_0.vault_update import vault_update_balances

namespace ModificationConstants:
const BALANCE_SHIFT = %[ 2**64 %]
const VAULT_SHIFT = %[ 2**31 %]
const BALANCE_SHIFT = 2 ** 64
const VAULT_SHIFT = 2 ** 31
const FULL_WITHDRAWAL_SHIFT = BALANCE_SHIFT * VAULT_SHIFT
end

Expand Down
29 changes: 29 additions & 0 deletions src/starkware/cairo/builtin_selection/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
python_lib(cairo_builtin_selection_lib
PREFIX starkware/cairo/builtin_selection

FILES
inner_select_builtins.cairo
select_builtins.cairo
select_input_builtins.cairo
validate_builtins.cairo
)

full_python_test(cairo_builtin_selection_test
PREFIX starkware/cairo/builtin_selection
PYTHON python3.7
TESTED_MODULES starkware/cairo/builtin_selection

FILES
select_input_builtins_test.py
validate_builtins_test.py

LIBS
cairo_builtin_selection_lib
cairo_common_lib
cairo_common_test_utils_lib
cairo_compile_lib
cairo_test_utils
cairo_vm_lib
starkware_crypto_lib
pip_pytest
)
Loading

0 comments on commit e882321

Please sign in to comment.