Skip to content
Draft
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
17 changes: 15 additions & 2 deletions packages/tx/src/features/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Common, Mainnet } from '@ethereumjs/common'
import {
Address,
MAX_INTEGER,
MAX_UINT63,
MAX_UINT64,
bigIntToHex,
bytesToBigInt,
Expand Down Expand Up @@ -32,6 +33,18 @@ export function valueBoundaryCheck(
) {
for (const [key, value] of Object.entries(values)) {
switch (bits) {
case 63:
if (cannotEqual) {
if (value !== undefined && value >= MAX_UINT63) {
// TODO: error msgs got raised to a error string handler first, now throws "generic" error
throw new Error(`${key} cannot equal or exceed MAX_UINT63 (2^63-1), given ${value}`)
}
} else {
if (value !== undefined && value > MAX_UINT63) {
throw new Error(`${key} cannot exceed MAX_UINT63 (2^63-1), given ${value}`)
}
}
break
case 64:
if (cannotEqual) {
if (value !== undefined && value >= MAX_UINT64) {
Expand Down Expand Up @@ -108,8 +121,8 @@ export function sharedConstructor(
// Validate value/r/s
valueBoundaryCheck({ value: tx.value, r: tx.r, s: tx.s })

// geth limits gasLimit to 2^64-1
valueBoundaryCheck({ gasLimit: tx.gasLimit }, 64)
// https://eips.ethereum.org/EIPS/eip-4803
valueBoundaryCheck({ gasLimit: tx.gasLimit }, 63, true)

// EIP-2681 limits nonce to 2^64-1 (cannot equal 2^64-1)
valueBoundaryCheck({ nonce: tx.nonce }, 64, true)
Expand Down
8 changes: 8 additions & 0 deletions packages/tx/test/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,5 +471,13 @@ describe('[BaseTransaction]', () => {
'throws when 64 bit integer equals or exceeds MAX_UINT64',
)
}
try {
valueBoundaryCheck({ a: MAX_UINT64 }, 63, true)
} catch (err: any) {
assert.ok(
err.message.includes('2^63'),
'throws when 63 bit integer equals or exceeds MAX_UINT63',
)
}
})
})
5 changes: 5 additions & 0 deletions packages/util/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { hexToBytes } from './bytes.js'
*/
export const MAX_UINT64 = BigInt('0xffffffffffffffff')

/**
* 2^63-1
*/
export const MAX_UINT63 = BigInt(2) ** BigInt(63) - BigInt(1)

/**
* The max integer that the evm can handle (2^256-1)
*/
Expand Down
Loading