Skip to content

Commit 8005e7f

Browse files
committed
refactor computation logic to reduce needless code duplication
suggested changes from PR #2018
1 parent acd05a1 commit 8005e7f

File tree

2 files changed

+20
-64
lines changed

2 files changed

+20
-64
lines changed

eth/vm/forks/london/computation.py

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
from eth_utils import encode_hex, keccak
2-
3-
from eth import constants
4-
from eth.abc import ComputationAPI, MessageAPI, StateAPI, TransactionContextAPI
5-
from eth.exceptions import OutOfGas, ReservedBytesInCode
1+
from eth.exceptions import ReservedBytesInCode
62
from eth.vm.forks.berlin.computation import (
73
BerlinComputation,
84
)
95

106
from .opcodes import LONDON_OPCODES
117
from ..london.constants import EIP3541_RESERVED_STARTING_BYTE
12-
from ..spurious_dragon.constants import EIP170_CODE_SIZE_LIMIT
138

149

1510
class LondonComputation(BerlinComputation):
@@ -20,60 +15,8 @@ class LondonComputation(BerlinComputation):
2015
opcodes = LONDON_OPCODES
2116

2217
@classmethod
23-
def apply_create_message(
24-
cls,
25-
state: StateAPI,
26-
message: MessageAPI,
27-
transaction_context: TransactionContextAPI
28-
) -> ComputationAPI:
29-
30-
snapshot = state.snapshot()
31-
32-
# EIP161 nonce incrementation
33-
state.increment_nonce(message.storage_address)
34-
35-
computation = cls.apply_message(state, message, transaction_context)
36-
37-
if computation.is_error:
38-
state.revert(snapshot)
39-
return computation
40-
else:
41-
contract_code = computation.output
42-
43-
if contract_code and len(contract_code) >= EIP170_CODE_SIZE_LIMIT:
44-
computation.error = OutOfGas(
45-
f"Contract code size exceeds EIP170 limit of {EIP170_CODE_SIZE_LIMIT}."
46-
f" Got code of size: {len(contract_code)}"
47-
)
48-
state.revert(snapshot)
49-
elif contract_code:
50-
contract_code_gas_cost = len(contract_code) * constants.GAS_CODEDEPOSIT
51-
try:
52-
computation.consume_gas(
53-
contract_code_gas_cost,
54-
reason="Write contract code for CREATE / CREATE2",
55-
)
56-
except OutOfGas as err:
57-
computation.error = err
58-
state.revert(snapshot)
59-
else:
60-
if contract_code[:1] == EIP3541_RESERVED_STARTING_BYTE:
61-
# As per EIP-3541, gas is still consumed on a revert of this nature
62-
state.revert(snapshot)
63-
raise ReservedBytesInCode(
64-
"Contract code begins with EIP3541 reserved byte '0xEF'."
65-
)
66-
else:
67-
if cls.logger:
68-
cls.logger.debug2(
69-
"SETTING CODE: %s -> length: %s | hash: %s",
70-
encode_hex(message.storage_address),
71-
len(contract_code),
72-
encode_hex(keccak(contract_code))
73-
)
74-
75-
state.set_code(message.storage_address, contract_code)
76-
state.commit(snapshot)
77-
else:
78-
state.commit(snapshot)
79-
return computation
18+
def validate_new_contract_code(cls, contract_code: bytes) -> None:
19+
if contract_code[:1] == EIP3541_RESERVED_STARTING_BYTE:
20+
raise ReservedBytesInCode(
21+
"Contract code begins with EIP3541 reserved byte '0xEF'."
22+
)

eth/vm/forks/spurious_dragon/computation.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from eth_hash.auto import keccak
2+
23
from eth_utils import (
34
encode_hex,
45
)
@@ -12,12 +13,13 @@
1213
)
1314
from eth.exceptions import (
1415
OutOfGas,
16+
VMError,
1517
)
1618
from eth.vm.forks.homestead.computation import (
1719
HomesteadComputation,
1820
)
1921

20-
from .constants import EIP170_CODE_SIZE_LIMIT
22+
from ..spurious_dragon.constants import EIP170_CODE_SIZE_LIMIT
2123
from .opcodes import SPURIOUS_DRAGON_OPCODES
2224

2325

@@ -68,6 +70,12 @@ def apply_create_message(
6870
computation.error = err
6971
state.revert(snapshot)
7072
else:
73+
try:
74+
cls.validate_new_contract_code(contract_code)
75+
except VMError as err:
76+
state.revert(snapshot)
77+
raise err
78+
7179
if cls.logger:
7280
cls.logger.debug2(
7381
"SETTING CODE: %s -> length: %s | hash: %s",
@@ -81,3 +89,8 @@ def apply_create_message(
8189
else:
8290
state.commit(snapshot)
8391
return computation
92+
93+
@classmethod
94+
def validate_new_contract_code(cls, contract_code: bytes) -> None:
95+
# helps facilitate EIP-3541 validation in LondonComputation
96+
pass

0 commit comments

Comments
 (0)