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

Fix RPC pending block behaviour #349

Merged
merged 4 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 starknet_devnet/blueprints/rpc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async def get_events(
keys = [] if keys is None else [int(k, 0) for k in keys]
to_block = (
int(state.starknet_wrapper.blocks.get_number_of_blocks())
if to_block == "latest"
if to_block in ["latest", "pending"]
else int(to_block) + 1
)
for block_number in range(int(from_block), to_block):
Expand Down
22 changes: 6 additions & 16 deletions starknet_devnet/blueprints/rpc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,22 @@
BlockId,
RpcError,
Felt,
RpcErrorCode,
)
from starknet_devnet.util import StarknetDevnetException
from starknet_devnet.state import state


def block_tag_to_block_number(block_id: BlockId) -> BlockId:
"""
Changes block_id from tag to dict with "block_number" field
Changes block_id from "latest" / "pending" tag to dict with "block_number" field
"""
if isinstance(block_id, str):
if block_id == "latest":
if block_id in ["latest", "pending"]:
return {
"block_number": state.starknet_wrapper.blocks.get_number_of_blocks() - 1
}

if block_id == "pending":
raise RpcError(
code=-1,
message="Calls with block_id == 'pending' are not supported currently.",
)

raise RpcError(code=24, message="Block not found")
raise RpcError(code=RpcErrorCode.INVALID_PARAMS.value, message="Invalid params")

return block_id

Expand All @@ -37,8 +31,7 @@ async def get_block_by_block_id(block_id: BlockId) -> dict:
"""
Get block using different method depending on block_id type
"""
if block_id in ["latest", "pending"]:
block_id = block_tag_to_block_number(block_id)
block_id = block_tag_to_block_number(block_id)

try:
if "block_hash" in block_id:
Expand Down Expand Up @@ -77,10 +70,7 @@ async def assert_block_id_is_latest_or_pending(block_id: BlockId) -> None:
if block_id in ("latest", "pending"):
return

raise RpcError(
code=-1,
message="Calls must be made with block_id of the latest or pending block. Other block_id are not supported.",
)
raise RpcError(code=RpcErrorCode.INVALID_PARAMS.value, message="Invalid params")


def rpc_felt(value: Union[int, str]) -> Felt:
Expand Down
2 changes: 1 addition & 1 deletion test/rpc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _block_to_block_id(block: dict, key: str) -> dict:
return block_id_map[key]


@pytest.fixture(name="block_id", params=["hash", "number", "tag"])
@pytest.fixture(name="block_id", params=["hash", "number", "tag", "tag_pending"])
def fixture_block_id(gateway_block, request) -> dict:
"""
BlockId of gateway_block depending on type in request
Expand Down
7 changes: 4 additions & 3 deletions test/rpc/test_rpc_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"""

from test.rpc.rpc_utils import rpc_call
import pytest

import pytest
from starkware.starknet.public.abi import get_selector_from_name

from starknet_devnet.blueprints.rpc.structures.types import RpcErrorCode
from starknet_devnet.blueprints.rpc.utils import rpc_felt


Expand Down Expand Up @@ -145,8 +146,8 @@ def test_call_raises_on_incorrect_block_hash(deploy_info):
)

assert ex["error"] == {
"code": -1,
"message": "Calls must be made with block_id of the latest or pending block. Other block_id are not supported.",
"code": RpcErrorCode.INVALID_PARAMS.value,
"message": "Invalid params",
}


Expand Down
5 changes: 3 additions & 2 deletions test/rpc/test_rpc_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
from starkware.starknet.public.abi import get_storage_var_address

from starknet_devnet.blueprints.rpc.structures.types import RpcErrorCode
from starknet_devnet.blueprints.rpc.utils import rpc_felt


Expand Down Expand Up @@ -93,6 +94,6 @@ def test_get_storage_at_raises_on_incorrect_block_id(deploy_info):
)

assert ex["error"] == {
"code": -1,
"message": "Calls must be made with block_id of the latest or pending block. Other block_id are not supported.",
"code": RpcErrorCode.INVALID_PARAMS.value,
"message": "Invalid params",
}