Skip to content

Implement Berlin fork stub #1969

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

Merged
merged 1 commit into from
Feb 19, 2021
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
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 import (
MuirGlacierVM,
)
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(MuirGlacierVM):
# 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)
19 changes: 19 additions & 0 deletions eth/vm/forks/berlin/opcodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import copy
from typing import Dict

from eth_utils.toolz import merge

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


UPDATED_OPCODES: Dict[int, Opcode] = {
# 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,
)