11import {
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 ) )
0 commit comments