Skip to content

Commit

Permalink
More consistent use of BlockNumber type (#1850)
Browse files Browse the repository at this point in the history
  • Loading branch information
pipermerriam committed Sep 18, 2019
1 parent dc9c03b commit e525493
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 31 deletions.
14 changes: 7 additions & 7 deletions eth/abc.py
Expand Up @@ -245,7 +245,7 @@ def hash(self) -> Hash32:

@property
@abstractmethod
def number(self) -> int:
def number(self) -> BlockNumber:
...

@property
Expand Down Expand Up @@ -678,7 +678,7 @@ def stack_push_bytes(self, value: bytes) -> None:
class ExecutionContextAPI(ABC):
coinbase: Address
timestamp: int
block_number: int
block_number: BlockNumber
difficulty: int
gas_limit: int
prev_hashes: Sequence[Hash32]
Expand Down Expand Up @@ -1173,7 +1173,7 @@ def timestamp(self) -> int:

@property
@abstractmethod
def block_number(self) -> int:
def block_number(self) -> BlockNumber:
...

@property
Expand Down Expand Up @@ -1298,7 +1298,7 @@ def persist(self) -> None:
# Access self.prev_hashes (Read-only)
#
@abstractmethod
def get_ancestor_hash(self, block_number: int) -> Hash32:
def get_ancestor_hash(self, block_number: BlockNumber) -> Hash32:
...

#
Expand Down Expand Up @@ -1557,7 +1557,7 @@ def get_prev_hashes(cls,

@staticmethod
@abstractmethod
def get_uncle_reward(block_number: int, uncle: BlockAPI) -> int:
def get_uncle_reward(block_number: BlockNumber, uncle: BlockAPI) -> int:
"""
Return the reward which should be given to the miner of the given `uncle`.
Expand Down Expand Up @@ -1655,7 +1655,7 @@ def state_in_temp_block(self) -> ContextManager[StateAPI]:
class HeaderChainAPI(ABC):
header: BlockHeaderAPI
chain_id: int
vm_configuration: Tuple[Tuple[int, Type[VirtualMachineAPI]], ...]
vm_configuration: Tuple[Tuple[BlockNumber, Type[VirtualMachineAPI]], ...]

@abstractmethod
def __init__(self, base_db: AtomicDatabaseAPI, header: BlockHeaderAPI = None) -> None:
Expand Down Expand Up @@ -1709,7 +1709,7 @@ def import_header(self,


class ChainAPI(ConfigurableAPI):
vm_configuration: Tuple[Tuple[int, Type[VirtualMachineAPI]], ...]
vm_configuration: Tuple[Tuple[BlockNumber, Type[VirtualMachineAPI]], ...]
chain_id: int
chaindb: ChainDatabaseAPI

Expand Down
2 changes: 1 addition & 1 deletion eth/chains/base.py
Expand Up @@ -113,7 +113,7 @@ class BaseChain(Configurable, ChainAPI):
"""
chaindb: ChainDatabaseAPI = None
chaindb_class: Type[ChainDatabaseAPI] = None
vm_configuration: Tuple[Tuple[int, Type[VirtualMachineAPI]], ...] = None
vm_configuration: Tuple[Tuple[BlockNumber, Type[VirtualMachineAPI]], ...] = None
chain_id: int = None

@classmethod
Expand Down
8 changes: 6 additions & 2 deletions eth/chains/mainnet/__init__.py
Expand Up @@ -3,6 +3,7 @@
Type,
)

from eth_typing import BlockNumber
from eth_utils import (
decode_hex,
encode_hex,
Expand Down Expand Up @@ -77,7 +78,7 @@ class MainnetHomesteadVM(MainnetDAOValidatorVM):


MAINNET_FORK_BLOCKS = (
0,
eth_constants.GENESIS_BLOCK_NUMBER,
HOMESTEAD_MAINNET_BLOCK,
TANGERINE_WHISTLE_MAINNET_BLOCK,
SPURIOUS_DRAGON_MAINNET_BLOCK,
Expand All @@ -98,7 +99,10 @@ class MainnetHomesteadVM(MainnetDAOValidatorVM):

class BaseMainnetChain:
chain_id = MAINNET_CHAIN_ID
vm_configuration: Tuple[Tuple[int, Type[VirtualMachineAPI]], ...] = MAINNET_VM_CONFIGURATION
vm_configuration: Tuple[
Tuple[BlockNumber, Type[VirtualMachineAPI]],
...
] = MAINNET_VM_CONFIGURATION


class MainnetChain(BaseMainnetChain, Chain):
Expand Down
8 changes: 6 additions & 2 deletions eth/chains/ropsten/__init__.py
@@ -1,4 +1,5 @@
from typing import Tuple, Type
from eth_typing import BlockNumber
from eth_utils import decode_hex

from .constants import (
Expand Down Expand Up @@ -34,7 +35,10 @@


class BaseRopstenChain:
vm_configuration: Tuple[Tuple[int, Type[VirtualMachineAPI]], ...] = ROPSTEN_VM_CONFIGURATION
vm_configuration: Tuple[
Tuple[BlockNumber, Type[VirtualMachineAPI]],
...
] = ROPSTEN_VM_CONFIGURATION
chain_id: int = ROPSTEN_CHAIN_ID


Expand All @@ -50,7 +54,7 @@ class RopstenChain(BaseRopstenChain, Chain):
bloom=0,
mix_hash=constants.ZERO_HASH32,
nonce=constants.GENESIS_NONCE,
block_number=0,
block_number=constants.GENESIS_BLOCK_NUMBER,
parent_hash=constants.ZERO_HASH32,
receipt_root=constants.BLANK_ROOT_HASH,
uncles_hash=constants.EMPTY_UNCLE_HASH,
Expand Down
12 changes: 7 additions & 5 deletions eth/chains/tester/__init__.py
Expand Up @@ -9,6 +9,7 @@
Union,
)

from eth_typing import BlockNumber
from eth_utils.toolz import (
assoc,
last,
Expand All @@ -24,6 +25,7 @@
BlockHeaderAPI,
VirtualMachineAPI,
)
from eth.constants import GENESIS_BLOCK_NUMBER
from eth.chains.base import Chain
from eth.chains.mainnet import MainnetChain
from eth.validation import (
Expand Down Expand Up @@ -53,8 +55,8 @@ def create_header_from_parent(cls,
in MainnetChain.vm_configuration
)

ForkStartBlocks = Sequence[Tuple[int, Union[str, Type[VirtualMachineAPI]]]]
VMStartBlock = Tuple[int, Type[VirtualMachineAPI]]
ForkStartBlocks = Sequence[Tuple[BlockNumber, Union[str, Type[VirtualMachineAPI]]]]
VMStartBlock = Tuple[BlockNumber, Type[VirtualMachineAPI]]


@to_tuple
Expand All @@ -74,7 +76,7 @@ def _generate_vm_configuration(*fork_start_blocks: ForkStartBlocks,
# if no configuration was passed in, initialize the chain with the *latest*
# Mainnet VM rules active at block 0.
if not fork_start_blocks:
yield (0, last(MAINNET_VMS.values()))
yield (GENESIS_BLOCK_NUMBER, last(MAINNET_VMS.values()))
return

# Validate that there are no fork names which are not represented in the
Expand Down Expand Up @@ -103,7 +105,7 @@ def _generate_vm_configuration(*fork_start_blocks: ForkStartBlocks,
# If no VM is set to start at block 0, default to the frontier VM
start_blocks = set(start_block for start_block, _ in fork_start_blocks)
if 0 not in start_blocks:
yield 0, MAINNET_VMS['frontier']
yield GENESIS_BLOCK_NUMBER, MAINNET_VMS['frontier']

ordered_fork_start_blocks = sorted(fork_start_blocks, key=operator.itemgetter(0))

Expand Down Expand Up @@ -132,7 +134,7 @@ def _generate_vm_configuration(*fork_start_blocks: ForkStartBlocks,


class BaseMainnetTesterChain(Chain):
vm_configuration: Tuple[Tuple[int, Type[VirtualMachineAPI]], ...] = _generate_vm_configuration()
vm_configuration: Tuple[Tuple[BlockNumber, Type[VirtualMachineAPI]], ...] = _generate_vm_configuration() # noqa: E501


class MainnetTesterChain(BaseMainnetTesterChain):
Expand Down
3 changes: 2 additions & 1 deletion eth/constants.py
@@ -1,5 +1,6 @@
from eth_typing import (
Address,
BlockNumber,
Hash32
)

Expand Down Expand Up @@ -152,7 +153,7 @@
#
# Genesis Data
#
GENESIS_BLOCK_NUMBER = 0
GENESIS_BLOCK_NUMBER = BlockNumber(0)
GENESIS_DIFFICULTY = 17179869184
GENESIS_GAS_LIMIT = 5000
GENESIS_PARENT_HASH = ZERO_HASH32
Expand Down
5 changes: 3 additions & 2 deletions eth/rlp/headers.py
Expand Up @@ -14,6 +14,7 @@

from eth_typing import (
Address,
BlockNumber,
Hash32,
)

Expand Down Expand Up @@ -97,7 +98,7 @@ def __init__(self, **kwargs: HeaderParams) -> None:
@overload # noqa: F811
def __init__(self,
difficulty: int,
block_number: int,
block_number: BlockNumber,
gas_limit: int,
timestamp: int=None,
coinbase: Address=ZERO_ADDRESS,
Expand All @@ -115,7 +116,7 @@ def __init__(self,

def __init__(self, # type: ignore # noqa: F811
difficulty: int,
block_number: int,
block_number: BlockNumber,
gas_limit: int,
timestamp: int=None,
coinbase: Address=ZERO_ADDRESS,
Expand Down
12 changes: 6 additions & 6 deletions eth/tools/builder/chain/builders.py
Expand Up @@ -108,7 +108,7 @@ def chain_id(chain_id: int, chain_class: Type[ChainAPI]) -> Type[ChainAPI]:

@curry
def fork_at(vm_class: Type[VirtualMachineAPI],
at_block: int,
at_block: Union[int, BlockNumber],
chain_class: Type[ChainAPI]) -> Type[ChainAPI]:
"""
Adds the ``vm_class`` to the chain's ``vm_configuration``.
Expand Down Expand Up @@ -146,7 +146,7 @@ class FrontierOnlyChain(MiningChain):
else:
base_configuration = tuple()

vm_configuration = base_configuration + ((at_block, vm_class),)
vm_configuration = base_configuration + ((BlockNumber(at_block), vm_class),)
validate_vm_configuration(vm_configuration)
return chain_class.configure(vm_configuration=vm_configuration)

Expand Down Expand Up @@ -236,7 +236,7 @@ def dao_fork_at(dao_fork_block_number: BlockNumber,
latest_mainnet_at = petersburg_at

GENESIS_DEFAULTS = cast(
Tuple[Tuple[str, Union[int, None, bytes, Address, Hash32]], ...],
Tuple[Tuple[str, Union[BlockNumber, int, None, bytes, Address, Hash32]], ...],
(
('difficulty', 1),
('extra_data', constants.GENESIS_EXTRA_DATA),
Expand All @@ -257,7 +257,7 @@ def dao_fork_at(dao_fork_block_number: BlockNumber,

@to_dict
def _get_default_genesis_params(genesis_state: AccountState,
) -> Iterable[Tuple[str, Union[int, None, bytes, Address, Hash32]]]:
) -> Iterable[Tuple[str, Union[BlockNumber, int, None, bytes, Address, Hash32]]]: # noqa: E501
for key, value in GENESIS_DEFAULTS:
if key == 'state_root' and genesis_state:
# leave out the `state_root` if a genesis state was specified
Expand Down Expand Up @@ -496,15 +496,15 @@ def _chain_split(chain: ChainAPI) -> Iterable[ChainAPI]:


@curry
def at_block_number(block_number: BlockNumber, chain: MiningChainAPI) -> MiningChainAPI:
def at_block_number(block_number: Union[int, BlockNumber], chain: MiningChainAPI) -> MiningChainAPI:
"""
Rewind the chain back to the given block number. Calls to things like
``get_canonical_head`` will still return the canonical head of the chain,
however, you can use ``mine_block`` to mine fork chains.
"""
if not isinstance(chain, MiningChainAPI):
raise ValidationError("`at_block_number` may only be used with 'MiningChain")
at_block = chain.get_canonical_block_by_number(block_number)
at_block = chain.get_canonical_block_by_number(BlockNumber(block_number))

db = chain.chaindb.db
chain_at_block = type(chain)(db, chain.create_header_from_parent(at_block.header))
Expand Down
2 changes: 1 addition & 1 deletion eth/typing.py
Expand Up @@ -95,4 +95,4 @@ def __set__(self, oself: Any, value: TFunc) -> None:
self._func = value


HeaderParams = Union[Optional[int], bytes, Address, Hash32]
HeaderParams = Union[Optional[int], BlockNumber, bytes, Address, Hash32]
5 changes: 3 additions & 2 deletions eth/vm/execution_context.py
Expand Up @@ -4,6 +4,7 @@

from eth_typing import (
Address,
BlockNumber,
Hash32,
)

Expand All @@ -24,7 +25,7 @@ def __init__(
self,
coinbase: Address,
timestamp: int,
block_number: int,
block_number: BlockNumber,
difficulty: int,
gas_limit: int,
prev_hashes: Iterable[Hash32],
Expand All @@ -46,7 +47,7 @@ def timestamp(self) -> int:
return self._timestamp

@property
def block_number(self) -> int:
def block_number(self) -> BlockNumber:
return self._block_number

@property
Expand Down
3 changes: 2 additions & 1 deletion eth/vm/forks/frontier/blocks.py
Expand Up @@ -14,6 +14,7 @@
)

from eth_typing import (
BlockNumber,
Hash32,
)

Expand Down Expand Up @@ -75,7 +76,7 @@ def __init__(self,
# Helpers
#
@property
def number(self) -> int:
def number(self) -> BlockNumber:
return self.header.block_number

@property
Expand Down
3 changes: 2 additions & 1 deletion eth/vm/state.py
Expand Up @@ -8,6 +8,7 @@

from eth_typing import (
Address,
BlockNumber,
Hash32,
)
from eth_utils import (
Expand Down Expand Up @@ -96,7 +97,7 @@ def timestamp(self) -> int:
return self.execution_context.timestamp

@property
def block_number(self) -> int:
def block_number(self) -> BlockNumber:
"""
Return the current ``block_number`` from the current :attr:`~execution_context`
"""
Expand Down
1 change: 1 addition & 0 deletions newsfragments/1850.bugfix.rst
@@ -0,0 +1 @@
Update codebase to more consistently use the ``eth_typing.BlockNumber`` type.

0 comments on commit e525493

Please sign in to comment.