Skip to content
Open
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ benchmark:
poetry run brownie run examples/benchmark

mypyc:
mypyc dank_mids/_batch.py dank_mids/_demo_mode.py dank_mids/_envs.py dank_mids/_eth_utils.py dank_mids/_exceptions.py dank_mids/_logging.py dank_mids/_tasks.py dank_mids/_uid.py dank_mids/_vendor/aiolimiter/src dank_mids/_web3/abi.py dank_mids/_web3/formatters.py dank_mids/brownie_patch/__init__.py dank_mids/brownie_patch/_abi.py dank_mids/brownie_patch/call.py dank_mids/brownie_patch/overloaded.py dank_mids/brownie_patch/types.py dank_mids/helpers/__init__.py dank_mids/helpers/_codec.py dank_mids/helpers/_errors.py dank_mids/helpers/_gather.py dank_mids/helpers/_rate_limit.py dank_mids/helpers/_weaklist.py dank_mids/helpers/batch_size.py dank_mids/helpers/hashing.py dank_mids/helpers/lru_cache.py dank_mids/helpers/method.py dank_mids/stats/__init__.py dank_mids/constants.py dank_mids/controller.py dank_mids/ENVIRONMENT_VARIABLES.py --strict --pretty --disable-error-code=unused-ignore
mypyc dank_mids/_batch.py dank_mids/_demo_mode.py dank_mids/_envs.py dank_mids/_eth_utils.py dank_mids/_exceptions.py dank_mids/_logging.py dank_mids/_tasks.py dank_mids/_uid.py dank_mids/_vendor/aiolimiter/src dank_mids/_web3/abi.py dank_mids/_web3/formatters.py dank_mids/brownie_patch/__init__.py dank_mids/brownie_patch/_abi.py dank_mids/brownie_patch/call.py dank_mids/brownie_patch/overloaded.py dank_mids/brownie_patch/types.py dank_mids/helpers/__init__.py dank_mids/helpers/_codec.py dank_mids/helpers/_errors.py dank_mids/helpers/_gather.py dank_mids/helpers/_rate_limit.py dank_mids/helpers/_weaklist.py dank_mids/helpers/batch_size.py dank_mids/helpers/hashing.py dank_mids/helpers/lru_cache.py dank_mids/helpers/method.py dank_mids/stats/__init__.py dank_mids/constants.py dank_mids/controller.py dank_mids/ENVIRONMENT_VARIABLES.py dank_mids/semaphores.py --strict --pretty --disable-error-code=unused-ignore


# Vendoring
Expand Down
3 changes: 2 additions & 1 deletion dank_mids/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def __init__(self, w3: Web3) -> None:
self._latest_mc: Final = self.mc3 or self.mc2

self.eth_call_semaphores: Final = BlockSemaphore(
ENVS.method_semaphores["eth_call"]._value, name=f"eth_call {self}"
BlockNumber(ENVS.method_semaphores["eth_call"]._value),
name=f"eth_call {self}",
)
"""Used for managing concurrency of eth_calls."""

Expand Down
26 changes: 15 additions & 11 deletions dank_mids/semaphores.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
from decimal import Decimal
from typing import Final, Literal, Optional, Type, Union
from typing import Final, Literal, Optional, Type, Union, final

import a_sync
from a_sync.primitives.locks.prio_semaphore import (
_AbstractPrioritySemaphore,
_PrioritySemaphoreContextManager,
)
from eth_typing import HexStr
from eth_typing import BlockNumber, HexStr


_TOP_PRIORITY: Final = -1


class _BlockSemaphoreContextManager(_PrioritySemaphoreContextManager):
@final
class _BlockSemaphoreContextManager(_PrioritySemaphoreContextManager): # type: ignore [type-arg]
"""
A context manager for block-specific semaphores.

This class is used internally to manage concurrency for operations
related to specific blockchain blocks.
"""

_priority_name = "block"
_priority_name: Final = "block"
"""The noun that describes the priority, set to "block"."""

def __init__(
Expand All @@ -34,9 +34,8 @@ def __init__(
super().__init__(parent, priority, name)


# NOTE: keep this so we can include in type stubs
# class BlockSemaphore(_AbstractPrioritySemaphore[str, _BlockSemaphoreContextManager]): # type: ignore [type-var]
class BlockSemaphore(_AbstractPrioritySemaphore):
@final
class BlockSemaphore(_AbstractPrioritySemaphore[str, _BlockSemaphoreContextManager]):
"""A semaphore for managing concurrency based on block numbers.

This class extends :class:`_AbstractPrioritySemaphore` to provide block-specific concurrency control.
Expand All @@ -52,10 +51,15 @@ class BlockSemaphore(_AbstractPrioritySemaphore):
_context_manager_class: Type[_BlockSemaphoreContextManager]
"""The context manager class used by this semaphore."""

_top_priority: Literal[-1]
_top_priority: Literal[-1] # type: ignore [assignment]
"""The highest priority value, set to -1."""

def __init__(self, value=1, *, name=None) -> None:
def __init__(
self,
value: BlockNumber = BlockNumber(1),
*,
name: Optional[str] = None,
) -> None:
super().__init__(_BlockSemaphoreContextManager, -1, int(value), name=name)

def __getitem__(self, block: Union[int, HexStr, Literal["latest", None]]) -> "_BlockSemaphoreContextManager": # type: ignore [override]
Expand All @@ -67,7 +71,7 @@ def __getitem__(self, block: Union[int, HexStr, Literal["latest", None]]) -> "_B
priority = int(block, 16)
elif block not in {None, "latest"}:
# NOTE: We do this to generate an err if an unsuitable value was provided
priority = block
priority = block # type: ignore [assignment]
else:
priority = _TOP_PRIORITY
return super().__getitem__(priority)
Loading