Skip to content

Commit bc2e123

Browse files
committed
refactor(bal): Apply comments from first pass at PR
- Use ``Number`` instead of ``int`` where appropriate - Use ``default_factory=list`` without ``Optional`` and ``None`` where appropriate
1 parent 6999fa3 commit bc2e123

File tree

1 file changed

+18
-40
lines changed

1 file changed

+18
-40
lines changed

src/ethereum_test_types/block_access_list.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
these are simple data classes that can be composed together.
66
"""
77

8-
from typing import Any, ClassVar, Dict, List, Optional
8+
from typing import Any, ClassVar, Dict, List
99

1010
from pydantic import Field
1111

@@ -14,6 +14,7 @@
1414
Bytes,
1515
CamelModel,
1616
HexNumber,
17+
Number,
1718
RLPSerializable,
1819
StorageKey,
1920
)
@@ -23,16 +24,16 @@
2324
class BalNonceChange(CamelModel, RLPSerializable):
2425
"""Represents a nonce change in the block access list."""
2526

26-
tx_index: int = Field(..., description="Transaction index where the change occurred")
27-
post_nonce: int = Field(..., description="Nonce value after the transaction")
27+
tx_index: Number = Field(..., description="Transaction index where the change occurred")
28+
post_nonce: Number = Field(..., description="Nonce value after the transaction")
2829

2930
rlp_fields: ClassVar[List[str]] = ["tx_index", "post_nonce"]
3031

3132

3233
class BalBalanceChange(CamelModel, RLPSerializable):
3334
"""Represents a balance change in the block access list."""
3435

35-
tx_index: int = Field(..., description="Transaction index where the change occurred")
36+
tx_index: Number = Field(..., description="Transaction index where the change occurred")
3637
post_balance: HexNumber = Field(..., description="Balance after the transaction")
3738

3839
rlp_fields: ClassVar[List[str]] = ["tx_index", "post_balance"]
@@ -41,7 +42,7 @@ class BalBalanceChange(CamelModel, RLPSerializable):
4142
class BalCodeChange(CamelModel, RLPSerializable):
4243
"""Represents a code change in the block access list."""
4344

44-
tx_index: int = Field(..., description="Transaction index where the change occurred")
45+
tx_index: Number = Field(..., description="Transaction index where the change occurred")
4546
new_code: Bytes = Field(..., description="New code bytes")
4647

4748
rlp_fields: ClassVar[List[str]] = ["tx_index", "new_code"]
@@ -50,7 +51,7 @@ class BalCodeChange(CamelModel, RLPSerializable):
5051
class BalStorageChange(CamelModel, RLPSerializable):
5152
"""Represents a change to a specific storage slot."""
5253

53-
tx_index: int = Field(..., description="Transaction index where the change occurred")
54+
tx_index: Number = Field(..., description="Transaction index where the change occurred")
5455
post_value: StorageKey = Field(..., description="Value after the transaction")
5556

5657
rlp_fields: ClassVar[List[str]] = ["tx_index", "post_value"]
@@ -71,18 +72,20 @@ class BalAccountChange(CamelModel, RLPSerializable):
7172
"""Represents all changes to a specific account in a block."""
7273

7374
address: Address = Field(..., description="Account address")
74-
nonce_changes: Optional[List[BalNonceChange]] = Field(
75-
None, description="List of nonce changes"
75+
nonce_changes: List[BalNonceChange] = Field(
76+
default_factory=list, description="List of nonce changes"
7677
)
77-
balance_changes: Optional[List[BalBalanceChange]] = Field(
78-
None, description="List of balance changes"
78+
balance_changes: List[BalBalanceChange] = Field(
79+
default_factory=list, description="List of balance changes"
7980
)
80-
code_changes: Optional[List[BalCodeChange]] = Field(None, description="List of code changes")
81-
storage_changes: Optional[List[BalStorageSlot]] = Field(
82-
None, description="List of storage changes"
81+
code_changes: List[BalCodeChange] = Field(
82+
default_factory=list, description="List of code changes"
8383
)
84-
storage_reads: Optional[List[StorageKey]] = Field(
85-
None, description="List of storage slots that were read"
84+
storage_changes: List[BalStorageSlot] = Field(
85+
default_factory=list, description="List of storage changes"
86+
)
87+
storage_reads: List[StorageKey] = Field(
88+
default_factory=list, description="List of storage slots that were read"
8689
)
8790

8891
rlp_fields: ClassVar[List[str]] = [
@@ -94,31 +97,6 @@ class BalAccountChange(CamelModel, RLPSerializable):
9497
"code_changes",
9598
]
9699

97-
def to_list(self, signing: bool = False) -> List[Any]:
98-
"""
99-
Override to handle None list fields properly.
100-
None list fields should serialize as empty lists, not empty bytes.
101-
"""
102-
from ethereum_test_base_types.serialization import to_serializable_element
103-
104-
result: list[Any] = []
105-
for field_name in self.rlp_fields:
106-
value = getattr(self, field_name)
107-
108-
# Special handling for None list fields - they should be empty lists
109-
if value is None and field_name in [
110-
"storage_changes",
111-
"storage_reads",
112-
"balance_changes",
113-
"nonce_changes",
114-
"code_changes",
115-
]:
116-
result.append([])
117-
else:
118-
result.append(to_serializable_element(value))
119-
120-
return result
121-
122100

123101
class BlockAccessList(CamelModel, RLPSerializable):
124102
"""

0 commit comments

Comments
 (0)