diff --git a/src/ethereum/arrow_glacier/vm/instructions/memory.py b/src/ethereum/arrow_glacier/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/arrow_glacier/vm/instructions/memory.py +++ b/src/ethereum/arrow_glacier/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/base_types.py b/src/ethereum/base_types.py index 1cdc7ba6603..50f4f057072 100644 --- a/src/ethereum/base_types.py +++ b/src/ethereum/base_types.py @@ -1,18 +1,23 @@ """ -Numeric & Array Types -^^^^^^^^^^^^^^^^^^^^^ - -.. contents:: Table of Contents - :backlinks: none - :local: - -Introduction ------------- - Integer and array types which are used by—but not unique to—Ethereum. -""" -from __future__ import annotations +[`Uint`] represents non-negative integers of arbitrary size, while subclasses +of [`FixedUint`] (like [`U256`] or [`U32`]) represent non-negative integers of +particular sizes. + +Similarly, [`Bytes`] represents arbitrarily long byte sequences, while +subclasses of [`FixedBytes`] (like [`Bytes0`] or [`Bytes64`]) represent +sequences containing an exact number of bytes. + +[`Uint`]: ref:ethereum.base_types.Uint +[`FixedUint`]: ref:ethereum.base_types.FixedUint +[`U32`]: ref:ethereum.base_types.U32 +[`U256`]: ref:ethereum.base_types.U256 +[`Bytes`]: ref:ethereum.base_types.Bytes +[`FixedBytes`]: ref:ethereum.base_types.FixedBytes +[`Bytes0`]: ref:ethereum.base_types.Bytes0 +[`Bytes64`]: ref:ethereum.base_types.Bytes64 +""" from dataclasses import is_dataclass, replace from typing import ( @@ -31,25 +36,37 @@ @runtime_checkable class SlottedFreezable(Protocol): """ - Represents data classes created with `@slotted_freezable`. + A [`Protocol`] implemented by data classes annotated with + [`@slotted_freezable`]. + + [`@slotted_freezable`]: ref:ethereum.base_types.slotted_freezable + [`Protocol`]: https://docs.python.org/library/typing.html#typing.Protocol """ _frozen: bool -U8_MAX_VALUE = (2**8) - 1 -U32_MAX_VALUE = (2**32) - 1 -U32_CEIL_VALUE = 2**32 -U64_MAX_VALUE = (2**64) - 1 -U255_MAX_VALUE = (2**255) - 1 U255_CEIL_VALUE = 2**255 -U256_MAX_VALUE = (2**256) - 1 +""" +Smallest value that requires 256 bits to represent. Mostly used in signed +arithmetic operations, like [`sdiv`]. + +[`sdiv`]: ref:ethereum.frontier.vm.instructions.arithmetic.sdiv +""" + U256_CEIL_VALUE = 2**256 +""" +Smallest value that requires 257 bits to represent. Used when converting a +[`U256`] in two's complement format to a regular `int` in [`U256.to_signed`]. + +[`U256`]: ref:ethereum.base_types.U256 +[`U256.to_signed`]: ref:ethereum.base_types.U256.to_signed +""" class Uint(int): """ - Unsigned positive integer. + Unsigned integer of arbitrary size. """ __slots__ = () @@ -59,21 +76,14 @@ def from_be_bytes(cls: Type, buffer: "Bytes") -> "Uint": """ Converts a sequence of bytes into an arbitrarily sized unsigned integer from its big endian representation. - Parameters - ---------- - buffer : - Bytes to decode. - Returns - ------- - self : `Uint` - Unsigned integer decoded from `buffer`. """ return cls(int.from_bytes(buffer, "big")) @classmethod def from_le_bytes(cls: Type, buffer: "Bytes") -> "Uint": """ - Convert a series of little endian bytes to an unsigned integer. + Converts a sequence of bytes into an arbitrarily sized unsigned integer + from its little endian representation. """ return cls(int.from_bytes(buffer, "little")) @@ -274,21 +284,13 @@ def to_be_bytes32(self) -> "Bytes32": """ Converts this arbitrarily sized unsigned integer into its big endian representation with exactly 32 bytes. - Returns - ------- - big_endian : `Bytes32` - Big endian (most significant bits first) representation. """ return Bytes32(self.to_bytes(32, "big")) def to_be_bytes(self) -> "Bytes": """ Converts this arbitrarily sized unsigned integer into its big endian - representation. - Returns - ------- - big_endian : `Bytes` - Big endian (most significant bits first) representation. + representation, without padding. """ bit_length = self.bit_length() byte_length = (bit_length + 7) // 8 @@ -297,18 +299,7 @@ def to_be_bytes(self) -> "Bytes": def to_le_bytes(self, number_bytes: Optional[int] = None) -> "Bytes": """ Converts this arbitrarily sized unsigned integer into its little endian - representation. - - Parameters - ---------- - number_bytes : - Exact number of bytes to return (defaults to the fewest that can - represent this number.) - - Returns - ------- - little_endian : `Bytes` - Little endian (most significant bits last) representation. + representation, without padding. """ if number_bytes is None: bit_length = self.bit_length() @@ -316,16 +307,19 @@ def to_le_bytes(self, number_bytes: Optional[int] = None) -> "Bytes": return self.to_bytes(number_bytes, "little") -T = TypeVar("T", bound="FixedUInt") +T = TypeVar("T", bound="FixedUint") -class FixedUInt(int): +class FixedUint(int): """ Superclass for fixed size unsigned integers. Not intended to be used directly, but rather to be subclassed. """ - MAX_VALUE: ClassVar["FixedUInt"] + MAX_VALUE: ClassVar["FixedUint"] + """ + Largest value that can be represented by this integer type. + """ __slots__ = () @@ -354,17 +348,11 @@ def wrapping_add(self: T, right: int) -> T: """ Return a new instance containing `self + right (mod N)`. - Parameters - ---------- - - right : - Other operand for addition. - - Returns - ------- + Passing a `right` value greater than [`MAX_VALUE`] or less than zero + will raise a `ValueError`, even if the result would fit in this integer + type. - sum : T - The result of adding `self` and `right`, wrapped. + [`MAX_VALUE`]: ref:ethereum.base_types.FixedUint.MAX_VALUE """ if not isinstance(right, int): return NotImplemented @@ -393,17 +381,11 @@ def wrapping_sub(self: T, right: int) -> T: """ Return a new instance containing `self - right (mod N)`. - Parameters - ---------- + Passing a `right` value greater than [`MAX_VALUE`] or less than zero + will raise a `ValueError`, even if the result would fit in this integer + type. - right : - Subtrahend operand for subtraction. - - Returns - ------- - - difference : T - The result of subtracting `right` from `self`, wrapped. + [`MAX_VALUE`]: ref:ethereum.base_types.FixedUint.MAX_VALUE """ if not isinstance(right, int): return NotImplemented @@ -443,17 +425,11 @@ def wrapping_mul(self: T, right: int) -> T: """ Return a new instance containing `self * right (mod N)`. - Parameters - ---------- - - right : - Other operand for multiplication. + Passing a `right` value greater than [`MAX_VALUE`] or less than zero + will raise a `ValueError`, even if the result would fit in this integer + type. - Returns - ------- - - product : T - The result of multiplying `self` by `right`, wrapped. + [`MAX_VALUE`]: ref:ethereum.base_types.FixedUint.MAX_VALUE """ if not isinstance(right, int): return NotImplemented @@ -524,7 +500,7 @@ def __divmod__(self: T, right: int) -> Tuple[T, T]: if right < 0 or right > self.MAX_VALUE: raise ValueError() - result = super(FixedUInt, self).__divmod__(right) + result = super(FixedUint, self).__divmod__(right) return ( int.__new__(self.__class__, result[0]), int.__new__(self.__class__, result[1]), @@ -537,7 +513,7 @@ def __rdivmod__(self: T, left: int) -> Tuple[T, T]: if left < 0 or left > self.MAX_VALUE: raise ValueError() - result = super(FixedUInt, self).__rdivmod__(left) + result = super(FixedUint, self).__rdivmod__(left) return ( int.__new__(self.__class__, result[0]), int.__new__(self.__class__, result[1]), @@ -567,20 +543,13 @@ def wrapping_pow(self: T, right: int, modulo: Optional[int] = None) -> T: """ Return a new instance containing `self ** right (mod modulo)`. - Parameters - ---------- - - right : - Exponent operand. + If omitted, `modulo` defaults to `Uint(self.MAX_VALUE) + 1`. - modulo : - Optional modulus (defaults to `MAX_VALUE + 1`.) + Passing a `right` or `modulo` value greater than [`MAX_VALUE`] or + less than zero will raise a `ValueError`, even if the result would fit + in this integer type. - Returns - ------- - - power : T - The result of raising `self` to the power of `right`, wrapped. + [`MAX_VALUE`]: ref:ethereum.base_types.FixedUint.MAX_VALUE """ if modulo is not None: if not isinstance(modulo, int): @@ -676,11 +645,6 @@ def to_be_bytes(self) -> "Bytes": """ Converts this unsigned integer into its big endian representation, omitting leading zero bytes. - - Returns - ------- - big_endian : `Bytes` - Big endian (most significant bits first) representation. """ bit_length = self.bit_length() byte_length = (bit_length + 7) // 8 @@ -689,29 +653,23 @@ def to_be_bytes(self) -> "Bytes": # TODO: Implement neg, pos, abs ... -class U256(FixedUInt): +class U256(FixedUint): """ - Unsigned positive integer, which can represent `0` to `2 ** 256 - 1`, - inclusive. + Unsigned integer, which can represent `0` to `2 ** 256 - 1`, inclusive. """ MAX_VALUE: ClassVar["U256"] + """ + Largest value that can be represented by this integer type. + """ __slots__ = () @classmethod def from_be_bytes(cls: Type, buffer: "Bytes") -> "U256": """ - Converts a sequence of bytes into an arbitrarily sized unsigned integer + Converts a sequence of bytes into a fixed sized unsigned integer from its big endian representation. - Parameters - ---------- - buffer : - Bytes to decode. - Returns - ------- - self : `U256` - Unsigned integer decoded from `buffer`. """ if len(buffer) > 32: raise ValueError() @@ -721,15 +679,8 @@ def from_be_bytes(cls: Type, buffer: "Bytes") -> "U256": @classmethod def from_signed(cls: Type, value: int) -> "U256": """ - Converts a signed number into a 256-bit unsigned integer. - Parameters - ---------- - value : - Signed number - Returns - ------- - self : `U256` - Unsigned integer obtained from `value`. + Creates an unsigned integer representing `value` using two's + complement. """ if value >= 0: return cls(value) @@ -740,22 +691,14 @@ def to_be_bytes32(self) -> "Bytes32": """ Converts this 256-bit unsigned integer into its big endian representation with exactly 32 bytes. - Returns - ------- - big_endian : `Bytes32` - Big endian (most significant bits first) representation. """ return Bytes32(self.to_bytes(32, "big")) def to_signed(self) -> int: """ - Converts this 256-bit unsigned integer into a signed integer. - Returns - ------- - signed_int : `int` - Signed integer obtained from 256-bit unsigned integer. + Decodes a signed integer from its two's complement representation. """ - if self <= U255_MAX_VALUE: + if self.bit_length() < 256: # This means that the sign bit is 0 return int(self) @@ -763,17 +706,19 @@ def to_signed(self) -> int: return int(self) - U256_CEIL_VALUE -U256.MAX_VALUE = int.__new__(U256, U256_MAX_VALUE) -"""autoapi_noindex""" +U256.MAX_VALUE = int.__new__(U256, (2**256) - 1) -class U32(FixedUInt): +class U32(FixedUint): """ Unsigned positive integer, which can represent `0` to `2 ** 32 - 1`, inclusive. """ MAX_VALUE: ClassVar["U32"] + """ + Largest value that can be represented by this integer type. + """ __slots__ = () @@ -792,11 +737,6 @@ def to_le_bytes4(self) -> "Bytes4": """ Converts this fixed sized unsigned integer into its little endian representation, with exactly 4 bytes. - - Returns - ------- - little_endian : `Bytes4` - Little endian (most significant bits last) representation. """ return Bytes4(self.to_bytes(4, "little")) @@ -804,28 +744,25 @@ def to_le_bytes(self) -> "Bytes": """ Converts this fixed sized unsigned integer into its little endian representation, in the fewest bytes possible. - - Returns - ------- - little_endian : `Bytes` - Little endian (most significant bits last) representation. """ bit_length = self.bit_length() byte_length = (bit_length + 7) // 8 return self.to_bytes(byte_length, "little") -U32.MAX_VALUE = int.__new__(U32, U32_MAX_VALUE) -"""autoapi_noindex""" +U32.MAX_VALUE = int.__new__(U32, (2**32) - 1) -class U64(FixedUInt): +class U64(FixedUint): """ Unsigned positive integer, which can represent `0` to `2 ** 64 - 1`, inclusive. """ MAX_VALUE: ClassVar["U64"] + """ + Largest value that can be represented by this integer type. + """ __slots__ = () @@ -844,11 +781,6 @@ def to_le_bytes8(self) -> "Bytes8": """ Converts this fixed sized unsigned integer into its little endian representation, with exactly 8 bytes. - - Returns - ------- - little_endian : `Bytes8` - Little endian (most significant bits last) representation. """ return Bytes8(self.to_bytes(8, "little")) @@ -856,11 +788,6 @@ def to_le_bytes(self) -> "Bytes": """ Converts this fixed sized unsigned integer into its little endian representation, in the fewest bytes possible. - - Returns - ------- - little_endian : `Bytes` - Little endian (most significant bits last) representation. """ bit_length = self.bit_length() byte_length = (bit_length + 7) // 8 @@ -871,15 +798,6 @@ def from_be_bytes(cls: Type, buffer: "Bytes") -> "U64": """ Converts a sequence of bytes into an unsigned 64 bit integer from its big endian representation. - - Parameters - ---------- - buffer : - Bytes to decode. - Returns - ------- - self : `U64` - Unsigned integer decoded from `buffer`. """ if len(buffer) > 8: raise ValueError() @@ -887,8 +805,7 @@ def from_be_bytes(cls: Type, buffer: "Bytes") -> "U64": return cls(int.from_bytes(buffer, "big")) -U64.MAX_VALUE = int.__new__(U64, U64_MAX_VALUE) -"""autoapi_noindex""" +U64.MAX_VALUE = int.__new__(U64, (2**64) - 1) B = TypeVar("B", bound="FixedBytes") @@ -901,6 +818,9 @@ class FixedBytes(bytes): """ LENGTH: int + """ + Number of bytes in each instance of this class. + """ __slots__ = () @@ -922,6 +842,9 @@ class Bytes0(FixedBytes): """ LENGTH = 0 + """ + Number of bytes in each instance of this class. + """ class Bytes4(FixedBytes): @@ -930,6 +853,9 @@ class Bytes4(FixedBytes): """ LENGTH = 4 + """ + Number of bytes in each instance of this class. + """ class Bytes8(FixedBytes): @@ -938,6 +864,9 @@ class Bytes8(FixedBytes): """ LENGTH = 8 + """ + Number of bytes in each instance of this class. + """ class Bytes20(FixedBytes): @@ -946,6 +875,9 @@ class Bytes20(FixedBytes): """ LENGTH = 20 + """ + Number of bytes in each instance of this class. + """ class Bytes32(FixedBytes): @@ -954,6 +886,9 @@ class Bytes32(FixedBytes): """ LENGTH = 32 + """ + Number of bytes in each instance of this class. + """ class Bytes64(FixedBytes): @@ -962,6 +897,9 @@ class Bytes64(FixedBytes): """ LENGTH = 64 + """ + Number of bytes in each instance of this class. + """ class Bytes256(FixedBytes): @@ -970,9 +908,15 @@ class Bytes256(FixedBytes): """ LENGTH = 256 + """ + Number of bytes in each instance of this class. + """ Bytes = bytes +""" +Sequence of bytes (octets) of arbitrary length. +""" def _setattr_function(self: Any, attr: str, value: Any) -> None: @@ -1019,20 +963,10 @@ def slotted_freezable(cls: Any) -> Any: def modify(obj: S, f: Callable[[S], None]) -> S: """ - Create a mutable copy of `obj` (which must be `@slotted_freezable`) and - apply `f` to the copy before freezing it. - - Parameters - ---------- - obj : `S` - Object to copy. - f : `Callable[[S], None]` - Function to apply to `obj`. - - Returns - ------- - new_obj : `S` - Compact byte array. + Create a copy of `obj` (which must be [`@slotted_freezable`]), and modify + it by applying `f`. The returned copy will be frozen. + + [`@slotted_freezable`]: ref:ethereum.base_types.slotted_freezable """ assert is_dataclass(obj) assert isinstance(obj, SlottedFreezable) diff --git a/src/ethereum/berlin/vm/instructions/memory.py b/src/ethereum/berlin/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/berlin/vm/instructions/memory.py +++ b/src/ethereum/berlin/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/byzantium/vm/instructions/memory.py b/src/ethereum/byzantium/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/byzantium/vm/instructions/memory.py +++ b/src/ethereum/byzantium/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/constantinople/vm/instructions/memory.py b/src/ethereum/constantinople/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/constantinople/vm/instructions/memory.py +++ b/src/ethereum/constantinople/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/dao_fork/vm/instructions/memory.py b/src/ethereum/dao_fork/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/dao_fork/vm/instructions/memory.py +++ b/src/ethereum/dao_fork/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/ethash.py b/src/ethereum/ethash.py index 454d75db603..7ad6530cc12 100644 --- a/src/ethereum/ethash.py +++ b/src/ethereum/ethash.py @@ -28,7 +28,7 @@ from typing import Callable, Tuple, Union -from ethereum.base_types import U32, U32_MAX_VALUE, Bytes8, Uint +from ethereum.base_types import U32, Bytes8, Uint from ethereum.crypto.hash import Hash32, Hash64, keccak256, keccak512 from ethereum.utils.numeric import ( is_prime, @@ -264,7 +264,7 @@ def fnv(a: Union[Uint, U32], b: Union[Uint, U32]) -> U32: [FNV-1]: http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1 """ # This is a faster way of doing `number % (2 ** 32)`. - result = ((Uint(a) * 0x01000193) ^ Uint(b)) & U32_MAX_VALUE + result = ((Uint(a) * 0x01000193) ^ Uint(b)) & U32.MAX_VALUE return U32(result) diff --git a/src/ethereum/frontier/vm/instructions/memory.py b/src/ethereum/frontier/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/frontier/vm/instructions/memory.py +++ b/src/ethereum/frontier/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/gray_glacier/vm/instructions/memory.py b/src/ethereum/gray_glacier/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/gray_glacier/vm/instructions/memory.py +++ b/src/ethereum/gray_glacier/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/homestead/vm/instructions/memory.py b/src/ethereum/homestead/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/homestead/vm/instructions/memory.py +++ b/src/ethereum/homestead/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/istanbul/vm/instructions/memory.py b/src/ethereum/istanbul/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/istanbul/vm/instructions/memory.py +++ b/src/ethereum/istanbul/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/london/vm/instructions/memory.py b/src/ethereum/london/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/london/vm/instructions/memory.py +++ b/src/ethereum/london/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/muir_glacier/vm/instructions/memory.py b/src/ethereum/muir_glacier/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/muir_glacier/vm/instructions/memory.py +++ b/src/ethereum/muir_glacier/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/paris/vm/instructions/memory.py b/src/ethereum/paris/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/paris/vm/instructions/memory.py +++ b/src/ethereum/paris/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/rlp.py b/src/ethereum/rlp.py index 55e7dbe36eb..ba4a5608189 100644 --- a/src/ethereum/rlp.py +++ b/src/ethereum/rlp.py @@ -14,8 +14,6 @@ Defines the serialization and deserialization format used throughout Ethereum. """ -from __future__ import annotations - from dataclasses import astuple, fields, is_dataclass from typing import Any, List, Sequence, Tuple, Type, TypeVar, Union, cast @@ -23,7 +21,7 @@ from ethereum.exceptions import RLPDecodingError, RLPEncodingError from ethereum.utils.ensure import ensure -from .base_types import Bytes, Bytes0, Bytes20, FixedBytes, FixedUInt, Uint +from .base_types import Bytes, Bytes0, Bytes20, FixedBytes, FixedUint, Uint RLP = Any @@ -50,7 +48,7 @@ def encode(raw_data: RLP) -> Bytes: """ if isinstance(raw_data, (bytearray, bytes)): return encode_bytes(raw_data) - elif isinstance(raw_data, (Uint, FixedUInt)): + elif isinstance(raw_data, (Uint, FixedUint)): return encode(raw_data.to_be_bytes()) elif isinstance(raw_data, str): return encode_bytes(raw_data.encode()) @@ -280,7 +278,7 @@ def _decode_to(cls: Type[T], raw_rlp: RLP) -> T: elif issubclass(cls, Bytes): ensure(isinstance(raw_rlp, Bytes), RLPDecodingError) return raw_rlp - elif issubclass(cls, (Uint, FixedUInt)): + elif issubclass(cls, (Uint, FixedUint)): ensure(isinstance(raw_rlp, Bytes), RLPDecodingError) try: return cls.from_be_bytes(raw_rlp) # type: ignore diff --git a/src/ethereum/shanghai/vm/instructions/memory.py b/src/ethereum/shanghai/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/shanghai/vm/instructions/memory.py +++ b/src/ethereum/shanghai/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/spurious_dragon/vm/instructions/memory.py b/src/ethereum/spurious_dragon/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/spurious_dragon/vm/instructions/memory.py +++ b/src/ethereum/spurious_dragon/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/src/ethereum/tangerine_whistle/vm/instructions/memory.py b/src/ethereum/tangerine_whistle/vm/instructions/memory.py index eecac9c5855..6c54f6806e9 100644 --- a/src/ethereum/tangerine_whistle/vm/instructions/memory.py +++ b/src/ethereum/tangerine_whistle/vm/instructions/memory.py @@ -11,7 +11,7 @@ Implementations of the EVM Memory instructions. """ -from ethereum.base_types import U8_MAX_VALUE, U256, Bytes +from ethereum.base_types import U256, Bytes from .. import Evm from ..gas import ( @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None: # OPERATION evm.memory += b"\x00" * extend_memory.expand_by - normalized_bytes_value = Bytes([value & U8_MAX_VALUE]) + normalized_bytes_value = Bytes([value & 0xFF]) memory_write(evm.memory, start_position, normalized_bytes_value) # PROGRAM COUNTER diff --git a/tests/helpers/load_difficulty_tests.py b/tests/helpers/load_difficulty_tests.py index 86e27bb3228..7dd7f2dacbc 100644 --- a/tests/helpers/load_difficulty_tests.py +++ b/tests/helpers/load_difficulty_tests.py @@ -1,19 +1,9 @@ -from __future__ import annotations - import json import os from importlib import import_module from typing import Any, Dict, List -from ethereum.base_types import U256, Uint -from ethereum.utils.hexadecimal import ( - hex_to_bytes, - hex_to_bytes8, - hex_to_bytes32, - hex_to_hash, - hex_to_u256, - hex_to_uint, -) +from ethereum.utils.hexadecimal import hex_to_u256, hex_to_uint class DifficultyTestLoader: @@ -28,7 +18,7 @@ def __init__(self, network: str, fork_name: str): self.test_dir = f"tests/fixtures/DifficultyTests/df{fork_name}" try: self.test_files = [file for file in os.listdir(self.test_dir)] - except: + except OSError: self.test_files = [] self.fork = self._module("fork") diff --git a/tests/helpers/load_vm_tests.py b/tests/helpers/load_vm_tests.py index f901adbe6ab..1ba5106fed3 100644 --- a/tests/helpers/load_vm_tests.py +++ b/tests/helpers/load_vm_tests.py @@ -1,9 +1,7 @@ -from __future__ import annotations - import json import os from importlib import import_module -from typing import Any, List, TypeVar +from typing import Any, List from ethereum import rlp from ethereum.base_types import U64, U256, Uint diff --git a/tests/test_base_types.py b/tests/test_base_types.py index 3e5d629b9a8..761142b6312 100644 --- a/tests/test_base_types.py +++ b/tests/test_base_types.py @@ -1,6 +1,6 @@ import pytest -from ethereum.base_types import U256, U256_MAX_VALUE, FixedBytes, Uint +from ethereum.base_types import U256, FixedBytes, Uint def test_uint_new() -> None: @@ -1267,15 +1267,15 @@ def test_u256_bitwise_ixor_failed() -> None: def test_u256_invert() -> None: - assert ~U256(0) == U256_MAX_VALUE - assert ~U256(10) == U256_MAX_VALUE - 10 + assert ~U256(0) == int(U256.MAX_VALUE) + assert ~U256(10) == int(U256.MAX_VALUE) - 10 assert ~U256(2**256 - 1) == 0 def test_u256_rshift() -> None: - assert U256(U256_MAX_VALUE) >> 255 == 1 - assert U256(U256_MAX_VALUE) >> 256 == 0 - assert U256(U256_MAX_VALUE) >> 257 == 0 + assert U256.MAX_VALUE >> 255 == 1 + assert U256.MAX_VALUE >> 256 == 0 + assert U256.MAX_VALUE >> 257 == 0 assert U256(0) >> 20 == 0