diff --git a/src/baseTrie.ts b/src/baseTrie.ts index b23785e..fbb660f 100644 --- a/src/baseTrie.ts +++ b/src/baseTrie.ts @@ -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 @@ -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 } diff --git a/src/checkpointTrie.ts b/src/checkpointTrie.ts index b47dd3f..26abb29 100644 --- a/src/checkpointTrie.ts +++ b/src/checkpointTrie.ts @@ -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') /** @@ -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() - } }