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

genesis block, create block endpoint, performace optimizations, initial boot optimization, fix 188 #189

Merged
merged 30 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0ec3578
fix(118) warning on zero division with normal response
tabaktoni Jul 21, 2022
3ea96c8
fix: test zero gas price
tabaktoni Jul 21, 2022
22aa29b
fix: replace lstrip with split for rpc method name
tabaktoni Jul 21, 2022
092aee8
Feature: do initialization on package run, this slows down initial st…
tabaktoni Jul 22, 2022
28d52f5
Feature create empty block
tabaktoni Jul 22, 2022
6cf5d47
feature: boot indicator, refactor wrapper async
tabaktoni Jul 22, 2022
17845f0
revert RPC fix
tabaktoni Jul 22, 2022
5ea9bc1
Merge branch 'master' into fix/188-zero-division-gas-price
tabaktoni Jul 22, 2022
9958e44
Fix: validate transaction_receipts exist, exstrast to fun
tabaktoni Jul 25, 2022
c254ea5
FIx: removed async call
tabaktoni Jul 25, 2022
06f2b43
Fix: Lints and Transaction_receipts in empty block instead of None tr…
tabaktoni Jul 25, 2022
3ee69da
Merge branch 'master' into fix/188-zero-division-gas-price
tabaktoni Jul 25, 2022
bcb6f24
Fix: Tests
tabaktoni Jul 25, 2022
a95e31a
Fix: Tests with Genesis block
tabaktoni Jul 27, 2022
34be494
Fix: remove time testing, lint, initialize in server main instead of …
tabaktoni Jul 27, 2022
b8457e5
Merge branch 'master' into fix/188-zero-division-gas-price
tabaktoni Jul 27, 2022
393a28a
Feat: create_block docs and test
tabaktoni Jul 27, 2022
06a9fc6
Fix: test function naming
tabaktoni Jul 27, 2022
935e5f5
fix: cleanup
tabaktoni Aug 2, 2022
e0d7d7f
fix: remove check by timestamp
tabaktoni Aug 2, 2022
5e51da4
feature: added pylint quotes extension
tabaktoni Aug 2, 2022
c6fe2c6
Fix: test enhanced
tabaktoni Aug 2, 2022
7e5943d
fix: post create_block
tabaktoni Aug 2, 2022
909ec2e
fix: tipfeler and quotes
tabaktoni Aug 2, 2022
c2f0fcb
Merge branch 'master' into fix/188-zero-division-gas-price
tabaktoni Aug 2, 2022
199f4dd
cleanup
tabaktoni Aug 2, 2022
1455f70
Merge branch 'master' into fix/188-zero-division-gas-price
tabaktoni Aug 3, 2022
9fcf2d5
Fix: test and clenup
tabaktoni Aug 3, 2022
d78e17e
Merge branch 'master' into fix/188-zero-division-gas-price
tabaktoni Aug 3, 2022
7d95ac2
fix: starknet cleanup after merge, test
tabaktoni Aug 3, 2022
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
3 changes: 3 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[FORMAT]
max-line-length=150
string-quote=double
triple-quote=double
docstring-quote=double

[BASIC]
min-public-methods=1
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,38 @@ docker run \

A local block explorer (Voyager), as noted [here](https://voyager.online/local-version/), apparently cannot be set up to work with Devnet. Read more in [this issue](https://github.com/Shard-Labs/starknet-devnet/issues/60).

## Block

Devnet start with a genesis block.

GENESIS_BLOCK_NUMBER = 0

GENESIS_BLOCK_HASH = "0x0"

You can create empty block without transaction.

```
POST /create_block
```

Response:

```
{
"transactions": [],
"parent_block_hash": "0x0",
"timestamp": 1659457385,
"state_root": "004bee3ee...",
"gas_price": "0x174876e800",
"sequencer_address": "0x4bbfb0d1aa...",
"transaction_receipts": [],
"starknet_version": "0.9.1",
"block_hash": "0x1",
"block_number": 1,
"status": "ACCEPTED_ON_L2"
}
```

## Lite mode

To improve Devnet performance, instead of calculating the actual hash of deployment transactions and blocks, sequential numbering can be used (0x0, 0x1, 0x2, ...).
Expand Down
23 changes: 15 additions & 8 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ web3 = "~5.28.0"
psutil = "~5.9.1"
jsonschema = "~3.2.0"
pytest-xdist = "~2.5.0"
pylint-quotes = "~0.2.3"

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

set -e

poetry run pylint $(git ls-files '*.py')
poetry run pylint --load-plugins pylint_quotes $(git ls-files '*.py')
47 changes: 47 additions & 0 deletions starknet_devnet/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,50 @@ async def generate(
self.__state_updates[block_number] = state_update

return block

def generate_empty(
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
self, state: StarknetState, state_root: bytes, state_update = None
) -> StarknetBlock:
"""
Generate block without transaction
"""
block_number = self.get_number_of_blocks()
timestamp = state.state.block_info.block_timestamp

if block_number == 0:
parent_block_hash = 0
else:
last_block = self.__get_last_block()
parent_block_hash = last_block.block_hash

#Fake block number
block_hash = block_number

block = StarknetBlock.create(
block_hash=block_hash,
block_number=block_number,
state_root=state_root,
transactions=[],
timestamp=timestamp,
transaction_receipts=(),
status=BlockStatus.ACCEPTED_ON_L2,
gas_price=state.state.block_info.gas_price,
sequencer_address=state.general_config.sequencer_address,
parent_block_hash=parent_block_hash,
starknet_version=CAIRO_LANG_VERSION
)

self.__num2block[block_number] = block
self.__hash2num[block_hash] = block_number

if state_update is not None:
state_update = BlockStateUpdate(
block_hash=block_hash,
old_root=state_update.old_root,
new_root=state_update.new_root,
state_diff=state_update.state_diff,
)

self.__state_updates[block_number] = state_update

return block
6 changes: 6 additions & 0 deletions starknet_devnet/blueprints/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,9 @@ async def mint():
"unit": "wei",
"tx_hash": tx_hash
})

@base.route("/create_block", methods=["POST"])
async def create_block():
"""Create empty block"""
block = await state.starknet_wrapper.create_empty_block()
return Response(block.dumps(), status=200, mimetype="application/json")
29 changes: 16 additions & 13 deletions starknet_devnet/blueprints/feeder_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ def _get_block_object(block_hash: str, block_number: int):

return block

def _get_block_transaction_traces(block):
traces = []
if block.transaction_receipts:
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
for transaction in block.transaction_receipts:
tx_hash = hex(transaction.transaction_hash)
trace = state.starknet_wrapper.transactions.get_transaction_trace(tx_hash)

# expected trace is equal to response of get_transaction, but with the hash property
trace_dict = trace.dump()
trace_dict["transaction_hash"] = tx_hash
traces.append(trace_dict)

# assert correct structure
return BlockTransactionTraces.load({ "traces": traces })

@feeder_gateway.route("/is_alive", methods=["GET"])
def is_alive():
"""Health check endpoint."""
Expand Down Expand Up @@ -86,19 +101,7 @@ def get_block_traces():
block_number = request.args.get("blockNumber", type=custom_int)

block = _get_block_object(block_hash=block_hash, block_number=block_number)

traces = []
for transaction in block.transaction_receipts:
tx_hash = hex(transaction.transaction_hash)
trace = state.starknet_wrapper.transactions.get_transaction_trace(tx_hash)

# expected trace is equal to response of get_transaction, but with the hash property
trace_dict = trace.dump()
trace_dict["transaction_hash"] = tx_hash
traces.append(trace_dict)

# assert correct structure
block_transaction_traces = BlockTransactionTraces.load({ "traces": traces })
block_transaction_traces = _get_block_transaction_traces(block)

return jsonify(block_transaction_traces.dump())

Expand Down
6 changes: 3 additions & 3 deletions starknet_devnet/blueprints/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async def call(contract_address: str, entry_point_selector: str, calldata: list,
raise RpcError(code=-1, message=ex.message) from ex


async def estimate_fee():
async def estimate_fee(request_body: dict):
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
"""
Get the estimate fee for the transaction
"""
Expand All @@ -304,7 +304,7 @@ async def chain_id() -> str:
"""
Return the currently configured StarkNet chain id
"""
devnet_state = await state.starknet_wrapper.get_state()
devnet_state = state.starknet_wrapper.get_state()
config = devnet_state.general_config
chain: int = config.chain_id.value
return hex(chain)
Expand Down Expand Up @@ -526,7 +526,7 @@ def new_root() -> str:
}
transactions: list = await mapping[requested_scope]()

devnet_state = await state.starknet_wrapper.get_state()
devnet_state = state.starknet_wrapper.get_state()
config = devnet_state.general_config

block: RpcBlock = {
Expand Down
6 changes: 3 additions & 3 deletions starknet_devnet/fee_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ async def deploy(self):
state=newly_deployed_fee_token_state,
storage_updates={
# Running the constructor doesn't need to be simulated
get_selector_from_name('ERC20_name'): StorageLeaf(int.from_bytes(bytes(FeeToken.NAME, "ascii"), "big")),
get_selector_from_name('ERC20_symbol'): StorageLeaf(int.from_bytes(bytes(FeeToken.SYMBOL, "ascii"), "big")),
get_selector_from_name('ERC20_decimals'): StorageLeaf(18)
get_selector_from_name("ERC20_name"): StorageLeaf(int.from_bytes(bytes(FeeToken.NAME, "ascii"), "big")),
get_selector_from_name("ERC20_symbol"): StorageLeaf(int.from_bytes(bytes(FeeToken.SYMBOL, "ascii"), "big")),
get_selector_from_name("ERC20_decimals"): StorageLeaf(18)
}
)

Expand Down
3 changes: 3 additions & 0 deletions starknet_devnet/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pickle import UnpicklingError
import sys
import asyncio

from flask import Flask, jsonify
from flask_cors import CORS
Expand Down Expand Up @@ -102,6 +103,8 @@ def main():
set_start_time(args)
set_gas_price(args)

asyncio.run(state.starknet_wrapper.initialize())

try:
meinheld.listen((args.host, args.port))
print(f" * Listening on http://{args.host}:{args.port}/ (Press CTRL+C to quit)")
Expand Down
Loading