Skip to content

Commit

Permalink
fix: incomplete refactor oops (#27)
Browse files Browse the repository at this point in the history
* fix: incomplete refactor oops

* fix: incomplete refactor oops

* feat: import more stuff to data module

* fix: incomplete refactor oops

* fix: incomplete refactor oops

* fix: broken import

* fix: broken import

* fix: broken import

* chore: `black .`

* chore: bump version to 0.1.0

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
BobTheBuidler and github-actions[bot] authored Dec 4, 2024
1 parent da77315 commit e3d9293
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 62 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ A collection of [msgspec.Struct](https://jcristharif.com/msgspec/structs.html) d


#### Modules:
- [block](https://bobthebuidler.github.io/evmspec/source/evmspec.html#module-evmspec.block)
- [block](https://bobthebuidler.github.io/evmspec/source/evmspec.html#module-evmspec.structs.block)
- [log](https://bobthebuidler.github.io/evmspec/source/evmspec.html#module-evmspec.log)
- [receipt](https://bobthebuidler.github.io/evmspec/source/evmspec.html#module-evmspec.receipt)
- [trace](https://bobthebuidler.github.io/evmspec/source/evmspec.trace.html)
- [trace](https://bobthebuidler.github.io/evmspec/source/evmspec.structs.trace.html)
- [transaction](https://bobthebuidler.github.io/evmspec/source/evmspec.html#module-evmspec.transaction)
- and more

22 changes: 14 additions & 8 deletions evmspec/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from evmspec import block, structs, trace, transaction
from evmspec import structs
from evmspec.structs import (
ErigonBlockHeader,
FilterTrace,
FullTransactionReceipt,
Log,
TransactionReceipt,
block,
header,
trace,
transaction,
)
from evmspec.transaction import (
from evmspec.structs.transaction import (
TransactionRLP,
TransactionLegacy,
Transaction1559,
Expand All @@ -26,6 +30,8 @@
"structs",
# - header
"ErigonBlockHeader",
# - log
"Log",
# - receipt
"FullTransactionReceipt",
"TransactionReceipt",
Expand All @@ -44,12 +50,12 @@
This library provides a collection of msgspec.Struct definitions for use with the Ethereum Virtual Machine.
Modules:
:mod:`~evmspec.block`: Contains structures related to Ethereum blocks.
:mod:`~evmspec.header`: Contains structures related to Ethereum block headers.
:mod:`~evmspec.log`: Contains structures related to Ethereum logs.
:mod:`~evmspec.receipt`: Contains structures related to Ethereum transaction receipts.
:mod:`~evmspec.trace`: Contains structures related to Ethereum transaction traces.
:mod:`~evmspec.transaction`: Contains structures related to Ethereum transactions.
:mod:`~evmspec.structs.block`: Contains structures related to Ethereum blocks.
:mod:`~evmspec.structs.header`: Contains structures related to Ethereum block headers.
:mod:`~evmspec.structs.log`: Contains structures related to Ethereum logs.
:mod:`~evmspec.structs.receipt`: Contains structures related to Ethereum transaction receipts.
:mod:`~evmspec.structs.trace`: Contains structures related to Ethereum transaction traces.
:mod:`~evmspec.structs.transaction`: Contains structures related to Ethereum transactions.
Structs:
:class:`~evmspec.ErigonBlockHeader`: Represents a block header in the Erigon client.
Expand Down
6 changes: 5 additions & 1 deletion evmspec/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from evmspec.data import uints
from evmspec.data.main import (
from evmspec.data._ids import ChainId, LogIndex, TransactionIndex
from evmspec.data._main import (
Address,
BlockHash,
BlockNumber,
Expand All @@ -23,4 +24,7 @@
"uints",
"UnixTimestamp",
"Nonce",
"ChainId",
"LogIndex",
"TransactionIndex",
]
File renamed without changes.
4 changes: 2 additions & 2 deletions evmspec/_ids.py → evmspec/data/_ids.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from evmspec.data import uint
from evmspec.data._main import uint


class IntId(uint):
Expand Down Expand Up @@ -146,4 +146,4 @@ class LogIndex(IntId):
See Also:
- :class:`IntId`
"""
"""
34 changes: 28 additions & 6 deletions evmspec/data/main.py → evmspec/data/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING, Any, Callable, Tuple, Type, TypeVar, Union

from cachetools.func import ttl_cache
from eth_utils import to_checksum_address
from cchecksum import to_checksum_address
from hexbytes import HexBytes
from msgspec import Raw, Struct, json
from typing_extensions import Self
Expand All @@ -17,29 +17,34 @@
_T = TypeVar("_T")
"""A generic type variable."""

_DecodeHook = Callable[[Type[_T], Any], _T]
DecodeHook = Callable[[Type[_T], Any], _T]
"""A type alias for a function that decodes an object into a specific type."""


class Address(str):
"""
Represents an Ethereum address with checksum validation.
Represents an Ethereum address in its EIP-55 checksum format.
This class ensures that any Ethereum address is stored in its checksummed format,
which is a mixed-case encoding of the address that includes a checksum.
as defined by EIP-55. It uses a custom Cython implementation for the checksum
conversion to optimize performance.
Examples:
>>> addr = Address("0x52908400098527886E0F7030069857D2E4169EE7")
>>> print(addr)
0x52908400098527886E0F7030069857D2E4169EE7
See Also:
- `eth_utils.to_checksum_address`: Function used for checksum validation.
- `cchecksum.to_checksum_address`: Function used for checksum conversion.
"""

def __new__(cls, address: str):
"""Creates a new Address instance with checksum validation.
This function takes a hex address and returns it in the checksummed format
as defined by EIP-55. It uses a custom Cython implementation for the
checksum conversion to optimize performance.
Args:
address: A string representing the Ethereum address.
Expand All @@ -49,13 +54,20 @@ def __new__(cls, address: str):
Examples:
>>> Address("0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe")
Address('0xDe0B295669a9FD93d5F28D9Ec85E40f4cb697BAe')
See Also:
- `cchecksum.to_checksum_address`: Function used for checksum conversion.
"""
return super().__new__(cls, to_checksum_address(address))

@classmethod
def _decode_hook(cls, typ: Type["Address"], obj: str):
"""Decodes an object into an Address instance with checksum validation.
This function takes a hex address and returns it in the checksummed format
as defined by EIP-55. It uses a custom Cython implementation for the
checksum conversion to optimize performance.
Args:
typ: The type that is expected to be decoded to.
obj: The object to decode, expected to be a string representation of an Ethereum address.
Expand All @@ -69,6 +81,9 @@ def _decode_hook(cls, typ: Type["Address"], obj: str):
Note:
This method utilizes :meth:`cls.checksum` as a class method to ensure the address is checksummed.
See Also:
- `cchecksum.to_checksum_address`: Function used for checksum conversion.
"""
return cls.checksum(obj)

Expand All @@ -77,6 +92,10 @@ def _decode_hook(cls, typ: Type["Address"], obj: str):
def checksum(cls, address: str) -> Self:
"""Returns the checksummed version of the address.
This function takes a hex address and returns it in the checksummed format
as defined by EIP-55. It uses a custom Cython implementation for the
checksum conversion to optimize performance.
Args:
address: A string representing the Ethereum address.
Expand All @@ -86,6 +105,9 @@ def checksum(cls, address: str) -> Self:
Examples:
>>> Address.checksum("0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe")
Address('0xDe0B295669a9FD93d5F28D9Ec85E40f4cb697BAe')
See Also:
- `cchecksum.to_checksum_address`: Function used for checksum conversion.
"""
return cls(address)

Expand Down Expand Up @@ -377,7 +399,7 @@ class TransactionHash(HexBytes32):
async def get_receipt(
self,
decode_to: ReceiptDataType,
decode_hook: _DecodeHook[ReceiptDataType] = _decode_hook,
decode_hook: DecodeHook[ReceiptDataType] = _decode_hook,
) -> "TransactionReceipt":
"""Async method to get the transaction receipt.
Expand Down
4 changes: 2 additions & 2 deletions evmspec/data/uints.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from hexbytes import HexBytes

from evmspec.data.main import uint
from evmspec.data._main import uint


class _UintData(uint):
Expand Down Expand Up @@ -139,4 +139,4 @@ class uint256(_UintData):
)
setattr(sys.modules[__name__], cls_name, new_cls)

__all__ = [f"uint{bytes*8}" for bytes in range(1, 32)]
__all__ = [f"uint{bytes*8}" for bytes in range(1, 32)]
11 changes: 11 additions & 0 deletions evmspec/structs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
from evmspec.structs.header import ErigonBlockHeader
from evmspec.structs.log import Log
from evmspec.structs.receipt import FullTransactionReceipt, TransactionReceipt
from evmspec.structs.trace import FilterTrace
from evmspec.structs.transaction import (
Transaction,
AnyTransaction,
TransactionRLP,
TransactionLegacy,
Transaction1559,
Transaction2930,
)

__all__ = [
# - header
"ErigonBlockHeader",
# - log
"Log",
# - receipt
"FullTransactionReceipt",
"TransactionReceipt",
Expand Down
18 changes: 14 additions & 4 deletions evmspec/block.py → evmspec/structs/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
from hexbytes import HexBytes
from msgspec import UNSET, Raw, ValidationError, field, json

from evmspec._ids import IntId
from evmspec.data import *
from evmspec.transaction import Transaction, TransactionRLP
from evmspec.data import (
Address,
BlockHash,
BlockNumber,
Nonce,
TransactionHash,
UnixTimestamp,
Wei,
_decode_hook,
uint,
)
from evmspec.data._ids import IntId
from evmspec.structs.transaction import Transaction, TransactionRLP


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -239,4 +249,4 @@ def withdrawals(self) -> Tuple[StakingWithdrawal, ...]:
"""
return json.decode(
self._withdrawals, type=Tuple[StakingWithdrawal, ...], dec_hook=_decode_hook
)
)
5 changes: 2 additions & 3 deletions evmspec/structs/header.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from hexbytes import HexBytes

from dictstruct import LazyDictStruct
from hexbytes import HexBytes

from evmspec.data import Address, UnixTimestamp, uint

Expand Down Expand Up @@ -112,4 +111,4 @@ class ErigonBlockHeader(LazyDictStruct, frozen=True, kw_only=True, forbid_unknow
... )
>>> header.difficulty
uint(1000)
"""
"""
3 changes: 2 additions & 1 deletion evmspec/structs/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from dictstruct import LazyDictStruct
from hexbytes import HexBytes

from evmspec._ids import LogIndex, TransactionIndex
from evmspec.data import (
Address,
BlockHash,
Expand All @@ -13,6 +12,8 @@
uint,
uints,
)
from evmspec.data._ids import LogIndex, TransactionIndex


_ADDRESS_TOPIC_PREFIX = HexBytes("0") * 12

Expand Down
4 changes: 2 additions & 2 deletions evmspec/structs/receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from hexbytes import HexBytes
from msgspec import UNSET, Raw, field, json

from evmspec._ids import TransactionIndex
from evmspec.data import Address, BlockNumber, TransactionHash, Wei, uint, _decode_hook
from evmspec.data import Address, BlockNumber, TransactionHash, Wei, _decode_hook, uint
from evmspec.data._ids import TransactionIndex
from evmspec.structs.log import Log


Expand Down
20 changes: 10 additions & 10 deletions evmspec/structs/trace/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
from typing import Union

from evmspec.trace import call, create, reward, suicide
from evmspec.structs.trace import call, create, reward, suicide

FilterTrace = Union[call.Trace, create.Trace, reward.Trace, suicide.Trace]
"""A type alias for filtering trace types.
FilterTrace is a Union of the following trace types:
- :class:`evmspec.trace.call.Trace`
- :class:`evmspec.trace.create.Trace`
- :class:`evmspec.trace.reward.Trace`
- :class:`evmspec.trace.suicide.Trace`
- :class:`evmspec.structs.trace.call.Trace`
- :class:`evmspec.structs.trace.create.Trace`
- :class:`evmspec.structs.trace.reward.Trace`
- :class:`evmspec.structs.trace.suicide.Trace`
Examples:
You can use `FilterTrace` to specify a variable that can hold any of the trace types:
>>> from evmspec.trace import FilterTrace
>>> from evmspec.structs.trace import FilterTrace
>>> trace: FilterTrace = call.Trace(...)
>>> trace = create.Trace(...)
>>> trace = reward.Trace(...)
>>> trace = suicide.Trace(...)
See Also:
- :class:`evmspec.trace.call.Trace`
- :class:`evmspec.trace.create.Trace`
- :class:`evmspec.trace.reward.Trace`
- :class:`evmspec.trace.suicide.Trace`
- :class:`evmspec.structs.trace.call.Trace`
- :class:`evmspec.structs.trace.create.Trace`
- :class:`evmspec.structs.trace.reward.Trace`
- :class:`evmspec.structs.trace.suicide.Trace`
"""

__all__ = ["call", "create", "reward", "suicide", "FilterTrace"]
8 changes: 4 additions & 4 deletions evmspec/structs/trace/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class _ActionBase(
internal purposes.
Examples:
>>> from evmspec.trace._base import _ActionBase
>>> from evmspec.structs.trace._base import _ActionBase
>>> class MyAction(_ActionBase):
... pass
>>> action = MyAction(sender="0xabc...", value=1000, gas=21000)
Expand Down Expand Up @@ -95,7 +95,7 @@ class _ResultBase(
internal purposes.
Examples:
>>> from evmspec.trace._base import _ResultBase
>>> from evmspec.structs.trace._base import _ResultBase
>>> class MyResult(_ResultBase):
... pass
>>> result = MyResult(gasUsed=21000)
Expand Down Expand Up @@ -138,7 +138,7 @@ class _FilterTraceBase(
internal purposes.
Examples:
>>> from evmspec.trace._base import _FilterTraceBase
>>> from evmspec.structs.trace._base import _FilterTraceBase
>>> class MyTrace(_FilterTraceBase):
... pass
>>> trace = MyTrace(blockNumber=123456, blockHash="0xabc...", transactionHash="0xdef...", transactionPosition=1, traceAddress=[0, 1], subtraces=2)
Expand Down Expand Up @@ -238,4 +238,4 @@ def block(self) -> BlockNumber:
>>> trace.block
123456
"""
return self.blockNumber
return self.blockNumber
4 changes: 2 additions & 2 deletions evmspec/structs/trace/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from hexbytes import HexBytes
from msgspec import UNSET, Raw, field, json

from evmspec._enum import StringToIntEnumMeta
from evmspec.data import Address, _decode_hook
from evmspec.trace._base import _ActionBase, _FilterTraceBase, _ResultBase
from evmspec.data._enum import StringToIntEnumMeta
from evmspec.structs.trace._base import _ActionBase, _FilterTraceBase, _ResultBase


class Type(Enum, metaclass=StringToIntEnumMeta):
Expand Down
Loading

0 comments on commit e3d9293

Please sign in to comment.