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

Commit

Permalink
Merge 40b02ee into 7add2b2
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Aug 31, 2020
2 parents 7add2b2 + 40b02ee commit 5750d5c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
30 changes: 24 additions & 6 deletions src/baseTrie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ export class Trie {
return !!value
}

/**
* BaseTrie has no checkpointing so return false
*/
get isCheckpoint() {
return false
}

/**
* Gets a value given a `key`
* @param key - the key to search for
Expand Down Expand Up @@ -611,12 +618,23 @@ export class Trie {
const rlpNode = node.serialize()

if (rlpNode.length >= 32 || topLevel) {
const hashRoot = node.hash()
opStack.push({
type: 'put',
key: hashRoot,
value: rlpNode,
})
// Do not use TrieNode.hash() here otherwise serialize()
// is applied twice (performance)
const hashRoot = keccak(rlpNode)

if (remove && this.isCheckpoint) {
opStack.push({
type: 'del',
key: hashRoot,
})
} else {
opStack.push({
type: 'put',
key: hashRoot,
value: rlpNode,
})
}

return hashRoot
}

Expand Down
37 changes: 1 addition & 36 deletions src/checkpointTrie.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Trie as BaseTrie } from './baseTrie'
import { ScratchReadStream } from './scratchReadStream'
import { ScratchDB } from './scratch'
import { DB, BatchDBOp } from './db'
import { TrieNode } from './trieNode'
import { DB } from './db'
const WriteStream = require('level-ws')

/**
Expand Down Expand Up @@ -139,38 +138,4 @@ export class CheckpointTrie extends BaseTrie {
trie.db = scratch
return new ScratchReadStream(trie)
}

/**
* Formats node to be saved by `levelup.batch`.
* @private
* @param node - the node to format.
* @param topLevel - if the node is at the top level.
* @param opStack - the opStack to push the node's data.
* @param remove - whether to remove the node (only used for CheckpointTrie).
* @returns The node's hash used as the key or the rawNode.
*/
_formatNode(node: TrieNode, topLevel: boolean, opStack: BatchDBOp[], remove: boolean = false) {
const rlpNode = node.serialize()

if (rlpNode.length >= 32 || topLevel) {
const hashRoot = node.hash()

if (remove && this.isCheckpoint) {
opStack.push({
type: 'del',
key: hashRoot,
})
} else {
opStack.push({
type: 'put',
key: hashRoot,
value: rlpNode,
})
}

return hashRoot
}

return node.raw()
}
}

0 comments on commit 5750d5c

Please sign in to comment.