diff --git a/.travis.yml b/.travis.yml index 984811d..f637413 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: node_js node_js: - - "6" - "8" - "9" - "10" + - "12" env: - CXX=g++-4.8 services: @@ -32,4 +32,3 @@ matrix: node_js: "8" env: CXX=g++-4.8 TEST_SUITE=test:browser script: npm run build && npm run $TEST_SUITE - diff --git a/README.md b/README.md index ca746ef..ac6e0f3 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # SYNOPSIS -[![NPM Package](https://img.shields.io/npm/v/merkle-patricia-tree.svg?style=flat-square)](https://www.npmjs.org/package/merkle-patricia-tree) -[![Build Status](https://img.shields.io/travis/ethereumjs/merkle-patricia-tree.svg?branch=master&style=flat-square)](https://travis-ci.org/ethereumjs/merkle-patricia-tree) -[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/merkle-patricia-tree.svg?style=flat-square)](https://coveralls.io/r/ethereumjs/merkle-patricia-tree) -[![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs-lib.svg?style=flat-square)](https://gitter.im/ethereum/ethereumjs-lib) or #ethereumjs on freenode +[![NPM Package](https://img.shields.io/npm/v/merkle-patricia-tree)](https://www.npmjs.org/package/merkle-patricia-tree) +[![Build Status](https://img.shields.io/travis/ethereumjs/merkle-patricia-tree/master)](https://travis-ci.org/ethereumjs/merkle-patricia-tree) +[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/merkle-patricia-tree.svg)](https://coveralls.io/r/ethereumjs/merkle-patricia-tree) +[![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs-lib.svg)](https://gitter.im/ethereum/ethereumjs-lib) [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) @@ -30,8 +30,8 @@ const Trie = require('merkle-patricia-tree').BaseTrie, db = level('./testdb'), trie = new Trie(db) -trie.put('test', 'one', function() { - trie.get('test', function(err, value) { +trie.put(Buffer.from('test'), Buffer.from('one'), function() { + trie.get(Buffer.from('test'), function(err, value) { if (value) console.log(value.toString()) }) }) @@ -40,9 +40,9 @@ trie.put('test', 'one', function() { ## Merkle Proofs ```javascript -Trie.prove(trie, 'test', function(err, prove) { +Trie.prove(trie, Buffer.from('test'), function(err, prove) { if (err) return cb(err) - Trie.verifyProof(trie.root, 'test', prove, function(err, value) { + Trie.verifyProof(trie.root, Buffer.from('test'), prove, function(err, value) { if (err) return cb(err) console.log(value.toString()) cb() @@ -117,7 +117,7 @@ trie.get(address, function(err, data) { # API -[./docs/](./docs/index.md) +[Documentation](./docs/README.md) # TESTING diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..b93a80e --- /dev/null +++ b/docs/README.md @@ -0,0 +1,11 @@ +[merkle-patricia-tree](README.md) + +# merkle-patricia-tree + +## Index + +### Modules + +* ["baseTrie"](modules/_basetrie_.md) +* ["checkpointTrie"](modules/_checkpointtrie_.md) +* ["secure"](modules/_secure_.md) diff --git a/docs/classes/_basetrie_.trie.md b/docs/classes/_basetrie_.trie.md new file mode 100644 index 0000000..78cddf7 --- /dev/null +++ b/docs/classes/_basetrie_.trie.md @@ -0,0 +1,618 @@ +[merkle-patricia-tree](../README.md) › ["baseTrie"](../modules/_basetrie_.md) › [Trie](_basetrie_.trie.md) + +# Class: Trie + +Use `require('merkel-patricia-tree').BaseTrie` for the base interface. In Ethereum applications +stick with the Secure Trie Overlay `require('merkel-patricia-tree').SecureTrie`. +The API for the raw and the secure interface are about the same. + +**`param`** A [levelup](https://github.com/Level/levelup) instance. By default creates an in-memory [memdown](https://github.com/Level/memdown) instance. +If the db is `null` or left undefined, then the trie will be stored in memory via [memdown](https://github.com/Level/memdown) + +**`param`** A hex `String` or `Buffer` for the root of a previously stored trie + +**`prop`** {Buffer} root The current root of the `trie` + +**`prop`** {Buffer} EMPTY_TRIE_ROOT the Root for an empty trie + +## Hierarchy + +* **Trie** + + ↳ [CheckpointTrie](_checkpointtrie_.checkpointtrie.md) + +## Index + +### Constructors + +* [constructor](_basetrie_.trie.md#constructor) + +### Properties + +* [EMPTY_TRIE_ROOT](_basetrie_.trie.md#empty_trie_root) +* [db](_basetrie_.trie.md#db) + +### Accessors + +* [root](_basetrie_.trie.md#root) + +### Methods + +* [_createInitialNode](_basetrie_.trie.md#_createinitialnode) +* [_deleteNode](_basetrie_.trie.md#_deletenode) +* [_findDbNodes](_basetrie_.trie.md#_finddbnodes) +* [_findValueNodes](_basetrie_.trie.md#_findvaluenodes) +* [_formatNode](_basetrie_.trie.md#_formatnode) +* [_lookupNode](_basetrie_.trie.md#_lookupnode) +* [_putNode](_basetrie_.trie.md#_putnode) +* [_saveStack](_basetrie_.trie.md#private-_savestack) +* [_updateNode](_basetrie_.trie.md#private-_updatenode) +* [_walkTrie](_basetrie_.trie.md#_walktrie) +* [batch](_basetrie_.trie.md#batch) +* [checkRoot](_basetrie_.trie.md#checkroot) +* [copy](_basetrie_.trie.md#copy) +* [createReadStream](_basetrie_.trie.md#createreadstream) +* [del](_basetrie_.trie.md#del) +* [delRaw](_basetrie_.trie.md#delraw) +* [findPath](_basetrie_.trie.md#findpath) +* [get](_basetrie_.trie.md#get) +* [getRaw](_basetrie_.trie.md#getraw) +* [put](_basetrie_.trie.md#put) +* [putRaw](_basetrie_.trie.md#putraw) +* [setRoot](_basetrie_.trie.md#setroot) +* [fromProof](_basetrie_.trie.md#static-fromproof) +* [prove](_basetrie_.trie.md#static-prove) +* [verifyProof](_basetrie_.trie.md#static-verifyproof) + +## Constructors + +### constructor + +\+ **new Trie**(`db?`: LevelUp | null, `root?`: Buffer): *[Trie](_basetrie_.trie.md)* + +*Defined in [baseTrie.ts:37](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L37)* + +**Parameters:** + +Name | Type | +------ | ------ | +`db?` | LevelUp | null | +`root?` | Buffer | + +**Returns:** *[Trie](_basetrie_.trie.md)* + +## Properties + +### EMPTY_TRIE_ROOT + +• **EMPTY_TRIE_ROOT**: *Buffer* + +*Defined in [baseTrie.ts:34](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L34)* + +___ + +### db + +• **db**: *DB* + +*Defined in [baseTrie.ts:35](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L35)* + +## Accessors + +### root + +• **get root**(): *Buffer* + +*Defined in [baseTrie.ts:103](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L103)* + +**Returns:** *Buffer* + +• **set root**(`value`: Buffer): *void* + +*Defined in [baseTrie.ts:99](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L99)* + +**Parameters:** + +Name | Type | +------ | ------ | +`value` | Buffer | + +**Returns:** *void* + +## Methods + +### _createInitialNode + +▸ **_createInitialNode**(`key`: Buffer, `value`: Buffer, `cb`: ErrorCallback): *void* + +*Defined in [baseTrie.ts:735](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L735)* + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`value` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### _deleteNode + +▸ **_deleteNode**(`k`: Buffer, `stack`: TrieNode[], `cb`: Function): *any* + +*Defined in [baseTrie.ts:617](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L617)* + +**Parameters:** + +Name | Type | +------ | ------ | +`k` | Buffer | +`stack` | TrieNode[] | +`cb` | Function | + +**Returns:** *any* + +___ + +### _findDbNodes + +▸ **_findDbNodes**(`onFound`: Function, `cb`: Function): *void* + +*Defined in [baseTrie.ts:346](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L346)* + +**Parameters:** + +Name | Type | +------ | ------ | +`onFound` | Function | +`cb` | Function | + +**Returns:** *void* + +___ + +### _findValueNodes + +▸ **_findValueNodes**(`onFound`: Function, `cb`: Function): *void* + +*Defined in [baseTrie.ts:320](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L320)* + +**Parameters:** + +Name | Type | +------ | ------ | +`onFound` | Function | +`cb` | Function | + +**Returns:** *void* + +___ + +### _formatNode + +▸ **_formatNode**(`node`: TrieNode, `topLevel`: boolean, `opStack`: BatchDBOp[], `remove`: boolean): *Buffer | null | Buffer‹› | Buffer‹›[][]* + +*Defined in [baseTrie.ts:743](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L743)* + +**Parameters:** + +Name | Type | Default | +------ | ------ | ------ | +`node` | TrieNode | - | +`topLevel` | boolean | - | +`opStack` | BatchDBOp[] | - | +`remove` | boolean | false | + +**Returns:** *Buffer | null | Buffer‹› | Buffer‹›[][]* + +___ + +### _lookupNode + +▸ **_lookupNode**(`node`: Buffer | Buffer[], `cb`: Function): *void* + +*Defined in [baseTrie.ts:230](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L230)* + +**Parameters:** + +Name | Type | +------ | ------ | +`node` | Buffer | Buffer[] | +`cb` | Function | + +**Returns:** *void* + +___ + +### _putNode + +▸ **_putNode**(`node`: TrieNode, `cb`: ErrorCallback): *void* + +*Defined in [baseTrie.ts:247](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L247)* + +**Parameters:** + +Name | Type | +------ | ------ | +`node` | TrieNode | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### `Private` _saveStack + +▸ **_saveStack**(`key`: number[], `stack`: TrieNode[], `opStack`: BatchDBOp[], `cb`: ErrorCallback): *void* + +*Defined in [baseTrie.ts:588](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L588)* + +saves a stack + +**`method`** _saveStack + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`key` | number[] | the key. Should follow the stack | +`stack` | TrieNode[] | a stack of nodes to the value given by the key | +`opStack` | BatchDBOp[] | a stack of levelup operations to commit at the end of this funciton | +`cb` | ErrorCallback | | + +**Returns:** *void* + +___ + +### `Private` _updateNode + +▸ **_updateNode**(`k`: Buffer, `value`: Buffer, `keyRemainder`: number[], `stack`: TrieNode[], `cb`: ErrorCallback): *void* + +*Defined in [baseTrie.ts:370](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L370)* + +Updates a node + +**`method`** _updateNode + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`k` | Buffer | - | +`value` | Buffer | - | +`keyRemainder` | number[] | - | +`stack` | TrieNode[] | - | +`cb` | ErrorCallback | the callback | + +**Returns:** *void* + +___ + +### _walkTrie + +▸ **_walkTrie**(`root`: Buffer, `onNode`: Function, `onDone`: Function): *any* + +*Defined in [baseTrie.ts:473](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L473)* + +**Parameters:** + +Name | Type | +------ | ------ | +`root` | Buffer | +`onNode` | Function | +`onDone` | Function | + +**Returns:** *any* + +___ + +### batch + +▸ **batch**(`ops`: BatchDBOp[], `cb`: ErrorCallback): *void* + +*Defined in [baseTrie.ts:799](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L799)* + +The given hash of operations (key additions or deletions) are executed on the DB + +**`method`** batch + +**`memberof`** Trie + +**`example`** +var ops = [ + { type: 'del', key: Buffer.from('father') } + , { type: 'put', key: Buffer.from('name'), value: Buffer.from('Yuri Irsenovich Kim') } + , { type: 'put', key: Buffer.from('dob'), value: Buffer.from('16 February 1941') } + , { type: 'put', key: Buffer.from('spouse'), value: Buffer.from('Kim Young-sook') } + , { type: 'put', key: Buffer.from('occupation'), value: Buffer.from('Clown') } +] +trie.batch(ops) + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`ops` | BatchDBOp[] | - | +`cb` | ErrorCallback | | + +**Returns:** *void* + +___ + +### checkRoot + +▸ **checkRoot**(`root`: Buffer, `cb`: Function): *void* + +*Defined in [baseTrie.ts:819](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L819)* + +Checks if a given root exists + +**Parameters:** + +Name | Type | +------ | ------ | +`root` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### copy + +▸ **copy**(): *[Trie](_basetrie_.trie.md)* + +*Defined in [baseTrie.ts:778](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L778)* + +**Returns:** *[Trie](_basetrie_.trie.md)* + +___ + +### createReadStream + +▸ **createReadStream**(): *ReadStream* + +*Defined in [baseTrie.ts:772](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L772)* + +The `data` event is given an `Object` hat has two properties; the `key` and the `value`. Both should be Buffers. + +**`method`** createReadStream + +**`memberof`** Trie + +**Returns:** *ReadStream* + +Returns a [stream](https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_readable) of the contents of the `trie` + +___ + +### del + +▸ **del**(`key`: Buffer, `cb`: Function): *void* + +*Defined in [baseTrie.ts:183](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L183)* + +deletes a value given a `key` + +**`method`** del + +**`memberof`** Trie + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### delRaw + +▸ **delRaw**(`key`: Buffer, `cb`: ErrorCallback): *void* + +*Defined in [baseTrie.ts:225](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L225)* + +Deletes key directly from underlying key/value db. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### findPath + +▸ **findPath**(`key`: Buffer, `cb`: Function): *void* + +*Defined in [baseTrie.ts:266](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L266)* + +Tries to find a path to the node for the given key +It returns a `stack` of nodes to the closet node + +**`method`** findPath + +**`memberof`** Trie + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### get + +▸ **get**(`key`: Buffer, `cb`: Function): *void* + +*Defined in [baseTrie.ts:125](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L125)* + +Gets a value given a `key` + +**`method`** get + +**`memberof`** Trie + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`key` | Buffer | the key to search for | +`cb` | Function | A callback `Function` which is given the arguments `err` - for errors that may have occured and `value` - the found value in a `Buffer` or if no value was found `null` | + +**Returns:** *void* + +___ + +### getRaw + +▸ **getRaw**(`key`: Buffer, `cb`: Function): *void* + +*Defined in [baseTrie.ts:208](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L208)* + +Retrieves a value directly from key/value db. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### put + +▸ **put**(`key`: Buffer, `value`: Buffer, `cb`: ErrorCallback): *void* + +*Defined in [baseTrie.ts:147](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L147)* + +Stores a given `value` at the given `key` + +**`method`** put + +**`memberof`** Trie + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`key` | Buffer | - | +`value` | Buffer | - | +`cb` | ErrorCallback | A callback `Function` which is given the argument `err` - for errors that may have occured | + +**Returns:** *void* + +___ + +### putRaw + +▸ **putRaw**(`key`: Buffer, `value`: Buffer, `cb`: ErrorCallback): *void* + +*Defined in [baseTrie.ts:217](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L217)* + +Writes a value under given key directly to the +key/value db. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`value` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### setRoot + +▸ **setRoot**(`value?`: Buffer): *void* + +*Defined in [baseTrie.ts:107](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L107)* + +**Parameters:** + +Name | Type | +------ | ------ | +`value?` | Buffer | + +**Returns:** *void* + +___ + +### `Static` fromProof + +▸ **fromProof**(`proofNodes`: Buffer[], `cb`: Function, `proofTrie?`: [Trie](_basetrie_.trie.md)): *void* + +*Defined in [baseTrie.ts:49](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L49)* + +**Parameters:** + +Name | Type | +------ | ------ | +`proofNodes` | Buffer[] | +`cb` | Function | +`proofTrie?` | [Trie](_basetrie_.trie.md) | + +**Returns:** *void* + +___ + +### `Static` prove + +▸ **prove**(`trie`: [Trie](_basetrie_.trie.md), `key`: Buffer, `cb`: Function): *void* + +*Defined in [baseTrie.ts:70](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L70)* + +**Parameters:** + +Name | Type | +------ | ------ | +`trie` | [Trie](_basetrie_.trie.md) | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### `Static` verifyProof + +▸ **verifyProof**(`rootHash`: Buffer, `key`: Buffer, `proofNodes`: Buffer[], `cb`: Function): *void* + +*Defined in [baseTrie.ts:85](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L85)* + +**Parameters:** + +Name | Type | +------ | ------ | +`rootHash` | Buffer | +`key` | Buffer | +`proofNodes` | Buffer[] | +`cb` | Function | + +**Returns:** *void* diff --git a/docs/classes/_checkpointtrie_.checkpointtrie.md b/docs/classes/_checkpointtrie_.checkpointtrie.md new file mode 100644 index 0000000..93bb5aa --- /dev/null +++ b/docs/classes/_checkpointtrie_.checkpointtrie.md @@ -0,0 +1,832 @@ +[merkle-patricia-tree](../README.md) › ["checkpointTrie"](../modules/_checkpointtrie_.md) › [CheckpointTrie](_checkpointtrie_.checkpointtrie.md) + +# Class: CheckpointTrie + +## Hierarchy + +* [Trie](_basetrie_.trie.md) + + ↳ **CheckpointTrie** + + ↳ [SecureTrie](_secure_.securetrie.md) + +## Index + +### Constructors + +* [constructor](_checkpointtrie_.checkpointtrie.md#constructor) + +### Properties + +* [EMPTY_TRIE_ROOT](_checkpointtrie_.checkpointtrie.md#empty_trie_root) +* [_checkpoints](_checkpointtrie_.checkpointtrie.md#_checkpoints) +* [_mainDB](_checkpointtrie_.checkpointtrie.md#_maindb) +* [_scratch](_checkpointtrie_.checkpointtrie.md#_scratch) +* [db](_checkpointtrie_.checkpointtrie.md#db) + +### Accessors + +* [isCheckpoint](_checkpointtrie_.checkpointtrie.md#ischeckpoint) +* [root](_checkpointtrie_.checkpointtrie.md#root) + +### Methods + +* [_createInitialNode](_checkpointtrie_.checkpointtrie.md#_createinitialnode) +* [_createScratchReadStream](_checkpointtrie_.checkpointtrie.md#private-_createscratchreadstream) +* [_deleteNode](_checkpointtrie_.checkpointtrie.md#_deletenode) +* [_enterCpMode](_checkpointtrie_.checkpointtrie.md#private-_entercpmode) +* [_exitCpMode](_checkpointtrie_.checkpointtrie.md#private-_exitcpmode) +* [_findDbNodes](_checkpointtrie_.checkpointtrie.md#_finddbnodes) +* [_findValueNodes](_checkpointtrie_.checkpointtrie.md#_findvaluenodes) +* [_formatNode](_checkpointtrie_.checkpointtrie.md#_formatnode) +* [_lookupNode](_checkpointtrie_.checkpointtrie.md#_lookupnode) +* [_putNode](_checkpointtrie_.checkpointtrie.md#_putnode) +* [_saveStack](_checkpointtrie_.checkpointtrie.md#private-_savestack) +* [_updateNode](_checkpointtrie_.checkpointtrie.md#private-_updatenode) +* [_walkTrie](_checkpointtrie_.checkpointtrie.md#_walktrie) +* [batch](_checkpointtrie_.checkpointtrie.md#batch) +* [checkRoot](_checkpointtrie_.checkpointtrie.md#checkroot) +* [checkpoint](_checkpointtrie_.checkpointtrie.md#checkpoint) +* [commit](_checkpointtrie_.checkpointtrie.md#commit) +* [copy](_checkpointtrie_.checkpointtrie.md#copy) +* [createReadStream](_checkpointtrie_.checkpointtrie.md#createreadstream) +* [del](_checkpointtrie_.checkpointtrie.md#del) +* [delRaw](_checkpointtrie_.checkpointtrie.md#delraw) +* [findPath](_checkpointtrie_.checkpointtrie.md#findpath) +* [get](_checkpointtrie_.checkpointtrie.md#get) +* [getRaw](_checkpointtrie_.checkpointtrie.md#getraw) +* [put](_checkpointtrie_.checkpointtrie.md#put) +* [putRaw](_checkpointtrie_.checkpointtrie.md#putraw) +* [revert](_checkpointtrie_.checkpointtrie.md#revert) +* [setRoot](_checkpointtrie_.checkpointtrie.md#setroot) +* [fromProof](_checkpointtrie_.checkpointtrie.md#static-fromproof) +* [prove](_checkpointtrie_.checkpointtrie.md#static-prove) +* [verifyProof](_checkpointtrie_.checkpointtrie.md#static-verifyproof) + +## Constructors + +### constructor + +\+ **new CheckpointTrie**(...`args`: any): *[CheckpointTrie](_checkpointtrie_.checkpointtrie.md)* + +*Overrides [Trie](_basetrie_.trie.md).[constructor](_basetrie_.trie.md#constructor)* + +*Defined in [checkpointTrie.ts:14](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L14)* + +**Parameters:** + +Name | Type | +------ | ------ | +`...args` | any | + +**Returns:** *[CheckpointTrie](_checkpointtrie_.checkpointtrie.md)* + +## Properties + +### EMPTY_TRIE_ROOT + +• **EMPTY_TRIE_ROOT**: *Buffer* + +*Inherited from [Trie](_basetrie_.trie.md).[EMPTY_TRIE_ROOT](_basetrie_.trie.md#empty_trie_root)* + +*Defined in [baseTrie.ts:34](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L34)* + +___ + +### _checkpoints + +• **_checkpoints**: *Buffer[]* + +*Defined in [checkpointTrie.ts:14](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L14)* + +___ + +### _mainDB + +• **_mainDB**: *DB* + +*Defined in [checkpointTrie.ts:12](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L12)* + +___ + +### _scratch + +• **_scratch**: *ScratchDB | null* + +*Defined in [checkpointTrie.ts:13](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L13)* + +___ + +### db + +• **db**: *DB* + +*Inherited from [Trie](_basetrie_.trie.md).[db](_basetrie_.trie.md#db)* + +*Defined in [baseTrie.ts:35](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L35)* + +## Accessors + +### isCheckpoint + +• **get isCheckpoint**(): *boolean* + +*Defined in [checkpointTrie.ts:29](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L29)* + +Is the trie during a checkpoint phase? + +**Returns:** *boolean* + +___ + +### root + +• **get root**(): *Buffer* + +*Inherited from [Trie](_basetrie_.trie.md).[root](_basetrie_.trie.md#root)* + +*Defined in [baseTrie.ts:103](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L103)* + +**Returns:** *Buffer* + +• **set root**(`value`: Buffer): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[root](_basetrie_.trie.md#root)* + +*Defined in [baseTrie.ts:99](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L99)* + +**Parameters:** + +Name | Type | +------ | ------ | +`value` | Buffer | + +**Returns:** *void* + +## Methods + +### _createInitialNode + +▸ **_createInitialNode**(`key`: Buffer, `value`: Buffer, `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_createInitialNode](_basetrie_.trie.md#_createinitialnode)* + +*Defined in [baseTrie.ts:735](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L735)* + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`value` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### `Private` _createScratchReadStream + +▸ **_createScratchReadStream**(`scratch`: ScratchDB): *ScratchReadStream‹›* + +*Defined in [checkpointTrie.ts:154](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L154)* + +Returns a `ScratchReadStream` based on the state updates +since checkpoint. + +**`method`** createScratchReadStream + +**Parameters:** + +Name | Type | +------ | ------ | +`scratch` | ScratchDB | + +**Returns:** *ScratchReadStream‹›* + +___ + +### _deleteNode + +▸ **_deleteNode**(`k`: Buffer, `stack`: TrieNode[], `cb`: Function): *any* + +*Inherited from [Trie](_basetrie_.trie.md).[_deleteNode](_basetrie_.trie.md#_deletenode)* + +*Defined in [baseTrie.ts:617](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L617)* + +**Parameters:** + +Name | Type | +------ | ------ | +`k` | Buffer | +`stack` | TrieNode[] | +`cb` | Function | + +**Returns:** *any* + +___ + +### `Private` _enterCpMode + +▸ **_enterCpMode**(): *void* + +*Defined in [checkpointTrie.ts:125](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L125)* + +Enter into checkpoint mode. + +**Returns:** *void* + +___ + +### `Private` _exitCpMode + +▸ **_exitCpMode**(`commitState`: boolean, `cb`: Function): *void* + +*Defined in [checkpointTrie.ts:134](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L134)* + +Exit from checkpoint mode. + +**Parameters:** + +Name | Type | +------ | ------ | +`commitState` | boolean | +`cb` | Function | + +**Returns:** *void* + +___ + +### _findDbNodes + +▸ **_findDbNodes**(`onFound`: Function, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_findDbNodes](_basetrie_.trie.md#_finddbnodes)* + +*Defined in [baseTrie.ts:346](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L346)* + +**Parameters:** + +Name | Type | +------ | ------ | +`onFound` | Function | +`cb` | Function | + +**Returns:** *void* + +___ + +### _findValueNodes + +▸ **_findValueNodes**(`onFound`: Function, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_findValueNodes](_basetrie_.trie.md#_findvaluenodes)* + +*Defined in [baseTrie.ts:320](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L320)* + +**Parameters:** + +Name | Type | +------ | ------ | +`onFound` | Function | +`cb` | Function | + +**Returns:** *void* + +___ + +### _formatNode + +▸ **_formatNode**(`node`: TrieNode, `topLevel`: boolean, `opStack`: BatchDBOp[], `remove`: boolean): *Buffer‹› | null | Buffer‹› | Buffer‹›[][]* + +*Overrides [Trie](_basetrie_.trie.md).[_formatNode](_basetrie_.trie.md#_formatnode)* + +*Defined in [checkpointTrie.ts:163](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L163)* + +**Parameters:** + +Name | Type | Default | +------ | ------ | ------ | +`node` | TrieNode | - | +`topLevel` | boolean | - | +`opStack` | BatchDBOp[] | - | +`remove` | boolean | false | + +**Returns:** *Buffer‹› | null | Buffer‹› | Buffer‹›[][]* + +___ + +### _lookupNode + +▸ **_lookupNode**(`node`: Buffer | Buffer[], `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_lookupNode](_basetrie_.trie.md#_lookupnode)* + +*Defined in [baseTrie.ts:230](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L230)* + +**Parameters:** + +Name | Type | +------ | ------ | +`node` | Buffer | Buffer[] | +`cb` | Function | + +**Returns:** *void* + +___ + +### _putNode + +▸ **_putNode**(`node`: TrieNode, `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_putNode](_basetrie_.trie.md#_putnode)* + +*Defined in [baseTrie.ts:247](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L247)* + +**Parameters:** + +Name | Type | +------ | ------ | +`node` | TrieNode | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### `Private` _saveStack + +▸ **_saveStack**(`key`: number[], `stack`: TrieNode[], `opStack`: BatchDBOp[], `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_saveStack](_basetrie_.trie.md#private-_savestack)* + +*Defined in [baseTrie.ts:588](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L588)* + +saves a stack + +**`method`** _saveStack + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`key` | number[] | the key. Should follow the stack | +`stack` | TrieNode[] | a stack of nodes to the value given by the key | +`opStack` | BatchDBOp[] | a stack of levelup operations to commit at the end of this funciton | +`cb` | ErrorCallback | | + +**Returns:** *void* + +___ + +### `Private` _updateNode + +▸ **_updateNode**(`k`: Buffer, `value`: Buffer, `keyRemainder`: number[], `stack`: TrieNode[], `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_updateNode](_basetrie_.trie.md#private-_updatenode)* + +*Defined in [baseTrie.ts:370](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L370)* + +Updates a node + +**`method`** _updateNode + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`k` | Buffer | - | +`value` | Buffer | - | +`keyRemainder` | number[] | - | +`stack` | TrieNode[] | - | +`cb` | ErrorCallback | the callback | + +**Returns:** *void* + +___ + +### _walkTrie + +▸ **_walkTrie**(`root`: Buffer, `onNode`: Function, `onDone`: Function): *any* + +*Inherited from [Trie](_basetrie_.trie.md).[_walkTrie](_basetrie_.trie.md#_walktrie)* + +*Defined in [baseTrie.ts:473](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L473)* + +**Parameters:** + +Name | Type | +------ | ------ | +`root` | Buffer | +`onNode` | Function | +`onDone` | Function | + +**Returns:** *any* + +___ + +### batch + +▸ **batch**(`ops`: BatchDBOp[], `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[batch](_basetrie_.trie.md#batch)* + +*Defined in [baseTrie.ts:799](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L799)* + +The given hash of operations (key additions or deletions) are executed on the DB + +**`method`** batch + +**`memberof`** Trie + +**`example`** +var ops = [ + { type: 'del', key: Buffer.from('father') } + , { type: 'put', key: Buffer.from('name'), value: Buffer.from('Yuri Irsenovich Kim') } + , { type: 'put', key: Buffer.from('dob'), value: Buffer.from('16 February 1941') } + , { type: 'put', key: Buffer.from('spouse'), value: Buffer.from('Kim Young-sook') } + , { type: 'put', key: Buffer.from('occupation'), value: Buffer.from('Clown') } +] +trie.batch(ops) + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`ops` | BatchDBOp[] | - | +`cb` | ErrorCallback | | + +**Returns:** *void* + +___ + +### checkRoot + +▸ **checkRoot**(`root`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[checkRoot](_basetrie_.trie.md#checkroot)* + +*Defined in [baseTrie.ts:819](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L819)* + +Checks if a given root exists + +**Parameters:** + +Name | Type | +------ | ------ | +`root` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### checkpoint + +▸ **checkpoint**(): *void* + +*Defined in [checkpointTrie.ts:39](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L39)* + +Creates a checkpoint that can later be reverted to or committed. +After this is called, no changes to the trie will be permanently saved +until `commit` is called. Calling `putRaw` overrides the checkpointing +mechanism and would directly write to db. + +**Returns:** *void* + +___ + +### commit + +▸ **commit**(`cb`: Function): *void* + +*Defined in [checkpointTrie.ts:56](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L56)* + +Commits a checkpoint to disk, if current checkpoint is not nested. If +nested, only sets the parent checkpoint as current checkpoint. + +**`method`** commit + +**`throws`** If not during a checkpoint phase + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`cb` | Function | the callback | + +**Returns:** *void* + +___ + +### copy + +▸ **copy**(`includeCheckpoints`: boolean): *[CheckpointTrie](_checkpointtrie_.checkpointtrie.md)* + +*Overrides [Trie](_basetrie_.trie.md).[copy](_basetrie_.trie.md#copy)* + +*Defined in [checkpointTrie.ts:101](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L101)* + +Returns a copy of the underlying trie with the interface +of CheckpointTrie. If during a checkpoint, the copy will +contain the checkpointing metadata (incl. reference to the same scratch). + +**Parameters:** + +Name | Type | Default | Description | +------ | ------ | ------ | ------ | +`includeCheckpoints` | boolean | true | If true and during a checkpoint, the copy will contain the checkpointing metadata and will use the same scratch as underlying db. | + +**Returns:** *[CheckpointTrie](_checkpointtrie_.checkpointtrie.md)* + +___ + +### createReadStream + +▸ **createReadStream**(): *ReadStream* + +*Inherited from [Trie](_basetrie_.trie.md).[createReadStream](_basetrie_.trie.md#createreadstream)* + +*Defined in [baseTrie.ts:772](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L772)* + +The `data` event is given an `Object` hat has two properties; the `key` and the `value`. Both should be Buffers. + +**`method`** createReadStream + +**`memberof`** Trie + +**Returns:** *ReadStream* + +Returns a [stream](https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_readable) of the contents of the `trie` + +___ + +### del + +▸ **del**(`key`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[del](_basetrie_.trie.md#del)* + +*Defined in [baseTrie.ts:183](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L183)* + +deletes a value given a `key` + +**`method`** del + +**`memberof`** Trie + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### delRaw + +▸ **delRaw**(`key`: Buffer, `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[delRaw](_basetrie_.trie.md#delraw)* + +*Defined in [baseTrie.ts:225](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L225)* + +Deletes key directly from underlying key/value db. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### findPath + +▸ **findPath**(`key`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[findPath](_basetrie_.trie.md#findpath)* + +*Defined in [baseTrie.ts:266](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L266)* + +Tries to find a path to the node for the given key +It returns a `stack` of nodes to the closet node + +**`method`** findPath + +**`memberof`** Trie + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### get + +▸ **get**(`key`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[get](_basetrie_.trie.md#get)* + +*Defined in [baseTrie.ts:125](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L125)* + +Gets a value given a `key` + +**`method`** get + +**`memberof`** Trie + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`key` | Buffer | the key to search for | +`cb` | Function | A callback `Function` which is given the arguments `err` - for errors that may have occured and `value` - the found value in a `Buffer` or if no value was found `null` | + +**Returns:** *void* + +___ + +### getRaw + +▸ **getRaw**(`key`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[getRaw](_basetrie_.trie.md#getraw)* + +*Defined in [baseTrie.ts:208](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L208)* + +Retrieves a value directly from key/value db. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### put + +▸ **put**(`key`: Buffer, `value`: Buffer, `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[put](_basetrie_.trie.md#put)* + +*Defined in [baseTrie.ts:147](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L147)* + +Stores a given `value` at the given `key` + +**`method`** put + +**`memberof`** Trie + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`key` | Buffer | - | +`value` | Buffer | - | +`cb` | ErrorCallback | A callback `Function` which is given the argument `err` - for errors that may have occured | + +**Returns:** *void* + +___ + +### putRaw + +▸ **putRaw**(`key`: Buffer, `value`: Buffer, `cb`: ErrorCallback): *void* + +*Overrides [Trie](_basetrie_.trie.md).[putRaw](_basetrie_.trie.md#putraw)* + +*Defined in [checkpointTrie.ts:117](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L117)* + +Writes a value under given key directly to the +key/value db, disregarding checkpoints. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`value` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### revert + +▸ **revert**(`cb`: Function): *void* + +*Defined in [checkpointTrie.ts:78](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L78)* + +Reverts the trie to the state it was at when `checkpoint` was first called. +If during a nested checkpoint, sets root to most recent checkpoint, and sets +parent checkpoint as current. + +**Parameters:** + +Name | Type | +------ | ------ | +`cb` | Function | + +**Returns:** *void* + +___ + +### setRoot + +▸ **setRoot**(`value?`: Buffer): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[setRoot](_basetrie_.trie.md#setroot)* + +*Defined in [baseTrie.ts:107](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L107)* + +**Parameters:** + +Name | Type | +------ | ------ | +`value?` | Buffer | + +**Returns:** *void* + +___ + +### `Static` fromProof + +▸ **fromProof**(`proofNodes`: Buffer[], `cb`: Function, `proofTrie?`: [Trie](_basetrie_.trie.md)): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[fromProof](_basetrie_.trie.md#static-fromproof)* + +*Defined in [baseTrie.ts:49](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L49)* + +**Parameters:** + +Name | Type | +------ | ------ | +`proofNodes` | Buffer[] | +`cb` | Function | +`proofTrie?` | [Trie](_basetrie_.trie.md) | + +**Returns:** *void* + +___ + +### `Static` prove + +▸ **prove**(`trie`: [Trie](_basetrie_.trie.md), `key`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[prove](_basetrie_.trie.md#static-prove)* + +*Defined in [baseTrie.ts:70](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L70)* + +**Parameters:** + +Name | Type | +------ | ------ | +`trie` | [Trie](_basetrie_.trie.md) | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### `Static` verifyProof + +▸ **verifyProof**(`rootHash`: Buffer, `key`: Buffer, `proofNodes`: Buffer[], `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[verifyProof](_basetrie_.trie.md#static-verifyproof)* + +*Defined in [baseTrie.ts:85](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L85)* + +**Parameters:** + +Name | Type | +------ | ------ | +`rootHash` | Buffer | +`key` | Buffer | +`proofNodes` | Buffer[] | +`cb` | Function | + +**Returns:** *void* diff --git a/docs/classes/_secure_.securetrie.md b/docs/classes/_secure_.securetrie.md new file mode 100644 index 0000000..6e48425 --- /dev/null +++ b/docs/classes/_secure_.securetrie.md @@ -0,0 +1,837 @@ +[merkle-patricia-tree](../README.md) › ["secure"](../modules/_secure_.md) › [SecureTrie](_secure_.securetrie.md) + +# Class: SecureTrie + +You can create a secure Trie where the keys are automatically hashed +using **keccak256** by using `require('merkle-patricia-tree/secure')`. +It has the same methods and constructor as `Trie`. + +**`class`** SecureTrie + +**`extends`** Trie + +## Hierarchy + + ↳ [CheckpointTrie](_checkpointtrie_.checkpointtrie.md) + + ↳ **SecureTrie** + +## Index + +### Constructors + +* [constructor](_secure_.securetrie.md#constructor) + +### Properties + +* [EMPTY_TRIE_ROOT](_secure_.securetrie.md#empty_trie_root) +* [_checkpoints](_secure_.securetrie.md#_checkpoints) +* [_mainDB](_secure_.securetrie.md#_maindb) +* [_scratch](_secure_.securetrie.md#_scratch) +* [db](_secure_.securetrie.md#db) + +### Accessors + +* [isCheckpoint](_secure_.securetrie.md#ischeckpoint) +* [root](_secure_.securetrie.md#root) + +### Methods + +* [_createInitialNode](_secure_.securetrie.md#_createinitialnode) +* [_createScratchReadStream](_secure_.securetrie.md#private-_createscratchreadstream) +* [_deleteNode](_secure_.securetrie.md#_deletenode) +* [_enterCpMode](_secure_.securetrie.md#private-_entercpmode) +* [_exitCpMode](_secure_.securetrie.md#private-_exitcpmode) +* [_findDbNodes](_secure_.securetrie.md#_finddbnodes) +* [_findValueNodes](_secure_.securetrie.md#_findvaluenodes) +* [_formatNode](_secure_.securetrie.md#_formatnode) +* [_lookupNode](_secure_.securetrie.md#_lookupnode) +* [_putNode](_secure_.securetrie.md#_putnode) +* [_saveStack](_secure_.securetrie.md#private-_savestack) +* [_updateNode](_secure_.securetrie.md#private-_updatenode) +* [_walkTrie](_secure_.securetrie.md#_walktrie) +* [batch](_secure_.securetrie.md#batch) +* [checkRoot](_secure_.securetrie.md#checkroot) +* [checkpoint](_secure_.securetrie.md#checkpoint) +* [commit](_secure_.securetrie.md#commit) +* [copy](_secure_.securetrie.md#copy) +* [createReadStream](_secure_.securetrie.md#createreadstream) +* [del](_secure_.securetrie.md#del) +* [delRaw](_secure_.securetrie.md#delraw) +* [findPath](_secure_.securetrie.md#findpath) +* [get](_secure_.securetrie.md#get) +* [getRaw](_secure_.securetrie.md#getraw) +* [put](_secure_.securetrie.md#put) +* [putRaw](_secure_.securetrie.md#putraw) +* [revert](_secure_.securetrie.md#revert) +* [setRoot](_secure_.securetrie.md#setroot) +* [fromProof](_secure_.securetrie.md#static-fromproof) +* [prove](_secure_.securetrie.md#static-prove) +* [verifyProof](_secure_.securetrie.md#static-verifyproof) + +## Constructors + +### constructor + +\+ **new SecureTrie**(...`args`: any): *[SecureTrie](_secure_.securetrie.md)* + +*Overrides [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[constructor](_checkpointtrie_.checkpointtrie.md#constructor)* + +*Defined in [secure.ts:13](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/secure.ts#L13)* + +**Parameters:** + +Name | Type | +------ | ------ | +`...args` | any | + +**Returns:** *[SecureTrie](_secure_.securetrie.md)* + +## Properties + +### EMPTY_TRIE_ROOT + +• **EMPTY_TRIE_ROOT**: *Buffer* + +*Inherited from [Trie](_basetrie_.trie.md).[EMPTY_TRIE_ROOT](_basetrie_.trie.md#empty_trie_root)* + +*Defined in [baseTrie.ts:34](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L34)* + +___ + +### _checkpoints + +• **_checkpoints**: *Buffer[]* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[_checkpoints](_checkpointtrie_.checkpointtrie.md#_checkpoints)* + +*Defined in [checkpointTrie.ts:14](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L14)* + +___ + +### _mainDB + +• **_mainDB**: *DB* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[_mainDB](_checkpointtrie_.checkpointtrie.md#_maindb)* + +*Defined in [checkpointTrie.ts:12](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L12)* + +___ + +### _scratch + +• **_scratch**: *ScratchDB | null* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[_scratch](_checkpointtrie_.checkpointtrie.md#_scratch)* + +*Defined in [checkpointTrie.ts:13](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L13)* + +___ + +### db + +• **db**: *DB* + +*Inherited from [Trie](_basetrie_.trie.md).[db](_basetrie_.trie.md#db)* + +*Defined in [baseTrie.ts:35](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L35)* + +## Accessors + +### isCheckpoint + +• **get isCheckpoint**(): *boolean* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[isCheckpoint](_checkpointtrie_.checkpointtrie.md#ischeckpoint)* + +*Defined in [checkpointTrie.ts:29](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L29)* + +Is the trie during a checkpoint phase? + +**Returns:** *boolean* + +___ + +### root + +• **get root**(): *Buffer* + +*Inherited from [Trie](_basetrie_.trie.md).[root](_basetrie_.trie.md#root)* + +*Defined in [baseTrie.ts:103](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L103)* + +**Returns:** *Buffer* + +• **set root**(`value`: Buffer): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[root](_basetrie_.trie.md#root)* + +*Defined in [baseTrie.ts:99](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L99)* + +**Parameters:** + +Name | Type | +------ | ------ | +`value` | Buffer | + +**Returns:** *void* + +## Methods + +### _createInitialNode + +▸ **_createInitialNode**(`key`: Buffer, `value`: Buffer, `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_createInitialNode](_basetrie_.trie.md#_createinitialnode)* + +*Defined in [baseTrie.ts:735](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L735)* + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`value` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### `Private` _createScratchReadStream + +▸ **_createScratchReadStream**(`scratch`: ScratchDB): *ScratchReadStream‹›* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[_createScratchReadStream](_checkpointtrie_.checkpointtrie.md#private-_createscratchreadstream)* + +*Defined in [checkpointTrie.ts:154](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L154)* + +Returns a `ScratchReadStream` based on the state updates +since checkpoint. + +**`method`** createScratchReadStream + +**Parameters:** + +Name | Type | +------ | ------ | +`scratch` | ScratchDB | + +**Returns:** *ScratchReadStream‹›* + +___ + +### _deleteNode + +▸ **_deleteNode**(`k`: Buffer, `stack`: TrieNode[], `cb`: Function): *any* + +*Inherited from [Trie](_basetrie_.trie.md).[_deleteNode](_basetrie_.trie.md#_deletenode)* + +*Defined in [baseTrie.ts:617](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L617)* + +**Parameters:** + +Name | Type | +------ | ------ | +`k` | Buffer | +`stack` | TrieNode[] | +`cb` | Function | + +**Returns:** *any* + +___ + +### `Private` _enterCpMode + +▸ **_enterCpMode**(): *void* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[_enterCpMode](_checkpointtrie_.checkpointtrie.md#private-_entercpmode)* + +*Defined in [checkpointTrie.ts:125](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L125)* + +Enter into checkpoint mode. + +**Returns:** *void* + +___ + +### `Private` _exitCpMode + +▸ **_exitCpMode**(`commitState`: boolean, `cb`: Function): *void* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[_exitCpMode](_checkpointtrie_.checkpointtrie.md#private-_exitcpmode)* + +*Defined in [checkpointTrie.ts:134](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L134)* + +Exit from checkpoint mode. + +**Parameters:** + +Name | Type | +------ | ------ | +`commitState` | boolean | +`cb` | Function | + +**Returns:** *void* + +___ + +### _findDbNodes + +▸ **_findDbNodes**(`onFound`: Function, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_findDbNodes](_basetrie_.trie.md#_finddbnodes)* + +*Defined in [baseTrie.ts:346](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L346)* + +**Parameters:** + +Name | Type | +------ | ------ | +`onFound` | Function | +`cb` | Function | + +**Returns:** *void* + +___ + +### _findValueNodes + +▸ **_findValueNodes**(`onFound`: Function, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_findValueNodes](_basetrie_.trie.md#_findvaluenodes)* + +*Defined in [baseTrie.ts:320](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L320)* + +**Parameters:** + +Name | Type | +------ | ------ | +`onFound` | Function | +`cb` | Function | + +**Returns:** *void* + +___ + +### _formatNode + +▸ **_formatNode**(`node`: TrieNode, `topLevel`: boolean, `opStack`: BatchDBOp[], `remove`: boolean): *Buffer‹› | null | Buffer‹› | Buffer‹›[][]* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[_formatNode](_checkpointtrie_.checkpointtrie.md#_formatnode)* + +*Overrides [Trie](_basetrie_.trie.md).[_formatNode](_basetrie_.trie.md#_formatnode)* + +*Defined in [checkpointTrie.ts:163](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L163)* + +**Parameters:** + +Name | Type | Default | +------ | ------ | ------ | +`node` | TrieNode | - | +`topLevel` | boolean | - | +`opStack` | BatchDBOp[] | - | +`remove` | boolean | false | + +**Returns:** *Buffer‹› | null | Buffer‹› | Buffer‹›[][]* + +___ + +### _lookupNode + +▸ **_lookupNode**(`node`: Buffer | Buffer[], `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_lookupNode](_basetrie_.trie.md#_lookupnode)* + +*Defined in [baseTrie.ts:230](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L230)* + +**Parameters:** + +Name | Type | +------ | ------ | +`node` | Buffer | Buffer[] | +`cb` | Function | + +**Returns:** *void* + +___ + +### _putNode + +▸ **_putNode**(`node`: TrieNode, `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_putNode](_basetrie_.trie.md#_putnode)* + +*Defined in [baseTrie.ts:247](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L247)* + +**Parameters:** + +Name | Type | +------ | ------ | +`node` | TrieNode | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### `Private` _saveStack + +▸ **_saveStack**(`key`: number[], `stack`: TrieNode[], `opStack`: BatchDBOp[], `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_saveStack](_basetrie_.trie.md#private-_savestack)* + +*Defined in [baseTrie.ts:588](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L588)* + +saves a stack + +**`method`** _saveStack + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`key` | number[] | the key. Should follow the stack | +`stack` | TrieNode[] | a stack of nodes to the value given by the key | +`opStack` | BatchDBOp[] | a stack of levelup operations to commit at the end of this funciton | +`cb` | ErrorCallback | | + +**Returns:** *void* + +___ + +### `Private` _updateNode + +▸ **_updateNode**(`k`: Buffer, `value`: Buffer, `keyRemainder`: number[], `stack`: TrieNode[], `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[_updateNode](_basetrie_.trie.md#private-_updatenode)* + +*Defined in [baseTrie.ts:370](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L370)* + +Updates a node + +**`method`** _updateNode + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`k` | Buffer | - | +`value` | Buffer | - | +`keyRemainder` | number[] | - | +`stack` | TrieNode[] | - | +`cb` | ErrorCallback | the callback | + +**Returns:** *void* + +___ + +### _walkTrie + +▸ **_walkTrie**(`root`: Buffer, `onNode`: Function, `onDone`: Function): *any* + +*Inherited from [Trie](_basetrie_.trie.md).[_walkTrie](_basetrie_.trie.md#_walktrie)* + +*Defined in [baseTrie.ts:473](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L473)* + +**Parameters:** + +Name | Type | +------ | ------ | +`root` | Buffer | +`onNode` | Function | +`onDone` | Function | + +**Returns:** *any* + +___ + +### batch + +▸ **batch**(`ops`: BatchDBOp[], `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[batch](_basetrie_.trie.md#batch)* + +*Defined in [baseTrie.ts:799](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L799)* + +The given hash of operations (key additions or deletions) are executed on the DB + +**`method`** batch + +**`memberof`** Trie + +**`example`** +var ops = [ + { type: 'del', key: Buffer.from('father') } + , { type: 'put', key: Buffer.from('name'), value: Buffer.from('Yuri Irsenovich Kim') } + , { type: 'put', key: Buffer.from('dob'), value: Buffer.from('16 February 1941') } + , { type: 'put', key: Buffer.from('spouse'), value: Buffer.from('Kim Young-sook') } + , { type: 'put', key: Buffer.from('occupation'), value: Buffer.from('Clown') } +] +trie.batch(ops) + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`ops` | BatchDBOp[] | - | +`cb` | ErrorCallback | | + +**Returns:** *void* + +___ + +### checkRoot + +▸ **checkRoot**(`root`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[checkRoot](_basetrie_.trie.md#checkroot)* + +*Defined in [baseTrie.ts:819](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L819)* + +Checks if a given root exists + +**Parameters:** + +Name | Type | +------ | ------ | +`root` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### checkpoint + +▸ **checkpoint**(): *void* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[checkpoint](_checkpointtrie_.checkpointtrie.md#checkpoint)* + +*Defined in [checkpointTrie.ts:39](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L39)* + +Creates a checkpoint that can later be reverted to or committed. +After this is called, no changes to the trie will be permanently saved +until `commit` is called. Calling `putRaw` overrides the checkpointing +mechanism and would directly write to db. + +**Returns:** *void* + +___ + +### commit + +▸ **commit**(`cb`: Function): *void* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[commit](_checkpointtrie_.checkpointtrie.md#commit)* + +*Defined in [checkpointTrie.ts:56](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L56)* + +Commits a checkpoint to disk, if current checkpoint is not nested. If +nested, only sets the parent checkpoint as current checkpoint. + +**`method`** commit + +**`throws`** If not during a checkpoint phase + +**Parameters:** + +Name | Type | Description | +------ | ------ | ------ | +`cb` | Function | the callback | + +**Returns:** *void* + +___ + +### copy + +▸ **copy**(): *[SecureTrie](_secure_.securetrie.md)* + +*Overrides [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[copy](_checkpointtrie_.checkpointtrie.md#copy)* + +*Defined in [secure.ts:28](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/secure.ts#L28)* + +**Returns:** *[SecureTrie](_secure_.securetrie.md)* + +___ + +### createReadStream + +▸ **createReadStream**(): *ReadStream* + +*Inherited from [Trie](_basetrie_.trie.md).[createReadStream](_basetrie_.trie.md#createreadstream)* + +*Defined in [baseTrie.ts:772](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L772)* + +The `data` event is given an `Object` hat has two properties; the `key` and the `value`. Both should be Buffers. + +**`method`** createReadStream + +**`memberof`** Trie + +**Returns:** *ReadStream* + +Returns a [stream](https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_readable) of the contents of the `trie` + +___ + +### del + +▸ **del**(`key`: Buffer, `cb`: ErrorCallback): *void* + +*Overrides [Trie](_basetrie_.trie.md).[del](_basetrie_.trie.md#del)* + +*Defined in [secure.ts:52](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/secure.ts#L52)* + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### delRaw + +▸ **delRaw**(`key`: Buffer, `cb`: ErrorCallback): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[delRaw](_basetrie_.trie.md#delraw)* + +*Defined in [baseTrie.ts:225](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L225)* + +Deletes key directly from underlying key/value db. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### findPath + +▸ **findPath**(`key`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[findPath](_basetrie_.trie.md#findpath)* + +*Defined in [baseTrie.ts:266](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L266)* + +Tries to find a path to the node for the given key +It returns a `stack` of nodes to the closet node + +**`method`** findPath + +**`memberof`** Trie + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### get + +▸ **get**(`key`: Buffer, `cb`: Function): *void* + +*Overrides [Trie](_basetrie_.trie.md).[get](_basetrie_.trie.md#get)* + +*Defined in [secure.ts:34](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/secure.ts#L34)* + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### getRaw + +▸ **getRaw**(`key`: Buffer, `cb`: Function): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[getRaw](_basetrie_.trie.md#getraw)* + +*Defined in [baseTrie.ts:208](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L208)* + +Retrieves a value directly from key/value db. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### put + +▸ **put**(`key`: Buffer, `val`: Buffer, `cb`: ErrorCallback): *void* + +*Overrides [Trie](_basetrie_.trie.md).[put](_basetrie_.trie.md#put)* + +*Defined in [secure.ts:43](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/secure.ts#L43)* + +For a falsey value, use the original key +to avoid double hashing the key. + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`val` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### putRaw + +▸ **putRaw**(`key`: Buffer, `value`: Buffer, `cb`: ErrorCallback): *void* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[putRaw](_checkpointtrie_.checkpointtrie.md#putraw)* + +*Overrides [Trie](_basetrie_.trie.md).[putRaw](_basetrie_.trie.md#putraw)* + +*Defined in [checkpointTrie.ts:117](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L117)* + +Writes a value under given key directly to the +key/value db, disregarding checkpoints. + +**`deprecated`** + +**Parameters:** + +Name | Type | +------ | ------ | +`key` | Buffer | +`value` | Buffer | +`cb` | ErrorCallback | + +**Returns:** *void* + +___ + +### revert + +▸ **revert**(`cb`: Function): *void* + +*Inherited from [CheckpointTrie](_checkpointtrie_.checkpointtrie.md).[revert](_checkpointtrie_.checkpointtrie.md#revert)* + +*Defined in [checkpointTrie.ts:78](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/checkpointTrie.ts#L78)* + +Reverts the trie to the state it was at when `checkpoint` was first called. +If during a nested checkpoint, sets root to most recent checkpoint, and sets +parent checkpoint as current. + +**Parameters:** + +Name | Type | +------ | ------ | +`cb` | Function | + +**Returns:** *void* + +___ + +### setRoot + +▸ **setRoot**(`value?`: Buffer): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[setRoot](_basetrie_.trie.md#setroot)* + +*Defined in [baseTrie.ts:107](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L107)* + +**Parameters:** + +Name | Type | +------ | ------ | +`value?` | Buffer | + +**Returns:** *void* + +___ + +### `Static` fromProof + +▸ **fromProof**(`proofNodes`: Buffer[], `cb`: Function, `proofTrie?`: [Trie](_basetrie_.trie.md)): *void* + +*Inherited from [Trie](_basetrie_.trie.md).[fromProof](_basetrie_.trie.md#static-fromproof)* + +*Defined in [baseTrie.ts:49](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.ts#L49)* + +**Parameters:** + +Name | Type | +------ | ------ | +`proofNodes` | Buffer[] | +`cb` | Function | +`proofTrie?` | [Trie](_basetrie_.trie.md) | + +**Returns:** *void* + +___ + +### `Static` prove + +▸ **prove**(`trie`: [SecureTrie](_secure_.securetrie.md), `key`: Buffer, `cb`: Function): *void* + +*Overrides [Trie](_basetrie_.trie.md).[prove](_basetrie_.trie.md#static-prove)* + +*Defined in [secure.ts:18](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/secure.ts#L18)* + +**Parameters:** + +Name | Type | +------ | ------ | +`trie` | [SecureTrie](_secure_.securetrie.md) | +`key` | Buffer | +`cb` | Function | + +**Returns:** *void* + +___ + +### `Static` verifyProof + +▸ **verifyProof**(`rootHash`: Buffer, `key`: Buffer, `proof`: Buffer[], `cb`: Function): *void* + +*Overrides [Trie](_basetrie_.trie.md).[verifyProof](_basetrie_.trie.md#static-verifyproof)* + +*Defined in [secure.ts:23](https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/secure.ts#L23)* + +**Parameters:** + +Name | Type | +------ | ------ | +`rootHash` | Buffer | +`key` | Buffer | +`proof` | Buffer[] | +`cb` | Function | + +**Returns:** *void* diff --git a/docs/index.md b/docs/index.md deleted file mode 100644 index 65ad2e1..0000000 --- a/docs/index.md +++ /dev/null @@ -1,493 +0,0 @@ - - -## SecureTrie - -[src/secure.js:12-55][1] - -**Extends Trie** - -You can create a secure Trie where the keys are automatically hashed -using **keccak256** by using `require('merkle-patricia-tree/secure')`. -It has the same methods and constructor as `Trie`. - -## Trie - -[src/baseTrie.js:25-768][2] - -Use `require('merkel-patricia-tree')` for the base interface. In Ethereum applications -stick with the Secure Trie Overlay `require('merkel-patricia-tree/secure')`. -The API for the raw and the secure interface are about the same - -### Parameters - -- `db` **[Object][3]?** A [levelup][4] instance. By default creates an in-memory [memdown][5] instance. - If the db is `null` or left undefined, then the trie will be stored in memory via [memdown][5] -- `root` **([Buffer][6] \| [String][7])?** A hex `String` or `Buffer` for the root of a previously stored trie - -### Properties - -- `root` **[Buffer][6]** The current root of the `trie` -- `EMPTY_TRIE_ROOT` **[Buffer][6]** the Root for an empty trie - -### get - -[src/baseTrie.js:92-104][8] - -Gets a value given a `key` - -#### Parameters - -- `key` **([Buffer][6] \| [String][7])** the key to search for -- `cb` **[Function][9]** A callback `Function` which is given the arguments `err` - for errors that may have occured and `value` - the found value in a `Buffer` or if no value was found `null` - -### put - -[src/baseTrie.js:114-138][10] - -Stores a given `value` at the given `key` - -#### Parameters - -- `key` **([Buffer][6] \| [String][7])** -- `Value` **([Buffer][6] \| [String][7])** -- `cb` **[Function][9]** A callback `Function` which is given the argument `err` - for errors that may have occured - -### del - -[src/baseTrie.js:147-163][11] - -deletes a value given a `key` - -#### Parameters - -- `key` **([Buffer][6] \| [String][7])** -- `callback` **[Function][9]** the callback `Function` - -### findPath - -[src/baseTrie.js:226-272][12] - -Tries to find a path to the node for the given key -It returns a `stack` of nodes to the closet node - -#### Parameters - -- `null-null` **([String][7] \| [Buffer][6])** key - the search key -- `null-null` **[Function][9]** cb - the callback function. Its is given the following - arguments- err - any errors encontered - - node - the last node found - - keyRemainder - the remaining key nibbles not accounted for - - stack - an array of nodes that forms the path to node we are searching for - -### createReadStream - -[src/baseTrie.js:716-718][13] - -The `data` event is given an `Object` hat has two properties; the `key` and the `value`. Both should be Buffers. - -Returns **[stream.Readable][14]** Returns a [stream][15] of the contents of the `trie` - -### batch - -[src/baseTrie.js:743-753][16] - -The given hash of operations (key additions or deletions) are executed on the DB - -#### Parameters - -- `ops` **[Array][17]** -- `cb` **[Function][9]** - -#### Examples - -```javascript -var ops = [ - { type: 'del', key: 'father' } - , { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' } - , { type: 'put', key: 'dob', value: '16 February 1941' } - , { type: 'put', key: 'spouse', value: 'Kim Young-sook' } - , { type: 'put', key: 'occupation', value: 'Clown' } -] -trie.batch(ops) -``` - -### checkRoot - -[src/baseTrie.js:762-767][18] - -Checks if a given root exists - -#### Parameters - -- `root` **[Buffer][6]** -- `cb` **[Function][9]** - -## Merkle Proof - -Static functions for creating/verifying a merkle proof. - - -## Internal Util Functions - -These are not exposed. - - -### addHexPrefix - -[src/util/hex.js:7-22][19] - -Prepends hex prefix to an array of nibbles. - -#### Parameters - -- `Array` **[Array][17]** of nibbles - -Returns **[Array][17]** returns buffer of encoded data -\* - -### asyncFirstSeries - -[src/util/async.js:38-54][20] - -Take a collection of async fns, call the cb on the first to return a truthy value. -If all run without a truthy result, return undefined - -#### Parameters - -- `array` -- `iterator` -- `cb` - -### doKeysMatch - -[src/util/nibbles.js:56-59][21] - -Compare two nibble array keys. - -#### Parameters - -- `keyA` **[Array][17]** -- `keyB` **[Array][17]** - -## - -[src/db.js:3-3][22] - -DB is a thin wrapper around the underlying levelup db, -which validates inputs and sets encoding type. - -## - -[src/util/async.js:3-6][23] - -Take two or more functions and returns a function that will execute all of -the given functions - -## - -[src/scratch.js:4-4][24] - -An in-memory wrap over `DB` with an upstream DB -which will be queried when a key is not found -in the in-memory scratch. This class is used to implement -checkpointing functionality in CheckpointTrie. - -## constructor - -[src/db.js:15-17][25] - -Initialize a DB instance. If `leveldb` is not provided, DB -defaults to an [in-memory store][5]. - -### Parameters - -- `leveldb` **[Object][3]?** An abstract-leveldown compliant store - -## get - -[src/db.js:26-36][26] - -Retrieves a raw value from leveldb. - -### Parameters - -- `key` **[Buffer][6]** -- `cb` **[Function][9]** A callback `Function`, which is given the arguments - `err` - for errors that may have occured - and `value` - the found value in a `Buffer` or if no value was found `null`. - -## put - -[src/db.js:45-50][27] - -Writes a value directly to leveldb. - -### Parameters - -- `key` **[Buffer][6]** The key as a `Buffer` or `String` -- `val` -- `cb` **[Function][9]** A callback `Function`, which is given the argument - `err` - for errors that may have occured -- `value` **[Buffer][6]** The value to be stored - -## del - -[src/db.js:58-62][28] - -Removes a raw value in the underlying leveldb. - -### Parameters - -- `key` **[Buffer][6]** -- `cb` **[Function][9]** A callback `Function`, which is given the argument - `err` - for errors that may have occured - -## batch - -[src/db.js:70-74][29] - -Performs a batch operation on db. - -### Parameters - -- `opStack` **[Array][17]** A stack of levelup operations -- `cb` **[Function][9]** A callback `Function`, which is given the argument - `err` - for errors that may have occured - -## copy - -[src/db.js:80-82][30] - -Returns a copy of the DB instance, with a reference -to the **same** underlying leveldb instance. - -## isCheckpoint - -[src/checkpointTrie.js:22-24][31] - -Is the trie during a checkpoint phase? - -## copy - -[src/checkpointTrie.js:97-106][32] - -Returns a copy of the underlying trie with the interface -of CheckpointTrie. If during a checkpoint, the copy will -contain the checkpointing metadata (incl. reference to the same scratch). - -### Parameters - -- `includeCheckpoints` **[boolean][33]** If true and during a checkpoint, the copy will - contain the checkpointing metadata and will use the same scratch as underlying db. (optional, default `true`) - -## putRaw - -[src/checkpointTrie.js:113-115][34] - -Writes a value under given key directly to the -key/value db, disregarding checkpoints. - -### Parameters - -- `key` -- `value` -- `cb` - -**Meta** - -- **deprecated**: This is deprecated. - - -## get - -[src/scratch.js:22-35][35] - -Similar to `DB.get`, but first searches in-memory -scratch DB, if key not found, searches upstream DB. - -### Parameters - -- `key` -- `cb` - -## checkpoint - -[src/checkpointTrie.js:33-41][36] - -Creates a checkpoint that can later be reverted to or committed. -After this is called, no changes to the trie will be permanently saved -until `commit` is called. Calling `putRaw` overrides the checkpointing -mechanism and would directly write to db. - -## put - -[src/secure.js:42-49][37] - -For a falsey value, use the original key -to avoid double hashing the key. - -### Parameters - -- `key` -- `val` -- `cb` - -## commit - -[src/checkpointTrie.js:50-65][38] - -Commits a checkpoint to disk, if current checkpoint is not nested. If -nested, only sets the parent checkpoint as current checkpoint. - -### Parameters - -- `cb` **[Function][9]** the callback - - -- Throws **any** If not during a checkpoint phase - -## revert - -[src/checkpointTrie.js:74-88][39] - -Reverts the trie to the state it was at when `checkpoint` was first called. -If during a nested checkpoint, sets root to most recent checkpoint, and sets -parent checkpoint as current. - -### Parameters - -- `cb` **[Function][9]** the callback - -## getRaw - -[src/baseTrie.js:169-171][40] - -Retrieves a value directly from key/value db. - -### Parameters - -- `key` -- `cb` - -**Meta** - -- **deprecated**: This is deprecated. - - -## putRaw - -[src/baseTrie.js:178-180][41] - -Writes a value under given key directly to the -key/value db. - -### Parameters - -- `key` -- `value` -- `cb` - -**Meta** - -- **deprecated**: This is deprecated. - - -## delRaw - -[src/baseTrie.js:186-188][42] - -Deletes key directly from underlying key/value db. - -### Parameters - -- `key` -- `cb` - -**Meta** - -- **deprecated**: This is deprecated. - - -[1]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/secure.js#L12-L55 "Source code on GitHub" - -[2]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L25-L768 "Source code on GitHub" - -[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object - -[4]: https://github.com/Level/levelup - -[5]: https://github.com/Level/memdown - -[6]: https://nodejs.org/api/buffer.html - -[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String - -[8]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L92-L104 "Source code on GitHub" - -[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function - -[10]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L114-L138 "Source code on GitHub" - -[11]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L147-L163 "Source code on GitHub" - -[12]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L226-L272 "Source code on GitHub" - -[13]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L716-L718 "Source code on GitHub" - -[14]: https://nodejs.org/api/stream.html#stream_class_stream_readable - -[15]: https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_readable - -[16]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L743-L753 "Source code on GitHub" - -[17]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array - -[18]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L762-L767 "Source code on GitHub" - -[19]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/util/hex.js#L7-L22 "Source code on GitHub" - -[20]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/util/async.js#L38-L54 "Source code on GitHub" - -[21]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/util/nibbles.js#L56-L59 "Source code on GitHub" - -[22]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/db.js#L3-L3 "Source code on GitHub" - -[23]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/util/async.js#L3-L6 "Source code on GitHub" - -[24]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/scratch.js#L4-L4 "Source code on GitHub" - -[25]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/db.js#L15-L17 "Source code on GitHub" - -[26]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/db.js#L26-L36 "Source code on GitHub" - -[27]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/db.js#L45-L50 "Source code on GitHub" - -[28]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/db.js#L58-L62 "Source code on GitHub" - -[29]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/db.js#L70-L74 "Source code on GitHub" - -[30]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/db.js#L80-L82 "Source code on GitHub" - -[31]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/checkpointTrie.js#L22-L24 "Source code on GitHub" - -[32]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/checkpointTrie.js#L97-L106 "Source code on GitHub" - -[33]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean - -[34]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/checkpointTrie.js#L113-L115 "Source code on GitHub" - -[35]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/scratch.js#L22-L35 "Source code on GitHub" - -[36]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/checkpointTrie.js#L33-L41 "Source code on GitHub" - -[37]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/secure.js#L42-L49 "Source code on GitHub" - -[38]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/checkpointTrie.js#L50-L65 "Source code on GitHub" - -[39]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/checkpointTrie.js#L74-L88 "Source code on GitHub" - -[40]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L169-L171 "Source code on GitHub" - -[41]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L178-L180 "Source code on GitHub" - -[42]: https://github.com/ethereumjs/merkle-patricia-tree/blob/2685d6759e18abeba0967b151bf965a5b1f9eec8/src/baseTrie.js#L186-L188 "Source code on GitHub" diff --git a/docs/modules/_basetrie_.md b/docs/modules/_basetrie_.md new file mode 100644 index 0000000..dcd7e84 --- /dev/null +++ b/docs/modules/_basetrie_.md @@ -0,0 +1,9 @@ +[merkle-patricia-tree](../README.md) › ["baseTrie"](_basetrie_.md) + +# Module: "baseTrie" + +## Index + +### Classes + +* [Trie](../classes/_basetrie_.trie.md) diff --git a/docs/modules/_checkpointtrie_.md b/docs/modules/_checkpointtrie_.md new file mode 100644 index 0000000..0982f98 --- /dev/null +++ b/docs/modules/_checkpointtrie_.md @@ -0,0 +1,9 @@ +[merkle-patricia-tree](../README.md) › ["checkpointTrie"](_checkpointtrie_.md) + +# Module: "checkpointTrie" + +## Index + +### Classes + +* [CheckpointTrie](../classes/_checkpointtrie_.checkpointtrie.md) diff --git a/docs/modules/_secure_.md b/docs/modules/_secure_.md new file mode 100644 index 0000000..b12ae9d --- /dev/null +++ b/docs/modules/_secure_.md @@ -0,0 +1,9 @@ +[merkle-patricia-tree](../README.md) › ["secure"](_secure_.md) + +# Module: "secure" + +## Index + +### Classes + +* [SecureTrie](../classes/_secure_.securetrie.md) diff --git a/package.json b/package.json index 63d7ace..f537b2c 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "prepublishOnly": "npm run test && npm run build", "coverage": "nyc npm run test:node && nyc report --reporter=text-lcov > .nyc_output/lcov.info", "coveralls": "npm run coverage && coveralls < .nyc_output/lcov.info", - "docs:build": "typedoc src", + "docs:build": "npx typedoc", "tslint": "ethereumjs-config-tslint", "tslint:fix": "ethereumjs-config-tslint-fix", "lint": "ethereumjs-config-lint", @@ -76,8 +76,8 @@ "prettier": "^1.18.2", "tape": "^4.10.1", "tslint": "^5.18.0", - "typedoc": "^0.15.0", - "typedoc-plugin-markdown": "^2.1.0", + "typedoc": "next", + "typedoc-plugin-markdown": "^2.2.16", "typescript": "^3.5.3", "typestrict": "^1.0.2" }, diff --git a/src/baseTrie.ts b/src/baseTrie.ts index 6b25872..4661fe9 100644 --- a/src/baseTrie.ts +++ b/src/baseTrie.ts @@ -119,7 +119,7 @@ export class Trie { * Gets a value given a `key` * @method get * @memberof Trie - * @param {Buffer|String} key - the key to search for + * @param {Buffer} key - the key to search for * @param {Function} cb A callback `Function` which is given the arguments `err` - for errors that may have occured and `value` - the found value in a `Buffer` or if no value was found `null` */ get(key: Buffer, cb: Function) { @@ -140,8 +140,8 @@ export class Trie { * Stores a given `value` at the given `key` * @method put * @memberof Trie - * @param {Buffer|String} key - * @param {Buffer|String} Value + * @param {Buffer} key + * @param {Buffer} Value * @param {Function} cb A callback `Function` which is given the argument `err` - for errors that may have occured */ put(key: Buffer, value: Buffer, cb: ErrorCallback) { @@ -177,7 +177,7 @@ export class Trie { * deletes a value given a `key` * @method del * @memberof Trie - * @param {Buffer|String} key + * @param {Buffer} key * @param {Function} callback the callback `Function` */ del(key: Buffer, cb: Function) { @@ -786,11 +786,11 @@ export class Trie { * @memberof Trie * @example * var ops = [ - * { type: 'del', key: 'father' } - * , { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' } - * , { type: 'put', key: 'dob', value: '16 February 1941' } - * , { type: 'put', key: 'spouse', value: 'Kim Young-sook' } - * , { type: 'put', key: 'occupation', value: 'Clown' } + * { type: 'del', key: Buffer.from('father') } + * , { type: 'put', key: Buffer.from('name'), value: Buffer.from('Yuri Irsenovich Kim') } + * , { type: 'put', key: Buffer.from('dob'), value: Buffer.from('16 February 1941') } + * , { type: 'put', key: Buffer.from('spouse'), value: Buffer.from('Kim Young-sook') } + * , { type: 'put', key: Buffer.from('occupation'), value: Buffer.from('Clown') } * ] * trie.batch(ops) * @param {Array} ops diff --git a/test/checkpoint.js b/test/checkpoint.js index da670cb..6020f94 100644 --- a/test/checkpoint.js +++ b/test/checkpoint.js @@ -1,69 +1,69 @@ const tape = require('tape') const Trie = require('../dist/checkpointTrie').CheckpointTrie -tape('testing checkpoints', function (tester) { +tape('testing checkpoints', function(tester) { let trie, preRoot, postRoot, trieCopy const it = tester.test - it('setup', function (t) { + it('setup', function(t) { trie = new Trie() - trie.put('do', 'verb', function () { - trie.put('doge', 'coin', function () { + trie.put(Buffer.from('do'), Buffer.from('verb'), function() { + trie.put(Buffer.from('doge'), Buffer.from('coin'), function() { preRoot = trie.root.toString('hex') t.end() }) }) }) - it('should copy trie and get value before checkpoint', function (t) { + it('should copy trie and get value before checkpoint', function(t) { trieCopy = trie.copy() t.equal(trieCopy.root.toString('hex'), preRoot) - trieCopy.get('do', function (err, res) { + trieCopy.get(Buffer.from('do'), function(err, res) { t.error(err) t.ok(Buffer.from('verb').equals(res)) t.end() }) }) - it('should create a checkpoint', function (t) { + it('should create a checkpoint', function(t) { trie.checkpoint() t.end() }) - it('should save to the cache', function (t) { - trie.put('test', 'something', function () { - trie.put('love', 'emotion', function () { + it('should save to the cache', function(t) { + trie.put(Buffer.from('test'), Buffer.from('something'), function() { + trie.put(Buffer.from('love'), Buffer.from('emotion'), function() { postRoot = trie.root.toString('hex') t.end() }) }) }) - it('should get values from before checkpoint', function (t) { - trie.get('doge', function (err, res) { + it('should get values from before checkpoint', function(t) { + trie.get(Buffer.from('doge'), function(err, res) { t.error(err) t.ok(Buffer.from('coin').equals(res)) t.end() }) }) - it('should get values from cache', function (t) { - trie.get('love', function (err, res) { + it('should get values from cache', function(t) { + trie.get(Buffer.from('love'), function(err, res) { t.error(err) t.ok(Buffer.from('emotion').equals(res)) t.end() }) }) - it('should copy trie and get upstream and cache values after checkpoint', function (t) { + it('should copy trie and get upstream and cache values after checkpoint', function(t) { trieCopy = trie.copy() t.equal(trieCopy.root.toString('hex'), postRoot) t.equal(trieCopy._checkpoints.length, 1) t.ok(trieCopy.isCheckpoint) - trieCopy.get('do', function (err, res) { + trieCopy.get(Buffer.from('do'), function(err, res) { t.error(err) t.ok(Buffer.from('verb').equals(res)) - trieCopy.get('love', function (err, res) { + trieCopy.get(Buffer.from('love'), function(err, res) { t.error(err) t.ok(Buffer.from('emotion').equals(res)) t.end() @@ -71,28 +71,28 @@ tape('testing checkpoints', function (tester) { }) }) - it('should revert to the orginal root', function (t) { + it('should revert to the orginal root', function(t) { t.equal(trie.isCheckpoint, true) - trie.revert(function () { + trie.revert(function() { t.equal(trie.root.toString('hex'), preRoot) t.equal(trie.isCheckpoint, false) t.end() }) }) - it('should not get values from cache after revert', function (t) { - trie.get('love', function (err, res) { + it('should not get values from cache after revert', function(t) { + trie.get(Buffer.from('love'), function(err, res) { t.error(err) t.notOk(res) t.end() }) }) - it('should commit a checkpoint', function (t) { + it('should commit a checkpoint', function(t) { trie.checkpoint() - trie.put('test', 'something', function () { - trie.put('love', 'emotion', function () { - trie.commit(function () { + trie.put(Buffer.from('test'), Buffer.from('something'), function() { + trie.put(Buffer.from('love'), Buffer.from('emotion'), function() { + trie.commit(function() { t.equal(trie.isCheckpoint, false) t.equal(trie.root.toString('hex'), postRoot) t.end() @@ -101,23 +101,23 @@ tape('testing checkpoints', function (tester) { }) }) - it('should get new values after commit', function (t) { - trie.get('love', function (err, res) { + it('should get new values after commit', function(t) { + trie.get(Buffer.from('love'), function(err, res) { t.error(err) t.ok(Buffer.from('emotion').equals(res)) t.end() }) }) - it('should commit a nested checkpoint', function (t) { + it('should commit a nested checkpoint', function(t) { trie.checkpoint() var root - trie.put('test', 'something else', function () { + trie.put(Buffer.from('test'), Buffer.from('something else'), function() { root = trie.root trie.checkpoint() - trie.put('the feels', 'emotion', function () { + trie.put(Buffer.from('the feels'), Buffer.from('emotion'), function() { trie.revert() - trie.commit(function () { + trie.commit(function() { t.equal(trie.isCheckpoint, false) t.equal(trie.root.toString('hex'), root.toString('hex')) t.end() diff --git a/test/encodeing.js b/test/encodeing.js index c481ebd..3bcb8e9 100644 --- a/test/encodeing.js +++ b/test/encodeing.js @@ -4,9 +4,9 @@ const trie = new Trie() const trie2 = new Trie() const hex = 'FF44A3B3' -tape('encoding hexprefixes ', function (t) { - trie.put(new Buffer(hex, 'hex'), 'test', function () { - trie2.put('0x' + hex, 'test', function () { +tape('encoding hexprefixes ', function(t) { + trie.put(new Buffer(hex, 'hex'), Buffer.from('test'), function() { + trie2.put('0x' + hex, Buffer.from('test'), function() { t.equal(trie.root.toString('hex'), trie2.root.toString('hex')) t.end() }) diff --git a/test/index.js b/test/index.js index 3847ebc..37e5338 100644 --- a/test/index.js +++ b/test/index.js @@ -4,14 +4,14 @@ const rlp = require('rlp') const tape = require('tape') const ethUtil = require('ethereumjs-util') -tape('simple save and retrive', function (tester) { +tape('simple save and retrive', function(tester) { var it = tester.test - it('should not crash if given a non-existant root', function (t) { + it('should not crash if given a non-existant root', function(t) { var root = new Buffer('3f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d', 'hex') var trie = new Trie(null, root) - trie.get('test', function (err, value) { + trie.get(Buffer.from('test'), function(err, value) { t.equal(value, null) t.notEqual(err, null) t.end() @@ -19,63 +19,66 @@ tape('simple save and retrive', function (tester) { }) var trie = new Trie() - it('save a value', function (t) { - trie.put('test', 'one', t.end) + it('save a value', function(t) { + trie.put(Buffer.from('test'), Buffer.from('one'), t.end) }) - it('should get a value', function (t) { - trie.get('test', function (err, value) { + it('should get a value', function(t) { + trie.get(Buffer.from('test'), function(err, value) { t.equal(value.toString(), 'one') t.end(err) }) }) - it('should update a value', function (t) { - trie.put('test', new Buffer('two'), function () { - trie.get('test', function (err, value) { + it('should update a value', function(t) { + trie.put(Buffer.from('test'), Buffer.from('two'), function() { + trie.get(Buffer.from('test'), function(err, value) { t.equal(value.toString(), 'two') t.end(err) }) }) }) - it('should delete a value', function (t) { - trie.del('test', function (stack) { - trie.get('test', function (err, value) { + it('should delete a value', function(t) { + trie.del(Buffer.from('test'), function(stack) { + trie.get(Buffer.from('test'), function(err, value) { t.notok(value) t.end(err) }) }) }) - it('should recreate a value', function (t) { + it('should recreate a value', function(t) { trie.put(new Buffer('test'), new Buffer('one'), t.end) }) - it('should get updated a value', function (t) { - trie.get('test', function (err, value) { + it('should get updated a value', function(t) { + trie.get(Buffer.from('test'), function(err, value) { t.equal(value.toString(), 'one') t.end(err) }) }) - it('should create a branch here', function (t) { - trie.put(new Buffer('doge'), new Buffer('coin'), function () { - t.equal('de8a34a8c1d558682eae1528b47523a483dd8685d6db14b291451a66066bf0fc', trie.root.toString('hex')) + it('should create a branch here', function(t) { + trie.put(new Buffer('doge'), new Buffer('coin'), function() { + t.equal( + 'de8a34a8c1d558682eae1528b47523a483dd8685d6db14b291451a66066bf0fc', + trie.root.toString('hex'), + ) t.end() }) }) - it('should get a value that is in a branch', function (t) { - trie.get(new Buffer('doge'), function (err, value) { + it('should get a value that is in a branch', function(t) { + trie.get(new Buffer('doge'), function(err, value) { t.equal(value.toString(), 'coin') t.end(err) }) }) - it('should delete from a branch', function (t) { - trie.del('doge', function (err1, stack) { - trie.get('doge', function (err2, value) { + it('should delete from a branch', function(t) { + trie.del(Buffer.from('doge'), function(err1, stack) { + trie.get(Buffer.from('doge'), function(err2, value) { t.equal(value, null) t.end(err1 || err2) }) @@ -83,122 +86,157 @@ tape('simple save and retrive', function (tester) { }) }) -tape('storing longer values', function (tester) { +tape('storing longer values', function(tester) { var it = tester.test var trie = new Trie() var longString = 'this will be a really really really long value' var longStringRoot = 'b173e2db29e79c78963cff5196f8a983fbe0171388972106b114ef7f5c24dfa3' - it('should store a longer string', function (t) { - trie.put(new Buffer('done'), new Buffer(longString), function (err1, value) { - trie.put(new Buffer('doge'), new Buffer('coin'), function (err2, value) { + it('should store a longer string', function(t) { + trie.put(new Buffer('done'), new Buffer(longString), function(err1, value) { + trie.put(new Buffer('doge'), new Buffer('coin'), function(err2, value) { t.equal(longStringRoot, trie.root.toString('hex')) t.end(err1 || err2) }) }) }) - it('should retreive a longer value', function (t) { - trie.get(new Buffer('done'), function (err, value) { + it('should retreive a longer value', function(t) { + trie.get(new Buffer('done'), function(err, value) { t.equal(value.toString(), longString) t.end(err) }) }) - it('should when being modiefied delete the old value', function (t) { + it('should when being modiefied delete the old value', function(t) { trie.put(new Buffer('done'), new Buffer('test'), t.end) }) }) -tape('testing Extentions and branches', function (tester) { +tape('testing Extentions and branches', function(tester) { var trie = new Trie() var it = tester.test - it('should store a value', function (t) { + it('should store a value', function(t) { trie.put(new Buffer('doge'), new Buffer('coin'), t.end) }) - it('should create extention to store this value', function (t) { - trie.put(new Buffer('do'), new Buffer('verb'), function () { - t.equal('f803dfcb7e8f1afd45e88eedb4699a7138d6c07b71243d9ae9bff720c99925f9', trie.root.toString('hex')) + it('should create extention to store this value', function(t) { + trie.put(new Buffer('do'), new Buffer('verb'), function() { + t.equal( + 'f803dfcb7e8f1afd45e88eedb4699a7138d6c07b71243d9ae9bff720c99925f9', + trie.root.toString('hex'), + ) t.end() }) }) - it('should store this value under the extention ', function (t) { - trie.put(new Buffer('done'), new Buffer('finished'), function () { - t.equal('409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', trie.root.toString('hex')) + it('should store this value under the extention ', function(t) { + trie.put(new Buffer('done'), new Buffer('finished'), function() { + t.equal( + '409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', + trie.root.toString('hex'), + ) t.end() }) }) }) -tape('testing Extentions and branches - reverse', function (tester) { +tape('testing Extentions and branches - reverse', function(tester) { var it = tester.test var trie = new Trie() - it('should create extention to store this value', function (t) { + it('should create extention to store this value', function(t) { trie.put(new Buffer('do'), new Buffer('verb'), t.end) }) - it('should store a value', function (t) { + it('should store a value', function(t) { trie.put(new Buffer('doge'), new Buffer('coin'), t.end) }) - it('should store this value under the extention ', function (t) { - trie.put(new Buffer('done'), new Buffer('finished'), function () { - t.equal('409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', trie.root.toString('hex')) + it('should store this value under the extention ', function(t) { + trie.put(new Buffer('done'), new Buffer('finished'), function() { + t.equal( + '409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', + trie.root.toString('hex'), + ) t.end() }) }) }) -tape('testing deletions cases', function (tester) { +tape('testing deletions cases', function(tester) { var it = tester.test var trie = new Trie() - it('should delete from a branch->branch-branch', function (t) { - async.parallel([ - async.apply(trie.put.bind(trie), new Buffer([11, 11, 11]), 'first'), - async.apply(trie.put.bind(trie), new Buffer([12, 22, 22]), 'create the first branch'), - async.apply(trie.put.bind(trie), new Buffer([12, 34, 44]), 'create the last branch') - ], function () { - trie.del(new Buffer([12, 22, 22]), function () { - trie.get(new Buffer([12, 22, 22]), function (err, val) { - t.equal(null, val) - trie = new Trie() - t.end(err) + it('should delete from a branch->branch-branch', function(t) { + async.parallel( + [ + async.apply(trie.put.bind(trie), new Buffer([11, 11, 11]), Buffer.from('first')), + async.apply( + trie.put.bind(trie), + new Buffer([12, 22, 22]), + Buffer.from('create the first branch'), + ), + async.apply( + trie.put.bind(trie), + new Buffer([12, 34, 44]), + Buffer.from('create the last branch'), + ), + ], + function() { + trie.del(new Buffer([12, 22, 22]), function() { + trie.get(new Buffer([12, 22, 22]), function(err, val) { + t.equal(null, val) + trie = new Trie() + t.end(err) + }) }) - }) - }) + }, + ) }) - it('should delete from a branch->branch-extention', function (t) { - async.parallel([ - async.apply(trie.put.bind(trie), new Buffer([11, 11, 11]), 'first'), - async.apply(trie.put.bind(trie), new Buffer([12, 22, 22]), 'create the first branch'), - async.apply(trie.put.bind(trie), new Buffer([12, 33, 33]), 'create the middle branch'), - async.apply(trie.put.bind(trie), new Buffer([12, 33, 44]), 'create the last branch') - ], function () { - trie.del(new Buffer([12, 22, 22]), function () { - trie.get(new Buffer([12, 22, 22]), function (err, val) { - t.equal(null, val) - t.end(err) + it('should delete from a branch->branch-extention', function(t) { + async.parallel( + [ + async.apply(trie.put.bind(trie), new Buffer([11, 11, 11]), Buffer.from('first')), + async.apply( + trie.put.bind(trie), + new Buffer([12, 22, 22]), + Buffer.from('create the first branch'), + ), + async.apply( + trie.put.bind(trie), + new Buffer([12, 33, 33]), + Buffer.from('create the middle branch'), + ), + async.apply( + trie.put.bind(trie), + new Buffer([12, 33, 44]), + Buffer.from('create the last branch'), + ), + ], + function() { + trie.del(new Buffer([12, 22, 22]), function() { + trie.get(new Buffer([12, 22, 22]), function(err, val) { + t.equal(null, val) + t.end(err) + }) }) - }) - }) + }, + ) }) - it('should delete from a extention->branch-extention', function (t) { - trie.put(new Buffer([11, 11, 11]), 'first', function () { + it('should delete from a extention->branch-extention', function(t) { + trie.put(new Buffer([11, 11, 11]), Buffer.from('first'), function() { // create the top branch - trie.put(new Buffer([12, 22, 22]), 'create the first branch', function () { + trie.put(new Buffer([12, 22, 22]), Buffer.from('create the first branch'), function() { // crete the middle branch - trie.put(new Buffer([12, 33, 33]), 'create the middle branch', function () { - trie.put(new Buffer([12, 33, 44]), 'create the last branch', function () { + trie.put(new Buffer([12, 33, 33]), Buffer.from('create the middle branch'), function() { + trie.put(new Buffer([12, 33, 44]), Buffer.from('create the last branch'), function() { // delete the middle branch - trie.del(new Buffer([11, 11, 11]), function () { - trie.get(new Buffer([11, 11, 11]), function (err, val) { + trie.del(new Buffer([11, 11, 11]), function() { + trie.get(new Buffer([11, 11, 11]), function(err, val) { t.equal(null, val) t.end(err) }) @@ -209,16 +247,16 @@ tape('testing deletions cases', function (tester) { }) }) - it('should delete from a extention->branch-branch', function (t) { - trie.put(new Buffer([11, 11, 11]), 'first', function () { + it('should delete from a extention->branch-branch', function(t) { + trie.put(new Buffer([11, 11, 11]), Buffer.from('first'), function() { // create the top branch - trie.put(new Buffer([12, 22, 22]), 'create the first branch', function () { + trie.put(new Buffer([12, 22, 22]), Buffer.from('create the first branch'), function() { // crete the middle branch - trie.put(new Buffer([12, 33, 33]), 'create the middle branch', function () { - trie.put(new Buffer([12, 34, 44]), 'create the last branch', function () { + trie.put(new Buffer([12, 33, 33]), Buffer.from('create the middle branch'), function() { + trie.put(new Buffer([12, 34, 44]), Buffer.from('create the last branch'), function() { // delete the middle branch - trie.del(new Buffer([11, 11, 11]), function () { - trie.get(new Buffer([11, 11, 11]), function (err, val) { + trie.del(new Buffer([11, 11, 11]), function() { + trie.get(new Buffer([11, 11, 11]), function(err, val) { t.equal(null, val) t.end(err) }) @@ -230,7 +268,7 @@ tape('testing deletions cases', function (tester) { }) }) -tape('it should create the genesis state root from ethereum', function (tester) { +tape('it should create the genesis state root from ethereum', function(tester) { var it = tester.test var trie4 = new Trie() var g = new Buffer('8a40bfaa73256b60764c1bf40675a99083efb075', 'hex') @@ -245,16 +283,17 @@ tape('it should create the genesis state root from ethereum', function (tester) startAmount[0] = 1 var account = [startAmount, 0, stateRoot, ethUtil.keccak256()] var rlpAccount = rlp.encode(account) - var cppRlp = 'f85e9a010000000000000000000000000000000000000000000000000080a00000000000000000000000000000000000000000000000000000000000000000a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' + var cppRlp = + 'f85e9a010000000000000000000000000000000000000000000000000080a00000000000000000000000000000000000000000000000000000000000000000a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' var genesisStateRoot = '2f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d' tester.equal(cppRlp, rlpAccount.toString('hex')) - it('shall match the root', function (t) { - trie4.put(g, rlpAccount, function () { - trie4.put(j, rlpAccount, function () { - trie4.put(v, rlpAccount, function () { - trie4.put(a, rlpAccount, function () { + it('shall match the root', function(t) { + trie4.put(g, rlpAccount, function() { + trie4.put(j, rlpAccount, function() { + trie4.put(v, rlpAccount, function() { + trie4.put(a, rlpAccount, function() { t.equal(trie4.root.toString('hex'), genesisStateRoot) t.end() }) diff --git a/test/offical.js b/test/offical.js index 31590ee..2514826 100644 --- a/test/offical.js +++ b/test/offical.js @@ -2,58 +2,76 @@ const Trie = require('../dist/index').CheckpointTrie const async = require('async') const tape = require('tape') -tape('offical tests', function (t) { +tape('offical tests', function(t) { const jsonTests = require('./fixture/trietest.json').tests const testNames = Object.keys(jsonTests) let trie = new Trie() - async.eachSeries(testNames, function (i, done) { - let inputs = jsonTests[i].in - let expect = jsonTests[i].root - - async.eachSeries(inputs, function (input, done) { - for (i = 0; i < 2; i++) { - if (input[i] && input[i].slice(0, 2) === '0x') { - input[i] = new Buffer(input[i].slice(2), 'hex') - } - } - - trie.put(new Buffer(input[0]), input[1], function () { - done() - }) - }, function () { - t.equal('0x' + trie.root.toString('hex'), expect) - trie = new Trie() - done() - }) - }, t.end) + async.eachSeries( + testNames, + function(i, done) { + let inputs = jsonTests[i].in + let expect = jsonTests[i].root + + async.eachSeries( + inputs, + function(input, done) { + for (i = 0; i < 2; i++) { + if (input[i] && input[i].slice(0, 2) === '0x') { + input[i] = new Buffer(input[i].slice(2), 'hex') + } else if (input[i] && typeof input[i] === 'string') { + input[i] = Buffer.from(input[i]) + } + } + + trie.put(new Buffer(input[0]), input[1], function() { + done() + }) + }, + function() { + t.equal('0x' + trie.root.toString('hex'), expect) + trie = new Trie() + done() + }, + ) + }, + t.end, + ) }) -tape('offical tests any order', function (t) { +tape('offical tests any order', function(t) { const jsonTests = require('./fixture/trieanyorder.json').tests var testNames = Object.keys(jsonTests) var trie = new Trie() - async.eachSeries(testNames, function (i, done) { - var test = jsonTests[i] - var keys = Object.keys(test.in) - - async.eachSeries(keys, function (key, done) { - var val = test.in[key] - - if (key.slice(0, 2) === '0x') { - key = new Buffer(key.slice(2), 'hex') - } - - if (val && val.slice(0, 2) === '0x') { - val = new Buffer(val.slice(2), 'hex') - } - - trie.put(new Buffer(key), new Buffer(val), function () { - done() - }) - }, function () { - t.equal('0x' + trie.root.toString('hex'), test.root) - trie = new Trie() - done() - }) - }, t.end) + async.eachSeries( + testNames, + function(i, done) { + var test = jsonTests[i] + var keys = Object.keys(test.in) + + async.eachSeries( + keys, + function(key, done) { + var val = test.in[key] + + if (key.slice(0, 2) === '0x') { + key = new Buffer(key.slice(2), 'hex') + } + + if (val && val.slice(0, 2) === '0x') { + val = new Buffer(val.slice(2), 'hex') + } + + trie.put(new Buffer(key), new Buffer(val), function() { + done() + }) + }, + function() { + t.equal('0x' + trie.root.toString('hex'), test.root) + trie = new Trie() + done() + }, + ) + }, + t.end, + ) }) diff --git a/test/proof.js b/test/proof.js index 9a22980..7cd6a28 100644 --- a/test/proof.js +++ b/test/proof.js @@ -2,239 +2,276 @@ const Trie = require('../dist/index').CheckpointTrie const async = require('async') const tape = require('tape') -tape('simple merkle proofs generation and verification', function (tester) { +tape('simple merkle proofs generation and verification', function(tester) { var it = tester.test - it('create a merkle proof and verify it', function (t) { + it('create a merkle proof and verify it', function(t) { var trie = new Trie() - async.series([ - function (cb) { - trie.put('key1aa', '0123456789012345678901234567890123456789xx', cb) - }, - function (cb) { - trie.put('key2bb', 'aval2', cb) - }, - function (cb) { - trie.put('key3cc', 'aval3', cb) - }, - function (cb) { - Trie.prove(trie, 'key2bb', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key2bb', prove, function (err, val) { + async.series( + [ + function(cb) { + trie.put( + Buffer.from('key1aa'), + Buffer.from('0123456789012345678901234567890123456789xx'), + cb, + ) + }, + function(cb) { + trie.put(Buffer.from('key2bb'), Buffer.from('aval2'), cb) + }, + function(cb) { + trie.put(Buffer.from('key3cc'), Buffer.from('aval3'), cb) + }, + function(cb) { + Trie.prove(trie, Buffer.from('key2bb'), function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), 'aval2') - cb() + Trie.verifyProof(trie.root, Buffer.from('key2bb'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), 'aval2') + cb() + }) }) - }) - }, - function (cb) { - Trie.prove(trie, 'key1aa', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) { + }, + function(cb) { + Trie.prove(trie, Buffer.from('key1aa'), function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789xx') - cb() + Trie.verifyProof(trie.root, Buffer.from('key1aa'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789xx') + cb() + }) }) - }) - }, - function (cb) { - Trie.prove(trie, 'key2bb', function (err, prove) { - t.equal(err, null, 'Path to key2 should create valid proof of absence') - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key2', prove, function (err, val) { - // In this case, the proof _happens_ to contain enough nodes to prove `key2` because - // traversing into `key22` would touch all the same nodes as traversing into `key2` - t.equal(val, null, 'Expected value at a random key to be null') - t.equal(err, null, 'Path to key2 should show a null value') - cb() + }, + function(cb) { + Trie.prove(trie, Buffer.from('key2bb'), function(err, prove) { + t.equal(err, null, 'Path to key2 should create valid proof of absence') + if (err) return cb(err) + Trie.verifyProof(trie.root, Buffer.from('key2'), prove, function(err, val) { + // In this case, the proof _happens_ to contain enough nodes to prove `key2` because + // traversing into `key22` would touch all the same nodes as traversing into `key2` + t.equal(val, null, 'Expected value at a random key to be null') + t.equal(err, null, 'Path to key2 should show a null value') + cb() + }) }) - }) - }, - function (cb) { - let myKey = 'anyrandomkey' - Trie.prove(trie, myKey, function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, myKey, prove, function (err, val) { - t.equal(val, null, 'Expected value to be null') - t.equal(err, null, err) - cb() + }, + function(cb) { + let myKey = Buffer.from('anyrandomkey') + Trie.prove(trie, myKey, function(err, prove) { + if (err) return cb(err) + Trie.verifyProof(trie.root, myKey, prove, function(err, val) { + t.equal(val, null, 'Expected value to be null') + t.equal(err, null, err) + cb() + }) }) - }) - }, - function (cb) { - let myKey = 'anothergarbagekey' // should generate a valid proof of null - Trie.prove(trie, myKey, function (err, prove) { - if (err) return cb(err) - prove.push(Buffer.from('123456'))// extra nodes are just ignored - Trie.verifyProof(trie.root, myKey, prove, function (err, val) { - t.equal(val, null, 'Expected value to be null') - t.equal(err, null, err) - cb() + }, + function(cb) { + let myKey = Buffer.from('anothergarbagekey') // should generate a valid proof of null + Trie.prove(trie, myKey, function(err, prove) { + if (err) return cb(err) + prove.push(Buffer.from('123456')) // extra nodes are just ignored + Trie.verifyProof(trie.root, myKey, prove, function(err, val) { + t.equal(val, null, 'Expected value to be null') + t.equal(err, null, err) + cb() + }) }) - }) - }, - function (cb) { - trie.put('another', '3498h4riuhgwe', cb) - }, - function (cb) { - // to throw an error we need to request proof for one key - Trie.prove(trie, 'another', function (err, prove) { - if (err) return cb(err) - // and try to use that proof on another key - Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) { - t.equal(val, null, 'Expected value: to be null ') - // this proof would be insignificant to prove `key1aa` - t.notEqual(err, null, 'Expected error: Missing node in DB') - t.notEqual(err, undefined, 'Expected error: Missing node in DB') - cb() + }, + function(cb) { + trie.put(Buffer.from('another'), Buffer.from('3498h4riuhgwe'), cb) + }, + function(cb) { + // to throw an error we need to request proof for one key + Trie.prove(trie, Buffer.from('another'), function(err, prove) { + if (err) return cb(err) + // and try to use that proof on another key + Trie.verifyProof(trie.root, Buffer.from('key1aa'), prove, function(err, val) { + t.equal(val, null, 'Expected value: to be null ') + // this proof would be insignificant to prove `key1aa` + t.notEqual(err, null, 'Expected error: Missing node in DB') + t.notEqual(err, undefined, 'Expected error: Missing node in DB') + cb() + }) }) - }) - } - ], function (err) { - t.end(err) - }) + }, + ], + function(err) { + t.end(err) + }, + ) }) - it('create a merkle proof and verify it with a single long key', function (t) { + it('create a merkle proof and verify it with a single long key', function(t) { var trie = new Trie() - async.series([ - function (cb) { - trie.put('key1aa', '0123456789012345678901234567890123456789xx', cb) - }, - function (cb) { - Trie.prove(trie, 'key1aa', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) { + async.series( + [ + function(cb) { + trie.put( + Buffer.from('key1aa'), + Buffer.from('0123456789012345678901234567890123456789xx'), + cb, + ) + }, + function(cb) { + Trie.prove(trie, Buffer.from('key1aa'), function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789xx') - cb() + Trie.verifyProof(trie.root, Buffer.from('key1aa'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789xx') + cb() + }) }) - }) - } - ], function (err) { - t.end(err) - }) + }, + ], + function(err) { + t.end(err) + }, + ) }) - it('create a merkle proof and verify it with a single short key', function (t) { + it('create a merkle proof and verify it with a single short key', function(t) { var trie = new Trie() - async.series([ - function (cb) { - trie.put('key1aa', '01234', cb) - }, - function (cb) { - Trie.prove(trie, 'key1aa', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) { + async.series( + [ + function(cb) { + trie.put(Buffer.from('key1aa'), Buffer.from('01234'), cb) + }, + function(cb) { + Trie.prove(trie, Buffer.from('key1aa'), function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), '01234') - cb() + Trie.verifyProof(trie.root, Buffer.from('key1aa'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), '01234') + cb() + }) }) - }) - } - ], function (err) { - t.end(err) - }) + }, + ], + function(err) { + t.end(err) + }, + ) }) - it('create a merkle proof and verify it whit keys in the midle', function (t) { + it('create a merkle proof and verify it with keys in the middle', function(t) { var trie = new Trie() - async.series([ - function (cb) { - trie.put('key1aa', '0123456789012345678901234567890123456789xxx', cb) - }, - function (cb) { - trie.put('key1', '0123456789012345678901234567890123456789Very_Long', cb) - }, - function (cb) { - trie.put('key2bb', 'aval3', cb) - }, - function (cb) { - trie.put('key2', 'short', cb) - }, - function (cb) { - trie.put('key3cc', 'aval3', cb) - }, - function (cb) { - trie.put('key3', '1234567890123456789012345678901', cb) - }, - function (cb) { - Trie.prove(trie, 'key1', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key1', prove, function (err, val) { + async.series( + [ + function(cb) { + trie.put( + Buffer.from('key1aa'), + Buffer.from('0123456789012345678901234567890123456789xxx'), + cb, + ) + }, + function(cb) { + trie.put( + Buffer.from('key1'), + Buffer.from('0123456789012345678901234567890123456789Very_Long'), + cb, + ) + }, + function(cb) { + trie.put(Buffer.from('key2bb'), Buffer.from('aval3'), cb) + }, + function(cb) { + trie.put(Buffer.from('key2'), Buffer.from('short'), cb) + }, + function(cb) { + trie.put(Buffer.from('key3cc'), Buffer.from('aval3'), cb) + }, + function(cb) { + trie.put(Buffer.from('key3'), Buffer.from('1234567890123456789012345678901'), cb) + }, + function(cb) { + Trie.prove(trie, 'key1', function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789Very_Long') - cb() + Trie.verifyProof(trie.root, Buffer.from('key1'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789Very_Long') + cb() + }) }) - }) - }, - function (cb) { - Trie.prove(trie, 'key2', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key2', prove, function (err, val) { + }, + function(cb) { + Trie.prove(trie, 'key2', function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), 'short') - cb() + Trie.verifyProof(trie.root, Buffer.from('key2'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), 'short') + cb() + }) }) - }) - }, - function (cb) { - Trie.prove(trie, 'key3', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key3', prove, function (err, val) { + }, + function(cb) { + Trie.prove(trie, 'key3', function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), '1234567890123456789012345678901') - cb() + Trie.verifyProof(trie.root, Buffer.from('key3'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), '1234567890123456789012345678901') + cb() + }) }) - }) - } - ], function (err) { - t.end(err) - }) + }, + ], + function(err) { + t.end(err) + }, + ) }) - it('should succeed with a simple embedded extension-branch', function (t) { + it('should succeed with a simple embedded extension-branch', function(t) { var trie = new Trie() - async.series([ - (cb) => { - trie.put('a', 'a', cb) - }, (cb) => { - trie.put('b', 'b', cb) - }, (cb) => { - trie.put('c', 'c', cb) - }, (cb) => { - Trie.prove(trie, 'a', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'a', prove, function (err, val) { + async.series( + [ + cb => { + trie.put(Buffer.from('a'), Buffer.from('a'), cb) + }, + cb => { + trie.put(Buffer.from('b'), Buffer.from('b'), cb) + }, + cb => { + trie.put(Buffer.from('c'), Buffer.from('c'), cb) + }, + cb => { + Trie.prove(trie, Buffer.from('a'), function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), 'a') - cb() + Trie.verifyProof(trie.root, Buffer.from('a'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), 'a') + cb() + }) }) - }) - }, (cb) => { - Trie.prove(trie, 'b', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'b', prove, function (err, val) { + }, + cb => { + Trie.prove(trie, Buffer.from('b'), function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), 'b') - cb() + Trie.verifyProof(trie.root, Buffer.from('b'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), 'b') + cb() + }) }) - }) - }, (cb) => { - Trie.prove(trie, 'c', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'c', prove, function (err, val) { + }, + cb => { + Trie.prove(trie, Buffer.from('c'), function(err, prove) { if (err) return cb(err) - t.equal(val.toString('utf8'), 'c') - cb() + Trie.verifyProof(trie.root, Buffer.from('c'), prove, function(err, val) { + if (err) return cb(err) + t.equal(val.toString('utf8'), 'c') + cb() + }) }) - }) - }], function (err) { - t.end(err) - }) + }, + ], + function(err) { + t.end(err) + }, + ) }) }) diff --git a/test/secure.js b/test/secure.js index a1bf705..4417008 100644 --- a/test/secure.js +++ b/test/secure.js @@ -2,14 +2,14 @@ const Trie = require('../dist/secure').SecureTrie const async = require('async') const tape = require('tape') -tape('SecureTrie', function (t) { +tape('SecureTrie', function(t) { const trie = new Trie() const k = Buffer.from('foo') const v = Buffer.from('bar') - t.test('put and get value', function (st) { - trie.put(k, v, function () { - trie.get(k, function (err, res) { + t.test('put and get value', function(st) { + trie.put(k, v, function() { + trie.get(k, function(err, res) { st.error(err) st.ok(v.equals(res)) st.end() @@ -17,9 +17,9 @@ tape('SecureTrie', function (t) { }) }) - t.test('copy trie', function (st) { + t.test('copy trie', function(st) { const t = trie.copy() - t.get(k, function (err, res) { + t.get(k, function(err, res) { st.error(err) st.ok(v.equals(res)) st.end() @@ -27,64 +27,79 @@ tape('SecureTrie', function (t) { }) }) -tape('SecureTrie proof', function (t) { - t.test('create a merkle proof and verify it with a single short key', function (st) { +tape('SecureTrie proof', function(t) { + t.test('create a merkle proof and verify it with a single short key', function(st) { const trie = new Trie() - async.series([ - function (cb) { - trie.put('key1aa', '01234', cb) - }, - function (cb) { - Trie.prove(trie, 'key1aa', function (err, prove) { - if (err) return cb(err) - Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) { + async.series( + [ + function(cb) { + trie.put(Buffer.from('key1aa'), Buffer.from('01234'), cb) + }, + function(cb) { + Trie.prove(trie, Buffer.from('key1aa'), function(err, prove) { if (err) return cb(err) - st.equal(val.toString('utf8'), '01234') - cb() + Trie.verifyProof(trie.root, Buffer.from('key1aa'), prove, function(err, val) { + if (err) return cb(err) + st.equal(val.toString('utf8'), '01234') + cb() + }) }) - }) - } - ], function (err) { - st.end(err) - }) + }, + ], + function(err) { + st.end(err) + }, + ) }) }) -tape('secure tests', function (it) { +tape('secure tests', function(it) { let trie = new Trie() const jsonTests = require('./fixture/trietest_secureTrie.json').tests - it.test('empty values', function (t) { - async.eachSeries(jsonTests.emptyValues.in, function (row, cb) { - trie.put(new Buffer(row[0]), row[1], cb) - }, function (err) { - t.equal('0x' + trie.root.toString('hex'), jsonTests.emptyValues.root) - t.end(err) - }) + it.test('empty values', function(t) { + async.eachSeries( + jsonTests.emptyValues.in, + function(row, cb) { + trie.put(Buffer.from(row[0]), row[1] ? Buffer.from(row[1]) : null, cb) + }, + function(err) { + t.equal('0x' + trie.root.toString('hex'), jsonTests.emptyValues.root) + t.end(err) + }, + ) }) - it.test('branchingTests', function (t) { + it.test('branchingTests', function(t) { trie = new Trie() - async.eachSeries(jsonTests.branchingTests.in, function (row, cb) { - trie.put(row[0], row[1], cb) - }, function () { - t.equal('0x' + trie.root.toString('hex'), jsonTests.branchingTests.root) - t.end() - }) + async.eachSeries( + jsonTests.branchingTests.in, + function(row, cb) { + trie.put(Buffer.from(row[0]), row[1] ? Buffer.from(row[1]) : null, cb) + }, + function() { + t.equal('0x' + trie.root.toString('hex'), jsonTests.branchingTests.root) + t.end() + }, + ) }) - it.test('jeff', function (t) { - async.eachSeries(jsonTests.jeff.in, function (row, cb) { - var val = row[1] - if (val) { - val = new Buffer(row[1].slice(2), 'hex') - } + it.test('jeff', function(t) { + async.eachSeries( + jsonTests.jeff.in, + function(row, cb) { + var val = row[1] + if (val) { + val = new Buffer(row[1].slice(2), 'hex') + } - trie.put(new Buffer(row[0].slice(2), 'hex'), val, cb) - }, function () { - t.equal('0x' + trie.root.toString('hex'), jsonTests.jeff.root) - t.end() - }) + trie.put(new Buffer(row[0].slice(2), 'hex'), val, cb) + }, + function() { + t.equal('0x' + trie.root.toString('hex'), jsonTests.jeff.root.toString('hex')) + t.end() + }, + ) }) }) diff --git a/test/streams.js b/test/streams.js index c5b6169..dfb0d5d 100644 --- a/test/streams.js +++ b/test/streams.js @@ -1,96 +1,114 @@ const Trie = require('../dist/index').CheckpointTrie const describe = require('tape') -describe('kv stream test', function (tester) { +describe('kv stream test', function(tester) { var it = tester.test var trie = new Trie() - var init = [{ - type: 'del', - key: 'father' - }, { - type: 'put', - key: 'name', - value: 'Yuri Irsenovich Kim' - }, { - type: 'put', - key: 'dob', - value: '16 February 1941' - }, { - type: 'put', - key: 'spouse', - value: 'Kim Young-sook' - }, { - type: 'put', - key: 'occupation', - value: 'Clown' - }, { - type: 'put', - key: 'nameads', - value: 'Yuri Irsenovich Kim' - }, { - type: 'put', - key: 'namfde', - value: 'Yuri Irsenovich Kim' - }, { - type: 'put', - key: 'namsse', - value: 'Yuri Irsenovich Kim' - }, { - type: 'put', - key: 'dofab', - value: '16 February 1941' - }, { - type: 'put', - key: 'spoudse', - value: 'Kim Young-sook' - }, { - type: 'put', - key: 'occupdsation', - value: 'Clown' - }, { - type: 'put', - key: 'dozzzb', - value: '16 February 1941' - }, { - type: 'put', - key: 'spouszze', - value: 'Kim Young-sook' - }, { - type: 'put', - key: 'occupatdfion', - value: 'Clown' - }, { - type: 'put', - key: 'dssob', - value: '16 February 1941' - }, { - type: 'put', - key: 'spossuse', - value: 'Kim Young-sook' - }, { - type: 'put', - key: 'occupssation', - value: 'Clown' - }] + var init = [ + { + type: 'del', + key: Buffer.from('father'), + }, + { + type: 'put', + key: Buffer.from('name'), + value: Buffer.from('Yuri Irsenovich Kim'), + }, + { + type: 'put', + key: Buffer.from('dob'), + value: Buffer.from('16 February 1941'), + }, + { + type: 'put', + key: Buffer.from('spouse'), + value: Buffer.from('Kim Young-sook'), + }, + { + type: 'put', + key: Buffer.from('occupation'), + value: Buffer.from('Clown'), + }, + { + type: 'put', + key: Buffer.from('nameads'), + value: Buffer.from('Yuri Irsenovich Kim'), + }, + { + type: 'put', + key: Buffer.from('namfde'), + value: Buffer.from('Yuri Irsenovich Kim'), + }, + { + type: 'put', + key: Buffer.from('namsse'), + value: Buffer.from('Yuri Irsenovich Kim'), + }, + { + type: 'put', + key: Buffer.from('dofab'), + value: Buffer.from('16 February 1941'), + }, + { + type: 'put', + key: Buffer.from('spoudse'), + value: Buffer.from('Kim Young-sook'), + }, + { + type: 'put', + key: Buffer.from('occupdsation'), + value: Buffer.from('Clown'), + }, + { + type: 'put', + key: Buffer.from('dozzzb'), + value: Buffer.from('16 February 1941'), + }, + { + type: 'put', + key: Buffer.from('spouszze'), + value: Buffer.from('Kim Young-sook'), + }, + { + type: 'put', + key: Buffer.from('occupatdfion'), + value: Buffer.from('Clown'), + }, + { + type: 'put', + key: Buffer.from('dssob'), + value: Buffer.from('16 February 1941'), + }, + { + type: 'put', + key: Buffer.from('spossuse'), + value: Buffer.from('Kim Young-sook'), + }, + { + type: 'put', + key: Buffer.from('occupssation'), + value: Buffer.from('Clown'), + }, + ] var valObj = {} - init.forEach(function (i) { + init.forEach(function(i) { if (i.type === 'put') valObj[i.key] = i.value }) - it('should populate trie', function (t) { - trie.batch(init, function () { + it('should populate trie', function(t) { + trie.batch(init, function() { t.end() }) }) - it('should fetch all of the nodes', function (t) { + it('should fetch all of the nodes', function(t) { var stream = trie.createReadStream() - stream.on('data', function (d) { - t.equal(valObj[d.key.toString()], d.value.toString()) + stream.on('data', function(d) { + t.equal(valObj[d.key.toString()].toString(), d.value.toString()) delete valObj[d.key.toString()] }) - stream.on('end', function () { + stream.on('end', function() { var keys = Object.keys(valObj) t.equal(keys.length, 0) t.end() @@ -98,56 +116,63 @@ describe('kv stream test', function (tester) { }) }) -describe('db stream test', function (tester) { +describe('db stream test', function(tester) { var it = tester.test var trie = new Trie() - var init = [{ - type: 'put', - key: 'color', - value: 'purple' - }, { - type: 'put', - key: 'food', - value: 'sushi' - }, { - type: 'put', - key: 'fight', - value: 'fire' - }, { - type: 'put', - key: 'colo', - value: 'trolo' - }, { - type: 'put', - key: 'color', - value: 'blue' - }, { - type: 'put', - key: 'color', - value: 'pink' - }] + var init = [ + { + type: 'put', + key: Buffer.from('color'), + value: Buffer.from('purple'), + }, + { + type: 'put', + key: Buffer.from('food'), + value: Buffer.from('sushi'), + }, + { + type: 'put', + key: Buffer.from('fight'), + value: Buffer.from('fire'), + }, + { + type: 'put', + key: Buffer.from('colo'), + value: Buffer.from('trolo'), + }, + { + type: 'put', + key: Buffer.from('color'), + value: Buffer.from('blue'), + }, + { + type: 'put', + key: Buffer.from('color'), + value: Buffer.from('pink'), + }, + ] var expectedNodes = { '3c38d9aa6ad288c8e27da701e17fe99a5b67c8b12fd0469651c80494d36bc4c1': true, - 'd5f61e1ff2b918d1c2a2c4b1732a3c68bd7e3fd64f35019f2f084896d4546298': true, - 'e64329dadee2fb8a113b4c88cfe973aeaa9b523d4dc8510b84ca23f9d5bfbd90': true, - 'c916d458bfb5f27603c5bd93b00f022266712514a59cde749f19220daffc743f': true, - '2386bfb0de9cf93902a110f5ab07b917ffc0b9ea599cb7f4f8bb6fd1123c866c': true + d5f61e1ff2b918d1c2a2c4b1732a3c68bd7e3fd64f35019f2f084896d4546298: true, + e64329dadee2fb8a113b4c88cfe973aeaa9b523d4dc8510b84ca23f9d5bfbd90: true, + c916d458bfb5f27603c5bd93b00f022266712514a59cde749f19220daffc743f: true, + '2386bfb0de9cf93902a110f5ab07b917ffc0b9ea599cb7f4f8bb6fd1123c866c': true, } - it('should populate trie', function (t) { + it('should populate trie', function(t) { trie.checkpoint() trie.batch(init, t.end) }) - it('should only fetch nodes in the current trie', function (t) { + it('should only fetch nodes in the current trie', function(t) { var stream = trie._createScratchReadStream() - stream.on('data', function (d) { + stream.on('data', function(d) { var key = d.key.toString('hex') t.ok(!!expectedNodes[key]) delete expectedNodes[key] }) - stream.on('end', function () { + stream.on('end', function() { t.equal(Object.keys(expectedNodes).length, 0) t.end() }) diff --git a/typedoc.json b/typedoc.json index 9a9761a..09d7f85 100644 --- a/typedoc.json +++ b/typedoc.json @@ -1,8 +1,13 @@ { + "inputFiles": ["src/index.ts"], "out": "docs", - "mode": "file", + "mode": "library", + "plugin": "typedoc-plugin-markdown", "readme": "none", - "theme": "markdown", - "mdEngine": "github", - "excludeNotExported": true + "gitRevision": "master", + "exclude": "src/**/!(secure|checkpointTrie|baseTrie).ts", + "excludeNotExported": true, + "excludeExternals": true, + "excludePrivate": true, + "excludeProtected": true }