Skip to content
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
34 changes: 0 additions & 34 deletions packages/evm/src/eof/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const EOFError = {
MAX_STACK_HEIGHT: 'expected maxStackHeight',
MAX_STACK_HEIGHT_LIMIT: 'stack height limit of 1024 exceeded: ',
MIN_CODE_SECTIONS: 'should have at least 1 code section',
MAX_CODE_SECTIONS: 'can have at most 1024 code sections',
CODE_SECTION: 'expected a code section',
DATA_SECTION: 'Expected data section',
CONTAINER_SECTION: 'expected a container section',
Expand Down Expand Up @@ -66,39 +65,6 @@ export const EOFError = {
RETURN_STACK_OVERFLOW: 'Return stack overflow',
INVALID_EXTCALL_TARGET: 'invalid extcall target: address > 20 bytes',
INVALID_RETURN_CONTRACT_DATA_SIZE: 'invalid RETURNCONTRACT: data size lower than expected',
INVALID_EOF_FORMAT: 'invalid EOF format',
} as const

export type SimpleErrors = (typeof SimpleErrors)[keyof typeof SimpleErrors]

export const SimpleErrors = {
MIN_CONTAINER_SIZE: 'err: container size less than minimum valid size',
INVALID_CONTAINER_SIZE: 'err: invalid container size',
TYPE_SIZE: 'err: type section size invalid',
CODE0_MSH: 'err: computed max stack height for code section 0 does not match expect',
UNDERFLOW: 'err: stack underflow',
CODE0_IO: 'err: input and output of first code section must be 0',
VERIFY_UINT: 'Uint does not match expected value ',
VERIFY_BYTES: 'Bytes do not match expected value',
INVALID_TYPE_SIZE: 'err: type section invalid',
CODE_SIZE: 'missing code size',
CODE_SECTION_SIZE: 'code section should be at least one byte',
INVALID_CODE_SIZE: 'code size does not match type size',
DATA_SIZE: 'missing data size',
TYPE_SECTIONS: 'need to have a type section for each code section',
INPUTS: 'expected inputs',
OUTPUTS: 'expected outputs',
MAX_INPUTS: 'inputs exceeds 127, the maximum, got: ',
MAX_OUTPUTS: 'outputs exceeds 127, the maximum, got: ',
CODE0_INPUTS: 'first code section should have 0 inputs',
CODE0_OUTPUTS: 'first code section should have 0 outputs',
MAX_STACK_HEIGHT: 'expected maxStackHeight',
MAX_STACK_HEIGHT_LIMIT: 'stack height limit of 1024 exceeded: ',
MIN_CODE_SECTIONS: 'should have at least 1 code section',
MAX_CODE_SECTIONS: 'can have at most 1024 code sections',
CODE_SECTION: 'expected a code section',
DATA_SECTION: 'Expected data section',
DANGLING_BYTES: 'got dangling bytes in body',
} as const

export function validationErrorMsg(type: EOFError, ...args: any) {
Expand Down
10 changes: 5 additions & 5 deletions packages/evm/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export type EVMErrorType = (typeof EVMErrorMessages)[keyof typeof EVMErrorMessages]

export const EVMErrorMessages = {
export const EVMErrorTypeString = 'EVMError'

const EVMErrorMessages = {
OUT_OF_GAS: 'out of gas',
CODESTORE_OUT_OF_GAS: 'code store out of gas',
CODESIZE_EXCEEDS_MAXIMUM: 'code size to deposit exceeds maximum code size',
Expand All @@ -17,9 +19,6 @@ export const EVMErrorMessages = {
REFUND_EXHAUSTED: 'refund exhausted',
VALUE_OVERFLOW: 'value overflow',
INSUFFICIENT_BALANCE: 'insufficient balance',
INVALID_BEGINSUB: 'invalid BEGINSUB',
INVALID_RETURNSUB: 'invalid RETURNSUB',
INVALID_JUMPSUB: 'invalid JUMPSUB',
INVALID_BYTECODE_RESULT: 'invalid bytecode deployed',
INITCODE_SIZE_VIOLATION: 'initcode exceeds max initcode size',
INVALID_INPUT_LENGTH: 'invalid input length',
Expand All @@ -37,9 +36,10 @@ export const EVMErrorMessages = {
export class EVMError {
error: EVMErrorType
errorType: string
static errorMessages: Record<keyof typeof EVMErrorMessages, EVMErrorType> = EVMErrorMessages

constructor(error: EVMErrorType) {
this.error = error
this.errorType = 'EVMError'
this.errorType = EVMErrorTypeString
}
}
30 changes: 15 additions & 15 deletions packages/evm/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import { FORMAT } from './eof/constants.ts'
import { isEOF } from './eof/util.ts'
import { EVMError, EVMErrorMessages } from './errors.ts'
import { EVMError } from './errors.ts'
import { Interpreter } from './interpreter.ts'
import { Journal } from './journal.ts'
import { EVMPerformanceLogger } from './logger.ts'
Expand Down Expand Up @@ -64,43 +64,43 @@
return {
returnValue: new Uint8Array(0),
executionGasUsed: gasLimit,
exceptionError: new EVMError(EVMErrorMessages.OUT_OF_GAS),
exceptionError: new EVMError(EVMError.errorMessages.OUT_OF_GAS),
}
}
// CodeDeposit OOG Result
export function COOGResult(gasUsedCreateCode: bigint): ExecResult {
return {
returnValue: new Uint8Array(0),
executionGasUsed: gasUsedCreateCode,
exceptionError: new EVMError(EVMErrorMessages.CODESTORE_OUT_OF_GAS),
exceptionError: new EVMError(EVMError.errorMessages.CODESTORE_OUT_OF_GAS),
}
}

export function INVALID_BYTECODE_RESULT(gasLimit: bigint): ExecResult {
return {
returnValue: new Uint8Array(0),
executionGasUsed: gasLimit,
exceptionError: new EVMError(EVMErrorMessages.INVALID_BYTECODE_RESULT),
exceptionError: new EVMError(EVMError.errorMessages.INVALID_BYTECODE_RESULT),

Check warning on line 83 in packages/evm/src/evm.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/evm.ts#L83

Added line #L83 was not covered by tests
}
}

export function INVALID_EOF_RESULT(gasLimit: bigint): ExecResult {
return {
returnValue: new Uint8Array(0),
executionGasUsed: gasLimit,
exceptionError: new EVMError(EVMErrorMessages.INVALID_EOF_FORMAT),
exceptionError: new EVMError(EVMError.errorMessages.INVALID_EOF_FORMAT),

Check warning on line 91 in packages/evm/src/evm.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/evm.ts#L91

Added line #L91 was not covered by tests
}
}

export function CodesizeExceedsMaximumError(gasUsed: bigint): ExecResult {
return {
returnValue: new Uint8Array(0),
executionGasUsed: gasUsed,
exceptionError: new EVMError(EVMErrorMessages.CODESIZE_EXCEEDS_MAXIMUM),
exceptionError: new EVMError(EVMError.errorMessages.CODESIZE_EXCEEDS_MAXIMUM),
}
}

export function EvmErrorResult(error: EVMError, gasUsed: bigint): ExecResult {
export function EVMErrorResult(error: EVMError, gasUsed: bigint): ExecResult {
return {
returnValue: new Uint8Array(0),
executionGasUsed: gasUsed,
Expand Down Expand Up @@ -508,7 +508,7 @@
createdAddress: message.to,
execResult: {
returnValue: new Uint8Array(0),
exceptionError: new EVMError(EVMErrorMessages.INITCODE_SIZE_VIOLATION),
exceptionError: new EVMError(EVMError.errorMessages.INITCODE_SIZE_VIOLATION),
executionGasUsed: message.gasLimit,
},
}
Expand Down Expand Up @@ -567,7 +567,7 @@
createdAddress: message.to,
execResult: {
returnValue: new Uint8Array(0),
exceptionError: new EVMError(EVMErrorMessages.CREATE_COLLISION),
exceptionError: new EVMError(EVMError.errorMessages.CREATE_COLLISION),

Check warning on line 570 in packages/evm/src/evm.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/evm.ts#L570

Added line #L570 was not covered by tests
executionGasUsed: message.gasLimit,
},
}
Expand Down Expand Up @@ -871,8 +871,8 @@
let gasUsed = message.gasLimit - interpreterRes.runState!.gasLeft
if (interpreterRes.exceptionError) {
if (
interpreterRes.exceptionError.error !== EVMErrorMessages.REVERT &&
interpreterRes.exceptionError.error !== EVMErrorMessages.INVALID_EOF_FORMAT
interpreterRes.exceptionError.error !== EVMError.errorMessages.REVERT &&
interpreterRes.exceptionError.error !== EVMError.errorMessages.INVALID_EOF_FORMAT
) {
gasUsed = message.gasLimit
}
Expand Down Expand Up @@ -1019,7 +1019,7 @@
// There is one exception: if the CODESTORE_OUT_OF_GAS error is thrown
// (this only happens the Frontier/Chainstart fork)
// then the error is dismissed
if (err && err.error !== EVMErrorMessages.CODESTORE_OUT_OF_GAS) {
if (err && err.error !== EVMError.errorMessages.CODESTORE_OUT_OF_GAS) {
result.execResult.selfdestruct = new Set()
result.execResult.createdAddresses = new Set()
result.execResult.gasRefund = BIGINT_0
Expand All @@ -1028,7 +1028,7 @@
err &&
!(
this.common.hardfork() === Hardfork.Chainstart &&
err.error === EVMErrorMessages.CODESTORE_OUT_OF_GAS
err.error === EVMError.errorMessages.CODESTORE_OUT_OF_GAS
)
) {
result.execResult.logs = []
Expand Down Expand Up @@ -1159,7 +1159,7 @@
protected async _reduceSenderBalance(account: Account, message: Message): Promise<void> {
account.balance -= message.value
if (account.balance < BIGINT_0) {
throw new EVMError(EVMErrorMessages.INSUFFICIENT_BALANCE)
throw new EVMError(EVMError.errorMessages.INSUFFICIENT_BALANCE)
}
const result = this.journal.putAccount(message.caller, account)
if (this.DEBUG) {
Expand All @@ -1171,7 +1171,7 @@
protected async _addToBalance(toAccount: Account, message: MessageWithTo): Promise<void> {
const newBalance = toAccount.balance + message.value
if (newBalance > MAX_INTEGER) {
throw new EVMError(EVMErrorMessages.VALUE_OVERFLOW)
throw new EVMError(EVMError.errorMessages.VALUE_OVERFLOW)

Check warning on line 1174 in packages/evm/src/evm.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/evm.ts#L1174

Added line #L1174 was not covered by tests
}
toAccount.balance = newBalance
// putAccount as the nonce may have changed for contract creation
Expand Down
3 changes: 1 addition & 2 deletions packages/evm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EOFContainer, validateEOF } from './eof/container.ts'
import { EVMError, EVMErrorMessages } from './errors.ts'
import { EVMError } from './errors.ts'
import { EVM } from './evm.ts'
import { Message } from './message.ts'
import { getOpcodesForHF } from './opcodes/index.ts'
Expand Down Expand Up @@ -47,7 +47,6 @@ export {
EOFContainer,
EVM,
EVMError,
EVMErrorMessages,
EVMMockBlockchain,
getActivePrecompiles,
getOpcodesForHF,
Expand Down
38 changes: 19 additions & 19 deletions packages/evm/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { EOFContainerMode, validateEOF } from './eof/container.ts'
import { setupEOF } from './eof/setup.ts'
import { ContainerSectionType } from './eof/verify.ts'
import { EVMError, EVMErrorMessages } from './errors.ts'
import { EVMError, EVMErrorTypeString } from './errors.ts'
import { type EVMPerformanceLogger, type Timer } from './logger.ts'
import { Memory } from './memory.ts'
import { Message } from './message.ts'
Expand Down Expand Up @@ -219,14 +219,14 @@
// Bytecode contains invalid EOF magic byte
return {
runState: this._runState,
exceptionError: new EVMError(EVMErrorMessages.INVALID_BYTECODE_RESULT),
exceptionError: new EVMError(EVMError.errorMessages.INVALID_BYTECODE_RESULT),

Check warning on line 222 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L222

Added line #L222 was not covered by tests
}
}
if (code[2] !== VERSION) {
// Bytecode contains invalid EOF version number
return {
runState: this._runState,
exceptionError: new EVMError(EVMErrorMessages.INVALID_EOF_FORMAT),
exceptionError: new EVMError(EVMError.errorMessages.INVALID_EOF_FORMAT),

Check warning on line 229 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L229

Added line #L229 was not covered by tests
}
}
this._runState.code = code
Expand All @@ -239,7 +239,7 @@
} catch {
return {
runState: this._runState,
exceptionError: new EVMError(EVMErrorMessages.INVALID_EOF_FORMAT), // TODO: verify if all gas should be consumed
exceptionError: new EVMError(EVMError.errorMessages.INVALID_EOF_FORMAT), // TODO: verify if all gas should be consumed

Check warning on line 242 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L242

Added line #L242 was not covered by tests
}
}

Expand All @@ -256,7 +256,7 @@
// Trying to deploy an invalid EOF container
return {
runState: this._runState,
exceptionError: new EVMError(EVMErrorMessages.INVALID_EOF_FORMAT), // TODO: verify if all gas should be consumed
exceptionError: new EVMError(EVMError.errorMessages.INVALID_EOF_FORMAT), // TODO: verify if all gas should be consumed

Check warning on line 259 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L259

Added line #L259 was not covered by tests
}
}
}
Expand Down Expand Up @@ -336,11 +336,11 @@
this.performanceLogger.unpauseTimer(overheadTimer)
}
// re-throw on non-VM errors
if (!('errorType' in e && e.errorType === 'EVMError')) {
if (!('errorType' in e && e.errorType === EVMErrorTypeString)) {
throw e
}
// STOP is not an exception
if (e.error !== EVMErrorMessages.STOP) {
if (e.error !== EVMError.errorMessages.STOP) {
err = e
}
break
Expand Down Expand Up @@ -403,7 +403,7 @@

// Check for invalid opcode
if (opInfo.isInvalid) {
throw new EVMError(EVMErrorMessages.INVALID_OPCODE)
throw new EVMError(EVMError.errorMessages.INVALID_OPCODE)
}

// Reduce opcode's base fee
Expand Down Expand Up @@ -563,7 +563,7 @@
}
if (this._runState.gasLeft < BIGINT_0) {
this._runState.gasLeft = BIGINT_0
trap(EVMErrorMessages.OUT_OF_GAS)
trap(EVMError.errorMessages.OUT_OF_GAS)
}
}

Expand Down Expand Up @@ -599,7 +599,7 @@
this._runState.gasRefund -= amount
if (this._runState.gasRefund < BIGINT_0) {
this._runState.gasRefund = BIGINT_0
trap(EVMErrorMessages.REFUND_EXHAUSTED)
trap(EVMError.errorMessages.REFUND_EXHAUSTED)

Check warning on line 602 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L602

Added line #L602 was not covered by tests
}
}

Expand Down Expand Up @@ -681,7 +681,7 @@
*/
finish(returnData: Uint8Array): void {
this._result.returnValue = returnData
trap(EVMErrorMessages.STOP)
trap(EVMError.errorMessages.STOP)
}

/**
Expand All @@ -691,7 +691,7 @@
*/
revert(returnData: Uint8Array): void {
this._result.returnValue = returnData
trap(EVMErrorMessages.REVERT)
trap(EVMError.errorMessages.REVERT)

Check warning on line 694 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L694

Added line #L694 was not covered by tests
}

/**
Expand Down Expand Up @@ -1016,7 +1016,7 @@
if (
results.execResult.returnValue !== undefined &&
(!results.execResult.exceptionError ||
results.execResult.exceptionError.error === EVMErrorMessages.REVERT)
results.execResult.exceptionError.error === EVMError.errorMessages.REVERT)
) {
this._runState.returnBytes = results.execResult.returnValue
}
Expand Down Expand Up @@ -1117,14 +1117,14 @@
// Set return buffer in case revert happened
if (
results.execResult.exceptionError &&
results.execResult.exceptionError.error === EVMErrorMessages.REVERT
results.execResult.exceptionError.error === EVMError.errorMessages.REVERT

Check warning on line 1120 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L1120

Added line #L1120 was not covered by tests
) {
this._runState.returnBytes = results.execResult.returnValue
}

if (
!results.execResult.exceptionError ||
results.execResult.exceptionError.error === EVMErrorMessages.CODESTORE_OUT_OF_GAS
results.execResult.exceptionError.error === EVMError.errorMessages.CODESTORE_OUT_OF_GAS

Check warning on line 1127 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L1127

Added line #L1127 was not covered by tests
) {
for (const addressToSelfdestructHex of selfdestruct) {
this._result.selfdestruct.add(addressToSelfdestructHex)
Expand Down Expand Up @@ -1230,19 +1230,19 @@
})
}

trap(EVMErrorMessages.STOP)
trap(EVMError.errorMessages.STOP)
}

/**
* Creates a new log in the current environment.
*/
log(data: Uint8Array, numberOfTopics: number, topics: Uint8Array[]): void {
if (numberOfTopics < 0 || numberOfTopics > 4) {
trap(EVMErrorMessages.OUT_OF_RANGE)
trap(EVMError.errorMessages.OUT_OF_RANGE)

Check warning on line 1241 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L1241

Added line #L1241 was not covered by tests
}

if (topics.length !== numberOfTopics) {
trap(EVMErrorMessages.INTERNAL_ERROR)
trap(EVMError.errorMessages.INTERNAL_ERROR)

Check warning on line 1245 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L1245

Added line #L1245 was not covered by tests
}

const log: Log = [this._env.address.bytes, topics, data]
Expand All @@ -1259,7 +1259,7 @@
} else {
// EOF mode, call was either EXTCALL / EXTDELEGATECALL / EXTSTATICCALL
if (results.execResult.exceptionError !== undefined) {
if (results.execResult.exceptionError.error === EVMErrorMessages.REVERT) {
if (results.execResult.exceptionError.error === EVMError.errorMessages.REVERT) {

Check warning on line 1262 in packages/evm/src/interpreter.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/interpreter.ts#L1262

Added line #L1262 was not covered by tests
// Revert
return BIGINT_1
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/evm/src/opcodes/EIP2200.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { equalsBytes } from '@ethereumjs/util'

import { EVMErrorMessages } from '../errors.ts'
import { EVMError } from '../errors.ts'

import { adjustSstoreGasEIP2929 } from './EIP2929.ts'
import { trap } from './util.ts'
Expand All @@ -27,7 +27,7 @@
) {
// Fail if not enough gas is left
if (runState.interpreter.getGasLeft() <= common.param('sstoreSentryEIP2200Gas')) {
trap(EVMErrorMessages.OUT_OF_GAS)
trap(EVMError.errorMessages.OUT_OF_GAS)

Check warning on line 30 in packages/evm/src/opcodes/EIP2200.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/opcodes/EIP2200.ts#L30

Added line #L30 was not covered by tests
}

// Noop
Expand Down
Loading
Loading