Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Housekeeping; expose consensus type (closes #679) #820

Merged
merged 2 commits into from
Aug 2, 2023
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
33 changes: 29 additions & 4 deletions src/ethereum/fork_criteria.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import functools
from abc import ABC, abstractmethod
from typing import Tuple
from typing import Final, Tuple


# MyPy doesn't support decorators on abstract classes
Expand All @@ -25,9 +25,9 @@ class ForkCriteria(ABC):
Type that represents the condition required for a fork to occur.
"""

BLOCK_NUMBER = 0
TIMESTAMP = 1
UNSCHEDULED = 2
BLOCK_NUMBER: Final[int] = 0
TIMESTAMP: Final[int] = 1
UNSCHEDULED: Final[int] = 2

_internal: Tuple[int, int]

Expand Down Expand Up @@ -61,6 +61,13 @@ def check(self, block_number: int, timestamp: int) -> bool:
"""
...

@abstractmethod
def __repr__(self) -> str:
"""
String representation of this object.
"""
raise NotImplementedError()


class ByBlockNumber(ForkCriteria):
"""
Expand All @@ -79,6 +86,12 @@ def check(self, block_number: int, timestamp: int) -> bool:
"""
return block_number >= self.block_number

def __repr__(self) -> str:
"""
String representation of this object.
"""
return f"ByBlockNumber({self.block_number})"


class ByTimestamp(ForkCriteria):
"""
Expand All @@ -97,6 +110,12 @@ def check(self, block_number: int, timestamp: int) -> bool:
"""
return timestamp >= self.timestamp

def __repr__(self) -> str:
"""
String representation of this object.
"""
return f"ByTimestamp({self.timestamp})"


class Unscheduled(ForkCriteria):
"""
Expand All @@ -111,3 +130,9 @@ def check(self, block_number: int, timestamp: int) -> bool:
Unscheduled forks never occur.
"""
return False

def __repr__(self) -> str:
"""
String representation of this object.
"""
return "Unscheduled()"
7 changes: 2 additions & 5 deletions src/ethereum_spec_tools/evm_tools/fixture_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,10 @@ def network(self) -> str:
def proof_of_stake(self) -> bool:
"""Whether the fork is proof of stake"""
forks = Hardfork.discover()
merge_fork_found = False
for fork in forks:
if fork.name == "ethereum.paris":
merge_fork_found = True
if fork.name == "ethereum." + self._fork_module:
break
return merge_fork_found
return fork.consensus.is_pos()
raise Exception(f"fork {self._fork_module} not discovered")

@property
def Block(self) -> Any:
Expand Down
39 changes: 37 additions & 2 deletions src/ethereum_spec_tools/forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,36 @@

import importlib
import pkgutil
from enum import Enum, auto
from pkgutil import ModuleInfo
from types import ModuleType
from typing import Any, Dict, Iterator, List, Optional, Type, TypeVar

import ethereum
from ethereum.fork_criteria import ByBlockNumber, ByTimestamp, ForkCriteria


class ConsensusType(Enum):
"""
How a fork chooses its canonical chain.
"""

PROOF_OF_WORK = auto()
PROOF_OF_STAKE = auto()

def is_pow(self) -> bool:
"""
Returns True if self == PROOF_OF_WORK.
"""
return self == ConsensusType.PROOF_OF_WORK

def is_pos(self) -> bool:
"""
Returns True if self == PROOF_OF_STAKE.
"""
return self == ConsensusType.PROOF_OF_STAKE


H = TypeVar("H", bound="Hardfork")


Expand Down Expand Up @@ -98,12 +121,24 @@ def load_from_json(cls: Type[H], json: Any) -> List[H]:
def __init__(self, mod: ModuleType) -> None:
self.mod = mod

@property
def consensus(self) -> ConsensusType:
"""
How this fork chooses its canonical chain.
"""
if hasattr(self.module("fork"), "validate_proof_of_work"):
return ConsensusType.PROOF_OF_WORK
else:
return ConsensusType.PROOF_OF_STAKE

@property
def criteria(self) -> ForkCriteria:
"""
Criteria to trigger this hardfork.
"""
return self.mod.FORK_CRITERIA # type: ignore
criteria = self.mod.FORK_CRITERIA # type: ignore[attr-defined]
assert isinstance(criteria, ForkCriteria)
return criteria

@property
def block(self) -> int:
Expand Down Expand Up @@ -167,7 +202,7 @@ def __repr__(self) -> str:
self.__class__.__name__
+ "("
+ f"name={self.name!r}, "
+ f"block={self.block}, "
+ f"criteria={self.criteria}, "
+ "..."
+ ")"
)
Expand Down
Loading