Skip to content

Commit

Permalink
Implement Berlin fork stub
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf authored and ralexstokes committed Jun 2, 2020
1 parent 85f5b09 commit b069648
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
31 changes: 31 additions & 0 deletions eth/vm/forks/berlin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import (
Type,
)

from eth.rlp.blocks import BaseBlock
from eth.vm.forks.constantinople import (
ConstantinopleVM,
)
from eth.vm.state import BaseState

from .blocks import BerlinBlock
from .headers import (
compute_berlin_difficulty,
configure_berlin_header,
create_berlin_header_from_parent,
)
from .state import BerlinState


class BerlinVM(ConstantinopleVM):
# fork name
fork = 'berlin'

# classes
block_class: Type[BaseBlock] = BerlinBlock
_state_class: Type[BaseState] = BerlinState

# Methods
create_header_from_parent = staticmethod(create_berlin_header_from_parent) # type: ignore
compute_difficulty = staticmethod(compute_berlin_difficulty) # type: ignore
configure_header = configure_berlin_header
22 changes: 22 additions & 0 deletions eth/vm/forks/berlin/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from rlp.sedes import (
CountableList,
)
from eth.rlp.headers import (
BlockHeader,
)
from eth.vm.forks.muir_glacier.blocks import (
MuirGlacierBlock,
)

from .transactions import (
BerlinTransaction,
)


class BerlinBlock(MuirGlacierBlock):
transaction_class = BerlinTransaction
fields = [
('header', BlockHeader),
('transactions', CountableList(transaction_class)),
('uncles', CountableList(BlockHeader))
]
20 changes: 20 additions & 0 deletions eth/vm/forks/berlin/computation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from eth.vm.forks.muir_glacier.computation import (
MUIR_GLACIER_PRECOMPILES
)
from eth.vm.forks.muir_glacier.computation import (
MuirGlacierComputation,
)

from .opcodes import BERLIN_OPCODES

BERLIN_PRECOMPILES = MUIR_GLACIER_PRECOMPILES


class BerlinComputation(MuirGlacierComputation):
"""
A class for all execution computations in the ``Berlin`` fork.
Inherits from :class:`~eth.vm.forks.muir_glacier.MuirGlacierComputation`
"""
# Override
opcodes = BERLIN_OPCODES
_precompiles = BERLIN_PRECOMPILES
13 changes: 13 additions & 0 deletions eth/vm/forks/berlin/headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from eth.vm.forks.muir_glacier.headers import (
configure_header,
create_header_from_parent,
compute_muir_glacier_difficulty,
)


compute_berlin_difficulty = compute_muir_glacier_difficulty

create_berlin_header_from_parent = create_header_from_parent(
compute_berlin_difficulty
)
configure_berlin_header = configure_header(compute_berlin_difficulty)
18 changes: 18 additions & 0 deletions eth/vm/forks/berlin/opcodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import copy

from eth_utils.toolz import merge


from eth.vm.forks.muir_glacier.opcodes import (
MUIR_GLACIER_OPCODES,
)


UPDATED_OPCODES = {
# New opcodes
}

BERLIN_OPCODES = merge(
copy.deepcopy(MUIR_GLACIER_OPCODES),
UPDATED_OPCODES,
)
9 changes: 9 additions & 0 deletions eth/vm/forks/berlin/state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from eth.vm.forks.muir_glacier.state import (
MuirGlacierState
)

from .computation import BerlinComputation


class BerlinState(MuirGlacierState):
computation_class = BerlinComputation
42 changes: 42 additions & 0 deletions eth/vm/forks/berlin/transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from eth_keys.datatypes import PrivateKey
from eth_typing import Address

from eth.vm.forks.muir_glacier.transactions import (
MuirGlacierTransaction,
MuirGlacierUnsignedTransaction,
)

from eth._utils.transactions import (
create_transaction_signature,
)


class BerlinTransaction(MuirGlacierTransaction):
@classmethod
def create_unsigned_transaction(cls,
*,
nonce: int,
gas_price: int,
gas: int,
to: Address,
value: int,
data: bytes) -> 'BerlinUnsignedTransaction':
return BerlinUnsignedTransaction(nonce, gas_price, gas, to, value, data)


class BerlinUnsignedTransaction(MuirGlacierUnsignedTransaction):
def as_signed_transaction(self,
private_key: PrivateKey,
chain_id: int=None) -> BerlinTransaction:
v, r, s = create_transaction_signature(self, private_key, chain_id=chain_id)
return BerlinTransaction(
nonce=self.nonce,
gas_price=self.gas_price,
gas=self.gas,
to=self.to,
value=self.value,
data=self.data,
v=v,
r=r,
s=s,
)

0 comments on commit b069648

Please sign in to comment.