Skip to content
Merged
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
8 changes: 6 additions & 2 deletions docs/writing_tests/test_markers.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def type_4_default_transaction(sender: Account, pre: Alloc):
@pytest.mark.with_all_typed_transactions
@pytest.mark.valid_from("Prague")
def test_something_with_all_tx_types(
state_test: StateTestFiller,
state_test: StateTestFiller,
pre: Alloc,
typed_transaction: Transaction
):
Expand Down Expand Up @@ -327,14 +327,18 @@ In this example, the test will be marked as expected to fail when it is being ex

This marker is used to mark tests that are slow to run. These tests are not run during [`tox` checks](./verifying_changes.md), and are only run when a release is being prepared.

### `@pytest.mark.pre_alloc_modify`
### `@pytest.mark.pre_alloc_mutable`

This marker is used to mark tests that modify the pre-alloc in a way that would be impractical to reproduce in a real-world scenario.

Examples of this include:

- Modifying the pre-alloc to have a balance of 2^256 - 1.
- Address collisions that would require hash collisions.
- EOA accounts containing code
- EOA accounts with a hard-coded nonce
- Contracts having zero-nonce
- Deploying a contract to a hard-coded address

### `@pytest.mark.skip()`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Base composite types for Ethereum test cases."""

import hashlib
import json
from dataclasses import dataclass
from typing import (
Any,
Expand Down Expand Up @@ -367,6 +369,11 @@ class Account(CamelModel):
state.
"""

model_config = {
**CamelModel.model_config,
"frozen": True,
Comment thread
fselmo marked this conversation as resolved.
}

@dataclass(kw_only=True)
class NonceMismatchError(Exception):
"""
Expand Down Expand Up @@ -514,6 +521,16 @@ def __bool__(self: "Account") -> bool:
"""Return True on a non-empty account."""
return any((self.nonce, self.balance, self.code, self.storage))

def hash(self) -> Hash:
"""Return the hash of the account given its properties."""
data = self.model_dump(mode="json")
blob = json.dumps(
data,
sort_keys=True,
separators=(",", ":"),
).encode("utf-8")
return Hash(hashlib.sha256(blob).digest())

@classmethod
def with_code(cls: Type, code: BytesConvertible) -> "Account":
"""Create account with provided `code` and nonce of `1`."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ..base_types import Address, Hash, Wei
from ..base_types_json import to_json
from ..composite_types import AccessList
from ..composite_types import AccessList, Account


@pytest.mark.parametrize(
Expand Down Expand Up @@ -290,3 +290,57 @@ def test_json_deserialization(
)
model_type = type(model_instance)
assert model_type(**json) == model_instance


@pytest.mark.parametrize(
"account_1, account_2, equal",
[
(Account(), Account(), True),
(Account(nonce=1), Account(nonce=2), False),
(Account(nonce=1), Account(nonce=1), True),
(Account(nonce=1), Account(nonce=1, code="0x1234"), False),
(Account(nonce=1, code="0x1234"), Account(nonce=1), False),
(
Account(nonce=1, code="0x1234"),
Account(nonce=1, code="0x1234"),
True,
),
(
Account(nonce=1, code="0x1234"),
Account(nonce=1, code="0x5678"),
False,
),
(
Account(nonce=1, code="0x1234"),
Account(nonce=2, code="0x5678"),
False,
),
(
Account(nonce=1, code="0x1234"),
Account(nonce=2, code="0x1234"),
False,
),
(
Account(nonce=1, code="0x1234"),
Account(nonce=1, code="0x1234", storage={0: 0, 1: 1}),
False,
),
(
Account(nonce=1, code="0x1234", storage={1: 1, 0: 0}),
Account(nonce=1, code="0x1234", storage={0: 0, 1: 1}),
True,
),
],
)
def test_account_hash(
account_1: Account, account_2: Account, equal: bool
) -> None:
"""Test two different accounts to return the same hash."""
if equal:
assert account_1.hash() == account_2.hash(), (
f"Account 1: {account_1.hash()}, Account 2: {account_2.hash()}"
)
else:
assert account_1.hash() != account_2.hash(), (
f"Account 1: {account_1.hash()}, Account 2: {account_2.hash()}"
)
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ def pytest_collection_modifyitems(
elif marker.name == "valid_at_transition_to":
items_for_removal.append(i)
continue
elif marker.name == "pre_alloc_modify":
elif marker.name == "pre_alloc_mutable":
item.add_marker(
pytest.mark.skip(
reason="Pre-alloc modification not supported"
Expand Down
Loading
Loading