Skip to content

Commit 55d6595

Browse files
authored
enhance(testing): add common BytesConcatenation to execution_testing (#1858)
1 parent daeb043 commit 55d6595

File tree

6 files changed

+38
-55
lines changed

6 files changed

+38
-55
lines changed

packages/testing/src/execution_testing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Account,
66
Address,
77
Bytes,
8+
BytesConcatenation,
89
CoerceBytes,
910
Hash,
1011
Storage,
@@ -143,6 +144,7 @@
143144
"BlockException",
144145
"Bytecode",
145146
"Bytes",
147+
"BytesConcatenation",
146148
"CalldataCase",
147149
"Case",
148150
"ChainConfig",

packages/testing/src/execution_testing/base_types/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
BLSPublicKey,
77
BLSSignature,
88
Bytes,
9+
BytesConcatenation,
910
CoerceBytes,
1011
FixedSizeBytes,
1112
ForkHash,
@@ -56,6 +57,7 @@
5657
"BLSPublicKey",
5758
"BLSSignature",
5859
"Bytes",
60+
"BytesConcatenation",
5961
"CamelModel",
6062
"CoerceBytes",
6163
"EmptyOmmersRoot",

packages/testing/src/execution_testing/base_types/base_types.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
"""Basic type primitives used to define other types."""
22

3+
from abc import ABCMeta
34
from hashlib import sha256
45
from re import sub
5-
from typing import Annotated, Any, ClassVar, SupportsBytes, Type, TypeVar
6+
from typing import (
7+
Annotated,
8+
Any,
9+
ClassVar,
10+
Sized,
11+
SupportsBytes,
12+
Type,
13+
TypeVar,
14+
)
615

716
from Crypto.Hash import keccak
817
from pydantic import GetCoreSchemaHandler, StringConstraints
@@ -233,6 +242,22 @@ def __new__(cls, input_bytes: BytesConvertible = b"") -> Self:
233242
return super(Bytes, cls).__new__(cls, to_bytes(input_bytes))
234243

235244

245+
class BytesConcatenation(SupportsBytes, Sized, metaclass=ABCMeta):
246+
"""A class that can be concatenated with bytes."""
247+
248+
def __len__(self) -> int:
249+
"""Return length of the object when converted to bytes."""
250+
return len(bytes(self))
251+
252+
def __add__(self, other: bytes | SupportsBytes) -> bytes:
253+
"""Concatenates the object with another bytes object."""
254+
return bytes(self) + bytes(other)
255+
256+
def __radd__(self, other: bytes | SupportsBytes) -> bytes:
257+
"""Concatenates the object with another bytes object."""
258+
return bytes(other) + bytes(self)
259+
260+
236261
class FixedSizeHexNumber(int, ToStringSchema):
237262
"""
238263
A base class that helps represent an integer as a fixed byte-length

tests/benchmark/compute/helpers.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
from enum import Enum, auto
55
from typing import Sequence, cast
66

7-
from execution_testing import Fork, Hash, Op
7+
from execution_testing import BytesConcatenation, Fork, Hash, Op
88

9-
from tests.osaka.eip7951_p256verify_precompiles.spec import (
10-
BytesConcatenation as P256BytesConcatenation,
11-
)
129
from tests.osaka.eip7951_p256verify_precompiles.spec import (
1310
FieldElement,
1411
)
15-
from tests.prague.eip2537_bls_12_381_precompiles.spec import (
16-
BytesConcatenation as BLSBytesConcatenation,
17-
)
1812

1913
DEFAULT_BINOP_ARGS = (
2014
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
@@ -98,10 +92,7 @@ def sar(x: int, s: int) -> int:
9892

9993
def concatenate_parameters(
10094
parameters: (
101-
Sequence[str]
102-
| Sequence[P256BytesConcatenation]
103-
| Sequence[BLSBytesConcatenation]
104-
| Sequence[bytes]
95+
Sequence[str] | Sequence[BytesConcatenation] | Sequence[bytes]
10596
),
10697
) -> bytes:
10798
"""
@@ -124,8 +115,7 @@ def concatenate_parameters(
124115
p,
125116
(
126117
bytes,
127-
P256BytesConcatenation,
128-
BLSBytesConcatenation,
118+
BytesConcatenation,
129119
FieldElement,
130120
),
131121
)
@@ -134,12 +124,7 @@ def concatenate_parameters(
134124
parameters_bytes_list = [
135125
bytes(p)
136126
for p in cast(
137-
Sequence[
138-
P256BytesConcatenation
139-
| BLSBytesConcatenation
140-
| bytes
141-
| FieldElement
142-
],
127+
Sequence[BytesConcatenation | bytes | FieldElement],
143128
parameters,
144129
)
145130
]

tests/osaka/eip7951_p256verify_precompiles/spec.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""Defines EIP-7951 specification constants and functions."""
22

33
from dataclasses import dataclass
4-
from typing import Sized, SupportsBytes
54

6-
from execution_testing import Address, Bytes
5+
from execution_testing import Address, Bytes, BytesConcatenation
76

87

98
@dataclass(frozen=True)
@@ -19,22 +18,6 @@ class ReferenceSpec:
1918
)
2019

2120

22-
class BytesConcatenation(SupportsBytes, Sized):
23-
"""A class that can be concatenated with bytes."""
24-
25-
def __len__(self) -> int:
26-
"""Return length of the object when converted to bytes."""
27-
return len(bytes(self))
28-
29-
def __add__(self, other: bytes | SupportsBytes) -> bytes:
30-
"""Concatenates the object with another bytes object."""
31-
return bytes(self) + bytes(other)
32-
33-
def __radd__(self, other: bytes | SupportsBytes) -> bytes:
34-
"""Concatenates the object with another bytes object."""
35-
return bytes(other) + bytes(self)
36-
37-
3821
@dataclass(frozen=True)
3922
class FieldElement(BytesConcatenation):
4023
"""Dataclass that defines a single field element."""

tests/prague/eip2537_bls_12_381_precompiles/spec.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from dataclasses import dataclass
44
from enum import Enum, auto
5-
from typing import Callable, Sized, SupportsBytes, Tuple
5+
from typing import Callable, Tuple
6+
7+
from execution_testing import BytesConcatenation
68

79

810
@dataclass(frozen=True)
@@ -18,22 +20,6 @@ class ReferenceSpec:
1820
)
1921

2022

21-
class BytesConcatenation(SupportsBytes, Sized):
22-
"""A class that can be concatenated with bytes."""
23-
24-
def __len__(self) -> int:
25-
"""Return length of the object when converted to bytes."""
26-
return len(bytes(self))
27-
28-
def __add__(self, other: bytes | SupportsBytes) -> bytes:
29-
"""Concatenates the object with another bytes object."""
30-
return bytes(self) + bytes(other)
31-
32-
def __radd__(self, other: bytes | SupportsBytes) -> bytes:
33-
"""Concatenates the object with another bytes object."""
34-
return bytes(other) + bytes(self)
35-
36-
3723
@dataclass(frozen=True)
3824
class FP(BytesConcatenation):
3925
"""Dataclass that defines a single element of Fp."""

0 commit comments

Comments
 (0)