Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Convert trieNode to ES6 class #71

Merged
merged 9 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 45 additions & 46 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You can create a secure Trie where the keys are automatically hashed using **SHA

## Trie

[src/baseTrie.js:25-781][2]
[src/baseTrie.js:23-779][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

Expand All @@ -27,7 +27,7 @@ Use `require('merkel-patricia-tree')` for the base interface. In Ethereum applic

### get

[src/baseTrie.js:65-77][9]
[src/baseTrie.js:63-75][9]

Gets a value given a `key`

Expand All @@ -38,7 +38,7 @@ Gets a value given a `key`

### put

[src/baseTrie.js:87-111][11]
[src/baseTrie.js:85-109][11]

Stores a given `value` at the given `key`

Expand All @@ -50,7 +50,7 @@ Stores a given `value` at the given `key`

### del

[src/baseTrie.js:120-136][12]
[src/baseTrie.js:118-134][12]

deletes a value given a `key`

Expand All @@ -61,7 +61,7 @@ deletes a value given a `key`

### getRaw

[src/baseTrie.js:145-162][13]
[src/baseTrie.js:143-160][13]

Retrieves a raw value in the underlying db

Expand All @@ -72,7 +72,7 @@ Retrieves a raw value in the underlying db

### putRaw

[src/baseTrie.js:192-201][14]
[src/baseTrie.js:190-199][14]

Writes a value directly to the underlining db

Expand All @@ -84,7 +84,7 @@ Writes a value directly to the underlining db

### delRaw

[src/baseTrie.js:210-218][15]
[src/baseTrie.js:208-216][15]

Removes a raw value in the underlying db

Expand All @@ -95,7 +95,7 @@ Removes a raw value in the underlying db

### findPath

[src/baseTrie.js:252-298][16]
[src/baseTrie.js:250-296][16]

Tries to find a path to the node for the given key
It returns a `stack` of nodes to the closet node
Expand All @@ -111,15 +111,15 @@ It returns a `stack` of nodes to the closet node

### createReadStream

[src/baseTrie.js:730-732][17]
[src/baseTrie.js:728-730][17]

The `data` event is given an `Object` hat has two properties; the `key` and the `value`. Both should be Buffers.

Returns **[stream.Readable][18]** Returns a [stream][19] of the contents of the `trie`

### batch

[src/baseTrie.js:756-766][20]
[src/baseTrie.js:754-764][20]

The given hash of operations (key additions or deletions) are executed on the DB

Expand All @@ -143,7 +143,7 @@ trie.batch(ops)

### checkRoot

[src/baseTrie.js:775-780][22]
[src/baseTrie.js:773-778][22]

Checks if a given root exists

Expand Down Expand Up @@ -189,21 +189,20 @@ These are not exposed.

### addHexPrefix

[src/trieNode.js:164-179][26]
[src/util/hex.js:7-22][26]

Prepends hex prefix to an array of nibbles.

#### Parameters

- `key`
- `terminator`
- `dataArr` **[Array][21]**
- `Array` **[Array][21]** of nibbles

Returns **[Buffer][6]** returns buffer of encoded data
hexPrefix
Returns **[Array][21]** returns buffer of encoded data
\*

### asyncFirstSeries

[src/util.js:63-79][27]
[src/util/async.js:38-54][27]

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
Expand All @@ -214,27 +213,27 @@ If all run without a truthy result, return undefined
- `iterator`
- `cb`

### callTogether
### doKeysMatch

[src/util.js:37-57][28]
[src/util/nibbles.js:56-59][28]

Take two or more functions and returns a function that will execute all of
the given functions
Compare two nibble array keys.

### doKeysMatch
#### Parameters

[src/util.js:28-31][29]
- `keyA` **[Array][21]**
- `keyB` **[Array][21]**

Compare two 'nibble array' keys
##

#### Parameters
[src/util/async.js:3-6][29]

- `keyA`
- `keyB`
Take two or more functions and returns a function that will execute all of
the given functions

[1]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/secure.js#L10-L15 "Source code on GitHub"
[1]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/secure.js#L10-L15 "Source code on GitHub"

[2]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L25-L781 "Source code on GitHub"
[2]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L23-L779 "Source code on GitHub"

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

Expand All @@ -248,44 +247,44 @@ Compare two 'nibble array' keys

[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

[9]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L65-L77 "Source code on GitHub"
[9]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L63-L75 "Source code on GitHub"

[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function

[11]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L87-L111 "Source code on GitHub"
[11]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L85-L109 "Source code on GitHub"

[12]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L120-L136 "Source code on GitHub"
[12]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L118-L134 "Source code on GitHub"

[13]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L145-L162 "Source code on GitHub"
[13]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L143-L160 "Source code on GitHub"

[14]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L192-L201 "Source code on GitHub"
[14]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L190-L199 "Source code on GitHub"

[15]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L210-L218 "Source code on GitHub"
[15]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L208-L216 "Source code on GitHub"

[16]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L252-L298 "Source code on GitHub"
[16]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L250-L296 "Source code on GitHub"

[17]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L730-L732 "Source code on GitHub"
[17]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L728-L730 "Source code on GitHub"

[18]: https://nodejs.org/api/stream.html#stream_class_stream_readable

[19]: https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_readable

[20]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L756-L766 "Source code on GitHub"
[20]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L754-L764 "Source code on GitHub"

[21]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[22]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/baseTrie.js#L775-L780 "Source code on GitHub"
[22]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/baseTrie.js#L773-L778 "Source code on GitHub"

[23]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/proof.js#L12-L29 "Source code on GitHub"
[23]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/proof.js#L12-L29 "Source code on GitHub"

[24]: #trie

[25]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/proof.js#L39-L100 "Source code on GitHub"
[25]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/proof.js#L39-L100 "Source code on GitHub"

[26]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/trieNode.js#L164-L179 "Source code on GitHub"
[26]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/util/hex.js#L7-L22 "Source code on GitHub"

[27]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/util.js#L63-L79 "Source code on GitHub"
[27]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/util/async.js#L38-L54 "Source code on GitHub"

[28]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/util.js#L37-L57 "Source code on GitHub"
[28]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/util/nibbles.js#L56-L59 "Source code on GitHub"

[29]: https://github.com/alextsg/merkle-patricia-tree/blob/c2da4c34d8001120dfc08aa45e2a9894a7927aab/src/util.js#L28-L31 "Source code on GitHub"
[29]: https://git@github.com/:ethereumjs/merkle-patricia-tree/blob/f6dbe761448c6a886a2e8fdb920c396c29d8c99b/src/util/async.js#L3-L6 "Source code on GitHub"
4 changes: 4 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module.exports = function (config) {
'karma-firefox-launcher',
'karma-detect-browsers'
],
browserify: {
debug: true,
transform: ['babelify']
},
detectBrowsers: {
enabled: true,
usePhantomJS: false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "npm run test:node && npm run test:browser",
"coverage": "istanbul cover tape ./test/*.js",
"coverage": "istanbul cover babel-tape-runner ./test/*.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not understanding the intention respectively the effect of this change, could you describe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coverage tests were failing due to some ES6 syntax. Unit tests were already using babel-tape-runner ("test:node": "babel-tape-runner ./test/*.js",).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok.

"coveralls": "npm run coverage && coveralls <coverage/lcov.info",
"lint": "standard",
"prepublishOnly": "npm run build",
Expand Down
12 changes: 5 additions & 7 deletions src/baseTrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ const semaphore = require('semaphore')
const TrieNode = require('./trieNode')
const ReadStream = require('./readStream')
const PrioritizedTaskExecutor = require('./prioritizedTaskExecutor')
const matchingNibbleLength = require('./util').matchingNibbleLength
const doKeysMatch = require('./util').doKeysMatch
const callTogether = require('./util').callTogether
const asyncFirstSeries = require('./util').asyncFirstSeries
const { callTogether, asyncFirstSeries } = require('./util/async')
const { stringToNibbles, matchingNibbleLength, doKeysMatch } = require('./util/nibbles')

/**
* 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
Expand Down Expand Up @@ -251,7 +249,7 @@ module.exports = class Trie {
*/
findPath (targetKey, cb) {
const stack = []
targetKey = TrieNode.stringToNibbles(targetKey)
targetKey = stringToNibbles(targetKey)

this._walkTrie(this.root, processNode, cb)

Expand Down Expand Up @@ -359,7 +357,7 @@ module.exports = class Trie {
const lastNode = stack.pop()

// add the new nodes
key = TrieNode.stringToNibbles(key)
key = stringToNibbles(key)

// Check if the last node is a leaf and the key matches to this
let matchLeaf = false
Expand Down Expand Up @@ -626,7 +624,7 @@ module.exports = class Trie {

if (!Array.isArray(key)) {
// convert key to nibbles
key = TrieNode.stringToNibbles(key)
key = stringToNibbles(key)
}

if (!parentNode) {
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint-interface.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const level = require('level-mem')
const async = require('async')
const WriteStream = require('level-ws')
const callTogether = require('./util').callTogether
const callTogether = require('./util/async').callTogether

const ScratchReadStream = require('./scratchReadStream')

Expand Down
4 changes: 2 additions & 2 deletions src/proof.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const TrieNode = require('./trieNode')
const ethUtil = require('ethereumjs-util')
const matchingNibbleLength = require('./util').matchingNibbleLength
const { stringToNibbles, matchingNibbleLength } = require('./util/nibbles')

/**
* Returns a merkle proof for a given key
Expand Down Expand Up @@ -37,7 +37,7 @@ exports.prove = function (trie, key, cb) {
* @param {Function} cb A callback `Function` (arguments {Error} `err`, {String} `val`)
*/
exports.verifyProof = function (rootHash, key, proof, cb) {
key = TrieNode.stringToNibbles(key)
key = stringToNibbles(key)
var wantHash = ethUtil.toBuffer(rootHash)
for (var i = 0; i < proof.length; i++) {
var p = ethUtil.toBuffer(proof[i])
Expand Down
4 changes: 2 additions & 2 deletions src/readStream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Readable = require('readable-stream').Readable
const TrieNode = require('./trieNode')
const { nibblesToBuffer } = require('./util/nibbles')

module.exports = class TrieReadStream extends Readable {
constructor (trie) {
Expand All @@ -14,7 +14,7 @@ module.exports = class TrieReadStream extends Readable {
this._started = true
this.trie._findValueNodes((nodeRef, node, key, next) => {
this.push({
key: TrieNode.nibblesToBuffer(key),
key: nibblesToBuffer(key),
value: node.value
})

Expand Down
Loading