Skip to content

Commit

Permalink
monorepo: throw EthereumJSErrors, not default js errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Feb 24, 2025
1 parent be421b3 commit 8585be6
Show file tree
Hide file tree
Showing 113 changed files with 993 additions and 681 deletions.
22 changes: 21 additions & 1 deletion config/eslint.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module.exports = {
rules: {
'implicit-dependencies/no-implicit': 'off',
'import/no-extraneous-dependencies': 'off',
'no-restricted-syntax': 'off',
},
},
{
Expand All @@ -144,14 +145,14 @@ module.exports = {
'import/no-extraneous-dependencies': 'off',
'no-console': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'no-restricted-syntax': 'off'
},
},
{
files: ['packages/statemanager/src/**', 'packages/vm/src/**', ],
rules: {
'@typescript-eslint/no-use-before-define': 'off',
'no-invalid-this': 'off',
'no-restricted-syntax': 'off',
},
},
{
Expand All @@ -163,11 +164,30 @@ module.exports = {
'@typescript-eslint/no-unused-vars': 'off',
},
},
{
files: ['packages/devp2p/src/ext/**'],
rules: {
'no-restricted-syntax': 'off'
},
},
{
files: ['packages/client/src/ext/**'],
rules: {
'no-restricted-syntax': 'off'
},
},
{
files: ['packages/wallet/**'],
rules: {
'github/array-foreach': 'warn',
'no-prototype-builtins': 'warn',
'no-restricted-syntax': 'off'
},
},
{
files: ['packages/rlp/**'],
rules: {
'no-restricted-syntax': 'off'
},
},
],
Expand Down
37 changes: 21 additions & 16 deletions packages/binarytree/src/binaryTree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
EthereumJSErrorUnsetCode,
Lock,
bitsToBytes,
bytesToBits,
Expand Down Expand Up @@ -55,7 +56,7 @@ export class BinaryTree {
this._opts = opts

if (opts.db instanceof CheckpointDB) {
throw new Error('Cannot pass in an instance of CheckpointDB')
throw EthereumJSErrorUnsetCode('Cannot pass in an instance of CheckpointDB')
}
this._db = new CheckpointDB({ db: opts.db, cacheSize: opts.cacheSize })

Expand Down Expand Up @@ -97,7 +98,7 @@ export class BinaryTree {
}

if (value.length !== this._hashLen) {
throw new Error(`Invalid root length. Roots are ${this._hashLen} bytes`)
throw EthereumJSErrorUnsetCode(`Invalid root length. Roots are ${this._hashLen} bytes`)
}

this._root = value
Expand Down Expand Up @@ -130,7 +131,8 @@ export class BinaryTree {
* If the stem is not found, will return an empty array.
*/
async get(stem: Uint8Array, suffixes: number[]): Promise<(Uint8Array | null)[]> {
if (stem.length !== 31) throw new Error(`expected stem with length 31; got ${stem.length}`)
if (stem.length !== 31)
throw EthereumJSErrorUnsetCode(`expected stem with length 31; got ${stem.length}`)
this.DEBUG && this.debug(`Stem: ${bytesToHex(stem)}; Suffix: ${suffixes}`, ['get'])
const stemPath = await this.findPath(stem)
if (stemPath.node instanceof StemBinaryNode) {
Expand Down Expand Up @@ -159,9 +161,10 @@ export class BinaryTree {
* @returns A Promise that resolves once the value is stored.
*/
async put(stem: Uint8Array, suffixes: number[], values: (Uint8Array | null)[]): Promise<void> {
if (stem.length !== 31) throw new Error(`expected stem with length 31, got ${stem.length}`)
if (stem.length !== 31)
throw EthereumJSErrorUnsetCode(`expected stem with length 31, got ${stem.length}`)
if (values.length > 0 && values.length !== suffixes.length)
throw new Error(
throw EthereumJSErrorUnsetCode(
`expected number of values (${values.length}) to equal number of suffixes (${suffixes.length})`,
)

Expand All @@ -178,7 +181,7 @@ export class BinaryTree {
const foundPath = await this.findPath(stem)

// We should always at least get the root node back
if (foundPath.stack.length === 0) throw new Error(`Root node not found in trie`)
if (foundPath.stack.length === 0) throw EthereumJSErrorUnsetCode(`Root node not found in trie`)

// Step 1) Create or update the stem node
let stemNode: StemBinaryNode
Expand Down Expand Up @@ -259,7 +262,9 @@ export class BinaryTree {
this.DEBUG &&
this.debug(`Updated parent internal node hash for path ${path.join(',')}`, ['put'])
} else {
throw new Error(`Expected internal node at path ${path.join(',')}, got ${node}`)
throw EthereumJSErrorUnsetCode(
`Expected internal node at path ${path.join(',')}, got ${node}`,
)
}
}

Expand Down Expand Up @@ -419,7 +424,7 @@ export class BinaryTree {

// Get the root node.
let rawNode = await this._db.get(this.root())
if (rawNode === undefined) throw new Error('root node should exist')
if (rawNode === undefined) throw EthereumJSErrorUnsetCode('root node should exist')
const rootNode = decodeBinaryNode(rawNode)

this.DEBUG && this.debug(`Starting with Root Node: [${bytesToHex(this.root())}]`, ['find_path'])
Expand Down Expand Up @@ -450,7 +455,7 @@ export class BinaryTree {

// Look up child node by its node hash.
rawNode = await this._db.get(childNode.hash)
if (rawNode === undefined) throw new Error(`missing node at ${childNode.path}`)
if (rawNode === undefined) throw EthereumJSErrorUnsetCode(`missing node at ${childNode.path}`)
const decodedNode = decodeBinaryNode(rawNode)

// Determine how many bits match between keyInBits and the stored path in childNode.
Expand Down Expand Up @@ -577,15 +582,15 @@ export class BinaryTree {
* @param proof
*/
async fromProof(_proof: any): Promise<void> {
throw new Error('Not implemented')
throw EthereumJSErrorUnsetCode('Not implemented')
}

/**
* Creates a proof from a tree and key that can be verified using {@link BinaryTree.verifyBinaryProof}.
* @param key
*/
async createBinaryProof(_key: Uint8Array): Promise<any> {
throw new Error('Not implemented')
throw EthereumJSErrorUnsetCode('Not implemented')
}

/**
Expand All @@ -601,15 +606,15 @@ export class BinaryTree {
_key: Uint8Array,
_proof: any,
): Promise<Uint8Array | null> {
throw new Error('Not implemented')
throw EthereumJSErrorUnsetCode('Not implemented')
}

/**
* The `data` event is given an `Object` that has two properties; the `key` and the `value`. Both should be Uint8Arrays.
* @return Returns a [stream](https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_class_stream_readable) of the contents of the `tree`
*/
createReadStream(): any {
throw new Error('Not implemented')
throw EthereumJSErrorUnsetCode('Not implemented')
}

/**
Expand Down Expand Up @@ -668,7 +673,7 @@ export class BinaryTree {
*/
async commit(): Promise<void> {
if (!this.hasCheckpoints()) {
throw new Error('trying to commit when not checkpointed')
throw EthereumJSErrorUnsetCode('trying to commit when not checkpointed')
}

await this._lock.acquire()
Expand All @@ -684,7 +689,7 @@ export class BinaryTree {
*/
async revert(): Promise<void> {
if (!this.hasCheckpoints()) {
throw new Error('trying to revert when not checkpointed')
throw EthereumJSErrorUnsetCode('trying to revert when not checkpointed')
}

await this._lock.acquire()
Expand All @@ -707,7 +712,7 @@ export class BinaryTree {
}

if (msg.length !== 32 && msg.length !== 64) {
throw new Error('Data must be 32 or 64 bytes')
throw EthereumJSErrorUnsetCode('Data must be 32 or 64 bytes')
}

return Uint8Array.from(this._opts.hashFunction.call(undefined, msg))
Expand Down
12 changes: 6 additions & 6 deletions packages/binarytree/src/node/internalNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RLP } from '@ethereumjs/rlp'
import { bitsToBytes, bytesToBits } from '@ethereumjs/util'
import { EthereumJSErrorUnsetCode, bitsToBytes, bytesToBits } from '@ethereumjs/util'

import { BinaryNodeType } from './types.js'

Expand All @@ -17,13 +17,13 @@ export class InternalBinaryNode {
static fromRawNode(rawNode: Uint8Array[]): InternalBinaryNode {
const nodeType = rawNode[0][0]
if (nodeType !== BinaryNodeType.Internal) {
throw new Error('Invalid node type')
throw EthereumJSErrorUnsetCode('Invalid node type')
}

// The length of the rawNode should be the # of children * 2 (for hash and path) + 1 for the node type

if (rawNode.length !== 2 * 2 + 1) {
throw new Error('Invalid node length')
throw EthereumJSErrorUnsetCode('Invalid node length')
}
const [, leftChildHash, rightChildHash, leftChildRawPath, rightChildRawPath] = rawNode

Expand All @@ -32,13 +32,13 @@ export class InternalBinaryNode {
const decoded = RLP.decode(rawPath)

if (!Array.isArray(decoded) || decoded.length !== 2) {
throw new Error('Invalid RLP encoding for child path')
throw EthereumJSErrorUnsetCode('Invalid RLP encoding for child path')
}

const [encodedLength, encodedPath] = decoded as Uint8Array[]

if (encodedLength.length !== 1) {
throw new Error('Invalid path length encoding')
throw EthereumJSErrorUnsetCode('Invalid path length encoding')
}

const pathLength = encodedLength[0]
Expand All @@ -62,7 +62,7 @@ export class InternalBinaryNode {
*/
static create(children?: (ChildBinaryNode | null)[]): InternalBinaryNode {
if (children !== undefined && children.length !== 2) {
throw new Error('Internal node must have 2 children')
throw EthereumJSErrorUnsetCode('Internal node must have 2 children')
}
return new InternalBinaryNode({ children })
}
Expand Down
5 changes: 3 additions & 2 deletions packages/binarytree/src/node/stemNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RLP } from '@ethereumjs/rlp'
import { EthereumJSErrorUnsetCode } from '@ethereumjs/util'

import { BinaryNodeType, NODE_WIDTH } from './types.js'

Expand All @@ -18,12 +19,12 @@ export class StemBinaryNode {
static fromRawNode(rawNode: Uint8Array[]): StemBinaryNode {
const nodeType = rawNode[0][0]
if (nodeType !== BinaryNodeType.Stem) {
throw new Error('Invalid node type')
throw EthereumJSErrorUnsetCode('Invalid node type')
}

// The length of the rawNode should be the # of values (node width) + 2 for the node type and the stem
if (rawNode.length !== NODE_WIDTH + 2) {
throw new Error('Invalid node length')
throw EthereumJSErrorUnsetCode('Invalid node length')
}

const stem = rawNode[1]
Expand Down
5 changes: 3 additions & 2 deletions packages/binarytree/src/node/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RLP } from '@ethereumjs/rlp'
import { EthereumJSErrorUnsetCode } from '@ethereumjs/util'

import { InternalBinaryNode } from './internalNode.js'
import { StemBinaryNode } from './stemNode.js'
Expand All @@ -12,14 +13,14 @@ export function decodeRawBinaryNode(raw: Uint8Array[]): BinaryNode {
case BinaryNodeType.Stem:
return StemBinaryNode.fromRawNode(raw)
default:
throw new Error('Invalid node type')
throw EthereumJSErrorUnsetCode('Invalid node type')
}
}

export function decodeBinaryNode(raw: Uint8Array) {
const decoded = RLP.decode(Uint8Array.from(raw)) as Uint8Array[]
if (!Array.isArray(decoded)) {
throw new Error('Invalid node')
throw EthereumJSErrorUnsetCode('Invalid node')
}
return decodeRawBinaryNode(decoded)
}
Expand Down
Loading

0 comments on commit 8585be6

Please sign in to comment.