Skip to content
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

EVM: error handling #3714

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

EVM: error handling #3714

wants to merge 2 commits into from

Conversation

jochem-brouwer
Copy link
Member

Part of #3712

Excerpt from evm/errors.ts:

export enum EvmErrorCode {
  UNSUPPORTED_FEATURE = 'EVM_ERROR_UNSUPPORTED_FEATURE',
  RUNTIME_ERROR = 'EVM_ERROR_RUNTIME_ERROR',
}

type EvmRuntimeErrorType = {
  code: EvmErrorCode.RUNTIME_ERROR
  reason: RuntimeErrorMessage | EOFError
} & (
  | { reason: RuntimeErrorMessage.REVERT; revertBytes: Uint8Array }
  | { reason: Exclude<RuntimeErrorMessage, RuntimeErrorMessage.REVERT> | EOFError }
)

export type EvmErrorType = { code: EvmErrorCode.UNSUPPORTED_FEATURE } | EvmRuntimeErrorType

export class EvmError extends EthereumJSError<EvmErrorType> {
  constructor(type: EvmErrorType, message?: string) {
    super(type, message)
  }
}

This is a Proof-of-Concept (I still need to do some cycles to clean this up though) which shows that we now have two EVM error types: a UNSUPPORTED_FEATURE or a RUNTIME_ERROR. In case of a RUNTIME_ERROR, typescript now demands a reason field in the error (this could for instance be "stack overflow", "stack underflow", etc.

What is nice: for the REVERT reason it demands a revertBytes field, which is actually the reverted bytes in case the EVM runs into a revert (from these bytes error messages can be derived, such as solidity revert strings!).

Errors now look like this:

image

(I had added the error.code field but I think we should keep it at error.type.code).

The error handler (taken / inspired from lodestar: https://github.com/ChainSafe/lodestar/blob/unstable/packages/utils/src/errors.ts) allows to add custom error classes (see EvmError class in this case, can add extra constructor arguments for environment situation), and also has a message argument which allows for custom message strings.

Note: super WIP. I am not completely satisfied yet with the results, because now EVM has a huge code bloat due to instantiating these error messages. I have to find a way to go back to the "old versions" (trap(ERROR.OUT_OF_GAS)) but still remaining type-safety (for the REVERT error string for instance).

Note, tests will likely fail because of the updated error format.

@jochem-brouwer jochem-brouwer added PR state: WIP type: test all hardforks This special label enables VM state and blockchain tests for all hardforks on the respective PR. package: evm labels Oct 1, 2024
@Maliksb11
Copy link

Okay 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: evm PR state: WIP type: test all hardforks This special label enables VM state and blockchain tests for all hardforks on the respective PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants