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

Improve logging #487

Merged
merged 18 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion page/docs/guide/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar_position: 13

## Predeployed accounts

Devnet predeploys `--accounts` with some `--initial-balance`. To hide the details of these accounts use `--hide-predeployed-accounts`. The accounts get charged for transactions according to the `--gas-price`. A `--seed` can be used to regenerate the same set of accounts. Read more about it in the [Run section](run.md).
Devnet predeploys `--accounts` with some `--initial-balance`. To hide the details of these accounts use `--hide-predeployed-contracts`. The accounts get charged for transactions according to the `--gas-price`. A `--seed` can be used to regenerate the same set of accounts. Read more about it in the [Run section](run.md).
FabijanC marked this conversation as resolved.
Show resolved Hide resolved

To get the code of the account (currently OpenZeppelin [v0.5.1](https://github.com/OpenZeppelin/cairo-contracts/releases/tag/v0.5.1)), use one of the following:

Expand Down
11 changes: 6 additions & 5 deletions page/docs/guide/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ sidebar_position: 1
Installing the package adds the `starknet-devnet` command.

```text
usage: starknet-devnet [-h] [-v] [--host HOST] [--port PORT] [--load-path LOAD_PATH] [--dump-path DUMP_PATH] [--dump-on DUMP_ON]
usage: starknet-devnet [-h] [-v] [-V] [--host HOST] [--port PORT] [--load-path LOAD_PATH] [--dump-path DUMP_PATH] [--dump-on DUMP_ON]
[--lite-mode] [--blocks-on-demand] [--accounts ACCOUNTS] [--initial-balance INITIAL_BALANCE] [--seed SEED]
[--hide-predeployed-accounts] [--start-time START_TIME] [--gas-price GAS_PRICE] [--allow-max-fee-zero]
[--hide-predeployed-contracts] [--start-time START_TIME] [--gas-price GAS_PRICE] [--allow-max-fee-zero]
[--timeout TIMEOUT] [--account-class ACCOUNT_CLASS] [--fork-network FORK_NETWORK] [--fork-block FORK_BLOCK]
[--fork-retries FORK_RETRIES] [--chain-id CHAIN_ID] [--disable-rpc-request-validation]
[--disable-rpc-response-validation]
Expand All @@ -18,7 +18,8 @@ Run a local instance of Starknet Devnet

optional arguments:
-h, --help show this help message and exit
-v, --version Print the version
-V, --version Print the version
tonypony220 marked this conversation as resolved.
Show resolved Hide resolved
-v, --verbose Show more verbose logs
--host HOST Specify the address to listen at; defaults to 127.0.0.1 (use the address the program outputs on start)
--port PORT, -p PORT Specify the port to listen at; defaults to 5050
--load-path LOAD_PATH
Expand All @@ -32,8 +33,8 @@ optional arguments:
--initial-balance INITIAL_BALANCE, -e INITIAL_BALANCE
Specify the initial balance of accounts to be predeployed; defaults to 1e+21
--seed SEED Specify the seed for randomness of accounts to be predeployed
--hide-predeployed-accounts
Prevents from printing the predeployed accounts details
--hide-predeployed-contracts
Prevents from printing the predeployed contracts details
--start-time START_TIME
Specify the start time of the genesis block in Unix time seconds
--gas-price GAS_PRICE, -g GAS_PRICE
Expand Down
2 changes: 1 addition & 1 deletion scripts/test_fork.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PORT2=5050
DEVNET2_URL="http://$HOST:$PORT2"


poetry run starknet-devnet --host "$HOST" --port "$PORT1" --seed 42 --accounts 1 --hide-predeployed-accounts &
poetry run starknet-devnet --host "$HOST" --port "$PORT1" --seed 42 --accounts 1 --hide-predeployed-contracts &
DEVNET1_PID=$!
curl --retry 20 --retry-delay 1 --retry-connrefused -s -o /dev/null "$DEVNET1_URL/is_alive"
echo "Started up devnet1; pid: $DEVNET1_PID"
Expand Down
3 changes: 3 additions & 0 deletions starknet_devnet/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ async def _mimic_constructor(self):
)

await set_balance(starknet.state, self.address, self.initial_balance)

def _print(self):
"""Predeployed Accounts logged in Accounts Wrapper"""
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 5 additions & 5 deletions starknet_devnet/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ def __init__(self, starknet_wrapper):
self.list = []

self.__generate()
if (
starknet_wrapper.config.accounts
and not starknet_wrapper.config.hide_predeployed_accounts
):
self.__print()

def __getitem__(self, index) -> Account:
return self.list[index]
Expand All @@ -43,6 +38,11 @@ async def deploy(self):
"""deploy listed accounts"""
for account in self.list:
await account.deploy()
if (
self.starknet_wrapper.config.accounts
and not self.starknet_wrapper.config.hide_predeployed_contracts
):
self.__print()

def add(self, account):
"""append account to list"""
Expand Down
81 changes: 45 additions & 36 deletions starknet_devnet/blueprints/base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
Base routes
"""

from flask import Blueprint, Response, jsonify, request
from starkware.starkware_utils.error_handling import StarkErrorCode

from starknet_devnet.fee_token import FeeToken
from starknet_devnet.state import state
from starknet_devnet.util import (
LogContext,
StarknetDevnetException,
check_valid_dump_path,
parse_hex_string,
Expand Down Expand Up @@ -129,40 +131,46 @@ def load():
async def increase_time():
"""Increases the block timestamp offset and generates a new block"""
request_dict = request.json or {}
time_s = extract_positive(request_dict, "time")

# Increase block time only when there are no pending transactions
if not state.starknet_wrapper.pending_txs:
state.starknet_wrapper.increase_block_time(time_s)
block = await state.starknet_wrapper.generate_latest_block()
return jsonify(
{"timestamp_increased_by": time_s, "block_hash": hex(block.block_hash)}
)
with LogContext().set_context_name("Increase time") as context:
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
context.update(request_dict)
time_s = extract_positive(request_dict, "time")

# Increase block time only when there are no pending transactions
if not state.starknet_wrapper.pending_txs:
state.starknet_wrapper.increase_block_time(time_s)
block = await state.starknet_wrapper.generate_latest_block()
return jsonify(
{"timestamp_increased_by": time_s, "block_hash": hex(block.block_hash)}
)

raise StarknetDevnetException(
code=StarkErrorCode.INVALID_REQUEST,
status_code=400,
message="Block time can be increased only if there are no pending transactions.",
)
raise StarknetDevnetException(
code=StarkErrorCode.INVALID_REQUEST,
status_code=400,
message="Block time can be increased only if there are no pending transactions.",
)


@base.route("/set_time", methods=["POST"])
async def set_time():
"""Sets the block timestamp offset and generates a new block"""
request_dict = request.json or {}
time_s = extract_positive(request_dict, "time")

# Set block time only when there are no pending transactions
if not state.starknet_wrapper.pending_txs:
state.starknet_wrapper.set_block_time(time_s)
block = await state.starknet_wrapper.generate_latest_block()
return jsonify({"block_timestamp": time_s, "block_hash": hex(block.block_hash)})

raise StarknetDevnetException(
code=StarkErrorCode.MALFORMED_REQUEST,
status_code=400,
message="Block time can be set only if there are no pending transactions.",
)
with LogContext().set_context_name("Set time") as context:
context.update(request_dict)
time_s = extract_positive(request_dict, "time")

# Set block time only when there are no pending transactions
if not state.starknet_wrapper.pending_txs:
state.starknet_wrapper.set_block_time(time_s)
block = await state.starknet_wrapper.generate_latest_block()
return jsonify(
{"block_timestamp": time_s, "block_hash": hex(block.block_hash)}
)

raise StarknetDevnetException(
code=StarkErrorCode.MALFORMED_REQUEST,
status_code=400,
message="Block time can be set only if there are no pending transactions.",
)


@base.route("/account_balance", methods=["GET"])
Expand Down Expand Up @@ -193,16 +201,17 @@ async def get_fee_token():
async def mint():
"""Mint token and transfer to the provided address"""
request_json = request.json or {}
with LogContext().set_context_name("Mint transaction") as context:
context.update(request_json)
address = hex_converter(request_json, "address")
amount = extract_positive(request_json, "amount")
is_lite = request_json.get("lite", False)

tx_hash = await state.starknet_wrapper.fee_token.mint(
to_address=address, amount=amount, lite=is_lite, context=context
)
new_balance = await state.starknet_wrapper.fee_token.get_balance(address)

address = hex_converter(request_json, "address")
amount = extract_positive(request_json, "amount")
is_lite = request_json.get("lite", False)

tx_hash = await state.starknet_wrapper.fee_token.mint(
to_address=address, amount=amount, lite=is_lite
)

new_balance = await state.starknet_wrapper.fee_token.get_balance(address)
return jsonify({"new_balance": new_balance, "unit": "wei", "tx_hash": tx_hash})


Expand Down
102 changes: 55 additions & 47 deletions starknet_devnet/blueprints/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
Gateway routes
"""

import json

from flask import Blueprint, jsonify, request
from starkware.starknet.definitions.error_codes import StarknetErrorCode
from starkware.starknet.definitions.transaction_type import TransactionType
from starkware.starkware_utils.error_handling import StarkErrorCode

from starknet_devnet.devnet_config import DumpOn
from starknet_devnet.state import state
from starknet_devnet.util import StarknetDevnetException, fixed_length_hex
from starknet_devnet.util import (
LogContext,
StarknetDevnetException,
extract_transaction_info_to_log,
fixed_length_hex,
)

from .shared import validate_transaction

Expand All @@ -19,50 +26,51 @@
@gateway.route("/add_transaction", methods=["POST"])
async def add_transaction():
"""Endpoint for accepting (state-changing) transactions."""

transaction = validate_transaction(request.get_data())
tx_type = transaction.tx_type

response_dict = {
"code": StarkErrorCode.TRANSACTION_RECEIVED.name,
}

if tx_type == TransactionType.DECLARE:
contract_class_hash, transaction_hash = await state.starknet_wrapper.declare(
transaction
)
response_dict["class_hash"] = hex(contract_class_hash)

elif tx_type == TransactionType.DEPLOY_ACCOUNT:
(
contract_address,
transaction_hash,
) = await state.starknet_wrapper.deploy_account(transaction)
response_dict["address"] = fixed_length_hex(contract_address)

elif tx_type == TransactionType.DEPLOY:
raise StarknetDevnetException(
code=StarknetErrorCode.DEPRECATED_TRANSACTION,
message="Deploy transaction is no longer supported.",
)

elif tx_type == TransactionType.INVOKE_FUNCTION:
(contract_address, transaction_hash) = await state.starknet_wrapper.invoke(
transaction
)
response_dict["address"] = fixed_length_hex(contract_address)

else:
raise StarknetDevnetException(
code=StarkErrorCode.MALFORMED_REQUEST,
message=f"Invalid tx_type: {tx_type.name}.",
status_code=400,
)

response_dict["transaction_hash"] = hex(transaction_hash)

# after tx
if state.dumper.dump_on == DumpOn.TRANSACTION:
state.dumper.dump()

with LogContext() as context:
context.update(extract_transaction_info_to_log(json.loads(request.get_data())))
transaction = validate_transaction(request.get_data())
tx_type = transaction.tx_type

response_dict = {
"code": StarkErrorCode.TRANSACTION_RECEIVED.name,
}

if tx_type == TransactionType.DECLARE:
(
contract_class_hash,
transaction_hash,
) = await state.starknet_wrapper.declare(transaction, context)
response_dict["class_hash"] = hex(contract_class_hash)

elif tx_type == TransactionType.DEPLOY_ACCOUNT:
(
contract_address,
transaction_hash,
) = await state.starknet_wrapper.deploy_account(transaction, context)
response_dict["address"] = fixed_length_hex(contract_address)

elif tx_type == TransactionType.DEPLOY:
raise StarknetDevnetException(
code=StarknetErrorCode.DEPRECATED_TRANSACTION,
message="Deploy transaction is no longer supported.",
)

elif tx_type == TransactionType.INVOKE_FUNCTION:
(contract_address, transaction_hash) = await state.starknet_wrapper.invoke(
transaction, context
)
response_dict["address"] = fixed_length_hex(contract_address)

else:
raise StarknetDevnetException(
code=StarkErrorCode.MALFORMED_REQUEST,
message=f"Invalid tx_type: {tx_type.name}.",
status_code=400,
)

response_dict["transaction_hash"] = hex(transaction_hash)

# after tx
if state.dumper.dump_on == DumpOn.TRANSACTION:
state.dumper.dump()
return jsonify(response_dict)
Loading