Skip to content

Commit

Permalink
Unify optimized implementations (#811)
Browse files Browse the repository at this point in the history
* Unify optimized implementations

* Update spec tools

* Use a decorator to explicitly declare which items are to be patched

* Use `Hardfork.discover()` to get forks

* Use `Hardfork.consensus` to test consensus type
  • Loading branch information
petertdavies authored Aug 5, 2023
1 parent d0d0a31 commit decd4e5
Show file tree
Hide file tree
Showing 72 changed files with 521 additions and 7,112 deletions.
15 changes: 0 additions & 15 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,91 +27,76 @@ packages =
ethereum/utils
ethereum/crypto
ethereum_optimized/
ethereum_optimized/frontier
ethereum/homestead
ethereum/homestead/utils
ethereum/homestead/vm
ethereum/homestead/vm/instructions
ethereum/homestead/vm/precompiled_contracts
ethereum_optimized/homestead
ethereum/dao_fork
ethereum/dao_fork/utils
ethereum/dao_fork/vm
ethereum/dao_fork/vm/instructions
ethereum/dao_fork/vm/precompiled_contracts
ethereum_optimized/dao_fork
ethereum/tangerine_whistle
ethereum/tangerine_whistle/utils
ethereum/tangerine_whistle/vm
ethereum/tangerine_whistle/vm/instructions
ethereum/tangerine_whistle/vm/precompiled_contracts
ethereum_optimized/tangerine_whistle
ethereum/spurious_dragon
ethereum/spurious_dragon/utils
ethereum/spurious_dragon/vm
ethereum/spurious_dragon/vm/instructions
ethereum/spurious_dragon/vm/precompiled_contracts
ethereum_optimized/spurious_dragon
ethereum/byzantium
ethereum/byzantium/utils
ethereum/byzantium/vm
ethereum/byzantium/vm/instructions
ethereum/byzantium/vm/precompiled_contracts
ethereum_optimized/byzantium
ethereum/constantinople
ethereum/constantinople/utils
ethereum/constantinople/vm
ethereum/constantinople/vm/instructions
ethereum/constantinople/vm/precompiled_contracts
ethereum_optimized/constantinople
ethereum/istanbul
ethereum/istanbul/utils
ethereum/istanbul/vm
ethereum/istanbul/vm/instructions
ethereum/istanbul/vm/precompiled_contracts
ethereum_optimized/istanbul
ethereum/muir_glacier
ethereum/muir_glacier/utils
ethereum/muir_glacier/vm
ethereum/muir_glacier/vm/instructions
ethereum/muir_glacier/vm/precompiled_contracts
ethereum_optimized/muir_glacier
ethereum/berlin
ethereum/berlin/utils
ethereum/berlin/vm
ethereum/berlin/vm/instructions
ethereum/berlin/vm/precompiled_contracts
ethereum_optimized/berlin
ethereum/london
ethereum/london/utils
ethereum/london/vm
ethereum/london/vm/instructions
ethereum/london/vm/precompiled_contracts
ethereum_optimized/london
ethereum/arrow_glacier
ethereum/arrow_glacier/utils
ethereum/arrow_glacier/vm
ethereum/arrow_glacier/vm/instructions
ethereum/arrow_glacier/vm/precompiled_contracts
ethereum_optimized/arrow_glacier
ethereum/gray_glacier
ethereum/gray_glacier/utils
ethereum/gray_glacier/vm
ethereum/gray_glacier/vm/instructions
ethereum/gray_glacier/vm/precompiled_contracts
ethereum_optimized/gray_glacier
ethereum/paris
ethereum/paris/utils
ethereum/paris/vm
ethereum/paris/vm/instructions
ethereum/paris/vm/precompiled_contracts
ethereum_optimized/paris
ethereum/shanghai
ethereum/shanghai/utils
ethereum/shanghai/vm
ethereum/shanghai/vm/instructions
ethereum/shanghai/vm/precompiled_contracts
ethereum_optimized/shanghai


package_dir =
Expand Down
88 changes: 53 additions & 35 deletions src/ethereum_optimized/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,64 @@
have been optimized for speed rather than clarity.
They can be monkey patched in during start up by calling the `monkey_patch()`
function.
function. This must be done before those modules are imported anywhere.
"""

from typing import Optional
from importlib import import_module
from typing import Any, Optional, cast

from ethereum_spec_tools.forks import Hardfork

from .fork import get_optimized_pow_patches
from .state_db import get_optimized_state_patches


def monkey_patch_optimized_state_db(
fork_name: str, state_path: Optional[str]
) -> None:
"""
Replace the state interface with one that supports high performance
updates and storing state in a database.
This function must be called before the state interface is imported
anywhere.
"""
slow_state = cast(Any, import_module("ethereum." + fork_name + ".state"))

optimized_state_db_patches = get_optimized_state_patches(fork_name)

for (name, value) in optimized_state_db_patches.items():
setattr(slow_state, name, value)

if state_path is not None:
slow_state.State.default_path = state_path


def monkey_patch_optimized_spec(fork_name: str) -> None:
"""
Replace the ethash implementation with one that supports higher
performance.
This function must be called before the spec interface is imported
anywhere.
"""
slow_spec = import_module("ethereum." + fork_name + ".fork")

optimized_pow_patches = get_optimized_pow_patches(fork_name)

for (name, value) in optimized_pow_patches.items():
setattr(slow_spec, name, value)


def monkey_patch(state_path: Optional[str]) -> None:
"""
Apply all monkey patches to the specification.
"""
from . import (
arrow_glacier,
berlin,
byzantium,
constantinople,
dao_fork,
frontier,
gray_glacier,
homestead,
istanbul,
london,
muir_glacier,
paris,
shanghai,
spurious_dragon,
tangerine_whistle,
)

frontier.monkey_patch(state_path)
homestead.monkey_patch(state_path)
dao_fork.monkey_patch(state_path)
tangerine_whistle.monkey_patch(state_path)
spurious_dragon.monkey_patch(state_path)
byzantium.monkey_patch(state_path)
constantinople.monkey_patch(state_path)
istanbul.monkey_patch(state_path)
muir_glacier.monkey_patch(state_path)
berlin.monkey_patch(state_path)
london.monkey_patch(state_path)
arrow_glacier.monkey_patch(state_path)
gray_glacier.monkey_patch(state_path)
paris.monkey_patch(state_path)
shanghai.monkey_patch(state_path)
forks = Hardfork.discover()

for fork in forks:
monkey_patch_optimized_state_db(fork.short_name, state_path)

# Only patch the POW code on POW forks
if fork.consensus.is_pow():
monkey_patch_optimized_spec(fork.short_name)
72 changes: 0 additions & 72 deletions src/ethereum_optimized/arrow_glacier/__init__.py

This file was deleted.

47 changes: 0 additions & 47 deletions src/ethereum_optimized/arrow_glacier/fork.py

This file was deleted.

Loading

0 comments on commit decd4e5

Please sign in to comment.