Skip to content

Commit ee42ec5

Browse files
authored
valueBoundaryCheck chores (#4083)
* Move test to more appropriate file and rename tested function * Remove addressed TODO comment
1 parent 8bbf0e2 commit ee42ec5

File tree

8 files changed

+61
-60
lines changed

8 files changed

+61
-60
lines changed

packages/tx/src/1559/tx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as EIP2718 from '../capabilities/eip2718.ts'
1313
import * as EIP2930 from '../capabilities/eip2930.ts'
1414
import * as Legacy from '../capabilities/legacy.ts'
1515
import { TransactionType, isAccessList } from '../types.ts'
16-
import { getBaseJSON, sharedConstructor, valueBoundaryCheck } from '../util/internal.ts'
16+
import { getBaseJSON, sharedConstructor, valueOverflowCheck } from '../util/internal.ts'
1717

1818
import { createFeeMarket1559Tx } from './constructors.ts'
1919

@@ -109,7 +109,7 @@ export class FeeMarket1559Tx
109109
this.maxFeePerGas = bytesToBigInt(toBytes(maxFeePerGas))
110110
this.maxPriorityFeePerGas = bytesToBigInt(toBytes(maxPriorityFeePerGas))
111111

112-
valueBoundaryCheck({
112+
valueOverflowCheck({
113113
maxFeePerGas: this.maxFeePerGas,
114114
maxPriorityFeePerGas: this.maxPriorityFeePerGas,
115115
})

packages/tx/src/2930/tx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as EIP2718 from '../capabilities/eip2718.ts'
1111
import * as EIP2930 from '../capabilities/eip2930.ts'
1212
import * as Legacy from '../capabilities/legacy.ts'
1313
import { TransactionType, isAccessList } from '../types.ts'
14-
import { getBaseJSON, sharedConstructor, valueBoundaryCheck } from '../util/internal.ts'
14+
import { getBaseJSON, sharedConstructor, valueOverflowCheck } from '../util/internal.ts'
1515

1616
import { createAccessList2930Tx } from './constructors.ts'
1717

@@ -105,7 +105,7 @@ export class AccessList2930Tx
105105

106106
this.gasPrice = bytesToBigInt(toBytes(gasPrice))
107107

108-
valueBoundaryCheck({ gasPrice: this.gasPrice })
108+
valueOverflowCheck({ gasPrice: this.gasPrice })
109109

110110
if (this.gasPrice * this.gasLimit > MAX_INTEGER) {
111111
const msg = Legacy.errorMsg(this, 'gasLimit * gasPrice cannot exceed MAX_INTEGER')

packages/tx/src/4844/tx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
getBaseJSON,
2222
sharedConstructor,
2323
validateNotArray,
24-
valueBoundaryCheck,
24+
valueOverflowCheck,
2525
} from '../util/internal.ts'
2626

2727
import { createBlob4844Tx } from './constructors.ts'
@@ -130,7 +130,7 @@ export class Blob4844Tx implements TransactionInterface<typeof TransactionType.B
130130
this.maxFeePerGas = bytesToBigInt(toBytes(maxFeePerGas))
131131
this.maxPriorityFeePerGas = bytesToBigInt(toBytes(maxPriorityFeePerGas))
132132

133-
valueBoundaryCheck({
133+
valueOverflowCheck({
134134
maxFeePerGas: this.maxFeePerGas,
135135
maxPriorityFeePerGas: this.maxPriorityFeePerGas,
136136
})

packages/tx/src/7702/tx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
getBaseJSON,
2323
sharedConstructor,
2424
validateNotArray,
25-
valueBoundaryCheck,
25+
valueOverflowCheck,
2626
} from '../util/internal.ts'
2727

2828
import { createEOACode7702Tx } from './constructors.ts'
@@ -131,7 +131,7 @@ export class EOACode7702Tx implements TransactionInterface<typeof TransactionTyp
131131
this.maxFeePerGas = bytesToBigInt(toBytes(maxFeePerGas))
132132
this.maxPriorityFeePerGas = bytesToBigInt(toBytes(maxPriorityFeePerGas))
133133

134-
valueBoundaryCheck({
134+
valueOverflowCheck({
135135
maxFeePerGas: this.maxFeePerGas,
136136
maxPriorityFeePerGas: this.maxPriorityFeePerGas,
137137
})

packages/tx/src/legacy/tx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { keccak256 } from 'ethereum-cryptography/keccak.js'
1515
import * as Legacy from '../capabilities/legacy.ts'
1616
import { paramsTx } from '../index.ts'
1717
import { Capability, TransactionType } from '../types.ts'
18-
import { getBaseJSON, sharedConstructor, valueBoundaryCheck } from '../util/internal.ts'
18+
import { getBaseJSON, sharedConstructor, valueOverflowCheck } from '../util/internal.ts'
1919

2020
import { createLegacyTx } from './constructors.ts'
2121

@@ -124,7 +124,7 @@ export class LegacyTx implements TransactionInterface<typeof TransactionType.Leg
124124
sharedConstructor(this, txData, opts)
125125

126126
this.gasPrice = bytesToBigInt(toBytes(txData.gasPrice))
127-
valueBoundaryCheck({ gasPrice: this.gasPrice })
127+
valueOverflowCheck({ gasPrice: this.gasPrice })
128128

129129
// Everything from BaseTransaction done here
130130
this.common.updateParams(opts.params ?? paramsTx) // TODO should this move higher?

packages/tx/src/util/internal.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ function checkMaxInitCodeSize(common: Common, length: number) {
6363
* Validates that an object with BigInt values cannot exceed the specified bit limit.
6464
* @param values Object containing string keys and BigInt values
6565
* @param bits Number of bits to check (64 or 256)
66-
* @param cannotEqual Pass true if the number also cannot equal one less the maximum value
66+
* @param cannotEqual Pass true if the number also cannot equal one less than the maximum value
6767
*/
68-
export function valueBoundaryCheck(
69-
// TODO: better method name
68+
export function valueOverflowCheck(
7069
values: { [key: string]: bigint | undefined },
7170
bits = 256,
7271
cannotEqual = false,
@@ -155,13 +154,13 @@ export function sharedConstructor(
155154
// Start validating the data
156155

157156
// Validate value/r/s
158-
valueBoundaryCheck({ value: tx.value, r: tx.r, s: tx.s })
157+
valueOverflowCheck({ value: tx.value, r: tx.r, s: tx.s })
159158

160159
// geth limits gasLimit to 2^64-1
161-
valueBoundaryCheck({ gasLimit: tx.gasLimit }, 64)
160+
valueOverflowCheck({ gasLimit: tx.gasLimit }, 64)
162161

163162
// EIP-2681 limits nonce to 2^64-1 (cannot equal 2^64-1)
164-
valueBoundaryCheck({ nonce: tx.nonce }, 64, true)
163+
valueOverflowCheck({ nonce: tx.nonce }, 64, true)
165164

166165
const createContract = tx.to === undefined || tx.to === null
167166
const allowUnlimitedInitCodeSize = opts.allowUnlimitedInitCodeSize ?? false

packages/tx/test/base.spec.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
22
import {
3-
MAX_INTEGER,
4-
MAX_UINT64,
53
SECP256K1_ORDER,
64
bytesToBigInt,
75
equalsBytes,
@@ -28,7 +26,6 @@ import {
2826
createLegacyTxFromRLP,
2927
paramsTx,
3028
} from '../src/index.ts'
31-
import { valueBoundaryCheck } from '../src/util/internal.ts'
3229

3330
import { eip1559TxsData } from './testData/eip1559txs.ts'
3431
import { eip2930TxsData } from './testData/eip2930txs.ts'
@@ -443,45 +440,4 @@ describe('[BaseTransaction]', () => {
443440
assert.strictEqual(tx.gasLimit, bytesToBigInt(bufferZero))
444441
assert.strictEqual(tx.nonce, bytesToBigInt(bufferZero))
445442
})
446-
447-
// TODO: move this to a different file (not part of base transaction anymore)
448-
it('valueBoundaryCheck()', () => {
449-
try {
450-
valueBoundaryCheck({ a: MAX_INTEGER }, 256, true)
451-
} catch (err: any) {
452-
assert.isTrue(
453-
err.message.includes('equal or exceed MAX_INTEGER'),
454-
'throws when value equals or exceeds MAX_INTEGER',
455-
)
456-
}
457-
try {
458-
valueBoundaryCheck({ a: MAX_INTEGER + BigInt(1) }, 256, false)
459-
} catch (err: any) {
460-
assert.isTrue(
461-
err.message.includes('exceed MAX_INTEGER'),
462-
'throws when value exceeds MAX_INTEGER',
463-
)
464-
}
465-
try {
466-
valueBoundaryCheck({ a: BigInt(0) }, 100, false)
467-
} catch (err: any) {
468-
assert.isTrue(
469-
err.message.includes('unimplemented bits value'),
470-
'throws when bits value other than 64 or 256 provided',
471-
)
472-
}
473-
try {
474-
valueBoundaryCheck({ a: MAX_UINT64 + BigInt(1) }, 64, false)
475-
} catch (err: any) {
476-
assert.isTrue(err.message.includes('2^64'), 'throws when 64 bit integer exceeds MAX_UINT64')
477-
}
478-
try {
479-
valueBoundaryCheck({ a: MAX_UINT64 }, 64, true)
480-
} catch (err: any) {
481-
assert.isTrue(
482-
err.message.includes('2^64'),
483-
'throws when 64 bit integer equals or exceeds MAX_UINT64',
484-
)
485-
}
486-
})
487443
})

packages/tx/test/util.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { MAX_INTEGER, MAX_UINT64 } from '@ethereumjs/util'
2+
import { assert, describe, it } from 'vitest'
3+
4+
import { valueOverflowCheck } from '../src/util/internal.ts'
5+
6+
describe('Helper methods should be correct', () => {
7+
it('valueBoundaryCheck()', () => {
8+
try {
9+
valueOverflowCheck({ a: MAX_INTEGER }, 256, true)
10+
} catch (err: any) {
11+
assert.isTrue(
12+
err.message.includes('equal or exceed MAX_INTEGER'),
13+
'throws when value equals or exceeds MAX_INTEGER',
14+
)
15+
}
16+
try {
17+
valueOverflowCheck({ a: MAX_INTEGER + BigInt(1) }, 256, false)
18+
} catch (err: any) {
19+
assert.isTrue(
20+
err.message.includes('exceed MAX_INTEGER'),
21+
'throws when value exceeds MAX_INTEGER',
22+
)
23+
}
24+
try {
25+
valueOverflowCheck({ a: BigInt(0) }, 100, false)
26+
} catch (err: any) {
27+
assert.isTrue(
28+
err.message.includes('unimplemented bits value'),
29+
'throws when bits value other than 64 or 256 provided',
30+
)
31+
}
32+
try {
33+
valueOverflowCheck({ a: MAX_UINT64 + BigInt(1) }, 64, false)
34+
} catch (err: any) {
35+
assert.isTrue(err.message.includes('2^64'), 'throws when 64 bit integer exceeds MAX_UINT64')
36+
}
37+
try {
38+
valueOverflowCheck({ a: MAX_UINT64 }, 64, true)
39+
} catch (err: any) {
40+
assert.isTrue(
41+
err.message.includes('2^64'),
42+
'throws when 64 bit integer equals or exceeds MAX_UINT64',
43+
)
44+
}
45+
})
46+
})

0 commit comments

Comments
 (0)