Skip to content

Commit

Permalink
Re-refactor pre-EOF:
Browse files Browse the repository at this point in the history
Put the state of the Computation class hierarchy mostly back to where it was, keeping some good cleanup and updating the ComputationAPI to match properties implemented in the base computation implementation class.

Some of the earlier `ethereum/tests` pointed to EOF code within the bytecode, not at the start. This led to some wrong class architecting to make it work with our current model, with the bigger refactor commit from last week. It would be best to put the state of the Computation hierarchy mostly back to how it was so as to not break too much of the existing structure. Massaging EOF into this model won't be too difficult with the (proper) way the EOF tests are written now.

Since there won't be a release until all these changes are in, the most this does is muddy the commit history a bit. But at least we gained a touch of housekeeping along the way.
  • Loading branch information
fselmo committed May 3, 2023
1 parent 8c8549d commit a1ef2b7
Show file tree
Hide file tree
Showing 78 changed files with 1,028 additions and 1,132 deletions.
7 changes: 0 additions & 7 deletions docs/api/api.abc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@ ComputationAPI
:members:


MessageComputationAPI
---------------------

.. autoclass:: eth.abc.MessageComputationAPI
:members:


AccountStorageDatabaseAPI
-------------------------

Expand Down
143 changes: 69 additions & 74 deletions eth/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def get_intrinsic_gas(self) -> int:
...

@abstractmethod
def gas_used_by(self, computation: 'MessageComputationAPI') -> int:
def gas_used_by(self, computation: 'ComputationAPI') -> int:
"""
Return the gas used by the given computation. In Frontier,
for example, this is sum of the intrinsic cost and the gas used
Expand Down Expand Up @@ -1440,7 +1440,7 @@ class OpcodeAPI(ABC):
mnemonic: str

@abstractmethod
def __call__(self, computation: 'MessageComputationAPI') -> None:
def __call__(self, computation: 'ComputationAPI') -> None:
"""
Execute the logic of the opcode.
"""
Expand All @@ -1449,7 +1449,7 @@ def __call__(self, computation: 'MessageComputationAPI') -> None:
@classmethod
@abstractmethod
def as_opcode(cls: Type[T],
logic_fn: Callable[['MessageComputationAPI'], None],
logic_fn: Callable[['ComputationAPI'], None],
mnemonic: str,
gas_cost: int) -> T:
"""
Expand Down Expand Up @@ -1872,15 +1872,34 @@ class ComputationAPI(
logger: ExtendedDebugLogger

state: "StateAPI"
msg: MessageAPI
transaction_context: TransactionContextAPI
code: CodeStreamAPI
return_data: bytes
children: List["ComputationAPI"]
return_data: bytes = b''
accounts_to_delete: Dict[Address, Address]

_memory: MemoryAPI
_stack: StackAPI
_gas_meter: GasMeterAPI
_error: VMError
_output: bytes = b''
_log_entries: List[Tuple[int, Address, Tuple[int, ...], bytes]]

# VM configuration
opcodes: Dict[int, OpcodeAPI]
_precompiles: Dict[Address, Callable[["ComputationAPI"], "ComputationAPI"]]

@abstractmethod
def __init__(self, state: "StateAPI") -> None:
def __init__(
self,
state: "StateAPI",
message: MessageAPI,
transaction_context: TransactionContextAPI,
) -> None:
"""
Instantiate the computation.
"""
...

@abstractmethod
Expand All @@ -1890,6 +1909,16 @@ def _configure_gas_meter(self) -> GasMeterAPI:
"""
...

# -- convenience -- #
@property
@abstractmethod
def is_origin_computation(self) -> bool:
"""
Return ``True`` if this computation is the outermost computation at
``depth == 0``.
"""
...

# -- error handling -- #
@property
@abstractmethod
Expand Down Expand Up @@ -2100,41 +2129,6 @@ def get_opcode_fn(self, opcode: int) -> OpcodeAPI:
"""
...


class MessageComputationAPI(
ComputationAPI,
ContextManager['MessageComputationAPI'],
):
"""
The base abstract class for all execution *message* computations.
"""

msg: MessageAPI
transaction_context: TransactionContextAPI

@abstractmethod
def __init__(
self,
state: "StateAPI",
message: MessageAPI,
transaction_context: TransactionContextAPI,
) -> None:
"""
Instantiate the message computation.
"""
...

# -- convenience -- #
@property
@abstractmethod
def is_origin_computation(self) -> bool:
"""
Return ``True`` if this message computation is the outermost computation at
``depth == 0``. Since EOF computations cannot exist without a message, this
is solely an inherent property of message computations.
"""
...

# -- runtime operations -- #
@abstractmethod
def prepare_child_message(self,
Expand All @@ -2145,34 +2139,34 @@ def prepare_child_message(self,
code: bytes,
**kwargs: Any) -> MessageAPI:
"""
Helper method for creating a child message computation.
Helper method for creating a child computation.
"""
...

@abstractmethod
def apply_child_message_computation(
def apply_child_computation(
self,
child_msg: MessageAPI,
) -> "MessageComputationAPI":
) -> "ComputationAPI":
"""
Apply the vm message ``child_msg`` as a child message computation.
Apply the vm message ``child_msg`` as a child computation.
"""
...

@abstractmethod
def generate_child_message_computation(
def generate_child_computation(
self,
child_msg: MessageAPI,
) -> "MessageComputationAPI":
) -> "ComputationAPI":
"""
Generate a child message computation from the given ``child_msg``.
Generate a child computation from the given ``child_msg``.
"""
...

@abstractmethod
def add_child_message_computation(
def add_child_computation(
self,
child_message_computation: "MessageComputationAPI",
child_computation: "ComputationAPI",
) -> None:
"""
Add the given ``child_computation``.
Expand Down Expand Up @@ -2235,7 +2229,7 @@ def apply_message(
state: "StateAPI",
message: MessageAPI,
transaction_context: TransactionContextAPI,
) -> "MessageComputationAPI":
) -> "ComputationAPI":
"""
Execute a VM message. This is where the VM-specific call logic exists.
"""
Expand All @@ -2244,10 +2238,11 @@ def apply_message(
@classmethod
@abstractmethod
def apply_create_message(
cls,
state: 'StateAPI',
message: MessageAPI,
transaction_context: TransactionContextAPI) -> 'MessageComputationAPI':
cls,
state: "StateAPI",
message: MessageAPI,
transaction_context: TransactionContextAPI,
) -> "ComputationAPI":
"""
Execute a VM message to create a new contract. This is where the VM-specific
create logic exists.
Expand All @@ -2261,7 +2256,7 @@ def apply_computation(
state: 'StateAPI',
message: MessageAPI,
transaction_context: TransactionContextAPI,
) -> 'MessageComputationAPI':
) -> "ComputationAPI":
"""
Execute the logic within the message: Either run the precompile, or
step through each opcode. Generally, the only VM-specific logic is for
Expand Down Expand Up @@ -2669,9 +2664,9 @@ def __init__(self, vm_state: 'StateAPI') -> None:
...

@abstractmethod
def __call__(self, transaction: SignedTransactionAPI) -> 'MessageComputationAPI':
def __call__(self, transaction: SignedTransactionAPI) -> "ComputationAPI":
"""
Execute the ``transaction`` and return a :class:`eth.abc.MessageComputationAPI`.
Execute the ``transaction`` and return a :class:`eth.abc.ComputationAPI`.
"""
...

Expand All @@ -2693,7 +2688,7 @@ def build_evm_message(self, transaction: SignedTransactionAPI) -> MessageAPI:
@abstractmethod
def build_computation(self,
message: MessageAPI,
transaction: SignedTransactionAPI) -> 'MessageComputationAPI':
transaction: SignedTransactionAPI) -> "ComputationAPI":
"""
Apply the ``message`` to the VM and use the given ``transaction`` to
retrieve the context from.
Expand All @@ -2704,7 +2699,7 @@ def build_computation(self,
@abstractmethod
def finalize_computation(self,
transaction: SignedTransactionAPI,
computation: 'MessageComputationAPI') -> 'MessageComputationAPI':
computation: "ComputationAPI") -> "ComputationAPI":
"""
Finalize the ``transaction``.
"""
Expand Down Expand Up @@ -2734,7 +2729,7 @@ class StateAPI(ConfigurableAPI):
Each :class:`~eth.abc.StateAPI` class must be configured with:
- ``message_computation_class``: The :class:`~eth.abc.MessageComputationAPI` class for
- ``computation_class``: The :class:`~eth.abc.ComputationAPI` class for
vm execution.
- ``transaction_context_class``: The :class:`~eth.abc.TransactionContextAPI`
class for vm execution.
Expand All @@ -2744,7 +2739,7 @@ class for vm execution.
#
execution_context: ExecutionContextAPI

message_computation_class: Type[MessageComputationAPI]
computation_class: Type[ComputationAPI]
transaction_context_class: Type[TransactionContextAPI]
account_db_class: Type[AccountDatabaseAPI]
transaction_executor_class: Type[TransactionExecutorAPI] = None
Expand Down Expand Up @@ -3103,7 +3098,7 @@ def get_ancestor_hash(self, block_number: BlockNumber) -> Hash32:
@abstractmethod
def get_computation(self,
message: MessageAPI,
transaction_context: TransactionContextAPI) -> MessageComputationAPI:
transaction_context: TransactionContextAPI) -> ComputationAPI:
"""
Return a computation instance for the given `message` and `transaction_context`
"""
Expand All @@ -3128,7 +3123,7 @@ def get_transaction_context_class(cls) -> Type[TransactionContextAPI]:
def apply_transaction(
self,
transaction: SignedTransactionAPI,
) -> MessageComputationAPI:
) -> ComputationAPI:
"""
Apply transaction to the vm state
Expand All @@ -3148,7 +3143,7 @@ def get_transaction_executor(self) -> TransactionExecutorAPI:
def costless_execute_transaction(
self,
transaction: SignedTransactionAPI,
) -> MessageComputationAPI:
) -> ComputationAPI:
"""
Execute the given ``transaction`` with a gas price of ``0``.
"""
Expand Down Expand Up @@ -3318,7 +3313,7 @@ def transaction_applied_hook(
transactions: Sequence[SignedTransactionAPI],
base_header: BlockHeaderAPI,
partial_header: BlockHeaderAPI,
computation: MessageComputationAPI,
computation: ComputationAPI,
receipt: ReceiptAPI) -> None:
"""
A hook for a subclass to use as a way to note that a transaction was applied.
Expand All @@ -3334,7 +3329,7 @@ def transaction_applied_hook(
def apply_transaction(self,
header: BlockHeaderAPI,
transaction: SignedTransactionAPI
) -> Tuple[ReceiptAPI, MessageComputationAPI]:
) -> Tuple[ReceiptAPI, ComputationAPI]:
"""
Apply the transaction to the current block. This is a wrapper around
:func:`~eth.vm.state.State.apply_transaction` with some extra orchestration logic.
Expand Down Expand Up @@ -3365,7 +3360,7 @@ def execute_bytecode(self,
value: int,
data: bytes,
code: bytes,
code_address: Address = None) -> MessageComputationAPI:
code_address: Address = None) -> ComputationAPI:
"""
Execute raw bytecode in the context of the current state of
the virtual machine. Note that this skips over some of the logic
Expand All @@ -3377,8 +3372,8 @@ def execute_bytecode(self,
- others...
For other potential surprises, check the implementation differences
between :meth:`MessageComputationAPI.apply_computation` and
:meth:`MessageComputationAPI.apply_message`. (depending on the VM fork)
between :meth:`ComputationAPI.apply_computation` and
:meth:`ComputationAPI.apply_message`. (depending on the VM fork)
"""
...

Expand All @@ -3387,7 +3382,7 @@ def apply_all_transactions(
self,
transactions: Sequence[SignedTransactionAPI],
base_header: BlockHeaderAPI
) -> Tuple[BlockHeaderAPI, Tuple[ReceiptAPI, ...], Tuple[MessageComputationAPI, ...]]:
) -> Tuple[BlockHeaderAPI, Tuple[ReceiptAPI, ...], Tuple[ComputationAPI, ...]]:
"""
Determine the results of applying all transactions to the base header.
This does *not* update the current block or header of the VM.
Expand All @@ -3411,7 +3406,7 @@ def apply_all_withdrawals(self, withdrawals: Sequence[WithdrawalAPI]) -> None:
def make_receipt(self,
base_header: BlockHeaderAPI,
transaction: SignedTransactionAPI,
computation: MessageComputationAPI,
computation: ComputationAPI,
state: StateAPI) -> ReceiptAPI:
"""
Generate the receipt resulting from applying the transaction.
Expand Down Expand Up @@ -4045,7 +4040,7 @@ def build_block_with_transactions_and_withdrawals(
transactions: Tuple[SignedTransactionAPI, ...],
parent_header: BlockHeaderAPI = None,
withdrawals: Tuple[WithdrawalAPI, ...] = None,
) -> Tuple[BlockAPI, Tuple[ReceiptAPI, ...], Tuple[MessageComputationAPI, ...]]:
) -> Tuple[BlockAPI, Tuple[ReceiptAPI, ...], Tuple[ComputationAPI, ...]]:
"""
Generate a block with the provided transactions. This does *not* import
that block into your chain. If you want this new block in your chain,
Expand Down Expand Up @@ -4271,7 +4266,7 @@ def mine_all(
*args: Any,
parent_header: BlockHeaderAPI = None,
**kwargs: Any,
) -> Tuple[BlockImportResult, Tuple[ReceiptAPI, ...], Tuple[MessageComputationAPI, ...]]:
) -> Tuple[BlockImportResult, Tuple[ReceiptAPI, ...], Tuple[ComputationAPI, ...]]:
"""
Build a block with the given transactions, and mine it.
Expand All @@ -4285,7 +4280,7 @@ def mine_all(
@abstractmethod
def apply_transaction(self,
transaction: SignedTransactionAPI
) -> Tuple[BlockAPI, ReceiptAPI, MessageComputationAPI]:
) -> Tuple[BlockAPI, ReceiptAPI, ComputationAPI]:
"""
Apply the transaction to the current tip block.
Expand Down
Loading

0 comments on commit a1ef2b7

Please sign in to comment.