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

Promisify rest of library#107

Merged
holgerd77 merged 4 commits intomasterfrom
promisifyLib
Apr 13, 2020
Merged

Promisify rest of library#107
holgerd77 merged 4 commits intomasterfrom
promisifyLib

Conversation

@ryanio
Copy link
Contributor

@ryanio ryanio commented Apr 3, 2020

This PR:

  1. Removes callbacks in favor of promises
  2. Removes use of async lib (The async library organization#55)
  3. Updates README examples to new syntax
  4. Removes deprecated methods (getRaw, putRaw, delRaw)

Closes #66


Pending TODOs:

  1. Investigate why _formatNode does not use the remove param inside the method (lost in a prior refactor?)

@lgtm-com

This comment has been minimized.

@github-actions
Copy link

github-actions bot commented Apr 8, 2020

Coverage Status

Coverage increased (+0.9%) to 94.992% when pulling 3a42145 on promisifyLib into ce10588 on master.

@ryanio ryanio marked this pull request as ready for review April 8, 2020 01:11
@holgerd77
Copy link
Member

Great to see the tests pass here, very cool! 😄

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

To be continued...

Can also be picked up (also partially).

- [Trie and Patricia Trie Overview](https://www.youtube.com/watch?v=jXAHLqQthKw&t=26s)

# EthereumJS

Copy link
Member

Choose a reason for hiding this comment

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

Did an overall README check, everything covered, examples look good as well.

}
}

go()
Copy link
Member

Choose a reason for hiding this comment

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

Tested both manually locally, updated benchmark suites at least run through. This seems to need more work to be usable - the checkpointing benchmark file e.g. is not taking the samples into account at all e.g., think due to a bug with a reused/side-used i counter variable as far as I see it - nothing for this PR though but just as a note.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah thanks for pointing out, I will give it a closer look.

"browserify": "^16.5.0",
"husky": "^4.2.3",
"karma": "^4.4.1",
"karma-chrome-launcher": "^3.1.0",
Copy link
Member

Choose a reason for hiding this comment

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

package.json changes look good, types from removed libraries (async) removed as well.

"readable-stream": "^3.6.0",
"rlp": "^2.2.3",
"semaphore": ">=1.0.1"
"semaphore-async-await": "^1.5.1"
Copy link
Member

Choose a reason for hiding this comment

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

* @param {Buffer|String} [root] 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
* @param {Buffer} [root] - A `Buffer` for the root of a previously stored trie
Copy link
Member

Choose a reason for hiding this comment

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

Note: significant API change here, input limited to Buffer.

const p = stack.map((stackElem) => {
return stackElem.serialize()
})
return p
Copy link
Member

Choose a reason for hiding this comment

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

Nice, elegantifying. 😄

// then update
await this._updateNode(key, value, remaining, stack)
}
this.lock.signal()
Copy link
Member

Choose a reason for hiding this comment

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

This new async/await compatible locking is really great! Generally functions like the above (put) are so much more readable now, phew! 👍

Also thanks for all the small improvements like changing execution order on conditionals, rethinking the appliance of asserts, code cleanups,... 🎊

foundNode = decodeNode(value)
}

return foundNode
Copy link
Member

Choose a reason for hiding this comment

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

del, _lookupNode ok.

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

To be continued... (again: can also be (partly) taken over).

* - 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
* @param {Buffer} key - the search key
Copy link
Member

Choose a reason for hiding this comment

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

For the record (and the release notes): significant API change here (limitation to Buffer).

let targetKey = bufferToNibbles(key)

// walk trie and process nodes
await this._walkTrie(this.root, async (nodeRef, node, keyProgress, walkController) => {
Copy link
Member

Choose a reason for hiding this comment

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

Inlined processNode function, ok.


// Resolve if _walkTrie finishes without finding any nodes
resolve({ node: null, remaining: [], stack })
})
Copy link
Member

Choose a reason for hiding this comment

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

Rest of findPath more or less same code in a somewhat changed structural form, ok.

* @returns {Promise}
*/
_updateNode(
async _updateNode(
Copy link
Member

Choose a reason for hiding this comment

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

For the record: significant API change here (limit value input to Buffer).

} else {
resolve()
}
})
Copy link
Member

Choose a reason for hiding this comment

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

Whew, this is a biggy. I have to say that I just can't follow the changes here in _walkTrie, eventually someone else can give this another look (//cc @s1na @evertonfraga) For the moment I am not getting the whole picture.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I think I finally got the replacement constructs, can give this a go now too. 😀

})
async checkRoot(root: Buffer): Promise<boolean> {
const value = await this._lookupNode(root)
return !!value
Copy link
Member

Choose a reason for hiding this comment

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

Phew, nice. 😀

Apart from walkTrie needing a second look the rest from baseTrie.js looks good.

await this._exitCpMode(true)
}

this.lock.signal()
Copy link
Member

Choose a reason for hiding this comment

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

Nice restructure. 👍

let scratch = scratchDb || this._scratch
if (!scratch) {
throw new Error('No scratch found to use')
}
Copy link
Member

Choose a reason for hiding this comment

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

For the record: new throw on "No scratch found to use".

let scratch = scratchDb || this._scratch
if (!scratch) {
throw new Error('No scratch found to use')
}
Copy link
Member

Choose a reason for hiding this comment

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

checkpointTrie.ts looks good.

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

Two files/functions not reviewed yet (baseTrie->walkTrie() and scratch.ts), otherwise looking good. Really great work @ryanio Ryan! 🤗 🎸 🌴 🎉


this._leveldb.batch(opStack, ENCODING_OPTS, cb)
async batch(opStack: BatchDBOp[]): Promise<void> {
await this._leveldb.batch(opStack, ENCODING_OPTS)
Copy link
Member

Choose a reason for hiding this comment

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

db.ts ok.

})
await walkController.next()
})
this.push(null)
Copy link
Member

Choose a reason for hiding this comment

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

readStream.ts changes ok.

})
await walkController.next()
})
this.push(null)
Copy link
Member

Choose a reason for hiding this comment

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

scratchReadStream.ts ok.

async del(key: Buffer): Promise<void> {
const hash = keccak256(key)
super.del(hash, cb)
await super.del(hash)
Copy link
Member

Choose a reason for hiding this comment

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

secure.ts ok.

return BranchNode.fromArray(raw)
} else if (raw.length === 2) {
const nibbles = stringToNibbles(raw[0])
const nibbles = bufferToNibbles(raw[0])
Copy link
Member

Choose a reason for hiding this comment

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

trieNode.ts ok.

await trie.commit()
t.equal(trie.isCheckpoint, false)
t.equal(trie.root.toString('hex'), root.toString('hex'))
t.end()
Copy link
Member

Choose a reason for hiding this comment

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

checkpoint.spec.ts ok.

await db.batch(ops)
const res = await db.get(k2)
st.ok(v2.equals(res!))
st.end()
Copy link
Member

Choose a reason for hiding this comment

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

db.spec.ts ok.

General (file unspecific) remark: the error cases in this library seem to be not very well tested, might be a field for some future improvements.

t.equal(Object.keys(expectedNodes).length, 0)
stream.on('end', () => {
let keys = Object.keys(expectedNodes)
t.equal(keys.length, 0)
Copy link
Member

Choose a reason for hiding this comment

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

Rest of the tests look good. 😄

}

asyncFirstSeries(getDBs, dbGet, cb)
return value
Copy link
Member

Choose a reason for hiding this comment

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

Leave scratch.ts also open since not seeing through. Maybe will have a look with some fresh eyes in the following days myself, too tired right now, or otherwise open for a look from someone else as well (//cc @s1na @evertonfraga).

Copy link
Member

Choose a reason for hiding this comment

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

Ok, finally got this whole construct decomposition. Phew. 😛 Extremely valuable to have this simplified on such a level, thanks Ryan for working yourself through stuff like this. This was really over-the-top complex and extremely hard to grasp before.

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

LGTM. 😄

Will approve and directly merge.

}

asyncFirstSeries(getDBs, dbGet, cb)
return value
Copy link
Member

Choose a reason for hiding this comment

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

Ok, finally got this whole construct decomposition. Phew. 😛 Extremely valuable to have this simplified on such a level, thanks Ryan for working yourself through stuff like this. This was really over-the-top complex and extremely hard to grasp before.

} else {
resolve()
}
})
Copy link
Member

Choose a reason for hiding this comment

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

Ok, I think I finally got the replacement constructs, can give this a go now too. 😀

const childRef = node.getBranch(childIndex)
if (!childRef) {
throw new Error('Could not get branch of childIndex')
}
Copy link
Member

Choose a reason for hiding this comment

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

For the record: new "Could not get branch of childIndex" error thrown here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Promise and callbacks for async functions

2 participants