icjs-mpt
Version:
This is an implementation of the modified merkle patricia tree as speficed in the Ethereum's yellow paper.
504 lines (309 loc) • 11 kB
Markdown
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
### Table of Contents
- [Trie][1]
- [Parameters][2]
- [Properties][3]
- [get][4]
- [Parameters][5]
- [put][6]
- [Parameters][7]
- [del][8]
- [Parameters][9]
- [getRaw][10]
- [Parameters][11]
- [putRaw][12]
- [Parameters][13]
- [delRaw][14]
- [Parameters][15]
- [findPath][16]
- [Parameters][17]
- [\_updateNode][18]
- [Parameters][19]
- [\_saveStack][20]
- [Parameters][21]
- [createReadStream][22]
- [batch][23]
- [Parameters][24]
- [Examples][25]
- [checkRoot][26]
- [Parameters][27]
- [PrioritizedTaskExecutor][28]
- [Parameters][29]
- [Properties][30]
- [execute][31]
- [Parameters][32]
- [checkpoint][33]
- [commit][34]
- [Parameters][35]
- [revert][36]
- [Parameters][37]
- [Trie.prove][38]
- [Parameters][39]
- [Trie.verifyProof][40]
- [Parameters][41]
- [addHexPrefix][42]
- [Parameters][43]
- [isTerminator][44]
- [Parameters][45]
- [stringToNibbles][46]
- [Parameters][47]
- [nibblesToBuffer][48]
- [Parameters][49]
- [getNodeType][50]
- [Parameters][51]
- [matchingNibbleLength][52]
- [Parameters][53]
- [doKeysMatch][54]
- [Parameters][55]
- [callTogether][56]
- [asyncFirstSeries][57]
- [Parameters][58]
- [SecureTrie][59]
## Trie
Use `require('merkel-patricia-tree')` for the base interface. In IrChain 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][60]?** An instance of [levelup][61] or a compatible API. If the db is `null` or left undefined, then the trie will be stored in memory via [memdown][62]
- `root` **([Buffer][63] \| [String][64])?** `A hex`String`or`Buffer\` for the root of a previously stored trie
### Properties
- `root` **[Buffer][63]** The current root of the `trie`
- `isCheckpoint` **[Boolean][65]** determines if you are saving to a checkpoint or directly to the db
- `EMPTY_TRIE_ROOT` **[Buffer][63]** the Root for an empty trie
## get
Gets a value given a `key`
### Parameters
- `key` **([Buffer][63] \| [String][64])** the key to search for
- `cb` **[Function][66]** 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
Stores a given `value` at the given `key`
### Parameters
- `key` **([Buffer][63] \| [String][64])**
- `Value` **([Buffer][63] \| [String][64])**
- `cb` **[Function][66]** A callback `Function` which is given the argument `err` - for errors that may have occured
## del
deletes a value given a `key`
### Parameters
- `key` **([Buffer][63] \| [String][64])**
- `callback` **[Function][66]** the callback `Function`
## getRaw
Retrieves a raw value in the underlying db
### Parameters
- `key` **[Buffer][63]**
- `callback` **[Function][66]** 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`.
## putRaw
Writes a value directly to the underlining db
### Parameters
- `key` **([Buffer][63] \| [String][64])** The key as a `Buffer` or `String`
- `value` **[Buffer][63]** The value to be stored
- `callback` **[Function][66]** A callback `Function`, which is given the argument `err` - for errors that may have occured
## delRaw
Removes a raw value in the underlying db
### Parameters
- `key` **([Buffer][63] \| [String][64])**
- `callback` **[Function][66]** A callback `Function`, which is given the argument `err` - for errors that may have occured
## findPath
Trys 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][64] \| [Buffer][63])** key - the search key
- `null-null` **[Function][66]** 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
## \_updateNode
Updates a node
### Parameters
- `key` **[Buffer][63]**
- `value` **([Buffer][63] \| [String][64])**
- `keyRemainder` **[Array][67]**
- `stack` **[Array][67]** \-
- `cb` **[Function][66]** the callback
## \_saveStack
saves a stack
### Parameters
- `key` **[Array][67]** the key. Should follow the stack
- `stack` **[Array][67]** a stack of nodes to the value given by the key
- `opStack` **[Array][67]** a stack of levelup operations to commit at the end of this funciton
- `cb` **[Function][66]**
## createReadStream
The `data` event is given an `Object` hat has two properties; the `key` and the `value`. Both should be Buffers.
Returns **[stream.Readable][68]** Returns a [stream][69] of the contents of the `trie`
## batch
The given hash of operations (key additions or deletions) are executed on the DB
### Parameters
- `ops` **[Array][67]**
- `cb` **[Function][66]**
### 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
Checks if a given root exists
### Parameters
- `root` **[Buffer][63]**
- `cb` **[Function][66]**
## PrioritizedTaskExecutor
Executes tasks up to maxPoolSize at a time, other items are put in a priority queue.
### Parameters
- `maxPoolSize` **[Number][70]** The maximum size of the pool
### Properties
- `maxPoolSize` **[Number][70]** The maximum size of the pool
- `currentPoolSize` **[Number][70]** The current size of the pool
- `queue` **[Array][67]** The task queue
### execute
Executes the task.
#### Parameters
- `priority` **[Number][70]** The priority of the task
- `task` **[Function][66]** The function that accepts the callback, which must be called upon the task completion.
## checkpoint
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
## commit
commits a checkpoint to disk
### Parameters
- `cb` **[Function][66]** the callback
## revert
Reverts the trie to the state it was at when `checkpoint` was first called.
### Parameters
- `cb` **[Function][66]** the callback
## Trie.prove
Returns a merkle proof for a given key
### Parameters
- `trie` **[Trie][71]**
- `key` **[String][64]**
- `cb` **[Function][66]** A callback `Function` (arguments {Error} `err`, {Array.<TrieNode>} `proof`)
## Trie.verifyProof
Verifies a merkle proof for a given key
### Parameters
- `rootHash` **[Buffer][63]**
- `key` **[String][64]**
- `proof` **[Array][67]<TrieNode>**
- `cb` **[Function][66]** A callback `Function` (arguments {Error} `err`, {String} `val`)
## addHexPrefix
### Parameters
- `key`
- `terminator`
- `dataArr` **[Array][67]**
Returns **[Buffer][63]** returns buffer of encoded data
hexPrefix
\*
## isTerminator
Determines if a key has Arnold Schwarzenegger in it.
### Parameters
- `key` **[Array][67]** an hexprefixed array of nibbles
## stringToNibbles
Converts a string OR a buffer to a nibble array.
### Parameters
- `key` **([Buffer][63] \| [String][64])**
## nibblesToBuffer
Converts a nibble array into a buffer.
### Parameters
- `arr`
## getNodeType
Determines the node type.
### Parameters
- `node`
Returns **[String][64]** the node type- leaf - if the node is a leaf
- branch - if the node is a branch
- extention - if the node is an extention
- unknown - if something else got borked
## matchingNibbleLength
Returns the number of in order matching nibbles of two give nibble arrayes
### Parameters
- `nib1` **[Array][67]**
- `nib2` **[Array][67]**
## doKeysMatch
Compare two 'nibble array' keys
### Parameters
- `keyA`
- `keyB`
## callTogether
Take two or more functions and returns a function that will execute all of
the given functions
## asyncFirstSeries
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`
## SecureTrie
**Extends Trie**
You can create a secure Trie where the keys are automatically hashed using **SHA3** by using `require('icjs-mpt/secure')`. It has the same methods and constuctor as `Trie`
[1]: #trie
[2]: #parameters
[3]: #properties
[4]: #get
[5]: #parameters-1
[6]: #put
[7]: #parameters-2
[8]: #del
[9]: #parameters-3
[10]: #getraw
[11]: #parameters-4
[12]: #putraw
[13]: #parameters-5
[14]: #delraw
[15]: #parameters-6
[16]: #findpath
[17]: #parameters-7
[18]: #_updatenode
[19]: #parameters-8
[20]: #_savestack
[21]: #parameters-9
[22]: #createreadstream
[23]: #batch
[24]: #parameters-10
[25]: #examples
[26]: #checkroot
[27]: #parameters-11
[28]: #prioritizedtaskexecutor
[29]: #parameters-12
[30]: #properties-1
[31]: #execute
[32]: #parameters-13
[33]: #checkpoint
[34]: #commit
[35]: #parameters-14
[36]: #revert
[37]: #parameters-15
[38]: #trieprove
[39]: #parameters-16
[40]: #trieverifyproof
[41]: #parameters-17
[42]: #addhexprefix
[43]: #parameters-18
[44]: #isterminator
[45]: #parameters-19
[46]: #stringtonibbles
[47]: #parameters-20
[48]: #nibblestobuffer
[49]: #parameters-21
[50]: #getnodetype
[51]: #parameters-22
[52]: #matchingnibblelength
[53]: #parameters-23
[54]: #dokeysmatch
[55]: #parameters-24
[56]: #calltogether
[57]: #asyncfirstseries
[58]: #parameters-25
[59]: #securetrie
[60]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[61]: https://github.com/rvagg/node-levelup/
[62]: https://github.com/rvagg/memdown
[63]: https://nodejs.org/api/buffer.html
[64]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[65]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[66]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
[67]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[68]: https://nodejs.org/api/stream.html#stream_class_stream_readable
[69]: https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_readable
[70]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[71]: #trie