Skip to content

Commit 8462802

Browse files
kclowescarver
andauthored
London basefee opcode 0x48 (#2015)
* London basefee opcode * raise NotImplementedError for basefee in base state * Add newsfragment * basefee -> base_fee * move stack_pop1_int -> stack_pop1_any Co-authored-by: Jason Carver <[email protected]> * basefee messaging and docs Co-authored-by: Jason Carver <[email protected]>
1 parent 09c04be commit 8462802

File tree

10 files changed

+74
-1
lines changed

10 files changed

+74
-1
lines changed

eth/abc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,17 @@ def gas_limit(self) -> int:
26702670
"""
26712671
...
26722672

2673+
@property
2674+
@abstractmethod
2675+
def base_fee(self) -> int:
2676+
"""
2677+
Return the current ``base_fee`` from the current :attr:`~execution_context`
2678+
2679+
Raises a ``NotImplementedError`` if called in an execution context
2680+
prior to the London hard fork.
2681+
"""
2682+
...
2683+
26732684
@abstractmethod
26742685
def get_gas_price(self, transaction: SignedTransactionAPI) -> int:
26752686
"""

eth/vm/forks/london/computation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
BerlinComputation,
33
)
44

5+
from .opcodes import LONDON_OPCODES
6+
57

68
class LondonComputation(BerlinComputation):
79
"""
810
A class for all execution computations in the ``London`` fork.
911
Inherits from :class:`~eth.vm.forks.berlin.BerlinComputation`
1012
"""
11-
pass
13+
opcodes = LONDON_OPCODES

eth/vm/forks/london/opcodes.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import copy
2+
from typing import Dict
3+
4+
from eth_utils.toolz import merge
5+
6+
from eth.vm.logic import (
7+
block,
8+
)
9+
from eth.vm import (
10+
mnemonics,
11+
opcode_values,
12+
)
13+
from eth.vm.opcode import (
14+
Opcode,
15+
as_opcode,
16+
)
17+
from eth import constants
18+
19+
from eth.vm.forks.berlin.opcodes import (
20+
BERLIN_OPCODES,
21+
)
22+
23+
24+
UPDATED_OPCODES: Dict[int, Opcode] = {
25+
opcode_values.BASEFEE: as_opcode(
26+
gas_cost=constants.GAS_BASE,
27+
logic_fn=block.basefee,
28+
mnemonic=mnemonics.BASEFEE,
29+
),
30+
}
31+
32+
33+
LONDON_OPCODES = merge(
34+
copy.deepcopy(BERLIN_OPCODES),
35+
UPDATED_OPCODES,
36+
)

eth/vm/forks/london/state.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,7 @@ def get_transaction_context(self: StateAPI,
135135
gas_price=effective_gas_price,
136136
origin=transaction.sender
137137
)
138+
139+
@property
140+
def base_fee(self: StateAPI) -> int:
141+
return self.execution_context.base_fee_per_gas

eth/vm/logic/block.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ def difficulty(computation: BaseComputation) -> None:
2727

2828
def gaslimit(computation: BaseComputation) -> None:
2929
computation.stack_push_int(computation.state.gas_limit)
30+
31+
32+
def basefee(computation: BaseComputation) -> None:
33+
computation.stack_push_int(computation.state.base_fee)

eth/vm/mnemonics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
NUMBER = 'NUMBER'
6565
DIFFICULTY = 'DIFFICULTY'
6666
GASLIMIT = 'GASLIMIT'
67+
BASEFEE = 'BASEFEE'
6768
#
6869
# Stack, Memory, Storage and Flow Operations
6970
#

eth/vm/opcode_values.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
# These opcodes seem to belong in the environment block, but we are out of opcode space in 0x3*
6464
CHAINID = 0x46
6565
SELFBALANCE = 0x47
66+
BASEFEE = 0x48
6667

6768
#
6869
# Block Information

eth/vm/state.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def difficulty(self) -> int:
8888
def gas_limit(self) -> int:
8989
return self.execution_context.gas_limit
9090

91+
@property
92+
def base_fee(self) -> int:
93+
raise NotImplementedError("Basefee opcode is not implemented prior to London hard fork")
94+
9195
def get_tip(self, transaction: SignedTransactionAPI) -> int:
9296
return transaction.gas_price
9397

newsfragments/2015.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add basefee opcode per EIP-3198

tests/core/opcodes/test_opcodes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ def test_add(vm_class, val1, val2, expected):
160160
assert result == expected
161161

162162

163+
def test_base_fee():
164+
computation = run_general_computation(LondonVM)
165+
computation.opcodes[opcode_values.BASEFEE](computation)
166+
167+
result = computation.stack_pop1_any()
168+
169+
assert result == 10 ** 9 # 1 gwei
170+
171+
163172
@pytest.mark.parametrize(
164173
'opcode_value, expected',
165174
(

0 commit comments

Comments
 (0)