diff --git a/.claude/commands/merge-eip-branches.md b/.claude/commands/merge-eip-branches.md new file mode 100644 index 00000000000..05c4f068975 --- /dev/null +++ b/.claude/commands/merge-eip-branches.md @@ -0,0 +1,150 @@ +# Merge EIP Adoption Branches + +Consolidate several single-EIP adoption branches into one fork that adopts +all of them. Run this skill before starting such a merge. Each source +branch was built by `/adopt-upstream-eip`, alone, against its own upstream +EIP branch, so no source branch knows what the combined fork does. + +## Inputs + +- The source branches, and `` they all target. +- ``, the branch they merge into. Example: `forks/monad_nine`. +- `` and its upstream integration branch, which already has every + EIP composed. Example: `upstream/forks/amsterdam`. + +## Read First + +`/adopt-upstream-eip` defines the fork shape, the shared-file cost rules, +the step-12 cleanup list and the definition of done. This skill only covers +what the merge adds. + +## 1. Prepare + +```bash +git worktree add -b origin/ +git branch --unset-upstream +``` + +Merge into a fresh worktree, and never check out or rewrite a source +branch. Note which commits already landed on `` after the +source branches were cut: a branch carrying its own copy of one of them +conflicts, and ``'s version wins. + +```bash +git log --oneline $(git merge-base origin/ )..origin/ +``` + +## 2. Merge + +Merge the largest branch first, then the rest, with real merge commits. +Squashing or cherry-picking hides which branch each conflict came from. + +```bash +git merge # once per source branch +``` + +## 3. Resolve the Conflicts Git Reports + +Expect these, in rising order of thought needed: + +- **`.github/configs/feature.yaml`** — every branch appends its own entry + at the same point. Keep all entries for now; step 5 collapses them. +- **`forks.py`, the `` declaration** — each branch declares the + class with one mixin. The merged class lists every mixin ahead of the + parent fork, in ascending EIP order. Drop any dead `pass` left after + `follows()`. +- **CI files already on ``** — keep the base branch's version. +- **`src/ethereum/forks//`** — two EIPs changing the same file. + Resolve against the upstream integration branch, not against either + branch alone. + +What must merge silently: the `follows()` trait, the +`BaseForkMeta._is_subclass_of` walk, the `ValidAtTransitionTo` guard and the +exclusion conftests. They are byte-identical across branches by design. +Check afterwards that one copy of each survived. + +## 4. Find the Silent Merges + +This is the main risk. Two branches editing the same file at different +lines merge cleanly and produce behavior neither branch had. Git reports +nothing. + +Find the candidates from the upstream spec changes: + +```bash +for n in ; do + base=$(git merge-base upstream/eips//eip-$n) + echo "### EIP-$n" + git diff --stat $base..upstream/eips//eip-$n -- src/ethereum/forks/ +done +``` + +Any file listed under two EIPs needs reading in full on the merge branch. +Where one EIP removes what another adds, the merged file must match the +upstream integration branch, which already has both. + +Two shapes seen in practice: + +- One EIP adds a log for a state change a later EIP removes. Git keeps + both: a log for something that no longer happens. +- Two branches add the same conftest hook at different offsets, one after + the imports and one appended. Git keeps both hook definitions in one + module. + +Also read every file the merge touched outside `src/ethereum/forks/monad*` +and `tests/monad*`, whether or not git flagged it. + +## 5. Clean Up the Scaffolding + +Follow `/adopt-upstream-eip` step 12, driven by what the merged fork now +adopts: + +- Delete the exclusion conftest for each adopted EIP's suite. Those suites + must now be selected and pass. +- Collapse the per-branch feature entries into one. When the branch name + follows `eips//eip-+`, the feature name must carry the same + numbers in the same order: `check_release_matrix.py` matches the sequence + as a literal string. Verify it: + + ```bash + python3 .github/scripts/check_release_matrix.py "" + ``` + +- Revert expectation scaffolding written for an intermediate fork shape. A + test edit that only made a fork with EIP-A but without EIP-B green is + scaffolding once B joins. + +To test whether an edit was scaffolding, revert it and diff against the +base branch: + +```bash +git revert --no-commit && git diff origin/ -- +``` + +An empty diff means the edit existed only for the intermediate shape. +Reverting cleanly needs the source branch to have kept scaffolding in its +own commit; a commit mixing scaffolding with permanent change has to be +undone by hand. + +Keep gates on a capability (`fork.is_eip_enabled()`, `fork >= +MONAD_EIGHT`) that stay true on the merged fork. They are permanent. + +## 6. Validate + +Run `/adopt-upstream-eip`'s step-3 shape check, its step-5 `--collect-only` +selection check, then the full-suite fill for the fork. The full suite +matters: the merge changes which suites select the fork. + +Measure any exclusion that survived the cleanup, one suite at a time, by +stripping its hook and filling that suite. State which ones you did not get +to measure. + +## Definition of Done + +- Every source branch merged, with the merge commits kept. +- Each file two EIPs touched read in full and matched against the upstream + integration branch. +- One copy of each shared addition; no duplicated hook or trait. +- Adopted suites selected and passing; their exclusions deleted. +- One feature entry, its name matching the branch's EIP sequence. +- Full-suite fill green, with any unmeasured exclusion named. diff --git a/.github/configs/feature.yaml b/.github/configs/feature.yaml index ba528347f43..8ec546059a8 100644 --- a/.github/configs/feature.yaml +++ b/.github/configs/feature.yaml @@ -6,3 +6,7 @@ monad_runloop: evm-type: eels # Like `monad`, but `--monad-runloop` and eestnet chain id `30143` fill-params: -m blockchain_test --from=MONAD_EIGHT --until=MONAD_TEN --chain-id=30143 --monad-runloop -k "not invalid_header" + +monad_eip7708+7843+7981+7997+8024+8246: + evm-type: eels + fill-params: -m blockchain_test --fork=MONAD_NEXT --chain-id=143 -k "not invalid_header" diff --git a/.github/workflows/check_release.yaml b/.github/workflows/check_release.yaml index 8b1f7447b6f..d26999766bf 100644 --- a/.github/workflows/check_release.yaml +++ b/.github/workflows/check_release.yaml @@ -6,9 +6,10 @@ run-name: ${{ github.event_name == 'workflow_dispatch' && format('Check Fixture # breaks surfaces on the branch that broke it rather than on the next # release attempt. # -# Two tiers, both running on every pull request and on pushes to the -# branches that release in parallel with the fork branch, -# `from-upstream` and `eips/**`. +# Two tiers, both running on pushes to the branches that release in +# parallel with the fork branch, `from-upstream` and `eips/**`. A pull +# request adds no trigger of its own: its pushes already run here, and +# an event per push and per synchronize rehearsed each push twice. # # `collect` is cheap: it reads the features the branch releases out of # `.github/configs/feature.yaml`, builds the release's job matrix @@ -32,15 +33,6 @@ on: - "whitelist.txt" - "docs/**" - "mkdocs.yml" - pull_request: - paths-ignore: - - "**.md" - - "LICENSE*" - - ".gitignore" - - ".vscode/**" - - "whitelist.txt" - - "docs/**" - - "mkdocs.yml" workflow_dispatch: inputs: depth: @@ -79,10 +71,7 @@ jobs: shell: bash env: INPUT_FEATURES: ${{ inputs.features }} - # The head branch, so a pull request from an EIP branch - # rehearses that branch's feature; `ref_name` is the merge ref - # on a pull request and the branch everywhere else. - BRANCH: ${{ github.head_ref || github.ref_name }} + BRANCH: ${{ github.ref_name }} run: | # The feature selection and the per-feature release matrix live # in (and are shared with the release workflow via) diff --git a/packages/testing/src/execution_testing/cli/pytest_commands/plugins/execute/rpc/hive.py b/packages/testing/src/execution_testing/cli/pytest_commands/plugins/execute/rpc/hive.py index 58ce18f480f..bbd287900a2 100644 --- a/packages/testing/src/execution_testing/cli/pytest_commands/plugins/execute/rpc/hive.py +++ b/packages/testing/src/execution_testing/cli/pytest_commands/plugins/execute/rpc/hive.py @@ -183,7 +183,11 @@ def build_genesis_header( requests_hash=Requests() if genesis_fork.header_requests_required() else None, - block_access_list_hash=BlockAccessList().rlp_hash + block_access_list_hash=( + BlockAccessList().rlp_hash + if genesis_fork.supports_block_access_lists() + else Hash(0) + ) if genesis_fork.header_bal_hash_required() else None, slot_number=0 if genesis_fork.header_slot_number_required() else None, diff --git a/packages/testing/src/execution_testing/cli/pytest_commands/plugins/forks/forks.py b/packages/testing/src/execution_testing/cli/pytest_commands/plugins/forks/forks.py index 6075481806c..29ef4dc86e3 100644 --- a/packages/testing/src/execution_testing/cli/pytest_commands/plugins/forks/forks.py +++ b/packages/testing/src/execution_testing/cli/pytest_commands/plugins/forks/forks.py @@ -1265,7 +1265,9 @@ def _process_with_marker_args( "Missing fork argument with 'valid_at_transition_to' marker." ) - if len(forks) > 1: + # A single EIP argument expands to one fork per enabling fork, so + # the limit is on the arguments rather than on the resolved forks. + if len(fork_args) > 1: raise Exception( "Too many forks specified to 'valid_at_transition_to' marker." ) diff --git a/packages/testing/src/execution_testing/evm_tools/t8n/block_environment.py b/packages/testing/src/execution_testing/evm_tools/t8n/block_environment.py index 3a602b9c6c4..ce61b3914f5 100644 --- a/packages/testing/src/execution_testing/evm_tools/t8n/block_environment.py +++ b/packages/testing/src/execution_testing/evm_tools/t8n/block_environment.py @@ -160,7 +160,7 @@ def _resolve_excess_blob_gas( } if fork.has_compute_requests_hash: arguments["requests_hash"] = Hash32(b"\0" * 32) - if fork.has_hash_block_access_list: + if fork.has_block_access_list_hash_header: arguments["block_access_list_hash"] = Hash32(b"\0" * 32) if fork.has_slot_number: arguments["slot_number"] = U64(0) diff --git a/packages/testing/src/execution_testing/fixtures/blockchain.py b/packages/testing/src/execution_testing/fixtures/blockchain.py index a7331378786..7d4269bfd95 100644 --- a/packages/testing/src/execution_testing/fixtures/blockchain.py +++ b/packages/testing/src/execution_testing/fixtures/blockchain.py @@ -377,7 +377,13 @@ def genesis(cls, fork: Fork, env: Environment, state_root: Hash) -> Self: if fork.header_requests_required(): extras["requests_hash"] = Requests() if fork.header_bal_hash_required(): - extras["block_access_list_hash"] = BlockAccessList().rlp_hash + # A fork can require the header field without building block + # access lists (e.g. Monad); the field is then fixed at zero. + extras["block_access_list_hash"] = ( + BlockAccessList().rlp_hash + if fork.supports_block_access_lists() + else Hash(0) + ) if fork.header_slot_number_required(): extras["slot_number"] = ( int(env.slot_number) if env.slot_number is not None else 0 diff --git a/packages/testing/src/execution_testing/forks/base_fork.py b/packages/testing/src/execution_testing/forks/base_fork.py index f842878c42f..e480bce1e79 100644 --- a/packages/testing/src/execution_testing/forks/base_fork.py +++ b/packages/testing/src/execution_testing/forks/base_fork.py @@ -333,12 +333,30 @@ def _maybe_transitioned(fork_cls: "BaseForkMeta") -> "BaseForkMeta": @staticmethod def _is_subclass_of(a: "BaseForkMeta", b: "BaseForkMeta") -> bool: """ - Check if `a` is a subclass of `b`, taking fork transitions into - account. + Check if `a` is a subclass of `b`, taking fork transitions and + declared succession into account. + + A fork can follow another fork it does not inherit from, which + places it after that fork (and after everything that fork comes + after) in the fork order without adopting its behavior. The + EIPs of a followed fork stay out of that ordering. """ a = BaseForkMeta._maybe_transitioned(a) b = BaseForkMeta._maybe_transitioned(b) - return issubclass(a, b) + if issubclass(a, b): + return True + # The metaclass sees its instances as plain classes, so the + # traits are reached through a cast, as elsewhere in this class. + # Succession places a fork after another fork, not after that + # fork's EIPs; an adopted EIP is reached by inheritance above. + if cast(Type["BaseFork"], b).is_eip(): + return False + followed = cast(Type["BaseFork"], a).follows() + while followed is not None: + if issubclass(followed, b): + return True + followed = followed.follows() + return False def __gt__(cls, other: "BaseForkMeta") -> bool: """Compare if a fork is newer than some other fork (cls > other).""" @@ -563,6 +581,17 @@ def header_bal_hash_required(cls) -> bool: """Return true if the header must contain block access list hash.""" pass + @classmethod + def supports_block_access_lists(cls) -> bool: + """ + Return true if the fork builds block access lists (EIP-7928). + + A fork can require the block access list hash header field + without building the lists, and then fixes the field at zero, so + this follows the EIP rather than the header requirement. + """ + return cls.is_eip_enabled(7928) + @classmethod @abstractmethod def empty_block_bal_item_count(cls) -> int: @@ -1407,6 +1436,17 @@ def enabling_forks(cls) -> Set[Type["BaseFork"]]: raise Exception(f"Class {cls.__name__} is not an EIP.") return cls._enabling_forks + @classmethod + def follows(cls) -> Type["BaseFork"] | None: + """ + Return the fork this one comes after without inheriting it. + + A fork that reuses another lineage's ordering overrides this; + comparisons then place it after that fork, and after everything + that fork comes after, while its behavior stays its own. + """ + return None + @classmethod def parent(cls) -> Type["BaseFork"] | None: """Return the parent fork.""" diff --git a/packages/testing/src/execution_testing/forks/forks/forks.py b/packages/testing/src/execution_testing/forks/forks/forks.py index ecf3b02f3ae..05b17c4d4fd 100644 --- a/packages/testing/src/execution_testing/forks/forks/forks.py +++ b/packages/testing/src/execution_testing/forks/forks/forks.py @@ -1805,12 +1805,6 @@ def _calculate_sstore_gas_mip8( return gas_cost -class MONAD_NEXT(MONAD_TEN): # noqa: N801 - """MONAD_NEXT fork, a placeholder identical to MONAD_TEN.""" - - pass - - class BPO1( Osaka, bpo_fork=True, @@ -1904,3 +1898,37 @@ def engine_payload_attribute_target_gas_limit(cls) -> bool: limit. """ return True + + +class MONAD_NEXT( # noqa: N801 + eips.EIP7708, + eips.EIP7843, + eips.EIP7981, + eips.EIP7997, + eips.EIP8024, + eips.EIP8246, + MONAD_TEN, + deployed=False, +): + """ + MONAD_NEXT fork. + + Amsterdam-based successor to MONAD_TEN, adopting the EIP-7708, + EIP-7843, EIP-7981, EIP-7997, EIP-8024 and EIP-8246 changes. The + Amsterdam changes it does not adopt stay out of the fork by not + being inherited at all; the fork order still places MONAD_NEXT + after Amsterdam through `follows`. + """ + + @classmethod + def follows(cls) -> type[BaseFork] | None: + """MONAD_NEXT comes after Amsterdam without inheriting it.""" + return Amsterdam + + @classmethod + def header_bal_hash_required(cls) -> bool: + """ + MONAD_NEXT headers carry the block access list hash field, fixed + at zero, without building block access lists (EIP-7928). + """ + return True diff --git a/packages/testing/src/execution_testing/forks/tests/test_forks.py b/packages/testing/src/execution_testing/forks/tests/test_forks.py index ea747d480f2..592cca401a9 100644 --- a/packages/testing/src/execution_testing/forks/tests/test_forks.py +++ b/packages/testing/src/execution_testing/forks/tests/test_forks.py @@ -10,6 +10,7 @@ from execution_testing.vm import Opcodes from ..base_fork import BaseFork, BaseForkMeta +from ..forks.eips import EIP2780, EIP7708, EIP8024 from ..forks.eips.paris.eip_3675 import EIP3675 from ..forks.forks import ( BPO1, @@ -18,6 +19,8 @@ BPO4, BPO5, MONAD_EIGHT, + MONAD_NEXT, + MONAD_TEN, Amsterdam, Berlin, Cancun, @@ -296,6 +299,25 @@ def test_fork_comparison() -> None: assert fork == Berlin +def test_succession_does_not_imply_eip_membership() -> None: + """ + A fork placed after another by `follows` does not gain its EIPs. + + Succession orders MONAD_NEXT after Amsterdam so the Amsterdam + suites can select it, while the EIPs it declines must still compare + as absent. An adopted EIP is reached through inheritance instead, so + it keeps comparing as present. + """ + assert MONAD_NEXT > Amsterdam + assert MONAD_NEXT > MONAD_TEN + + assert MONAD_NEXT >= EIP7708 + assert MONAD_NEXT.is_eip_enabled(7708) + + assert not MONAD_NEXT >= EIP2780 + assert not MONAD_NEXT.is_eip_enabled(2780) + + def test_transition_fork_comparison() -> None: """ Test comparing to a transition fork. @@ -382,6 +404,39 @@ class PreAllocTransitionFork(TransitionBaseClass): pass +def test_monad_next_is_not_deployed() -> None: + """ + A fork under development must stay out of the deployed fork range. + """ + deployed_forks = get_deployed_forks() + assert MONAD_TEN in deployed_forks + assert MONAD_NEXT not in deployed_forks + + +def opcode_names(fork: Type[BaseFork]) -> set[str]: + """Return the names of the opcodes a fork declares valid.""" + return {str(opcode) for opcode in fork.valid_opcodes()} + + +def test_adopted_opcodes_depend_on_mixin_order() -> None: + """ + The adopted opcodes reach MONAD_NEXT through its mixin order. + + MONAD_NINE returning Osaka's list directly is what ends the chain + there, which is what lets a mixin placed ahead of the Monad forks + contribute on the way out. Making that call cooperative, or + reordering MONAD_NEXT's bases, drops these opcodes silently. + """ + adopted = {"DUPN", "SWAPN", "EXCHANGE"} + assert adopted <= opcode_names(MONAD_NEXT) + assert not adopted & opcode_names(MONAD_TEN) + + class MixinAfterFork(MONAD_TEN, EIP8024): # noqa: N801 + """Dummy fork ordering an EIP mixin after the Monad fork.""" + + assert not adopted & opcode_names(MixinAfterFork) + + def test_pre_alloc() -> None: # noqa: D103 assert PrePreAllocFork.pre_allocation() == {"test": "test"} assert PreAllocFork.pre_allocation() == {"test": "test", "test2": "test2"} diff --git a/packages/testing/src/execution_testing/specs/blockchain.py b/packages/testing/src/execution_testing/specs/blockchain.py index c7f5398ae2e..cd2f85cf333 100644 --- a/packages/testing/src/execution_testing/specs/blockchain.py +++ b/packages/testing/src/execution_testing/specs/blockchain.py @@ -986,6 +986,16 @@ def generate_block_data( int(env.slot_number) if env.slot_number is not None else 0 ) + # Prepare block_access_list_hash for header initialization + bal_hash_field: Dict[str, Any] = {} + if fork.header_bal_hash_required() and ( + not fork.supports_block_access_lists() + ): + # Fork requires the block access list hash header field but + # doesn't build block access lists (e.g. Monad): fix value at + # zero. + bal_hash_field["block_access_list_hash"] = Hash(0) + header = FixtureHeader( **( transition_tool_output.result.model_dump( @@ -996,6 +1006,7 @@ def generate_block_data( exclude_none=True, exclude={"blob_gas_used", "slot_number"}, ) + | bal_hash_field ), blob_gas_used=blob_gas_used, transactions_trie=Transaction.list_root(txs), @@ -1059,7 +1070,7 @@ def generate_block_data( if t8n_bal_rlp is not None: t8n_bal = BlockAccessList.from_rlp(t8n_bal_rlp) - if fork.header_bal_hash_required(): + if fork.supports_block_access_lists(): assert t8n_bal is not None, ( "Block access list is required for this block but was not " "provided by the transition tool" diff --git a/src/ethereum/forks/monad_next/__init__.py b/src/ethereum/forks/monad_next/__init__.py index b6c71ab2450..fe79f2c61c8 100644 --- a/src/ethereum/forks/monad_next/__init__.py +++ b/src/ethereum/forks/monad_next/__init__.py @@ -1,6 +1,27 @@ """ -MONAD_NEXT fork is a placeholder for upcoming Monad changes and is -currently identical to MONAD_TEN. +MONAD_NEXT fork builds on MONAD_TEN, adopting part of Amsterdam. + +The Amsterdam changes it does not adopt stay out of the fork entirely. +The [EIP-7928] block access list hash header slot is carried but always +zero, so the header layout matches Amsterdam while no block access list +is built. + +### Changes + +- [EIP-7708: ETH transfers emit a log][EIP-7708] +- [EIP-7843: SLOTNUM][EIP-7843] +- [EIP-7981: Increase Access List Cost][EIP-7981] +- [EIP-7997: Deterministic Factory Predeploy][EIP-7997] +- [EIP-8024: Stack Access Instructions][EIP-8024] +- [EIP-8246: Remove SELFDESTRUCT balance burn][EIP-8246] + +[EIP-7708]: https://eips.ethereum.org/EIPS/eip-7708 +[EIP-7843]: https://eips.ethereum.org/EIPS/eip-7843 +[EIP-7928]: https://eips.ethereum.org/EIPS/eip-7928 +[EIP-7981]: https://eips.ethereum.org/EIPS/eip-7981 +[EIP-7997]: https://eips.ethereum.org/EIPS/eip-7997 +[EIP-8024]: https://eips.ethereum.org/EIPS/eip-8024 +[EIP-8246]: https://eips.ethereum.org/EIPS/eip-8246 """ from ethereum.fork_criteria import ByTimestamp, ForkCriteria diff --git a/src/ethereum/forks/monad_next/blocks.py b/src/ethereum/forks/monad_next/blocks.py index f6745f79de3..7a66282a7ab 100644 --- a/src/ethereum/forks/monad_next/blocks.py +++ b/src/ethereum/forks/monad_next/blocks.py @@ -248,6 +248,24 @@ class Header: [SHA2-256]: https://en.wikipedia.org/wiki/SHA-2 """ + block_access_list_hash: Hash32 + """ + Header slot introduced by [EIP-7928] for the hash of the Block Access + List. Monad does not build block access lists, so this field is always + zero. See [`validate_header`][vh]. + + [EIP-7928]: https://eips.ethereum.org/EIPS/eip-7928 + [vh]: ref:ethereum.forks.monad_next.fork.validate_header + """ + + slot_number: U64 + """ + The slot number of this block as provided by the consensus layer. + Introduced in [EIP-7843]. + + [EIP-7843]: https://eips.ethereum.org/EIPS/eip-7843 + """ + @final @slotted_freezable diff --git a/src/ethereum/forks/monad_next/fork.py b/src/ethereum/forks/monad_next/fork.py index 653f05d030f..baf76837c7d 100644 --- a/src/ethereum/forks/monad_next/fork.py +++ b/src/ethereum/forks/monad_next/fork.py @@ -61,8 +61,8 @@ BlockState, TransactionState, add_sender_authority, + clear_account_preserving_balance, create_ether, - destroy_account, extract_block_diff, forget_senders_authorities, get_account, @@ -246,6 +246,7 @@ def state_transition(chain: BlockChain, block: Block) -> None: prev_randao=block.header.prev_randao, excess_blob_gas=block.header.excess_blob_gas, parent_beacon_block_root=block.header.parent_beacon_block_root, + slot_number=block.header.slot_number, ) block_output = apply_body( @@ -402,6 +403,8 @@ def validate_header(chain: BlockChain, header: Header) -> None: raise InvalidBlock if header.ommers_hash != EMPTY_OMMER_HASH: raise InvalidBlock + if header.block_access_list_hash != Hash32(b"\x00" * 32): + raise InvalidBlock block_parent_hash = keccak256(rlp.encode(parent_header)) if header.parent_hash != block_parent_hash: @@ -973,9 +976,6 @@ def process_transaction( # transfer miner fees create_ether(tx_state, block_env.coinbase, U256(transaction_fee)) - for address in tx_output.accounts_to_delete: - destroy_account(tx_state, address) - # block_output.block_gas_used += tx_gas_used_after_refund block_output.block_gas_used += tx.gas block_output.blob_gas_used += tx_blob_gas_used @@ -995,6 +995,9 @@ def process_transaction( block_output.block_logs += tx_output.logs + for address in tx_output.accounts_to_delete: + clear_account_preserving_balance(tx_state, address) + incorporate_tx_into_block(tx_state) diff --git a/src/ethereum/forks/monad_next/state_tracker.py b/src/ethereum/forks/monad_next/state_tracker.py index 5b3d4be0bd2..1d0914461d2 100644 --- a/src/ethereum/forks/monad_next/state_tracker.py +++ b/src/ethereum/forks/monad_next/state_tracker.py @@ -423,10 +423,9 @@ def destroy_account(tx_state: TransactionState, address: Address) -> None: """ Completely remove the account at ``address`` and all of its storage. - This function is made available exclusively for the ``SELFDESTRUCT`` - opcode. It is expected that ``SELFDESTRUCT`` will be disabled in a - future hardfork and this function will be removed. Only supports same - transaction destruction. + Invoked by ``modify_state`` (and the coinbase fee-credit path) to + clean up an account that has become empty (zero nonce, empty + code, and zero balance) so it does not appear in the post-state. Parameters ---------- @@ -440,6 +439,30 @@ def destroy_account(tx_state: TransactionState, address: Address) -> None: set_account(tx_state, address, None) +def clear_account_preserving_balance( + tx_state: TransactionState, address: Address +) -> None: + """ + Clear an account's nonce, code, and storage while preserving its + balance. + + Parameters + ---------- + tx_state : + The transaction state. + address : + Address of the account to modify. + + """ + + def clear_account(account: Account) -> None: + account.nonce = Uint(0) + account.code_hash = EMPTY_CODE_HASH + + destroy_storage(tx_state, address) + modify_state(tx_state, address, clear_account) + + def destroy_storage(tx_state: TransactionState, address: Address) -> None: """ Completely remove the storage at ``address``. diff --git a/src/ethereum/forks/monad_next/transactions.py b/src/ethereum/forks/monad_next/transactions.py index 24bb4880a74..71bce9409f3 100644 --- a/src/ethereum/forks/monad_next/transactions.py +++ b/src/ethereum/forks/monad_next/transactions.py @@ -30,6 +30,22 @@ TX_MAX_GAS_LIMIT = Uint(30_000_000) +ACCESS_LIST_ADDRESS_FLOOR_TOKENS = Uint(80) +""" +Floor data tokens contributed by a single access list address per +[EIP-7981]. + +[EIP-7981]: https://eips.ethereum.org/EIPS/eip-7981 +""" + +ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS = Uint(128) +""" +Floor data tokens contributed by a single access list storage key per +[EIP-7981]. + +[EIP-7981]: https://eips.ethereum.org/EIPS/eip-7981 +""" + @final @slotted_freezable @@ -587,10 +603,6 @@ def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]: num_non_zeros = ulen(tx.data) - num_zeros tokens_in_calldata = num_zeros + num_non_zeros * Uint(4) - # EIP-7623 floor price (note: no EVM costs) - calldata_floor_gas_cost = ( - tokens_in_calldata * GasCosts.TX_DATA_TOKEN_FLOOR + GasCosts.TX_BASE - ) data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD @@ -600,12 +612,28 @@ def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]: create_cost = Uint(0) access_list_cost = Uint(0) + tokens_in_access_list = Uint(0) if has_access_list(tx): for access in tx.access_list: access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS access_list_cost += ( ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY ) + tokens_in_access_list += ACCESS_LIST_ADDRESS_FLOOR_TOKENS + tokens_in_access_list += ( + ulen(access.slots) * ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS + ) + + # Data token floor cost for access list bytes. + access_list_cost += tokens_in_access_list * GasCosts.TX_DATA_TOKEN_FLOOR + + # Total floor tokens. + total_floor_tokens = tokens_in_calldata + tokens_in_access_list + + # EIP-7623 floor price (note: no EVM costs) + calldata_floor_gas_cost = ( + total_floor_tokens * GasCosts.TX_DATA_TOKEN_FLOOR + GasCosts.TX_BASE + ) auth_cost = Uint(0) if isinstance(tx, SetCodeTransaction): diff --git a/src/ethereum/forks/monad_next/vm/__init__.py b/src/ethereum/forks/monad_next/vm/__init__.py index 15c53bc75b6..e686d622a2d 100644 --- a/src/ethereum/forks/monad_next/vm/__init__.py +++ b/src/ethereum/forks/monad_next/vm/__init__.py @@ -18,10 +18,11 @@ from ethereum_types.bytes import Bytes, Bytes0, Bytes32 from ethereum_types.numeric import U64, U256, Uint -from ethereum.crypto.hash import Hash32 +from ethereum.crypto.hash import Hash32, keccak256 from ethereum.exceptions import EthereumException from ethereum.merkle_patricia_trie import Trie from ethereum.state import Address +from ethereum.utils.byte import left_pad_zero_bytes from ..blocks import Log, Receipt, Withdrawal from ..fork_types import Authorization, VersionedHash @@ -30,6 +31,11 @@ __all__ = ("Environment", "Evm", "Message") +TRANSFER_TOPIC = keccak256(b"Transfer(address,address,uint256)") +SYSTEM_ADDRESS = Address( + bytes.fromhex("fffffffffffffffffffffffffffffffffffffffe") +) + @final @dataclass @@ -49,6 +55,7 @@ class BlockEnvironment: prev_randao: Bytes32 excess_blob_gas: U64 parent_beacon_block_root: Hash32 + slot_number: U64 @final @@ -237,3 +244,42 @@ def incorporate_child_on_error(evm: Evm, child_evm: Evm) -> None: # NOTE: absence of `evm.memory`, in particular of its high watermark # is intended for memory to deallocate on call frame exit. + + +def emit_transfer_log( + evm: Evm, + sender: Address, + recipient: Address, + transfer_amount: U256, +) -> None: + """ + Emit a LOG3 for all ETH transfers satisfying EIP-7708. + + Parameters + ---------- + evm : + The state of the ethereum virtual machine + sender : + The account address sending the transfer + recipient : + The account address receiving the transfer + transfer_amount : + The amount of ETH transacted + + """ + if transfer_amount == 0: + return + + padded_sender = left_pad_zero_bytes(sender, 32) + padded_recipient = left_pad_zero_bytes(recipient, 32) + log_entry = Log( + address=SYSTEM_ADDRESS, + topics=( + TRANSFER_TOPIC, + Hash32(padded_sender), + Hash32(padded_recipient), + ), + data=transfer_amount.to_be_bytes32(), + ) + + evm.logs = evm.logs + (log_entry,) diff --git a/src/ethereum/forks/monad_next/vm/gas.py b/src/ethereum/forks/monad_next/vm/gas.py index 870cee592ac..461f0f79a27 100644 --- a/src/ethereum/forks/monad_next/vm/gas.py +++ b/src/ethereum/forks/monad_next/vm/gas.py @@ -189,11 +189,15 @@ class GasCosts: OPCODE_CHAINID: Final[Uint] = BASE OPCODE_BASEFEE: Final[Uint] = BASE OPCODE_BLOBBASEFEE: Final[Uint] = BASE + OPCODE_SLOTNUM: Final[Uint] = BASE OPCODE_BLOBHASH: Final[Uint] = Uint(3) OPCODE_PUSH: Final[Uint] = VERY_LOW OPCODE_PUSH0: Final[Uint] = BASE OPCODE_DUP: Final[Uint] = VERY_LOW OPCODE_SWAP: Final[Uint] = VERY_LOW + OPCODE_DUPN: Final[Uint] = VERY_LOW + OPCODE_SWAPN: Final[Uint] = VERY_LOW + OPCODE_EXCHANGE: Final[Uint] = VERY_LOW # Dynamic Opcodes OPCODE_RETURNDATACOPY_BASE: Final[Uint] = VERY_LOW diff --git a/src/ethereum/forks/monad_next/vm/instructions/__init__.py b/src/ethereum/forks/monad_next/vm/instructions/__init__.py index 0da72c8ea5c..06295ec86f1 100644 --- a/src/ethereum/forks/monad_next/vm/instructions/__init__.py +++ b/src/ethereum/forks/monad_next/vm/instructions/__init__.py @@ -99,6 +99,7 @@ class Ops(enum.Enum): BASEFEE = 0x48 BLOBHASH = 0x49 BLOBBASEFEE = 0x4A + SLOTNUM = 0x4B # Control Flow Ops STOP = 0x00 @@ -188,6 +189,11 @@ class Ops(enum.Enum): SWAP15 = 0x9E SWAP16 = 0x9F + # EIP-8024: Stack access instructions + DUPN = 0xE6 + SWAPN = 0xE7 + EXCHANGE = 0xE8 + # Memory Operations MLOAD = 0x51 MSTORE = 0x52 @@ -251,6 +257,7 @@ class Ops(enum.Enum): Ops.PREVRANDAO: block_instructions.prev_randao, Ops.GASLIMIT: block_instructions.gas_limit, Ops.CHAINID: block_instructions.chain_id, + Ops.SLOTNUM: block_instructions.slot_number, Ops.MLOAD: memory_instructions.mload, Ops.MSTORE: memory_instructions.mstore, Ops.MSTORE8: memory_instructions.mstore8, @@ -350,6 +357,9 @@ class Ops(enum.Enum): Ops.SWAP14: stack_instructions.swap14, Ops.SWAP15: stack_instructions.swap15, Ops.SWAP16: stack_instructions.swap16, + Ops.DUPN: stack_instructions.dupn, + Ops.SWAPN: stack_instructions.swapn, + Ops.EXCHANGE: stack_instructions.exchange, Ops.LOG0: log_instructions.log0, Ops.LOG1: log_instructions.log1, Ops.LOG2: log_instructions.log2, diff --git a/src/ethereum/forks/monad_next/vm/instructions/block.py b/src/ethereum/forks/monad_next/vm/instructions/block.py index baa589c4395..4f9f9e5d5c3 100644 --- a/src/ethereum/forks/monad_next/vm/instructions/block.py +++ b/src/ethereum/forks/monad_next/vm/instructions/block.py @@ -259,3 +259,36 @@ def chain_id(evm: Evm) -> None: # PROGRAM COUNTER evm.pc += Uint(1) + + +def slot_number(evm: Evm) -> None: + """ + Push the current slot number onto the stack. + + The slot number is provided by the consensus layer and passed to the + execution layer through the engine API. + + Parameters + ---------- + evm : + The current EVM frame. + + Raises + ------ + :py:class:`~ethereum.forks.monad_next.vm.exceptions.StackOverflowError` + If `len(stack)` is equal to `1024`. + :py:class:`~ethereum.forks.monad_next.vm.exceptions.OutOfGasError` + If `evm.gas_left` is less than `2`. + + """ + # STACK + pass + + # GAS + charge_gas(evm, GasCosts.OPCODE_SLOTNUM) + + # OPERATION + push(evm.stack, U256(evm.message.block_env.slot_number)) + + # PROGRAM COUNTER + evm.pc += Uint(1) diff --git a/src/ethereum/forks/monad_next/vm/instructions/stack.py b/src/ethereum/forks/monad_next/vm/instructions/stack.py index ce94af6ce8e..0e72bd01f31 100644 --- a/src/ethereum/forks/monad_next/vm/instructions/stack.py +++ b/src/ethereum/forks/monad_next/vm/instructions/stack.py @@ -14,7 +14,7 @@ from functools import partial from typing import Callable -from ethereum_types.numeric import U256, Uint +from ethereum_types.numeric import U8, U256, Uint from .. import Evm, stack from ..exceptions import StackUnderflowError @@ -23,6 +23,7 @@ charge_gas, ) from ..memory import buffer_read +from ..stack import decode_pair, decode_single def pop(evm: Evm) -> None: @@ -210,3 +211,107 @@ def swap_n(evm: Evm, item_number: int) -> None: swap14: Callable[[Evm], None] = partial(swap_n, item_number=14) swap15: Callable[[Evm], None] = partial(swap_n, item_number=15) swap16: Callable[[Evm], None] = partial(swap_n, item_number=16) + + +def dupn(evm: Evm) -> None: + """ + Duplicate the Nth stack item (from top of the stack) to the top of stack. + The item number is read from the immediate byte following the opcode and + decoded using the EIP-8024 index shifting rules. + + Parameters + ---------- + evm : + The current EVM frame. + + """ + # STACK + pass + + # GAS + charge_gas(evm, GasCosts.OPCODE_DUPN) + + # OPERATION + immediate_data = U8( + buffer_read(evm.code, U256(evm.pc + Uint(1)), U256(1))[0] + ) + item_number = decode_single(immediate_data) + if int(item_number) > len(evm.stack): + raise StackUnderflowError + data_to_duplicate = evm.stack[-item_number] + stack.push(evm.stack, data_to_duplicate) + + # PROGRAM COUNTER + evm.pc += Uint(2) + + +def swapn(evm: Evm) -> None: + """ + Swap the top stack item with the Nth stack item. + The value N is read from the immediate byte following the opcode and + decoded using the EIP-8024 index shifting rules. + + Parameters + ---------- + evm : + The current EVM frame. + + """ + # STACK + pass + + # GAS + charge_gas(evm, GasCosts.OPCODE_SWAPN) + + # OPERATION + immediate_data = U8( + buffer_read(evm.code, U256(evm.pc + Uint(1)), U256(1))[0] + ) + item_number = decode_single(immediate_data) + # SWAPN with decoded value n swaps top (position 1) with position (n+1) + if int(item_number) + 1 > len(evm.stack): + raise StackUnderflowError + # stack[-1] is top (position 1), stack[-(item_number+1)] is position (n+1) + evm.stack[-1], evm.stack[-(item_number + U8(1))] = ( + evm.stack[-(item_number + U8(1))], + evm.stack[-1], + ) + + # PROGRAM COUNTER + evm.pc += Uint(2) + + +def exchange(evm: Evm) -> None: + """ + Exchange the Nth stack item with the Mth stack item. + The values N and M are decoded from the immediate byte using the + EIP-8024 index shifting rules. + + Parameters + ---------- + evm : + The current EVM frame. + + """ + # STACK + pass + + # GAS + charge_gas(evm, GasCosts.OPCODE_EXCHANGE) + + # OPERATION + immediate_data = U8( + buffer_read(evm.code, U256(evm.pc + Uint(1)), U256(1))[0] + ) + n, m = decode_pair(immediate_data) + # EXCHANGE swaps position (n+1) with position (m+1) + depth = max(n, m) + U8(1) + if int(depth) > len(evm.stack): + raise StackUnderflowError + evm.stack[-(n + U8(1))], evm.stack[-(m + U8(1))] = ( + evm.stack[-(m + U8(1))], + evm.stack[-(n + U8(1))], + ) + + # PROGRAM COUNTER + evm.pc += Uint(2) diff --git a/src/ethereum/forks/monad_next/vm/instructions/system.py b/src/ethereum/forks/monad_next/vm/instructions/system.py index 7b8634c5d7e..3e5de77d037 100644 --- a/src/ethereum/forks/monad_next/vm/instructions/system.py +++ b/src/ethereum/forks/monad_next/vm/instructions/system.py @@ -24,7 +24,6 @@ increment_nonce, is_account_alive, move_ether, - set_account_balance, ) from ...utils.address import ( compute_contract_address, @@ -35,6 +34,7 @@ from .. import ( Evm, Message, + emit_transfer_log, incorporate_child_on_error, incorporate_child_on_success, ) @@ -584,12 +584,13 @@ def selfdestruct(evm: Evm) -> None: originator_balance, ) + # EIP-7708: Emit a transfer log for the beneficiary transfer + if beneficiary != originator: + emit_transfer_log(evm, originator, beneficiary, originator_balance) + # register account for deletion only if it was created # in the same transaction if originator in evm.message.tx_env.state.created_accounts: - # If beneficiary is the same as originator, then - # the ether is burnt. - set_account_balance(evm.message.tx_env.state, originator, U256(0)) evm.accounts_to_delete.add(originator) # HALT the execution diff --git a/src/ethereum/forks/monad_next/vm/interpreter.py b/src/ethereum/forks/monad_next/vm/interpreter.py index 13089bc8125..f72394c5745 100644 --- a/src/ethereum/forks/monad_next/vm/interpreter.py +++ b/src/ethereum/forks/monad_next/vm/interpreter.py @@ -56,7 +56,7 @@ from ..vm.gas import GasCosts, charge_gas, page_index from ..vm.precompiled_contracts import MONAD_PRECOMPILE_ADDRESSES from ..vm.precompiled_contracts.mapping import PRE_COMPILED_CONTRACTS -from . import Evm, EvmMemory +from . import Evm, EvmMemory, emit_transfer_log from .exceptions import ( AddressCollision, ExceptionalHalt, @@ -412,6 +412,10 @@ def process_message(message: Message) -> Evm: message.current_target, message.value, ) + if message.caller != message.current_target: + emit_transfer_log( + evm, message.caller, message.current_target, message.value + ) try: if evm.message.code_address in PRE_COMPILED_CONTRACTS: diff --git a/src/ethereum/forks/monad_next/vm/runtime.py b/src/ethereum/forks/monad_next/vm/runtime.py index 0aa5ddd5e20..60fd42b52c9 100644 --- a/src/ethereum/forks/monad_next/vm/runtime.py +++ b/src/ethereum/forks/monad_next/vm/runtime.py @@ -28,6 +28,8 @@ def get_valid_jump_destinations(code: Bytes) -> Set[Uint]: * The jump destination should have the `JUMPDEST` opcode (0x5B). * The jump destination shouldn't be part of the data corresponding to `PUSH-N` opcodes. + * The jump destination shouldn't be part of the immediate byte + corresponding to `DUPN`, `SWAPN`, or `EXCHANGE` opcodes (EIP-8024). Note - Jump destinations are 0-indexed. @@ -63,6 +65,30 @@ def get_valid_jump_destinations(code: Bytes) -> Set[Uint]: # opcodes. push_data_size = current_opcode.value - Ops.PUSH1.value + 1 pc += Uint(push_data_size) + elif current_opcode in (Ops.DUPN, Ops.SWAPN): + # EIP-8024: DUPN/SWAPN invalid immediate range is + # 90 < x < 128, i.e. 0x5B (91) to 0x7F (127). + # Invalid immediates are not skipped so the byte + # remains at an instruction boundary. + if ( + pc + Uint(1) < ulen(code) + and 0x5B <= code[pc + Uint(1)] <= 0x7F + ): + pass + else: + pc += Uint(1) + elif current_opcode == Ops.EXCHANGE: + # EIP-8024: EXCHANGE invalid immediate range is + # 81 < x < 128, i.e. 0x52 (82) to 0x7F (127). + # Invalid immediates are not skipped so the byte + # remains at an instruction boundary. + if ( + pc + Uint(1) < ulen(code) + and 0x52 <= code[pc + Uint(1)] <= 0x7F + ): + pass + else: + pc += Uint(1) pc += Uint(1) diff --git a/src/ethereum/forks/monad_next/vm/stack.py b/src/ethereum/forks/monad_next/vm/stack.py index a87b0a47079..98ba815cb73 100644 --- a/src/ethereum/forks/monad_next/vm/stack.py +++ b/src/ethereum/forks/monad_next/vm/stack.py @@ -11,11 +11,84 @@ Implementation of the stack operators for the EVM. """ -from typing import List +from typing import List, Tuple -from ethereum_types.numeric import U256 +from ethereum_types.numeric import U8, U256 -from .exceptions import StackOverflowError, StackUnderflowError +from .exceptions import ( + InvalidParameter, + StackOverflowError, + StackUnderflowError, +) + + +def decode_single(x: U8) -> U8: + """ + Decode the immediate byte for DUPN/SWAPN to get the stack index. + + Return n with 17 <= n <= 235. + + Parameters + ---------- + x : int + The immediate byte value (0-90 or 128-255). + + Returns + ------- + int + The stack index n, where 17 <= n <= 235. + + Raises + ------ + InvalidParameter + If x is in the forbidden range (90 < x < 128 or x > 255). + + """ + if not (U8(0) <= x <= U8(90) or U8(128) <= x <= U8(255)): + raise InvalidParameter( + f"DUPN/SWAPN immediate byte {x} is out of range. " + "Valid range: 0 <= x <= 90 or 128 <= x <= 255" + ) + + return U8((int(x) + 145) % 256) + + +def decode_pair(x: U8) -> Tuple[U8, U8]: + """ + Decode the immediate byte for EXCHANGE to get two stack indices. + + Return (n, m) with 1 <= n <= 14 and n < m <= 30 - n. + + Parameters + ---------- + x : int + The immediate byte value (0-81 or 128-255). + + Returns + ------- + Tuple[int, int] + The two stack indices (n, m), where + 1 <= n <= 14 and n < m <= 30 - n. + + Raises + ------ + InvalidParameter + If x is in the forbidden range (81 < x < 128 or x > 255). + + """ + if not (U8(0) <= x <= U8(81) or U8(128) <= x <= U8(255)): + raise InvalidParameter( + f"EXCHANGE immediate byte {x} is in the forbidden " + "range 82 <= x <= 127\n" + "Valid range: 0 <= x <= 81 or 128 <= x <= 255" + ) + + k = U8(int(x) ^ 143) + q, r = divmod(k, U8(16)) + if q < r: + return q + U8(1), r + U8(1) + else: + return r + U8(1), U8(29) - q def pop(stack: List[U256]) -> U256: diff --git a/src/ethereum_spec_tools/loaders/fork_loader.py b/src/ethereum_spec_tools/loaders/fork_loader.py index d9e02de32f9..70467ed3b88 100644 --- a/src/ethereum_spec_tools/loaders/fork_loader.py +++ b/src/ethereum_spec_tools/loaders/fork_loader.py @@ -154,6 +154,20 @@ def has_hash_block_access_list(self) -> bool: return False return hasattr(module, "hash_block_access_list") + @property + def has_block_access_list_hash_header(self) -> bool: + """ + Check if the fork's header has a `block_access_list_hash` field. + + A fork can carry the header field without building block access + lists (e.g. Monad, where the field is always zero). + """ + try: + header = self._module("blocks").Header + return "block_access_list_hash" in header.__dataclass_fields__ + except (ModuleNotFoundError, AttributeError): + return False + @property def BlockAccessIndex(self) -> Any: """BlockAccessIndex type of the fork.""" diff --git a/tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/conftest.py b/tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/conftest.py new file mode 100644 index 00000000000..de52075d396 --- /dev/null +++ b/tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/conftest.py @@ -0,0 +1,10 @@ +"""Pytest (plugin) definitions local to EIP-2780 tests.""" + +import pytest + + +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: + """Mark all tests in this subdir as not valid for Monad forks.""" + metafunc.definition.add_marker( + pytest.mark.not_valid_for("MONAD_EIGHT", subsequent_forks=True) + ) diff --git a/tests/amsterdam/eip7778_block_gas_accounting_without_refunds/conftest.py b/tests/amsterdam/eip7778_block_gas_accounting_without_refunds/conftest.py new file mode 100644 index 00000000000..5cbdebdc9ef --- /dev/null +++ b/tests/amsterdam/eip7778_block_gas_accounting_without_refunds/conftest.py @@ -0,0 +1,10 @@ +"""Pytest (plugin) definitions local to EIP-7778 tests.""" + +import pytest + + +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: + """Mark all tests in this subdir as not valid for Monad forks.""" + metafunc.definition.add_marker( + pytest.mark.not_valid_for("MONAD_EIGHT", subsequent_forks=True) + ) diff --git a/tests/amsterdam/eip7928_block_level_access_lists/conftest.py b/tests/amsterdam/eip7928_block_level_access_lists/conftest.py new file mode 100644 index 00000000000..3bedcfe2651 --- /dev/null +++ b/tests/amsterdam/eip7928_block_level_access_lists/conftest.py @@ -0,0 +1,10 @@ +"""Pytest (plugin) definitions local to EIP-7928 tests.""" + +import pytest + + +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: + """Mark all tests in this subdir as not valid for Monad forks.""" + metafunc.definition.add_marker( + pytest.mark.not_valid_for("MONAD_EIGHT", subsequent_forks=True) + ) diff --git a/tests/amsterdam/eip7976_increase_calldata_floor_cost/conftest.py b/tests/amsterdam/eip7976_increase_calldata_floor_cost/conftest.py index f92a0e003a0..9a252257297 100644 --- a/tests/amsterdam/eip7976_increase_calldata_floor_cost/conftest.py +++ b/tests/amsterdam/eip7976_increase_calldata_floor_cost/conftest.py @@ -370,3 +370,10 @@ def tx( blob_versioned_hashes=blob_versioned_hashes, error=tx_error, ) + + +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: + """Mark all tests in this subdir as not valid for Monad forks.""" + metafunc.definition.add_marker( + pytest.mark.not_valid_for("MONAD_EIGHT", subsequent_forks=True) + ) diff --git a/tests/amsterdam/eip7981_increase_access_list_cost/test_access_list_cost.py b/tests/amsterdam/eip7981_increase_access_list_cost/test_access_list_cost.py index 2d093e6d0bb..981617c0ec0 100644 --- a/tests/amsterdam/eip7981_increase_access_list_cost/test_access_list_cost.py +++ b/tests/amsterdam/eip7981_increase_access_list_cost/test_access_list_cost.py @@ -124,10 +124,11 @@ def test_access_list_token_calculation( expected_floor_cost = ( expected_floor_tokens * gas_costs.TX_DATA_TOKEN_FLOOR + gas_costs.TX_BASE + ) + if fork.is_eip_enabled(2780): # EIP-2780 anchors the floor on the decomposed intrinsic base; the # tx targets a non-self account, adding the recipient-access charge. - + gas_costs.COLD_ACCOUNT_ACCESS - ) + expected_floor_cost += gas_costs.COLD_ACCOUNT_ACCESS actual_floor_cost = fork.transaction_data_floor_cost_calculator()( data=b"", access_list=access_list ) diff --git a/tests/amsterdam/eip7981_increase_access_list_cost/test_fork_transition.py b/tests/amsterdam/eip7981_increase_access_list_cost/test_fork_transition.py index 62a899b346a..92b02d7def7 100644 --- a/tests/amsterdam/eip7981_increase_access_list_cost/test_fork_transition.py +++ b/tests/amsterdam/eip7981_increase_access_list_cost/test_fork_transition.py @@ -7,12 +7,13 @@ transition timestamp) and pin the per-transaction gas paid on each side, plus the validity flip for gas limits inside the uplift gap. -The post-fork intrinsic composes three repricings; the hand-derived +The post-fork intrinsic composes up to three repricings; the hand-derived expectations below keep each term explicit so the EIP-7981 surcharge is individually visible: -- EIP-2780 decomposes the flat pre-fork `TX_BASE` into the lowered base - plus the `COLD_ACCOUNT_ACCESS` recipient charge. +- EIP-2780, on the forks that enable it, decomposes the flat pre-fork + `TX_BASE` into the lowered base plus the `COLD_ACCOUNT_ACCESS` + recipient charge. - EIP-8038 reprices the per-address and per-storage-key access list charges to the fork's cold access costs. - EIP-7981 adds four floor tokens per access list byte, charged at @@ -116,11 +117,12 @@ def test_access_list_intrinsic_across_amsterdam_transition( ) expected_post = ( post_costs.TX_BASE - + post_costs.COLD_ACCOUNT_ACCESS + addresses * post_costs.TX_ACCESS_LIST_ADDRESS + total_keys * post_costs.TX_ACCESS_LIST_STORAGE_KEY + surcharge ) + if post_fork.is_eip_enabled(2780): + expected_post += post_costs.COLD_ACCOUNT_ACCESS timestamps = [PRE_FORK_TIMESTAMP, POST_FORK_TIMESTAMP] expected_intrinsics = [expected_pre, expected_post] @@ -322,10 +324,10 @@ def test_access_list_floor_across_amsterdam_transition( post_costs.TX_DATA_TOKEN_STANDARD ) + calculate_access_list_floor_tokens(access_list) expected_post = int( - post_costs.TX_BASE - + post_costs.COLD_ACCOUNT_ACCESS - + post_tokens * post_costs.TX_DATA_TOKEN_FLOOR + post_costs.TX_BASE + post_tokens * post_costs.TX_DATA_TOKEN_FLOOR ) + if post_fork.is_eip_enabled(2780): + expected_post += int(post_costs.COLD_ACCOUNT_ACCESS) timestamps = [PRE_FORK_TIMESTAMP, POST_FORK_TIMESTAMP] expected_floors = [expected_pre, expected_post] diff --git a/tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py b/tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py index bd4f9c3031d..9bb208616c2 100644 --- a/tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py +++ b/tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py @@ -30,6 +30,7 @@ compute_create2_address, keccak256, ) +from execution_testing.forks import MONAD_EIGHT from ...prague.eip7702_set_code_tx.spec import Spec as Spec7702 from .spec import Spec, ref_spec_7997 @@ -538,6 +539,7 @@ def test_factory_receives_balance_via_selfdestruct( def test_factory_via_eip7702_delegation( state_test: StateTestFiller, pre: Alloc, + fork: Fork, ) -> None: """ An EOA delegates its code to the factory via an EIP-7702 @@ -549,6 +551,10 @@ def test_factory_via_eip7702_delegation( auth_signer = pre.fund_eoa() auth_signer_nonce = auth_signer.nonce + # Monad forbids the `CREATE2` of a delegated account's context, so + # the factory reverts there instead of deploying. + factory_reverts = fork >= MONAD_EIGHT + salt = 0x42 runtime_code = Op.PUSH1(0x01) + Op.PUSH1(0x00) + Op.RETURN initcode = Initcode(deploy_code=runtime_code) @@ -586,10 +592,12 @@ def test_factory_via_eip7702_delegation( ), post={ auth_signer: Account( - nonce=auth_signer_nonce + 2, + nonce=auth_signer_nonce + (1 if factory_reverts else 2), code=Spec7702.delegation_designation(Address(FACTORY)), ), - expected_address: Account(nonce=1, code=bytes(runtime_code)), + expected_address: Account.NONEXISTENT + if factory_reverts + else Account(nonce=1, code=bytes(runtime_code)), FACTORY: Account( nonce=1, balance=0, diff --git a/tests/amsterdam/eip7997_deterministic_factory_predeploy/test_fork_transition.py b/tests/amsterdam/eip7997_deterministic_factory_predeploy/test_fork_transition.py index 8f3d20b983b..2a92c2de067 100644 --- a/tests/amsterdam/eip7997_deterministic_factory_predeploy/test_fork_transition.py +++ b/tests/amsterdam/eip7997_deterministic_factory_predeploy/test_fork_transition.py @@ -32,7 +32,7 @@ FORK_TIMESTAMP = 15_000 -@pytest.mark.valid_at_transition_to("Amsterdam") +@pytest.mark.valid_at_transition_to("EIP7997") @pytest.mark.pre_alloc_mutable @pytest.mark.parametrize("pre_fork_nonce", [1, 2, 32]) def test_factory_deploys_across_transition( @@ -106,7 +106,7 @@ def test_factory_deploys_across_transition( ) -@pytest.mark.valid_at_transition_to("Amsterdam") +@pytest.mark.valid_at_transition_to("EIP7997") @pytest.mark.pre_alloc_mutable def test_factory_absent_across_transition( blockchain_test: BlockchainTestFiller, diff --git a/tests/amsterdam/eip8037_state_creation_gas_cost_increase/conftest.py b/tests/amsterdam/eip8037_state_creation_gas_cost_increase/conftest.py new file mode 100644 index 00000000000..2d16c275db4 --- /dev/null +++ b/tests/amsterdam/eip8037_state_creation_gas_cost_increase/conftest.py @@ -0,0 +1,10 @@ +"""Pytest (plugin) definitions local to EIP-8037 tests.""" + +import pytest + + +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: + """Mark all tests in this subdir as not valid for Monad forks.""" + metafunc.definition.add_marker( + pytest.mark.not_valid_for("MONAD_EIGHT", subsequent_forks=True) + ) diff --git a/tests/amsterdam/eip8038_state_access_gas_cost_increase/conftest.py b/tests/amsterdam/eip8038_state_access_gas_cost_increase/conftest.py new file mode 100644 index 00000000000..cd29eb50dcb --- /dev/null +++ b/tests/amsterdam/eip8038_state_access_gas_cost_increase/conftest.py @@ -0,0 +1,10 @@ +"""Pytest (plugin) definitions local to EIP-8038 tests.""" + +import pytest + + +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: + """Mark all tests in this subdir as not valid for Monad forks.""" + metafunc.definition.add_marker( + pytest.mark.not_valid_for("MONAD_EIGHT", subsequent_forks=True) + ) diff --git a/tests/amsterdam/eip8070_sparse_blobpool/conftest.py b/tests/amsterdam/eip8070_sparse_blobpool/conftest.py index cb2a757494c..dc3ee7c3ecd 100644 --- a/tests/amsterdam/eip8070_sparse_blobpool/conftest.py +++ b/tests/amsterdam/eip8070_sparse_blobpool/conftest.py @@ -148,3 +148,10 @@ def txs( ) txs.append(network_wrapped_tx) return txs + + +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: + """Mark all tests in this subdir as not valid for Monad forks.""" + metafunc.definition.add_marker( + pytest.mark.not_valid_for("MONAD_EIGHT", subsequent_forks=True) + ) diff --git a/tests/amsterdam/eip8282_builder_execution_requests/conftest.py b/tests/amsterdam/eip8282_builder_execution_requests/conftest.py index e17cee467fc..7a42ba76785 100644 --- a/tests/amsterdam/eip8282_builder_execution_requests/conftest.py +++ b/tests/amsterdam/eip8282_builder_execution_requests/conftest.py @@ -1,8 +1,17 @@ """Fixtures for the EIP-8282 builder execution request tests.""" +import pytest + from ...common.system_contract_request_fixtures import ( blocks, # noqa: F401 included_requests, # noqa: F401 system_contract_interactions_per_block_copy, # noqa: F401 timestamp, # noqa: F401 ) + + +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: + """Mark all tests in this subdir as not valid for Monad forks.""" + metafunc.definition.add_marker( + pytest.mark.not_valid_for("MONAD_EIGHT", subsequent_forks=True) + ) diff --git a/tests/monad_amsterdam/__init__.py b/tests/monad_amsterdam/__init__.py new file mode 100644 index 00000000000..b37e6faa138 --- /dev/null +++ b/tests/monad_amsterdam/__init__.py @@ -0,0 +1 @@ +"""Tests for the Amsterdam EIPs MONAD_NEXT adopts.""" diff --git a/tests/monad_amsterdam/eip7997_delegated_create/__init__.py b/tests/monad_amsterdam/eip7997_delegated_create/__init__.py new file mode 100644 index 00000000000..4cd6b5b4cb1 --- /dev/null +++ b/tests/monad_amsterdam/eip7997_delegated_create/__init__.py @@ -0,0 +1 @@ +"""Tests for the EIP-7997 factory against Monad's CREATE ban in 7702 frames.""" diff --git a/tests/monad_amsterdam/eip7997_delegated_create/test_factory_reach.py b/tests/monad_amsterdam/eip7997_delegated_create/test_factory_reach.py new file mode 100644 index 00000000000..f0c3382e86c --- /dev/null +++ b/tests/monad_amsterdam/eip7997_delegated_create/test_factory_reach.py @@ -0,0 +1,122 @@ +""" +Tests for reaching the EIP-7997 factory from a delegated frame. + +Monad forbids the create opcodes inside a frame executing an EOA's +delegated code. The ban is callee-scoped: `access_delegation` sets it on +the frame it builds, and a child frame the delegated code calls does not +inherit it. EIP-7997 makes a CREATE2 factory reachable at a fixed +address on every chain, so these tests pin both halves of that +boundary. +""" + +import pytest +from execution_testing import ( + Account, + Alloc, + Block, + BlockchainTestFiller, + Hash, + Op, + Transaction, +) +from execution_testing.test_types.helpers import compute_create2_address +from execution_testing.tools.tools_code.generators import Initcode + +from ...amsterdam.eip7997_deterministic_factory_predeploy.spec import ( + Spec, + ref_spec_7997, +) + +REFERENCE_SPEC_GIT_PATH = ref_spec_7997.git_path +REFERENCE_SPEC_VERSION = ref_spec_7997.version + +SALT = 0x42 + +slot_code_worked = 0x1 +value_code_worked = 0x1234 +slot_call_result = 0x2 + +pytestmark = [ + pytest.mark.valid_from("MONAD_NEXT"), + pytest.mark.pre_alloc_group( + "eip7997_delegated_create_tests", + reason="Tests the EIP-7997 factory reached from a delegated frame", + ), +] + + +@pytest.mark.parametrize("create_op", [Op.CREATE, Op.CREATE2]) +def test_create_halts_in_delegated_frame( + blockchain_test: BlockchainTestFiller, + pre: Alloc, + create_op: Op, +) -> None: + """ + A create opcode executed as an EOA's delegated code halts the frame. + + The halt is the only coverage of `CreateIn7702Context`; nothing is + deployed and the storage write that follows never lands. + """ + # The create halts before its initcode is read, so the empty + # memory the size refers to is immaterial. + delegate = pre.deploy_contract( + create_op(value=0, size=1) + + Op.SSTORE(slot_code_worked, value_code_worked) + ) + sender = pre.fund_eoa() + delegated = pre.fund_eoa(delegation=delegate) + + tx = Transaction( + to=delegated, + sender=sender, + ) + + blockchain_test( + pre=pre, + post={delegated: Account(storage={})}, + blocks=[Block(txs=[tx])], + ) + + +def test_factory_deploys_from_delegated_frame( + blockchain_test: BlockchainTestFiller, + pre: Alloc, +) -> None: + """ + A delegated frame reaches CREATE2 through the EIP-7997 factory. + + The ban applies to the frame running the delegated code, not to the + factory frame it calls, so the deployment succeeds. + """ + initcode = Initcode(deploy_code=Op.STOP) + deployed = compute_create2_address(Spec.FACTORY_ADDRESS, SALT, initcode) + + delegate = pre.deploy_contract( + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE) + + Op.SSTORE( + slot_call_result, + Op.CALL( + gas=Op.GAS, + address=Spec.FACTORY_ADDRESS, + args_offset=0, + args_size=Op.CALLDATASIZE, + ), + ) + ) + sender = pre.fund_eoa() + delegated = pre.fund_eoa(delegation=delegate) + + tx = Transaction( + to=delegated, + data=Hash(SALT) + bytes(initcode), + sender=sender, + ) + + blockchain_test( + pre=pre, + post={ + delegated: Account(storage={slot_call_result: 1}), + deployed: Account(code=Op.STOP), + }, + blocks=[Block(txs=[tx])], + ) diff --git a/tests/monad_eight/reserve_balance/test_transfers.py b/tests/monad_eight/reserve_balance/test_transfers.py index 5a06d5b19ce..6fabc7b20e1 100644 --- a/tests/monad_eight/reserve_balance/test_transfers.py +++ b/tests/monad_eight/reserve_balance/test_transfers.py @@ -572,6 +572,7 @@ def test_sc_wallet_send_value_with_selfdestruct( @pytest.mark.parametrize("pre_delegated", [True, False]) @pytest.mark.parametrize("delegate", [True, False]) @pytest.mark.parametrize("undelegate", [True, False]) +@pytest.mark.parametrize("selfdestruct_to_self", [True, False]) def test_sc_wallet_selfdestruct( blockchain_test: BlockchainTestFiller, pre: Alloc, @@ -581,16 +582,22 @@ def test_sc_wallet_selfdestruct( pre_delegated: bool, delegate: bool, undelegate: bool, + selfdestruct_to_self: bool, fork: Fork, ) -> None: """ Test reserve balance violations for a delegated EOA whose wallet code SELFDESTRUCTs on behalf of the EOA. - NOTE: this is different from test_sc_wallet_send_value_with_selfdestruct - in that SELFDESTRUCT is not used to send value. + Naming the EOA itself moves no value, so the reserve balance is only + at stake when the beneficiary is another account. """ - wallet_address = pre.deploy_contract(code=Op.SELFDESTRUCT(Op.ADDRESS)) + selfdestruct_target = Address(0x5656) + wallet_address = pre.deploy_contract( + code=Op.SELFDESTRUCT( + Op.ADDRESS if selfdestruct_to_self else selfdestruct_target + ) + ) if pre_delegated: sender = pre.fund_eoa(balance, delegation=wallet_address) @@ -629,7 +636,14 @@ def test_sc_wallet_selfdestruct( ) any_delegation = pre_delegated or delegate or undelegate - reverted = violation and any_delegation + # The authorizations apply before execution, so an undelegation + # leaves nothing for the call to run. + wallet_runs = (pre_delegated or delegate) and not undelegate + # Sweeping the sender to another account empties a delegated EOA, + # which no emptying transaction exception covers. + reverted = (violation and any_delegation) or ( + wallet_runs and not selfdestruct_to_self + ) storage = {} if reverted else {slot_code_worked: value_code_worked} blockchain_test( @@ -637,7 +651,8 @@ def test_sc_wallet_selfdestruct( post={ contract_address: Account( storage=storage, balance=value if not reverted else 0 - ) + ), + selfdestruct_target: None, }, blocks=[Block(txs=[tx_1])], ) @@ -1258,8 +1273,13 @@ def test_access_lists( @pytest.mark.parametrize("pre_delegated", [True, False]) @pytest.mark.parametrize("new_address_pre_funded", [True, False]) @pytest.mark.parametrize( - "selfdestruct,deploy_code", - [(True, None), (False, Bytecode()), (False, Op.STOP)], + "selfdestruct,selfdestruct_to_self,deploy_code", + [ + (True, False, None), + (True, True, None), + (False, False, Bytecode()), + (False, False, Op.STOP), + ], ) @pytest.mark.with_all_contract_creating_tx_types def test_creation_tx( @@ -1271,6 +1291,7 @@ def test_creation_tx( pre_delegated: bool, new_address_pre_funded: bool, selfdestruct: bool, + selfdestruct_to_self: bool, deploy_code: Bytecode | None, tx_type: int, fork: Fork, @@ -1287,7 +1308,9 @@ def test_creation_tx( selfdestruct_target = Address(0x5656) initcode = ( - Op.SELFDESTRUCT(address=selfdestruct_target) + Op.SELFDESTRUCT( + address=Op.ADDRESS if selfdestruct_to_self else selfdestruct_target + ) if selfdestruct else Initcode(deploy_code=deploy_code) ) @@ -1316,6 +1339,25 @@ def test_creation_tx( fork == MONAD_EIGHT and selfdestruct and new_address_pre_funded ) + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps the balance it retained. A zero balance still + # leaves the account empty, and so pruned. + selfdestructed_account = ( + Account( + nonce=0, + balance=value + pre_fund_value, + code=b"", + storage={}, + ) + if selfdestruct_to_self + and fork.is_eip_enabled(8246) + and not reverted + and value + pre_fund_value != 0 + else None + ) + blockchain_test( pre=pre, post={ @@ -1326,9 +1368,12 @@ def test_creation_tx( if not reverted and not selfdestruct else Account(balance=pre_fund_value) if new_address_pre_funded and reverted - else None, + else selfdestructed_account, selfdestruct_target: Account(balance=value + pre_fund_value) - if selfdestruct and not reverted and value + pre_fund_value != 0 + if selfdestruct + and not selfdestruct_to_self + and not reverted + and value + pre_fund_value != 0 else None, }, blocks=[Block(txs=txs)], @@ -1405,8 +1450,13 @@ def test_contract_unrestricted( @pytest.mark.parametrize("pre_delegated", [True, False]) @pytest.mark.parametrize("pre_funded", [True, False]) @pytest.mark.parametrize( - "selfdestruct,deploy_code", - [(True, None), (False, Bytecode()), (False, Op.STOP)], + "selfdestruct,selfdestruct_to_self,deploy_code", + [ + (True, False, None), + (True, True, None), + (False, False, Bytecode()), + (False, False, Op.STOP), + ], ) @pytest.mark.with_all_create_opcodes def test_contract_unrestricted_with_create( @@ -1417,6 +1467,7 @@ def test_contract_unrestricted_with_create( pre_delegated: bool, pre_funded: bool, selfdestruct: bool, + selfdestruct_to_self: bool, deploy_code: Bytecode | None, create_opcode: Op, fork: Fork, @@ -1436,7 +1487,9 @@ def test_contract_unrestricted_with_create( selfdestruct_target = Address(0x5656) initcode = ( - Op.SELFDESTRUCT(address=selfdestruct_target) + Op.SELFDESTRUCT( + address=Op.ADDRESS if selfdestruct_to_self else selfdestruct_target + ) if selfdestruct else Initcode(deploy_code=deploy_code) ) @@ -1467,15 +1520,26 @@ def test_contract_unrestricted_with_create( ) storage = {slot_code_worked: value_code_worked} + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps the balance it retained. A zero balance still + # leaves the account empty, and so pruned. + selfdestructed_account = ( + Account(nonce=0, balance=value, code=b"", storage={}) + if selfdestruct_to_self and fork.is_eip_enabled(8246) and value != 0 + else None + ) + blockchain_test( pre=pre, post={ factory_address: Account(storage=storage, balance=balance - value), new_contract_address: Account(balance=value, code=deploy_code) if not selfdestruct - else None, + else selfdestructed_account, selfdestruct_target: Account(balance=value) - if selfdestruct and value != 0 + if selfdestruct and value != 0 and not selfdestruct_to_self else None, }, blocks=[Block(txs=[tx_1])], @@ -1486,8 +1550,19 @@ def test_contract_unrestricted_with_create( @pytest.mark.parametrize("create_balance", [0, Spec.RESERVE_BALANCE // 2]) @pytest.mark.parametrize("call_balance", [0, Spec.RESERVE_BALANCE // 2]) @pytest.mark.parametrize("pull_balance", [0, Spec.RESERVE_BALANCE // 2]) -@pytest.mark.parametrize("same_tx", [True, False]) -@pytest.mark.parametrize("through_delegation", [True, False]) +@pytest.mark.parametrize( + "same_tx,through_delegation,selfdestruct_to_self", + [ + # Only a same-transaction creation reaches EIP-8246's branch, and + # a delegated frame destructs the delegating account rather than + # the created one, so the self target varies in one case alone. + (True, False, False), + (True, False, True), + (True, True, False), + (False, False, False), + (False, True, False), + ], +) @pytest.mark.with_all_create_opcodes def test_contract_unrestricted_with_selfdestruct( blockchain_test: BlockchainTestFiller, @@ -1506,6 +1581,8 @@ def test_contract_unrestricted_with_selfdestruct( # Whether the SELFDESTRUCT should be called on behalf of # a delegating account through_delegation: bool, + # Whether SELFDESTRUCT names the destructing account itself + selfdestruct_to_self: bool, create_opcode: Op, fork: Fork, ) -> None: @@ -1532,7 +1609,7 @@ def test_contract_unrestricted_with_selfdestruct( Op.SELFDESTRUCT(address=Op.CALLER), balance=pull_balance ) deploy_code = Op.CALL(address=pull_funder_address) + Op.SELFDESTRUCT( - address=selfdestruct_target + address=Op.ADDRESS if selfdestruct_to_self else selfdestruct_target ) initcode = Initcode(deploy_code=deploy_code) @@ -1620,6 +1697,16 @@ def test_contract_unrestricted_with_selfdestruct( storage = {slot_code_worked: value_code_worked} reverted = through_delegation and value > 0 and prefund_balance > 0 + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps the balance it retained. + selfdestructed_account = ( + Account(nonce=0, balance=value, code=b"", storage={}) + if selfdestruct_to_self and fork.is_eip_enabled(8246) and value != 0 + else None + ) + blockchain_test( pre=pre, post={ @@ -1633,7 +1720,7 @@ def test_contract_unrestricted_with_selfdestruct( code=deploy_code, ) if not same_tx or through_delegation - else None, + else selfdestructed_account, # Delegated account is deleted if there is no delegation delegated_address: Account( balance=0, @@ -1643,7 +1730,7 @@ def test_contract_unrestricted_with_selfdestruct( else None, # SELFDESTRUCT target is deleted if source was empty selfdestruct_target: Account(balance=value) - if value != 0 + if value != 0 and not selfdestruct_to_self else None, } if not reverted @@ -1677,8 +1764,13 @@ def test_contract_unrestricted_with_selfdestruct( @pytest.mark.with_all_create_opcodes @pytest.mark.parametrize("new_address_pre_funded", [True, False]) @pytest.mark.parametrize( - "selfdestruct,deploy_code", - [(True, None), (False, Bytecode()), (False, Op.STOP)], + "selfdestruct,selfdestruct_to_self,deploy_code", + [ + (True, False, None), + (True, True, None), + (False, False, Bytecode()), + (False, False, Op.STOP), + ], ) def test_contract_unrestricted_within_initcode( blockchain_test: BlockchainTestFiller, @@ -1689,6 +1781,7 @@ def test_contract_unrestricted_within_initcode( create_opcode: Op, new_address_pre_funded: bool, selfdestruct: bool, + selfdestruct_to_self: bool, deploy_code: Bytecode | None, fork: Fork, ) -> None: @@ -1710,7 +1803,11 @@ def test_contract_unrestricted_within_initcode( initcode = ( ( Op.CALL(value=value, address=target) - + Op.SELFDESTRUCT(address=selfdestruct_target) + + Op.SELFDESTRUCT( + address=Op.ADDRESS + if selfdestruct_to_self + else selfdestruct_target + ) ) if selfdestruct else Initcode( @@ -1756,6 +1853,16 @@ def test_contract_unrestricted_within_initcode( ) storage = {} if reverted else {slot_code_worked: value_code_worked} + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps the balance it retained. + selfdestructed_account = ( + Account(nonce=0, balance=balance - value, code=b"", storage={}) + if selfdestruct_to_self and fork.is_eip_enabled(8246) and not reverted + else None + ) + txs = [tx_1] if new_address_pre_funded: txs.insert( @@ -1773,12 +1880,12 @@ def test_contract_unrestricted_within_initcode( if reverted and new_address_pre_funded else Account(balance=balance - value, code=deploy_code) if not selfdestruct - else None, + else selfdestructed_account, target: Account(balance=value) if value != 0 and not reverted else None, selfdestruct_target: Account(balance=balance - value) - if selfdestruct and not reverted + if selfdestruct and not reverted and not selfdestruct_to_self else None, }, blocks=[Block(txs=txs)], @@ -1796,8 +1903,13 @@ def test_contract_unrestricted_within_initcode( @pytest.mark.parametrize("pre_delegated", [True, False]) @pytest.mark.parametrize("new_address_pre_funded", [True, False]) @pytest.mark.parametrize( - "selfdestruct,deploy_code", - [(True, None), (False, Bytecode()), (False, Op.STOP)], + "selfdestruct,selfdestruct_to_self,deploy_code", + [ + (True, False, None), + (True, True, None), + (False, False, Bytecode()), + (False, False, Op.STOP), + ], ) @pytest.mark.with_all_contract_creating_tx_types def test_unrestricted_in_creation_tx_initcode( @@ -1808,6 +1920,7 @@ def test_unrestricted_in_creation_tx_initcode( pre_delegated: bool, new_address_pre_funded: bool, selfdestruct: bool, + selfdestruct_to_self: bool, deploy_code: Bytecode | None, tx_type: int, fork: Fork, @@ -1830,7 +1943,11 @@ def test_unrestricted_in_creation_tx_initcode( initcode = ( ( Op.CALL(value=value, address=target) - + Op.SELFDESTRUCT(address=selfdestruct_target) + + Op.SELFDESTRUCT( + address=Op.ADDRESS + if selfdestruct_to_self + else selfdestruct_target + ) ) if selfdestruct else Initcode( @@ -1865,6 +1982,16 @@ def test_unrestricted_in_creation_tx_initcode( and balance - value < Spec.RESERVE_BALANCE ) + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps the balance it retained. + selfdestructed_account = ( + Account(nonce=0, balance=balance - value, code=b"", storage={}) + if selfdestruct_to_self and fork.is_eip_enabled(8246) and not reverted + else None + ) + blockchain_test( pre=pre, post={ @@ -1872,12 +1999,12 @@ def test_unrestricted_in_creation_tx_initcode( if reverted and new_address_pre_funded else Account(code=deploy_code, balance=balance - value) if not selfdestruct - else None, + else selfdestructed_account, target: Account(balance=value) if value != 0 and not reverted else None, selfdestruct_target: Account(balance=balance - value) - if selfdestruct and not reverted + if selfdestruct and not reverted and not selfdestruct_to_self else None, }, blocks=[Block(txs=txs)], diff --git a/tests/monad_nine/mip4_checkreservebalance/test_transfers.py b/tests/monad_nine/mip4_checkreservebalance/test_transfers.py index ec43d75dd91..ec4d6cdf523 100644 --- a/tests/monad_nine/mip4_checkreservebalance/test_transfers.py +++ b/tests/monad_nine/mip4_checkreservebalance/test_transfers.py @@ -417,6 +417,7 @@ def test_sc_wallet_send_value( @pytest.mark.parametrize("pre_delegated", [True, False]) @pytest.mark.parametrize("delegate", [True, False]) @pytest.mark.parametrize("undelegate", [True, False]) +@pytest.mark.parametrize("selfdestruct_to_self", [True, False]) def test_sc_wallet_selfdestruct( blockchain_test: BlockchainTestFiller, pre: Alloc, @@ -427,14 +428,23 @@ def test_sc_wallet_selfdestruct( pre_delegated: bool, delegate: bool, undelegate: bool, + selfdestruct_to_self: bool, fork: Fork, ) -> None: """ Test dippedIntoReserve() for a delegated EOA whose wallet code SELFDESTRUCTs on behalf of the EOA. + + Naming the EOA itself moves no value, so the reserve balance is only + at stake when the beneficiary is another account. """ refill_call = refill_factory() - wallet_address = pre.deploy_contract(code=Op.SELFDESTRUCT(Op.ADDRESS)) + selfdestruct_target = Address(0x5656) + wallet_address = pre.deploy_contract( + code=Op.SELFDESTRUCT( + Op.ADDRESS if selfdestruct_to_self else selfdestruct_target + ) + ) if pre_delegated: sender = pre.fund_eoa(balance, delegation=wallet_address) @@ -475,7 +485,17 @@ def test_sc_wallet_selfdestruct( ) any_delegation = pre_delegated or delegate or undelegate - expected_violation = 1 if (violation and any_delegation) else 0 + # The authorizations apply before execution, so an undelegation + # leaves nothing for the call to run. + wallet_runs = (pre_delegated or delegate) and not undelegate + # Sweeping the sender to another account empties a delegated EOA, + # which the probe reports whatever the transaction's own value does. + expected_violation = ( + 1 + if (violation and any_delegation) + or (wallet_runs and not selfdestruct_to_self) + else 0 + ) storage = { slot_violation_result: expected_violation, @@ -484,7 +504,14 @@ def test_sc_wallet_selfdestruct( blockchain_test( pre=pre, - post={contract_address: Account(storage=storage, balance=value)}, + post={ + contract_address: Account(storage=storage, balance=value), + # What the sweep sends depends on the gas billed, so only + # its presence is pinned. + selfdestruct_target: Account() + if wallet_runs and not selfdestruct_to_self + else None, + }, blocks=[Block(txs=[tx_1])], ) @@ -1078,7 +1105,10 @@ def test_contract_unrestricted( ) @pytest.mark.parametrize("pre_delegated", [True, False]) @pytest.mark.parametrize("pre_funded", [True, False]) -@pytest.mark.parametrize("selfdestruct", [True, False]) +@pytest.mark.parametrize( + "selfdestruct,selfdestruct_to_self", + [(True, False), (True, True), (False, False)], +) @pytest.mark.with_all_create_opcodes def test_contract_unrestricted_with_create( blockchain_test: BlockchainTestFiller, @@ -1088,6 +1118,7 @@ def test_contract_unrestricted_with_create( pre_delegated: bool, pre_funded: bool, selfdestruct: bool, + selfdestruct_to_self: bool, create_opcode: Op, fork: Fork, ) -> None: @@ -1105,7 +1136,9 @@ def test_contract_unrestricted_with_create( selfdestruct_target = Address(0x5656) initcode = ( - Op.SELFDESTRUCT(address=selfdestruct_target) + Op.SELFDESTRUCT( + address=Op.ADDRESS if selfdestruct_to_self else selfdestruct_target + ) if selfdestruct else Initcode(deploy_code=Op.STOP) ) @@ -1136,15 +1169,26 @@ def test_contract_unrestricted_with_create( ) storage = {slot_code_worked: value_code_worked, slot_violation_result: 0} + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps the balance it retained. A zero balance still + # leaves the account empty, and so pruned. + selfdestructed_account = ( + Account(nonce=0, balance=value, code=b"", storage={}) + if selfdestruct_to_self and fork.is_eip_enabled(8246) and value != 0 + else None + ) + blockchain_test( pre=pre, post={ factory_address: Account(storage=storage, balance=balance - value), new_contract_address: Account(balance=value, code=Op.STOP) if not selfdestruct - else None, + else selfdestructed_account, selfdestruct_target: Account(balance=value) - if selfdestruct and value != 0 + if selfdestruct and value != 0 and not selfdestruct_to_self else None, }, blocks=[Block(txs=[tx_1])], @@ -1155,8 +1199,19 @@ def test_contract_unrestricted_with_create( @pytest.mark.parametrize("create_balance", [0, Spec.RESERVE_BALANCE // 2]) @pytest.mark.parametrize("call_balance", [0, Spec.RESERVE_BALANCE // 2]) @pytest.mark.parametrize("pull_balance", [0, Spec.RESERVE_BALANCE // 2]) -@pytest.mark.parametrize("same_tx", [True, False]) -@pytest.mark.parametrize("through_delegation", [True, False]) +@pytest.mark.parametrize( + "same_tx,through_delegation,selfdestruct_to_self", + [ + # Only a same-transaction creation reaches EIP-8246's branch, and + # a delegated frame destructs the delegating account rather than + # the created one, so the self target varies in one case alone. + (True, False, False), + (True, False, True), + (True, True, False), + (False, False, False), + (False, True, False), + ], +) @pytest.mark.with_all_create_opcodes def test_contract_unrestricted_with_selfdestruct( blockchain_test: BlockchainTestFiller, @@ -1176,6 +1231,8 @@ def test_contract_unrestricted_with_selfdestruct( # Whether the SELFDESTRUCT should be called on behalf of # a delegating account through_delegation: bool, + # Whether SELFDESTRUCT names the destructing account itself + selfdestruct_to_self: bool, create_opcode: Op, fork: Fork, ) -> None: @@ -1205,7 +1262,7 @@ def test_contract_unrestricted_with_selfdestruct( ) deploy_code = Op.CALL(address=pull_funder_address) + Op.SELFDESTRUCT( - address=selfdestruct_target + address=Op.ADDRESS if selfdestruct_to_self else selfdestruct_target ) initcode = Initcode(deploy_code=deploy_code) @@ -1309,6 +1366,16 @@ def test_contract_unrestricted_with_selfdestruct( if same_tx: factory_storage[slot_violation_result] = expected_violation + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps the balance it retained. + selfdestructed_account = ( + Account(nonce=0, balance=value, code=b"", storage={}) + if selfdestruct_to_self and fork.is_eip_enabled(8246) and value != 0 + else None + ) + post = { # Factory is the caller to store result if same_tx # Factory is always left with no balance. @@ -1327,9 +1394,11 @@ def test_contract_unrestricted_with_selfdestruct( code=deploy_code, ) if not same_tx or through_delegation - else None, + else selfdestructed_account, # SELFDESTRUCT target is deleted if source was empty - selfdestruct_target: Account(balance=value) if value != 0 else None, + selfdestruct_target: Account(balance=value) + if value != 0 and not selfdestruct_to_self + else None, } blockchain_test( @@ -1350,8 +1419,13 @@ def test_contract_unrestricted_with_selfdestruct( @pytest.mark.with_all_create_opcodes @pytest.mark.parametrize("new_address_pre_funded", [True, False]) @pytest.mark.parametrize( - "selfdestruct,deploy_code", - [(True, None), (False, Bytecode()), (False, Op.STOP)], + "selfdestruct,selfdestruct_to_self,deploy_code", + [ + (True, False, None), + (True, True, None), + (False, False, Bytecode()), + (False, False, Op.STOP), + ], ) def test_contract_unrestricted_within_initcode( blockchain_test: BlockchainTestFiller, @@ -1362,6 +1436,7 @@ def test_contract_unrestricted_within_initcode( create_opcode: Op, new_address_pre_funded: bool, selfdestruct: bool, + selfdestruct_to_self: bool, deploy_code: Bytecode | None, fork: Fork, ) -> None: @@ -1392,7 +1467,7 @@ def test_contract_unrestricted_within_initcode( ) if selfdestruct: initcode = common_initcode + Op.SELFDESTRUCT( - address=selfdestruct_target + address=Op.ADDRESS if selfdestruct_to_self else selfdestruct_target ) else: initcode = Initcode( @@ -1462,6 +1537,22 @@ def test_contract_unrestricted_within_initcode( new_balance = balance - value + Spec.RESERVE_BALANCE + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps both the retained balance and the refill. + retained = balance - value if selfdestruct_to_self else 0 + selfdestructed_account = ( + Account( + nonce=0, + balance=retained + Spec.RESERVE_BALANCE, + code=b"", + storage={}, + ) + if fork.is_eip_enabled(8246) + else None + ) + txs = [tx_1] if new_address_pre_funded: txs.insert( @@ -1491,12 +1582,12 @@ def test_contract_unrestricted_within_initcode( balance=new_balance, code=deploy_code ) if not selfdestruct - else None, + else selfdestructed_account, target: Account(balance=value) if value != 0 else None, # SELFDESTRUCT runs during initcode (before factory # refill), so it sends balance - value only. selfdestruct_target: Account(balance=balance - value) - if selfdestruct + if selfdestruct and not selfdestruct_to_self else None, }, blocks=[Block(txs=txs)], @@ -1513,8 +1604,13 @@ def test_contract_unrestricted_within_initcode( ) @pytest.mark.parametrize("new_address_pre_funded", [True, False]) @pytest.mark.parametrize( - "selfdestruct,deploy_code", - [(True, None), (False, Bytecode()), (False, Op.STOP)], + "selfdestruct,selfdestruct_to_self,deploy_code", + [ + (True, False, None), + (True, True, None), + (False, False, Bytecode()), + (False, False, Op.STOP), + ], ) @pytest.mark.with_all_contract_creating_tx_types def test_unrestricted_in_creation_tx_initcode( @@ -1525,6 +1621,7 @@ def test_unrestricted_in_creation_tx_initcode( balance: int, new_address_pre_funded: bool, selfdestruct: bool, + selfdestruct_to_self: bool, deploy_code: Bytecode | None, tx_type: int, fork: Fork, @@ -1565,7 +1662,7 @@ def test_unrestricted_in_creation_tx_initcode( ) if selfdestruct: initcode = common_initcode + Op.SELFDESTRUCT( - address=selfdestruct_target + address=Op.ADDRESS if selfdestruct_to_self else selfdestruct_target ) else: initcode = Initcode( @@ -1596,6 +1693,16 @@ def test_unrestricted_in_creation_tx_initcode( new_balance = balance - value + Spec.RESERVE_BALANCE + # A SELFDESTRUCT to the destructing account itself moves nothing, so + # what it leaves behind is what EIP-8246 changes: before it, the + # balance is burnt and the account destroyed; after it, the account + # is cleared and keeps the balance it retained. + selfdestructed_account = ( + Account(nonce=0, balance=new_balance, code=b"", storage={}) + if selfdestruct_to_self and fork.is_eip_enabled(8246) + else None + ) + blockchain_test( pre=pre, post={ @@ -1606,10 +1713,10 @@ def test_unrestricted_in_creation_tx_initcode( ), new_address: Account(code=deploy_code, balance=new_balance) if not selfdestruct - else None, + else selfdestructed_account, target: Account(balance=value) if value != 0 else None, selfdestruct_target: Account(balance=new_balance) - if selfdestruct + if selfdestruct and not selfdestruct_to_self else None, }, blocks=[Block(txs=txs)],