Skip to content

Commit 8585be6

Browse files
monorepo: throw EthereumJSErrors, not default js errors
1 parent be421b3 commit 8585be6

File tree

113 files changed

+993
-681
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+993
-681
lines changed

config/eslint.cjs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ module.exports = {
135135
rules: {
136136
'implicit-dependencies/no-implicit': 'off',
137137
'import/no-extraneous-dependencies': 'off',
138+
'no-restricted-syntax': 'off',
138139
},
139140
},
140141
{
@@ -144,14 +145,14 @@ module.exports = {
144145
'import/no-extraneous-dependencies': 'off',
145146
'no-console': 'off',
146147
'@typescript-eslint/no-unused-vars': 'off',
148+
'no-restricted-syntax': 'off'
147149
},
148150
},
149151
{
150152
files: ['packages/statemanager/src/**', 'packages/vm/src/**', ],
151153
rules: {
152154
'@typescript-eslint/no-use-before-define': 'off',
153155
'no-invalid-this': 'off',
154-
'no-restricted-syntax': 'off',
155156
},
156157
},
157158
{
@@ -163,11 +164,30 @@ module.exports = {
163164
'@typescript-eslint/no-unused-vars': 'off',
164165
},
165166
},
167+
{
168+
files: ['packages/devp2p/src/ext/**'],
169+
rules: {
170+
'no-restricted-syntax': 'off'
171+
},
172+
},
173+
{
174+
files: ['packages/client/src/ext/**'],
175+
rules: {
176+
'no-restricted-syntax': 'off'
177+
},
178+
},
166179
{
167180
files: ['packages/wallet/**'],
168181
rules: {
169182
'github/array-foreach': 'warn',
170183
'no-prototype-builtins': 'warn',
184+
'no-restricted-syntax': 'off'
185+
},
186+
},
187+
{
188+
files: ['packages/rlp/**'],
189+
rules: {
190+
'no-restricted-syntax': 'off'
171191
},
172192
},
173193
],

packages/binarytree/src/binaryTree.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
EthereumJSErrorUnsetCode,
23
Lock,
34
bitsToBytes,
45
bytesToBits,
@@ -55,7 +56,7 @@ export class BinaryTree {
5556
this._opts = opts
5657

5758
if (opts.db instanceof CheckpointDB) {
58-
throw new Error('Cannot pass in an instance of CheckpointDB')
59+
throw EthereumJSErrorUnsetCode('Cannot pass in an instance of CheckpointDB')
5960
}
6061
this._db = new CheckpointDB({ db: opts.db, cacheSize: opts.cacheSize })
6162

@@ -97,7 +98,7 @@ export class BinaryTree {
9798
}
9899

99100
if (value.length !== this._hashLen) {
100-
throw new Error(`Invalid root length. Roots are ${this._hashLen} bytes`)
101+
throw EthereumJSErrorUnsetCode(`Invalid root length. Roots are ${this._hashLen} bytes`)
101102
}
102103

103104
this._root = value
@@ -130,7 +131,8 @@ export class BinaryTree {
130131
* If the stem is not found, will return an empty array.
131132
*/
132133
async get(stem: Uint8Array, suffixes: number[]): Promise<(Uint8Array | null)[]> {
133-
if (stem.length !== 31) throw new Error(`expected stem with length 31; got ${stem.length}`)
134+
if (stem.length !== 31)
135+
throw EthereumJSErrorUnsetCode(`expected stem with length 31; got ${stem.length}`)
134136
this.DEBUG && this.debug(`Stem: ${bytesToHex(stem)}; Suffix: ${suffixes}`, ['get'])
135137
const stemPath = await this.findPath(stem)
136138
if (stemPath.node instanceof StemBinaryNode) {
@@ -159,9 +161,10 @@ export class BinaryTree {
159161
* @returns A Promise that resolves once the value is stored.
160162
*/
161163
async put(stem: Uint8Array, suffixes: number[], values: (Uint8Array | null)[]): Promise<void> {
162-
if (stem.length !== 31) throw new Error(`expected stem with length 31, got ${stem.length}`)
164+
if (stem.length !== 31)
165+
throw EthereumJSErrorUnsetCode(`expected stem with length 31, got ${stem.length}`)
163166
if (values.length > 0 && values.length !== suffixes.length)
164-
throw new Error(
167+
throw EthereumJSErrorUnsetCode(
165168
`expected number of values (${values.length}) to equal number of suffixes (${suffixes.length})`,
166169
)
167170

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

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

183186
// Step 1) Create or update the stem node
184187
let stemNode: StemBinaryNode
@@ -259,7 +262,9 @@ export class BinaryTree {
259262
this.DEBUG &&
260263
this.debug(`Updated parent internal node hash for path ${path.join(',')}`, ['put'])
261264
} else {
262-
throw new Error(`Expected internal node at path ${path.join(',')}, got ${node}`)
265+
throw EthereumJSErrorUnsetCode(
266+
`Expected internal node at path ${path.join(',')}, got ${node}`,
267+
)
263268
}
264269
}
265270

@@ -419,7 +424,7 @@ export class BinaryTree {
419424

420425
// Get the root node.
421426
let rawNode = await this._db.get(this.root())
422-
if (rawNode === undefined) throw new Error('root node should exist')
427+
if (rawNode === undefined) throw EthereumJSErrorUnsetCode('root node should exist')
423428
const rootNode = decodeBinaryNode(rawNode)
424429

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

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

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

583588
/**
584589
* Creates a proof from a tree and key that can be verified using {@link BinaryTree.verifyBinaryProof}.
585590
* @param key
586591
*/
587592
async createBinaryProof(_key: Uint8Array): Promise<any> {
588-
throw new Error('Not implemented')
593+
throw EthereumJSErrorUnsetCode('Not implemented')
589594
}
590595

591596
/**
@@ -601,15 +606,15 @@ export class BinaryTree {
601606
_key: Uint8Array,
602607
_proof: any,
603608
): Promise<Uint8Array | null> {
604-
throw new Error('Not implemented')
609+
throw EthereumJSErrorUnsetCode('Not implemented')
605610
}
606611

607612
/**
608613
* The `data` event is given an `Object` that has two properties; the `key` and the `value`. Both should be Uint8Arrays.
609614
* @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`
610615
*/
611616
createReadStream(): any {
612-
throw new Error('Not implemented')
617+
throw EthereumJSErrorUnsetCode('Not implemented')
613618
}
614619

615620
/**
@@ -668,7 +673,7 @@ export class BinaryTree {
668673
*/
669674
async commit(): Promise<void> {
670675
if (!this.hasCheckpoints()) {
671-
throw new Error('trying to commit when not checkpointed')
676+
throw EthereumJSErrorUnsetCode('trying to commit when not checkpointed')
672677
}
673678

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

690695
await this._lock.acquire()
@@ -707,7 +712,7 @@ export class BinaryTree {
707712
}
708713

709714
if (msg.length !== 32 && msg.length !== 64) {
710-
throw new Error('Data must be 32 or 64 bytes')
715+
throw EthereumJSErrorUnsetCode('Data must be 32 or 64 bytes')
711716
}
712717

713718
return Uint8Array.from(this._opts.hashFunction.call(undefined, msg))

packages/binarytree/src/node/internalNode.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RLP } from '@ethereumjs/rlp'
2-
import { bitsToBytes, bytesToBits } from '@ethereumjs/util'
2+
import { EthereumJSErrorUnsetCode, bitsToBytes, bytesToBits } from '@ethereumjs/util'
33

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

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

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

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

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

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

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

4040
if (encodedLength.length !== 1) {
41-
throw new Error('Invalid path length encoding')
41+
throw EthereumJSErrorUnsetCode('Invalid path length encoding')
4242
}
4343

4444
const pathLength = encodedLength[0]
@@ -62,7 +62,7 @@ export class InternalBinaryNode {
6262
*/
6363
static create(children?: (ChildBinaryNode | null)[]): InternalBinaryNode {
6464
if (children !== undefined && children.length !== 2) {
65-
throw new Error('Internal node must have 2 children')
65+
throw EthereumJSErrorUnsetCode('Internal node must have 2 children')
6666
}
6767
return new InternalBinaryNode({ children })
6868
}

packages/binarytree/src/node/stemNode.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RLP } from '@ethereumjs/rlp'
2+
import { EthereumJSErrorUnsetCode } from '@ethereumjs/util'
23

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

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

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

2930
const stem = rawNode[1]

packages/binarytree/src/node/util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RLP } from '@ethereumjs/rlp'
2+
import { EthereumJSErrorUnsetCode } from '@ethereumjs/util'
23

34
import { InternalBinaryNode } from './internalNode.js'
45
import { StemBinaryNode } from './stemNode.js'
@@ -12,14 +13,14 @@ export function decodeRawBinaryNode(raw: Uint8Array[]): BinaryNode {
1213
case BinaryNodeType.Stem:
1314
return StemBinaryNode.fromRawNode(raw)
1415
default:
15-
throw new Error('Invalid node type')
16+
throw EthereumJSErrorUnsetCode('Invalid node type')
1617
}
1718
}
1819

1920
export function decodeBinaryNode(raw: Uint8Array) {
2021
const decoded = RLP.decode(Uint8Array.from(raw)) as Uint8Array[]
2122
if (!Array.isArray(decoded)) {
22-
throw new Error('Invalid node')
23+
throw EthereumJSErrorUnsetCode('Invalid node')
2324
}
2425
return decodeRawBinaryNode(decoded)
2526
}

0 commit comments

Comments
 (0)