Skip to content

Commit f10cb80

Browse files
authored
evm: consistent error message names (#4033)
* evm: singular evm error message * evm: eof err naming
1 parent 47529d0 commit f10cb80

File tree

6 files changed

+172
-164
lines changed

6 files changed

+172
-164
lines changed

packages/evm/src/eof/container.ts

+39-39
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
TYPE_MIN,
2323
VERSION,
2424
} from './constants.ts'
25-
import { EOFError, validationError } from './errors.ts'
25+
import { EOFErrorMessage, validationError } from './errors.ts'
2626
import { ContainerSectionType, verifyCode } from './verify.ts'
2727

2828
import type { EVM } from '../evm.ts'
@@ -64,7 +64,7 @@ class StreamReader {
6464
readBytes(amount: number, errorStr?: string) {
6565
const end = this.ptr + amount
6666
if (end > this.data.length) {
67-
validationError(EOFError.OUT_OF_BOUNDS, this.ptr, errorStr)
67+
validationError(EOFErrorMessage.OUT_OF_BOUNDS, this.ptr, errorStr)
6868
}
6969
const ptr = this.ptr
7070
this.ptr += amount
@@ -78,7 +78,7 @@ class StreamReader {
7878
*/
7979
readUint(errorStr?: string) {
8080
if (this.ptr >= this.data.length) {
81-
validationError(EOFError.OUT_OF_BOUNDS, this.ptr, errorStr)
81+
validationError(EOFErrorMessage.OUT_OF_BOUNDS, this.ptr, errorStr)
8282
}
8383
return this.data[this.ptr++]
8484
}
@@ -91,7 +91,7 @@ class StreamReader {
9191
*/
9292
verifyUint(expect: number, errorStr?: string) {
9393
if (this.readUint() !== expect) {
94-
validationError(EOFError.VERIFY_UINT, this.ptr - 1, errorStr)
94+
validationError(EOFErrorMessage.VERIFY_UINT, this.ptr - 1, errorStr)
9595
}
9696
}
9797

@@ -103,7 +103,7 @@ class StreamReader {
103103
readUint16(errorStr?: string) {
104104
const end = this.ptr + 2
105105
if (end > this.data.length) {
106-
validationError(EOFError.OUT_OF_BOUNDS, this.ptr, errorStr)
106+
validationError(EOFErrorMessage.OUT_OF_BOUNDS, this.ptr, errorStr)
107107
}
108108
const ptr = this.ptr
109109
this.ptr += 2
@@ -155,41 +155,41 @@ class EOFHeader {
155155
}
156156
const stream = new StreamReader(input)
157157
// Verify that the header starts with 0xEF0001
158-
stream.verifyUint(FORMAT, EOFError.FORMAT)
159-
stream.verifyUint(MAGIC, EOFError.MAGIC)
160-
stream.verifyUint(VERSION, EOFError.VERSION)
158+
stream.verifyUint(FORMAT, EOFErrorMessage.FORMAT)
159+
stream.verifyUint(MAGIC, EOFErrorMessage.MAGIC)
160+
stream.verifyUint(VERSION, EOFErrorMessage.VERSION)
161161
if (input.length < 15) {
162162
throw EthereumJSErrorWithoutCode('err: container size less than minimum valid size')
163163
}
164164
// Verify that the types section is present and its length is valid
165-
stream.verifyUint(KIND_TYPE, EOFError.KIND_TYPE)
166-
const typeSize = stream.readUint16(EOFError.TYPE_SIZE)
165+
stream.verifyUint(KIND_TYPE, EOFErrorMessage.KIND_TYPE)
166+
const typeSize = stream.readUint16(EOFErrorMessage.TYPE_SIZE)
167167
if (typeSize < TYPE_MIN) {
168-
validationError(EOFError.INVALID_TYPE_SIZE, typeSize)
168+
validationError(EOFErrorMessage.INVALID_TYPE_SIZE, typeSize)
169169
}
170170
if (typeSize % TYPE_DIVISOR !== 0) {
171-
validationError(EOFError.INVALID_TYPE_SIZE, typeSize)
171+
validationError(EOFErrorMessage.INVALID_TYPE_SIZE, typeSize)
172172
}
173173
if (typeSize > TYPE_MAX) {
174174
throw EthereumJSErrorWithoutCode(
175175
`err: number of code sections must not exceed 1024 (got ${typeSize})`,
176176
)
177177
}
178178
// Verify that the code section is present and its size is valid
179-
stream.verifyUint(KIND_CODE, EOFError.KIND_CODE)
180-
const codeSize = stream.readUint16(EOFError.CODE_SIZE)
179+
stream.verifyUint(KIND_CODE, EOFErrorMessage.KIND_CODE)
180+
const codeSize = stream.readUint16(EOFErrorMessage.CODE_SIZE)
181181
if (codeSize < CODE_MIN) {
182-
validationError(EOFError.MIN_CODE_SECTIONS)
182+
validationError(EOFErrorMessage.MIN_CODE_SECTIONS)
183183
}
184184
if (codeSize !== typeSize / TYPE_DIVISOR) {
185-
validationError(EOFError.TYPE_SECTIONS, typeSize / TYPE_DIVISOR, codeSize)
185+
validationError(EOFErrorMessage.TYPE_SECTIONS, typeSize / TYPE_DIVISOR, codeSize)
186186
}
187187
// Read the actual code sizes in the code section and verify that each section has the minimum size
188188
const codeSizes = []
189189
for (let i = 0; i < codeSize; i++) {
190-
const codeSectionSize = stream.readUint16(EOFError.CODE_SECTION)
190+
const codeSectionSize = stream.readUint16(EOFErrorMessage.CODE_SECTION)
191191
if (codeSectionSize < CODE_SIZE_MIN) {
192-
validationError(EOFError.CODE_SECTION_SIZE)
192+
validationError(EOFErrorMessage.CODE_SECTION_SIZE)
193193
}
194194
codeSizes.push(codeSectionSize)
195195
}
@@ -199,21 +199,21 @@ class EOFHeader {
199199
const containerSizes: number[] = []
200200
if (nextSection === KIND_CONTAINER) {
201201
// The optional container section is present, validate that the size is within bounds
202-
const containerSectionSize = stream.readUint16(EOFError.CONTAINER_SIZE)
202+
const containerSectionSize = stream.readUint16(EOFErrorMessage.CONTAINER_SIZE)
203203

204204
if (containerSectionSize < CONTAINER_MIN) {
205-
validationError(EOFError.CONTAINER_SECTION_SIZE)
205+
validationError(EOFErrorMessage.CONTAINER_SECTION_SIZE)
206206
}
207207
if (containerSectionSize > CONTAINER_MAX) {
208-
validationError(EOFError.CONTAINER_SECTION_SIZE)
208+
validationError(EOFErrorMessage.CONTAINER_SECTION_SIZE)
209209
}
210210

211211
// Read the actual container sections and validate that each section has the minimum size
212212
for (let i = 0; i < containerSectionSize; i++) {
213-
const containerSize = stream.readUint16(EOFError.CONTAINER_SECTION)
213+
const containerSize = stream.readUint16(EOFErrorMessage.CONTAINER_SECTION)
214214

215215
if (containerSize < CONTAINER_SIZE_MIN) {
216-
validationError(EOFError.CONTAINER_SECTION_MIN)
216+
validationError(EOFErrorMessage.CONTAINER_SECTION_MIN)
217217
}
218218

219219
containerSizes.push(containerSize)
@@ -224,15 +224,15 @@ class EOFHeader {
224224

225225
// Verify that the next section is of the data type
226226
if (nextSection !== KIND_DATA) {
227-
validationError(EOFError.KIND_DATA)
227+
validationError(EOFErrorMessage.KIND_DATA)
228228
}
229229

230230
this.dataSizePtr = stream.getPtr()
231231

232-
const dataSize = stream.readUint16(EOFError.DATA_SIZE)
232+
const dataSize = stream.readUint16(EOFErrorMessage.DATA_SIZE)
233233

234234
// Verify that the header ends with the TERMINATOR byte
235-
stream.verifyUint(TERMINATOR, EOFError.TERMINATOR)
235+
stream.verifyUint(TERMINATOR, EOFErrorMessage.TERMINATOR)
236236

237237
// Write all values to the header object
238238
this.typeSize = typeSize
@@ -327,25 +327,25 @@ class EOFBody {
327327
const typeSections: TypeSection[] = []
328328
// Read and parse each type section, and validate that the type section values are within valid bounds
329329
for (let i = 0; i < header.typeSize / 4; i++) {
330-
const inputs = stream.readUint(EOFError.INPUTS)
331-
const outputs = stream.readUint(EOFError.OUTPUTS)
332-
const maxStackHeight = stream.readUint16(EOFError.MAX_STACK_HEIGHT)
330+
const inputs = stream.readUint(EOFErrorMessage.INPUTS)
331+
const outputs = stream.readUint(EOFErrorMessage.OUTPUTS)
332+
const maxStackHeight = stream.readUint16(EOFErrorMessage.MAX_STACK_HEIGHT)
333333
if (i === 0) {
334334
if (inputs !== 0) {
335-
validationError(EOFError.CODE0_INPUTS)
335+
validationError(EOFErrorMessage.CODE0_INPUTS)
336336
}
337337
if (outputs !== 0x80) {
338-
validationError(EOFError.CODE0_OUTPUTS)
338+
validationError(EOFErrorMessage.CODE0_OUTPUTS)
339339
}
340340
}
341341
if (inputs > INPUTS_MAX) {
342-
validationError(EOFError.MAX_INPUTS, i, inputs)
342+
validationError(EOFErrorMessage.MAX_INPUTS, i, inputs)
343343
}
344344
if (outputs > OUTPUTS_MAX) {
345-
validationError(EOFError.MAX_OUTPUTS, i, outputs)
345+
validationError(EOFErrorMessage.MAX_OUTPUTS, i, outputs)
346346
}
347347
if (maxStackHeight > MAX_STACK_HEIGHT) {
348-
validationError(EOFError.MAX_STACK_HEIGHT_LIMIT, i, maxStackHeight)
348+
validationError(EOFErrorMessage.MAX_STACK_HEIGHT_LIMIT, i, maxStackHeight)
349349
}
350350
typeSections.push({
351351
inputs,
@@ -361,7 +361,7 @@ class EOFBody {
361361
const code = stream.readBytes(codeSize)
362362
codes.push(code)
363363
} catch {
364-
validationError(EOFError.CODE_SECTION, i)
364+
validationError(EOFErrorMessage.CODE_SECTION, i)
365365
}
366366
}
367367
// Write the entire code section to the entireCodeSection
@@ -374,7 +374,7 @@ class EOFBody {
374374
const container = stream.readBytes(containerSize)
375375
containers.push(container)
376376
} catch {
377-
validationError(EOFError.CONTAINER_SECTION, i)
377+
validationError(EOFErrorMessage.CONTAINER_SECTION, i)
378378
}
379379
}
380380

@@ -386,12 +386,12 @@ class EOFBody {
386386

387387
// Edge case: deployment code validation
388388
if (eofMode !== EOFContainerMode.Initmode && !dataSectionAllowedSmaller) {
389-
dataSection = stream.readBytes(header.dataSize, EOFError.DATA_SECTION)
389+
dataSection = stream.readBytes(header.dataSize, EOFErrorMessage.DATA_SECTION)
390390

391391
if (eofMode === EOFContainerMode.Default) {
392392
if (!stream.isAtEnd()) {
393393
// If there are dangling bytes in default container mode, this is invalid
394-
validationError(EOFError.DANGLING_BYTES)
394+
validationError(EOFErrorMessage.DANGLING_BYTES)
395395
}
396396
} else {
397397
// Tx init mode: the remaining bytes (if any) are used as CALLDATA in the EVM, in case of a Tx init
@@ -401,7 +401,7 @@ class EOFBody {
401401
dataSection = stream.readRemainder()
402402

403403
if (dataSection.length > header.dataSize) {
404-
validationError(EOFError.DANGLING_BYTES)
404+
validationError(EOFErrorMessage.DANGLING_BYTES)
405405
}
406406
}
407407

0 commit comments

Comments
 (0)