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

Commit

Permalink
Merge 11df2bd into 4d918be
Browse files Browse the repository at this point in the history
  • Loading branch information
s1na committed Jun 25, 2019
2 parents 4d918be + 11df2bd commit 1b8b772
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
29 changes: 27 additions & 2 deletions src/baseTrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = class Trie {
this.EMPTY_TRIE_ROOT = ethUtil.KECCAK256_RLP
this.sem = semaphore(1)

this.db = db || new DB()
this.db = db ? new DB(db) : new DB()

Object.defineProperty(this, 'root', {
set (value) {
Expand Down Expand Up @@ -162,6 +162,31 @@ module.exports = class Trie {
})
}

/**
* Retrieves a value directly from key/value db.
* @deprecated
*/
getRaw (key, cb) {
this.db.get(key, cb)
}

/**
* Writes a value under given key directly to the
* key/value db.
* @deprecated
*/
putRaw (key, value, cb) {
this.db.put(key, value, cb)
}

/**
* Deletes key directly from underlying key/value db.
* @deprecated
*/
delRaw (key, cb) {
this.db.del(key, cb)
}

// retrieves a node from dbs by hash
_lookupNode (node, cb) {
if (TrieNode.isRawNode(node)) {
Expand Down Expand Up @@ -696,7 +721,7 @@ module.exports = class Trie {
// and starting at the same root
copy () {
const db = this.db.copy()
return new Trie(db, this.root)
return new Trie(db._leveldb, this.root)
}

/**
Expand Down
23 changes: 17 additions & 6 deletions src/checkpointTrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,29 @@ module.exports = class CheckpointTrie extends BaseTrie {
* 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).
* @method copy
* @param {boolean} includeCheckpoints - If true and during a checkpoint, the copy will
* contain the checkpointing metadata and will use the same scratch as underlying db.
*/
copy () {
copy (includeCheckpoints = true) {
const db = this._mainDB.copy()
const trie = new CheckpointTrie(db, this.root)
if (this.isCheckpoint) {
const trie = new CheckpointTrie(db._leveldb, this.root)
if (includeCheckpoints && this.isCheckpoint) {
trie._checkpoints = this._checkpoints.slice()
trie._scratch = this._scratch.copy()
trie.db = trie._scratch
}
return trie
}

/**
* Writes a value under given key directly to the
* key/value db, disregarding checkpoints.
* @deprecated
*/
putRaw (key, value, cb) {
this._mainDB.put(key, value, cb)
}

/**
* Enter into checkpoint mode.
* @private
Expand All @@ -124,7 +134,7 @@ module.exports = class CheckpointTrie extends BaseTrie {

if (commitState) {
this._createScratchReadStream(scratch)
.pipe(WriteStream(this.db))
.pipe(WriteStream(this.db._leveldb))
.on('close', cb)
} else {
async.nextTick(cb)
Expand All @@ -139,7 +149,8 @@ module.exports = class CheckpointTrie extends BaseTrie {
*/
_createScratchReadStream (scratch) {
scratch = scratch || this._scratch
const trie = new BaseTrie(scratch, this.root)
const trie = new BaseTrie(scratch._leveldb, this.root)
trie.db = scratch
return new ScratchReadStream(trie)
}
}
5 changes: 3 additions & 2 deletions src/secure.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ module.exports = class SecureTrie extends CheckpointTrie {
}

copy () {
const db = this.db.copy()
return new SecureTrie(db, this.root)
const trie = super.copy(false)
const db = trie.db.copy()
return new SecureTrie(db._leveldb, this.root)
}

get (key, cb) {
Expand Down

0 comments on commit 1b8b772

Please sign in to comment.