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

Commit 7583aa9

Browse files
authored
Merge pull request #109 from ethereumjs/fixFormatNodeRemove
Better document `_formatNode`
2 parents 3a9abe9 + 71c3aac commit 7583aa9

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

src/baseTrie.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export class Trie {
340340
} else if (lastNode instanceof BranchNode) {
341341
stack.push(lastNode)
342342
if (keyRemainder.length !== 0) {
343-
// add an extention to a branch node
343+
// add an extension to a branch node
344344
keyRemainder.shift()
345345
// create a new leaf
346346
const newLeaf = new LeafNode(keyRemainder, value)
@@ -354,7 +354,7 @@ export class Trie {
354354
const matchingLength = matchingNibbleLength(lastKey, keyRemainder)
355355
const newBranchNode = new BranchNode()
356356

357-
// create a new extention node
357+
// create a new extension node
358358
if (matchingLength !== 0) {
359359
const newKey = lastNode.key.slice(0, matchingLength)
360360
const newExtNode = new ExtensionNode(newKey, value)
@@ -369,12 +369,12 @@ export class Trie {
369369
const branchKey = lastKey.shift() as number
370370

371371
if (lastKey.length !== 0 || lastNode instanceof LeafNode) {
372-
// shriking extention or leaf
372+
// shrinking extension or leaf
373373
lastNode.key = lastKey
374-
const formatedNode = this._formatNode(lastNode, false, toSave)
375-
newBranchNode.setBranch(branchKey, formatedNode as EmbeddedNode)
374+
const formattedNode = this._formatNode(lastNode, false, toSave)
375+
newBranchNode.setBranch(branchKey, formattedNode as EmbeddedNode)
376376
} else {
377-
// remove extention or attaching
377+
// remove extension or attaching
378378
this._formatNode(lastNode, false, toSave, true)
379379
newBranchNode.setBranch(branchKey, lastNode.value)
380380
}
@@ -395,7 +395,14 @@ export class Trie {
395395
await this._saveStack(key, stack, toSave)
396396
}
397397

398-
// walk tree
398+
/**
399+
* Walks a trie until finished.
400+
* @method _walkTrie
401+
* @private
402+
* @param {Buffer} root
403+
* @param {Function} onNode - callback to call when a node is found
404+
* @returns {Promise} - returns when finished walking trie
405+
*/
399406
async _walkTrie(root: Buffer, onNode: FoundNode): Promise<void> {
400407
return new Promise(async (resolve) => {
401408
const self = this
@@ -542,23 +549,23 @@ export class Trie {
542549
}
543550

544551
if (branchNode instanceof BranchNode) {
545-
// create an extention node
546-
// branch->extention->branch
552+
// create an extension node
553+
// branch->extension->branch
547554
// @ts-ignore
548-
const extentionNode = new ExtensionNode([branchKey], null)
549-
stack.push(extentionNode)
555+
const extensionNode = new ExtensionNode([branchKey], null)
556+
stack.push(extensionNode)
550557
key.push(branchKey)
551558
} else {
552559
const branchNodeKey = branchNode.key
553-
// branch key is an extention or a leaf
554-
// branch->(leaf or extention)
560+
// branch key is an extension or a leaf
561+
// branch->(leaf or extension)
555562
branchNodeKey.unshift(branchKey)
556563
branchNode.key = branchNodeKey.slice(0)
557564
key = key.concat(branchNodeKey)
558565
}
559566
stack.push(branchNode)
560567
} else {
561-
// parent is an extention
568+
// parent is an extension
562569
let parentKey = parentNode.key
563570

564571
if (branchNode instanceof BranchNode) {
@@ -569,7 +576,7 @@ export class Trie {
569576
stack.push(parentNode)
570577
} else {
571578
const branchNodeKey = branchNode.key
572-
// branch node is an leaf or extention and parent node is an exstention
579+
// branch node is an leaf or extension and parent node is an exstention
573580
// add two keys together
574581
// dont push the parent node
575582
branchNodeKey.unshift(branchKey)
@@ -654,8 +661,16 @@ export class Trie {
654661
await this._putNode(newNode)
655662
}
656663

657-
// formats node to be saved by levelup.batch.
658-
// returns either the hash that will be used key or the rawNode
664+
/**
665+
* Formats node to be saved by levelup.batch.
666+
* @method _formatNode
667+
* @private
668+
* @param {TrieNode} node - the node to format
669+
* @param {Boolean} topLevel - if the node is at the top level
670+
* @param {BatchDBOp[]} opStack - the opStack to push the node's data
671+
* @param {Boolean} remove - whether to remove the node (only used for CheckpointTrie)
672+
* @returns {Buffer | (EmbeddedNode | null)[]} - the node's hash used as the key or the rawNode
673+
*/
659674
_formatNode(
660675
node: TrieNode,
661676
topLevel: boolean,

src/checkpointTrie.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,16 @@ export class CheckpointTrie extends BaseTrie {
144144
return new ScratchReadStream(trie)
145145
}
146146

147-
// formats node to be saved by levelup.batch.
148-
// returns either the hash that will be used key or the rawNode
147+
/**
148+
* Formats node to be saved by levelup.batch.
149+
* @method _formatNode
150+
* @private
151+
* @param {TrieNode} node - the node to format
152+
* @param {Boolean} topLevel - if the node is at the top level
153+
* @param {BatchDBOp[]} opStack - the opStack to push the node's data
154+
* @param {Boolean} remove - whether to remove the node (only used for CheckpointTrie)
155+
* @returns {Buffer | (EmbeddedNode | null)[]} - the node's hash used as the key or the rawNode
156+
*/
149157
_formatNode(node: TrieNode, topLevel: boolean, opStack: BatchDBOp[], remove: boolean = false) {
150158
const rlpNode = node.serialize()
151159

test/index.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ tape('simple save and retrive', function (tester) {
102102
})
103103
})
104104

105-
tape('testing Extentions and branches', function (tester) {
105+
tape('testing extensions and branches', function (tester) {
106106
const it = tester.test
107107
const trie = new CheckpointTrie()
108108

@@ -111,7 +111,7 @@ tape('simple save and retrive', function (tester) {
111111
t.end()
112112
})
113113

114-
it('should create extention to store this value', async function (t) {
114+
it('should create extension to store this value', async function (t) {
115115
await trie.put(Buffer.from('do'), Buffer.from('verb'))
116116
t.equal(
117117
'f803dfcb7e8f1afd45e88eedb4699a7138d6c07b71243d9ae9bff720c99925f9',
@@ -120,7 +120,7 @@ tape('simple save and retrive', function (tester) {
120120
t.end()
121121
})
122122

123-
it('should store this value under the extention ', async function (t) {
123+
it('should store this value under the extension', async function (t) {
124124
await trie.put(Buffer.from('done'), Buffer.from('finished'))
125125
t.equal(
126126
'409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb',
@@ -130,11 +130,11 @@ tape('simple save and retrive', function (tester) {
130130
})
131131
})
132132

133-
tape('testing Extentions and branches - reverse', function (tester) {
133+
tape('testing extensions and branches - reverse', function (tester) {
134134
const it = tester.test
135135
const trie = new CheckpointTrie()
136136

137-
it('should create extention to store this value', async function (t) {
137+
it('should create extension to store this value', async function (t) {
138138
await trie.put(Buffer.from('do'), Buffer.from('verb'))
139139
t.end()
140140
})
@@ -144,7 +144,7 @@ tape('simple save and retrive', function (tester) {
144144
t.end()
145145
})
146146

147-
it('should store this value under the extention ', async function (t) {
147+
it('should store this value under the extension', async function (t) {
148148
await trie.put(Buffer.from('done'), Buffer.from('finished'))
149149
t.equal(
150150
'409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb',
@@ -155,7 +155,7 @@ tape('simple save and retrive', function (tester) {
155155
})
156156
})
157157

158-
tape('testing deletions cases', function (tester) {
158+
tape('testing deletion cases', function (tester) {
159159
const it = tester.test
160160
const trie = new CheckpointTrie()
161161

@@ -170,7 +170,7 @@ tape('testing deletions cases', function (tester) {
170170
t.end()
171171
})
172172

173-
it('should delete from a branch->branch-extention', async function (t) {
173+
it('should delete from a branch->branch-extension', async function (t) {
174174
await trie.put(Buffer.from([11, 11, 11]), Buffer.from('first'))
175175
await trie.put(Buffer.from([12, 22, 22]), Buffer.from('create the first branch'))
176176
await trie.put(Buffer.from([12, 33, 33]), Buffer.from('create the middle branch'))
@@ -182,7 +182,7 @@ tape('testing deletions cases', function (tester) {
182182
t.end()
183183
})
184184

185-
it('should delete from a extention->branch-extention', async function (t) {
185+
it('should delete from a extension->branch-extension', async function (t) {
186186
await trie.put(Buffer.from([11, 11, 11]), Buffer.from('first'))
187187
await trie.put(Buffer.from([12, 22, 22]), Buffer.from('create the first branch'))
188188
await trie.put(Buffer.from([12, 33, 33]), Buffer.from('create the middle branch'))
@@ -195,7 +195,7 @@ tape('testing deletions cases', function (tester) {
195195
t.end()
196196
})
197197

198-
it('should delete from a extention->branch-branch', async function (t) {
198+
it('should delete from a extension->branch-branch', async function (t) {
199199
await trie.put(Buffer.from([11, 11, 11]), Buffer.from('first'))
200200
await trie.put(Buffer.from([12, 22, 22]), Buffer.from('create the first branch'))
201201
await trie.put(Buffer.from([12, 33, 33]), Buffer.from('create the middle branch'))

0 commit comments

Comments
 (0)