Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
Cairo lang 0.8.1 (#70)
Browse files Browse the repository at this point in the history
* Fix CarriedState imports

* Fix default config values

* Return actual fee from execution info

* Add class hash to deploy transactions

* Parse transaction signature as hex

* Bump cairo lang version to 0.8.1

* Update Solidity files

* Add web3 as dev dependency

* Update default general config

* Use n steps fee weight from constants

* Replace current carried state with state

* Update python version in README

* Change npm install to npm ci
  • Loading branch information
badurinantun authored Apr 7, 2022
1 parent 213f1dc commit a6c8f8e
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 162 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pip install starknet-devnet

### Requirements

Works with Python versions <=3.8.9.
Works with Python versions >=3.7.2 and <=3.9.10.

On Ubuntu/Debian, first run:

Expand Down
113 changes: 55 additions & 58 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ homepage = "https://github.com/Shard-Labs/starknet-devnet"
keywords = ["starknet", "cairo", "testnet", "local", "server"]

[tool.poetry.dependencies]
python = "^3.7"
python = "^3.7.2"
Flask = {extras = ["async"], version = "^2.0.2"}
flask-cors = "^3.0.10"
cairo-lang = "0.8.0"
cairo-lang = "0.8.1"
dill = "^0.3.4"
meinheld = "^1.0.2"

[tool.poetry.dev-dependencies]
pylint = "^2.12.2"
web3 = "^5.28.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
2 changes: 1 addition & 1 deletion scripts/install_dev_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ echo "poetry: $(poetry --version)"

# install dependencies
poetry install
npm install
npm ci
2 changes: 1 addition & 1 deletion starknet_devnet/blueprints/feeder_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def get_state_update():

@feeder_gateway.route("/estimate_fee", methods=["POST"])
async def estimate_fee():
"""Currently a dummy implementation, always returning 0."""
"""Returns the estimated fee for a transaction."""
transaction = validate_transaction(request.data, InvokeFunction)
try:
actual_fee = await state.starknet_wrapper.calculate_actual_fee(transaction)
Expand Down
31 changes: 31 additions & 0 deletions starknet_devnet/general_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Default general config.
"""

from starkware.starknet.definitions.general_config import (
build_general_config,
DEFAULT_CHAIN_ID,
DEFAULT_FEE_TOKEN_ADDRESS,
DEFAULT_GAS_PRICE,
DEFAULT_MAX_STEPS,
DEFAULT_SEQUENCER_ADDRESS,
)
from starkware.starknet.definitions import constants

DEFAULT_GENERAL_CONFIG = build_general_config({
"cairo_resource_fee_weights": {
"n_steps": constants.N_STEPS_FEE_WEIGHT,
},
"contract_storage_commitment_tree_height": constants.CONTRACT_STATES_COMMITMENT_TREE_HEIGHT,
"event_commitment_tree_height": constants.EVENT_COMMITMENT_TREE_HEIGHT,
"global_state_commitment_tree_height": constants.CONTRACT_ADDRESS_BITS,
"invoke_tx_max_n_steps": DEFAULT_MAX_STEPS,
"min_gas_price": DEFAULT_GAS_PRICE,
"sequencer_address": hex(DEFAULT_SEQUENCER_ADDRESS),
"starknet_os_config": {
"chain_id": DEFAULT_CHAIN_ID.name,
"fee_token_address": hex(DEFAULT_FEE_TOKEN_ADDRESS)
},
"tx_version": constants.TRANSACTION_VERSION,
"tx_commitment_tree_height": constants.TRANSACTION_COMMITMENT_TREE_HEIGHT
})
2 changes: 1 addition & 1 deletion starknet_devnet/postman_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from abc import ABC, abstractmethod
from web3 import HTTPProvider, Web3

from starkware.contracts.utils import load_nearby_contract
from starkware.solidity.utils import load_nearby_contract
from starkware.starknet.testing.postman import Postman
from starkware.eth.eth_test_utils import EthAccount, EthContract

Expand Down
23 changes: 9 additions & 14 deletions starknet_devnet/starknet_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

import dill as pickle
from starkware.starknet.business_logic.internal_transaction import InternalInvokeFunction
from starkware.starknet.business_logic.state import CarriedState
from starkware.starknet.business_logic.transaction_fee import calculate_tx_fee_by_cairo_usage
from starkware.starknet.business_logic.state.state import CarriedState
from starkware.starknet.definitions.transaction_type import TransactionType
from starkware.starknet.services.api.gateway.contract_address import calculate_contract_address
from starkware.starknet.services.api.gateway.transaction import InvokeFunction, Deploy, Transaction
Expand All @@ -22,8 +21,8 @@
from starkware.starknet.services.api.feeder_gateway.block_hash import calculate_block_hash

from .origin import NullOrigin, Origin
from .general_config import DEFAULT_GENERAL_CONFIG
from .util import (
DEFAULT_GENERAL_CONFIG,
Choice, StarknetDevnetException, TxStatus, DummyExecutionInfo,
fixed_length_hex, enable_pickling, generate_state_update
)
Expand Down Expand Up @@ -177,7 +176,8 @@ async def deploy(self, deploy_transaction: Deploy):
tx_hash=tx_hash,
status=status,
execution_info=execution_info,
error_message=error_message
error_message=error_message,
contract_hash=state.state.contract_states[contract_address].state.contract_hash
)

return contract_address, tx_hash
Expand Down Expand Up @@ -315,7 +315,7 @@ async def __generate_block(self, tx_wrapper: TransactionWrapper):
timestamp = int(time.time())
signature = []
if "signature" in tx_wrapper.transaction["transaction"]:
signature = [int(sig_part) for sig_part in tx_wrapper.transaction["transaction"]["signature"]]
signature = [int(sig_part, 16) for sig_part in tx_wrapper.transaction["transaction"]["signature"]]

parent_block_hash = self.__get_last_block()["block_hash"] if block_number else fixed_length_hex(0)

Expand Down Expand Up @@ -386,7 +386,7 @@ def get_block_by_number(self, block_number: int):

# pylint: disable=too-many-arguments
async def __store_transaction(self, transaction: Transaction, contract_address: int, tx_hash: int, status: TxStatus,
execution_info: StarknetTransactionExecutionInfo, error_message: str=None
execution_info: StarknetTransactionExecutionInfo, error_message: str=None, contract_hash: bytes=None
):
"""Stores the provided data as a deploy transaction in `self.transactions`."""
if transaction.tx_type == TransactionType.DEPLOY:
Expand All @@ -395,7 +395,8 @@ async def __store_transaction(self, transaction: Transaction, contract_address:
contract_address=contract_address,
tx_hash=tx_hash,
status=status,
execution_info=execution_info
execution_info=execution_info,
contract_hash=contract_hash,
)
elif transaction.tx_type == TransactionType.INVOKE_FUNCTION:
tx_wrapper = InvokeTransactionWrapper(transaction, status, execution_info)
Expand Down Expand Up @@ -537,10 +538,4 @@ async def calculate_actual_fee(self, transaction: InvokeFunction):
state_copy = state.state._copy() # pylint: disable=protected-access
execution_info = await internal_tx.apply_state_updates(state_copy, state.general_config)

cairo_resource_usage = execution_info.call_info.execution_resources.to_dict()

return calculate_tx_fee_by_cairo_usage(
general_config=state.general_config,
cairo_resource_usage=cairo_resource_usage,
l1_gas_usage=0
)
return execution_info.actual_fee
16 changes: 13 additions & 3 deletions starknet_devnet/transaction_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DeployTransactionDetails(TransactionDetails):
"""Transaction details of `DeployTransaction`."""
constructor_calldata: List[str]
contract_address_salt: str
class_hash: str


@dataclass
Expand Down Expand Up @@ -118,7 +119,15 @@ class DeployTransactionWrapper(TransactionWrapper):
"""Wrapper of Deploy Transaction."""

# pylint: disable=too-many-arguments
def __init__(self, transaction: Deploy, contract_address: int, tx_hash: int, status: TxStatus, execution_info: StarknetTransactionExecutionInfo):
def __init__(
self,
transaction: Deploy,
contract_address: int,
tx_hash: int,
status: TxStatus,
execution_info: StarknetTransactionExecutionInfo,
contract_hash: bytes
):
super().__init__(
status,
execution_info,
Expand All @@ -127,7 +136,8 @@ def __init__(self, transaction: Deploy, contract_address: int, tx_hash: int, sta
contract_address=fixed_length_hex(contract_address),
transaction_hash=fixed_length_hex(tx_hash),
constructor_calldata=[hex(arg) for arg in transaction.constructor_calldata],
contract_address_salt=hex(transaction.contract_address_salt)
contract_address_salt=hex(transaction.contract_address_salt),
class_hash=fixed_length_hex(int.from_bytes(contract_hash, "big"))
)
)

Expand All @@ -146,6 +156,6 @@ def __init__(self, internal_tx: InternalInvokeFunction, status: TxStatus, execut
calldata=[hex(arg) for arg in internal_tx.calldata],
entry_point_selector=fixed_length_hex(internal_tx.entry_point_selector),
entry_point_type=internal_tx.entry_point_type.name,
signature=[str(sig_part) for sig_part in internal_tx.signature]
signature=[hex(sig_part) for sig_part in internal_tx.signature]
)
)
26 changes: 1 addition & 25 deletions starknet_devnet/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

from starkware.starkware_utils.error_handling import StarkException
from starkware.starknet.testing.contract import StarknetContract
from starkware.starknet.business_logic.state import CarriedState
from starkware.starknet.definitions.general_config import StarknetGeneralConfig
from starkware.starknet.business_logic.state.state import CarriedState

from . import __version__

Expand Down Expand Up @@ -242,26 +241,3 @@ def generate_state_update(previous_state: CarriedState, current_state: CarriedSt
"storage_diffs": storage_diffs
}
}

DEFAULT_GENERAL_CONFIG = StarknetGeneralConfig.load({
"event_commitment_tree_height": 64,
"global_state_commitment_tree_height": 251,
'gas_price': 100000000000,
'starknet_os_config': {
'chain_id': 'TESTNET',
'fee_token_address': '0x20abcf49dad3e9813d65bf1b8d54c5a0c9e6049a3027bd8c2ab315475c0a5c1'
},
'contract_storage_commitment_tree_height': 251,
'cairo_resource_fee_weights': {
'n_steps': 0.05,
'pedersen_builtin': 0.4,
'range_check_builtin': 0.4,
'ecdsa_builtin': 25.6,
'bitwise_builtin': 12.8,
'output_builtin': 0.0,
'ec_op_builtin': 0.0
}, 'invoke_tx_max_n_steps': 1000000,
'sequencer_address': '0x37b2cd6baaa515f520383bee7b7094f892f4c770695fc329a8973e841a971ae',
'tx_version': 0,
'tx_commitment_tree_height': 64
})
63 changes: 30 additions & 33 deletions test/contracts/solidity/IStarknetMessaging.sol
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.12;

interface IStarknetMessaging {
// This event needs to be compatible with the one defined in Output.sol.
event LogMessageToL1(
uint256 indexed from_address,
address indexed to_address,
uint256[] payload
);

// An event that is raised when a message is sent from L1 to L2.
event LogMessageToL2(
address indexed from_address,
uint256 indexed to_address,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);

// An event that is raised when a message from L2 to L1 is consumed.
event ConsumedMessageToL1(
uint256 indexed from_address,
address indexed to_address,
uint256[] payload
);

// An event that is raised when a message from L1 to L2 is consumed.
event ConsumedMessageToL2(
address indexed from_address,
uint256 indexed to_address,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);
import "./IStarknetMessagingEvents.sol";

interface IStarknetMessaging is IStarknetMessagingEvents {
/**
Sends a message to an L2 contract.
Returns the hash of the message.
*/
function sendMessageToL2(
uint256 to_address,
uint256 toAddress,
uint256 selector,
uint256[] calldata payload
) external returns (bytes32);

/**
Consumes a message that was sent from an L2 contract.
Returns the hash of the message.
*/
function consumeMessageFromL2(uint256 fromAddress, uint256[] calldata payload)
external
returns (bytes32);

/**
Starts the cancellation of an L1 to L2 message.
A message can be canceled messageCancellationDelay() seconds after this function is called.
Note: This function may only be called for a message that is currently pending and the caller
must be the sender of the that message.
*/
function startL1ToL2MessageCancellation(
uint256 toAddress,
uint256 selector,
uint256[] calldata payload,
uint256 nonce
) external;

/**
Cancels an L1 to L2 message, this function should be called messageCancellationDelay() seconds
after the call to startL1ToL2MessageCancellation().
*/
function cancelL1ToL2Message(
uint256 toAddress,
uint256 selector,
uint256[] calldata payload,
uint256 nonce
) external;
}
50 changes: 50 additions & 0 deletions test/contracts/solidity/IStarknetMessagingEvents.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.12;

interface IStarknetMessagingEvents {
// This event needs to be compatible with the one defined in Output.sol.
event LogMessageToL1(uint256 indexed fromAddress, address indexed toAddress, uint256[] payload);

// An event that is raised when a message is sent from L1 to L2.
event LogMessageToL2(
address indexed fromAddress,
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);

// An event that is raised when a message from L2 to L1 is consumed.
event ConsumedMessageToL1(
uint256 indexed fromAddress,
address indexed toAddress,
uint256[] payload
);

// An event that is raised when a message from L1 to L2 is consumed.
event ConsumedMessageToL2(
address indexed fromAddress,
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);

// An event that is raised when a message from L1 to L2 Cancellation is started.
event MessageToL2CancellationStarted(
address indexed fromAddress,
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);

// An event that is raised when a message from L1 to L2 is canceled.
event MessageToL2Canceled(
address indexed fromAddress,
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);
}
Loading

0 comments on commit a6c8f8e

Please sign in to comment.