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

Commit

Permalink
Fix block loading [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
FabijanC committed Aug 8, 2023
1 parent 4b03f7d commit 3954e17
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
24 changes: 17 additions & 7 deletions starknet_devnet/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,23 @@ async def abort_latest_block(self, block_hash: str) -> str:
numeric_hash = _parse_block_hash(block_hash)
block = self.__hash2block[numeric_hash]

# This is done like this because the block object's properties cannot be modified
block_dict = block.dump()
block_dict["status"] = BlockStatus.ABORTED.name
block_dict["transaction_receipts"] = None
del self.__num2hash[block_dict["block_number"]]
block_dict["block_number"] = None
self.__hash2block[numeric_hash] = StarknetBlock.load(block_dict)
# old block's properties cannot be modified, so we are constructing a new object
aborted_block = StarknetBlock(
block_hash=block.block_hash,
parent_block_hash=block.parent_block_hash,
block_number=None,
state_root=block.state_root,
status=BlockStatus.ABORTED,
gas_price=block.gas_price,
transactions=block.transactions,
timestamp=block.timestamp,
sequencer_address=block.sequencer_address,
transaction_receipts=None,
starknet_version=block.starknet_version,
)

del self.__num2hash[block.block_number]
self.__hash2block[numeric_hash] = aborted_block
self.__state_archive.remove(numeric_hash)

return block.block_hash
39 changes: 20 additions & 19 deletions test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from starkware.starknet.services.api.feeder_gateway.response_objects import (
TransactionInfo,
)
from starkware.starkware_utils.error_handling import StarkErrorCode

from starknet_devnet.general_config import DEFAULT_GENERAL_CONFIG

Expand Down Expand Up @@ -593,36 +594,36 @@ def get_block(
block_number=None, block_hash=None, parse=False, feeder_gateway_url=APP_URL
):
"""Get the block with block_number. If no number provided, return the last."""
args = ["get_block"]
if block_number:
args.extend(["--number", str(block_number)])
if block_hash:
args.extend(["--hash", str(block_hash)])

if parse:
output = run_starknet(
args, raise_on_nonzero=True, gateway_url=feeder_gateway_url
if block_hash is None:
block_number_str = (
block_number if isinstance(block_number, str) else block_number
)
return json.loads(output.stdout)
params = {"blockNumber": block_number_str}
else:
params = {"blockHash": str(block_hash)}

return run_starknet(args, raise_on_nonzero=False, gateway_url=feeder_gateway_url)
resp = requests.get(
f"{feeder_gateway_url}/feeder_gateway/get_block",
params=params,
)

return resp.json()


def assert_negative_block_input():
"""Test behavior if get_block provided with negative input."""
try:
get_block(-1, parse=True)
raise RuntimeError("Should have failed on negative block number")
except ReturnCodeAssertionError:
print("Correctly rejecting negative block number")
negative_block_number = -1
resp = get_block(block_number=negative_block_number, parse=True)
assert resp["code"] == str(StarkErrorCode.MALFORMED_REQUEST)
assert resp["message"] == f"Invalid block number: '{negative_block_number}'"


def assert_block(latest_block_number, latest_tx_hash):
"""Asserts the content of the block with block_number."""
too_big = 1000
error_message = get_block(block_number=too_big, parse=False).stderr
too_big = 1_000_000
error_resp = get_block(block_number=too_big, parse=False)
total_blocks_str = re.search(
"There are currently (.*) blocks.", error_message
"There are currently (.*) blocks.", error_resp["message"]
).group(1)
total_blocks = int(total_blocks_str)
extracted_last_block_number = total_blocks - 1
Expand Down

0 comments on commit 3954e17

Please sign in to comment.