Skip to content

Commit

Permalink
Adapt to StarkNet 0.10.2 (0xSpaceShard#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
FabijanC committed Nov 23, 2022
1 parent fc5e204 commit 563ca48
Show file tree
Hide file tree
Showing 20 changed files with 238 additions and 87 deletions.
5 changes: 3 additions & 2 deletions page/docs/guide/Interaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ sidebar_position: 2
- Interact with Devnet as you would with the official StarkNet [**Alpha testnet**](https://www.cairo-lang.org/docs/hello_starknet/amm.html?highlight=alpha#interaction-examples).
- The exact underlying API is not exposed for the same reason Alpha testnet does not expose it.
- To use Devnet with StarkNet CLI, provide Devnet's URL to the `--gateway_url` and `--feeder_gateway_url` options of StarkNet CLI commands.
- The following StarkNet CLI commands are supported:
- The following StarkNet gateway endpoints are supported (mostly corresponding to StarkNet CLI commands):
- `call`
- `declare`
- `deploy`
- `deploy_account`
- `estimate_fee`
- `estimate_fee_bulk`
- `get_block` (currently pending block is not supported)
- `get_block_traces`
- `get_class_by_hash`
Expand All @@ -28,4 +29,4 @@ sidebar_position: 2
- `invoke`
- `tx_status`
- The following StarkNet CLI commands are **not** supported:
- `get_contract_addresses`
- `get_contract_addresses`
6 changes: 3 additions & 3 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["starknet", "cairo", "testnet", "local", "server"]
python = ">=3.8,<3.10"
Flask = {extras = ["async"], version = "~2.0.3"}
flask-cors = "~3.0.10"
cairo-lang = "0.10.1"
cairo-lang = "0.10.2"
Werkzeug = "~2.0.3"
cloudpickle = "~2.1.0"
crypto-cpp-py = "~1.0.4"
Expand Down
21 changes: 19 additions & 2 deletions starknet_devnet/blueprints/feeder_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
feeder_gateway = Blueprint("feeder_gateway", __name__, url_prefix="/feeder_gateway")


def validate_request(data: bytes, cls):
def validate_request(data: bytes, cls, many=False):
"""Ensure `data` is valid Starknet function call. Returns an object of type specified with `cls`."""
try:
return cls.loads(data)
return cls.Schema().loads(data, many=many)
except (TypeError, ValidationError) as err:
raise StarknetDevnetException(
code=StarkErrorCode.MALFORMED_REQUEST,
Expand Down Expand Up @@ -297,6 +297,23 @@ async def estimate_fee():
return jsonify(fee_response)


@feeder_gateway.route("/estimate_fee_bulk", methods=["POST"])
async def estimate_fee_bulk():
"""Returns the estimated fee for a bulk of transactions."""

try:
# version 1
transactions = validate_request(request.data, Transaction, many=True)
except StarknetDevnetException:
# version 0
transactions = validate_request(request.data, InvokeFunction, many=True)

_, fee_responses = await state.starknet_wrapper.calculate_traces_and_fees(
transactions
)
return jsonify(fee_responses)


@feeder_gateway.route("/simulate_transaction", methods=["POST"])
async def simulate_transaction():
"""Returns the estimated fee for a transaction."""
Expand Down
2 changes: 1 addition & 1 deletion starknet_devnet/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

# account used by StarkNet CLI
OZ_ACCOUNT_CLASS_HASH = (
895370652103566112291566439803611591116951595367594863638369163604569619773
0x68CB33B3AB73EE34D2084CFCB7D07B24DB48095AD0907C10B6FDB7B0E91EF0A
)

LEGACY_RPC_TX_VERSION = 0
1 change: 1 addition & 0 deletions starknet_devnet/forked_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def get_forked_starknet(
gas_price=gas_price,
),
state_reader=state_reader,
contract_class_cache={},
),
general_config=DEFAULT_GENERAL_CONFIG,
)
Expand Down
77 changes: 47 additions & 30 deletions starknet_devnet/starknet_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,42 +572,59 @@ async def postman_flush(self) -> dict:

async def calculate_trace_and_fee(self, external_tx: InvokeFunction):
"""Calculates trace and fee by simulating tx on state copy."""
traces, fees = await self.calculate_traces_and_fees([external_tx])
assert len(traces) == len(fees) == 1
return traces[0], fees[0]

async def calculate_traces_and_fees(self, external_txs: List[InvokeFunction]):
"""Calculates traces and fees by simulating tx on state copy.
Uses the resulting state for each consecutive estimation"""
state = self.get_state()
try:
internal_tx = InternalInvokeFunctionForSimulate.from_external(
external_tx, state.general_config
)
except AssertionError as error:
raise StarknetDevnetException(
code=StarkErrorCode.MALFORMED_REQUEST,
status_code=400,
message="Invalid format of fee estimation request",
) from error
cached_state_copy = state.state

traces = []
fee_estimation_infos = []

execution_info = await internal_tx.apply_state_updates(
for external_tx in external_txs:
# pylint: disable=protected-access
state.state._copy(),
state.general_config,
)
cached_state_copy = cached_state_copy._copy()
try:
internal_tx = InternalInvokeFunctionForSimulate.from_external(
external_tx, state.general_config
)
except AssertionError as error:
raise StarknetDevnetException(
code=StarkErrorCode.MALFORMED_REQUEST,
status_code=400,
message="Invalid format of fee estimation request",
) from error

execution_info = await internal_tx.apply_state_updates(
cached_state_copy,
state.general_config,
)

trace = TransactionTrace(
validate_invocation=FunctionInvocation.from_optional_internal(
execution_info.validate_info
),
function_invocation=FunctionInvocation.from_optional_internal(
execution_info.call_info
),
fee_transfer_invocation=FunctionInvocation.from_optional_internal(
execution_info.fee_transfer_info
),
signature=external_tx.signature,
)
trace = TransactionTrace(
validate_invocation=FunctionInvocation.from_optional_internal(
execution_info.validate_info
),
function_invocation=FunctionInvocation.from_optional_internal(
execution_info.call_info
),
fee_transfer_invocation=FunctionInvocation.from_optional_internal(
execution_info.fee_transfer_info
),
signature=external_tx.signature,
)
traces.append(trace)

fee_estimation_info = get_fee_estimation_info(
execution_info.actual_fee, state.state.block_info.gas_price
)
fee_estimation_info = get_fee_estimation_info(
execution_info.actual_fee, state.state.block_info.gas_price
)
fee_estimation_infos.append(fee_estimation_info)

return trace, fee_estimation_info
assert len(traces) == len(fee_estimation_infos) == len(external_txs)
return traces, fee_estimation_infos

async def estimate_message_fee(self, call: CallL1Handler):
"""Estimate fee of message from L1 to L2"""
Expand Down
4 changes: 2 additions & 2 deletions test/expected/deploy_function_invocation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"call_type": "CALL",
"calldata": ["0x0"],
"caller_address": "0x0",
"class_hash": "0x585b5b6f0bb1f9ba151f6fdc921d671e90e83c46eb03fc25e1fdcc470398c32",
"contract_address": "0x207dee8b29749f496d50cec13664a0205ff450d8072182322c2f45d5d9b22fe",
"class_hash": "0x75dc4457c66bfb3808c67a918047b8f0973ea8b876056b72e99f651b91298ca",
"contract_address": "0x333c17a58a5e9d3e8f745d354fee89f83196e8fd0ded1c9766c24425276093f",
"entry_point_type": "CONSTRUCTOR",
"events": [],
"execution_resources": {
Expand Down
6 changes: 3 additions & 3 deletions test/expected/invoke_function_invocation.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"events": [],
"calldata": [
"0x1",
"0x207dee8b29749f496d50cec13664a0205ff450d8072182322c2f45d5d9b22fe",
"0x333c17a58a5e9d3e8f745d354fee89f83196e8fd0ded1c9766c24425276093f",
"0x362398bec32bc0ebb411203221a35a0301193a96f317ebe5e40be9f60d15320",
"0x0",
"0x2",
Expand All @@ -34,7 +34,7 @@
},
"messages": [],
"call_type": "CALL",
"class_hash": "0x585b5b6f0bb1f9ba151f6fdc921d671e90e83c46eb03fc25e1fdcc470398c32",
"class_hash": "0x75dc4457c66bfb3808c67a918047b8f0973ea8b876056b72e99f651b91298ca",
"result": [],
"events": [],
"calldata": [
Expand All @@ -44,7 +44,7 @@
"caller_address": "0x347be35996a21f6bf0623e75dbce52baba918ad5ae8d83b6f416045ab22961a",
"entry_point_type": "EXTERNAL",
"selector": "0x362398bec32bc0ebb411203221a35a0301193a96f317ebe5e40be9f60d15320",
"contract_address": "0x207dee8b29749f496d50cec13664a0205ff450d8072182322c2f45d5d9b22fe",
"contract_address": "0x333c17a58a5e9d3e8f745d354fee89f83196e8fd0ded1c9766c24425276093f",
"internal_calls": []
}
]
Expand Down
2 changes: 1 addition & 1 deletion test/expected/invoke_receipt.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"data": [
"0x347be35996a21f6bf0623e75dbce52baba918ad5ae8d83b6f416045ab22961a",
"0x388ca486b82e20cc81965d056b4cdcaacdffe0cf08e20ed8ba10ea97a487004",
"0x416017615e785233a2ec6b6a16058eef596a40171b2517add5167bd5ba1ab88",
"0x16010bdfe1800",
"0x0"
],
Expand Down
6 changes: 3 additions & 3 deletions test/expected/invoke_receipt_account_event.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"0x0",
"0xa"
],
"from_address": "0x2f248e35e11102ef6c9fd694ac81874b86165c858602882e037f72bd18ae216",
"from_address": "0x1fbcb39359c1d204c9fa13318451c208026c01b77513d4eac72b13798184372",
"keys": [
"0x3db3da4221c078e78bd987e54e1cc24570d89a7002cefa33e548d6c72c73f9d"
]
},
{
"data": [
"0x347be35996a21f6bf0623e75dbce52baba918ad5ae8d83b6f416045ab22961a",
"0x388ca486b82e20cc81965d056b4cdcaacdffe0cf08e20ed8ba10ea97a487004",
"0x5a569a2800",
"0x416017615e785233a2ec6b6a16058eef596a40171b2517add5167bd5ba1ab88",
"0x5a5c900900",
"0x0"
],
"from_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
Expand Down
6 changes: 3 additions & 3 deletions test/expected/invoke_receipt_event.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"0x0",
"0xa"
],
"from_address": "0x2f248e35e11102ef6c9fd694ac81874b86165c858602882e037f72bd18ae216",
"from_address": "0x1fbcb39359c1d204c9fa13318451c208026c01b77513d4eac72b13798184372",
"keys": [
"0x3db3da4221c078e78bd987e54e1cc24570d89a7002cefa33e548d6c72c73f9d"
]
},
{
"data": [
"0x347be35996a21f6bf0623e75dbce52baba918ad5ae8d83b6f416045ab22961a",
"0x388ca486b82e20cc81965d056b4cdcaacdffe0cf08e20ed8ba10ea97a487004",
"0x160e24a2c4000",
"0x416017615e785233a2ec6b6a16058eef596a40171b2517add5167bd5ba1ab88",
"0x160f992a32800",
"0x0"
],
"from_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
Expand Down
12 changes: 1 addition & 11 deletions test/rpc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
)
from starkware.starknet.business_logic.transaction.objects import InternalDeployAccount
from starkware.starknet.core.os.class_hash import compute_class_hash
from starkware.starknet.core.os.contract_address.contract_address import (
calculate_contract_address,
)
from starkware.starknet.definitions.general_config import DEFAULT_CHAIN_ID
from starkware.starknet.services.api.feeder_gateway.response_objects import (
DeployAccountSpecificInfo,
Expand Down Expand Up @@ -204,7 +201,7 @@ def prepare_deploy_account_tx(
private_key: int, public_key: int, account_salt: int, contract_class: ContractClass
) -> Tuple[DeployAccount, int]:
"""Return (signed deploy account tx, address)"""
deploy_account_tx = sign_deploy_account_tx(
account_address, deploy_account_tx = sign_deploy_account_tx(
private_key=private_key,
public_key=public_key,
class_hash=compute_class_hash(contract_class),
Expand All @@ -215,13 +212,6 @@ def prepare_deploy_account_tx(
nonce=0,
)

account_address = calculate_contract_address(
salt=account_salt,
contract_class=contract_class,
constructor_calldata=[public_key],
deployer_address=0,
)

return deploy_account_tx, account_address


Expand Down
6 changes: 3 additions & 3 deletions test/rpc/test_data/get_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@
INCREASE_BALANCE_BY_0_EVENT = [0, 0]
INCREASE_BALANCE_BY_1_EVENT = [0, 1]
EVENT_FEE_ADDRESS = (
1598625851760128517552627854997699631064626954749952456622017584404508471300
1848132115085043480193496279185058626650378119257763769763822937432943340424
)
FEE_CHARGING_IN_BLOCK_2_EVENT = [
int(PREDEPLOYED_ACCOUNT_ADDRESS, 16),
EVENT_FEE_ADDRESS,
143200000000000, # WEI
143300000000000, # WEI
0,
]
FEE_CHARGING_IN_BLOCK_3_EVENT = [
int(PREDEPLOYED_ACCOUNT_ADDRESS, 16),
EVENT_FEE_ADDRESS,
388000000000000, # WEI
388100000000000, # WEI
0,
]

Expand Down
Loading

0 comments on commit 563ca48

Please sign in to comment.