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

Cairo lang 0.8.1 #70

Merged
merged 15 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
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 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
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
21 changes: 8 additions & 13 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 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=self.__current_carried_state.contract_states[contract_address].state.contract_hash
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
)

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"))
badurinantun marked this conversation as resolved.
Show resolved Hide resolved
)
)

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]
)
)
4 changes: 2 additions & 2 deletions starknet_devnet/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +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.business_logic.state.state import CarriedState
from starkware.starknet.definitions.general_config import StarknetGeneralConfig

from . import __version__
Expand Down Expand Up @@ -246,7 +246,6 @@ def generate_state_update(previous_state: CarriedState, current_state: CarriedSt
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'
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -261,6 +260,7 @@ def generate_state_update(previous_state: CarriedState, current_state: CarriedSt
'output_builtin': 0.0,
'ec_op_builtin': 0.0
}, 'invoke_tx_max_n_steps': 1000000,
'min_gas_price': 10000000000,
'sequencer_address': '0x37b2cd6baaa515f520383bee7b7094f892f4c770695fc329a8973e841a971ae',
'tx_version': 0,
'tx_commitment_tree_height': 64
Expand Down
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
);
}
16 changes: 10 additions & 6 deletions test/contracts/solidity/MockStarknetMessaging.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ pragma solidity ^0.6.12;
import "./StarknetMessaging.sol";

contract MockStarknetMessaging is StarknetMessaging {
constructor(uint256 MessageCancellationDelay) public {
messageCancellationDelay(MessageCancellationDelay);
}

/**
Mocks a message from L2 to L1.
*/
function mockSendMessageFromL2(
uint256 from_address,
uint256 to_address,
uint256 fromAddress,
uint256 toAddress,
uint256[] calldata payload
) external {
bytes32 msgHash = keccak256(
abi.encodePacked(from_address, to_address, payload.length, payload)
abi.encodePacked(fromAddress, toAddress, payload.length, payload)
);
l2ToL1Messages()[msgHash] += 1;
}
Expand All @@ -22,14 +26,14 @@ contract MockStarknetMessaging is StarknetMessaging {
Mocks consumption of a message from L1 to L2.
*/
function mockConsumeMessageToL2(
uint256 from_address,
uint256 to_address,
uint256 fromAddress,
uint256 toAddress,
uint256 selector,
uint256[] calldata payload,
uint256 nonce
) external {
bytes32 msgHash = keccak256(
abi.encodePacked(from_address, to_address, nonce, selector, payload.length, payload)
abi.encodePacked(fromAddress, toAddress, nonce, selector, payload.length, payload)
);

require(l1ToL2Messages()[msgHash] > 0, "INVALID_MESSAGE_TO_CONSUME");
Expand Down
12 changes: 12 additions & 0 deletions test/contracts/solidity/NamedStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.6.12;

/*
Library to provide basic storage, in storage location out of the low linear address space.

New types of storage variables should be added here upon need.
*/
library NamedStorage {
Expand All @@ -28,6 +29,17 @@ library NamedStorage {
}
}

function uintToAddressMapping(string memory tag_)
internal
pure
returns (mapping(uint256 => address) storage randomVariable)
{
bytes32 location = keccak256(abi.encodePacked(tag_));
assembly {
randomVariable_slot := location
}
}

function addressToBoolMapping(string memory tag_)
internal
pure
Expand Down
Loading