Delete node error
Lion17 opened this issue · 6 comments
When you try to remove a single node from a branch with a value, an error occurs.
To reproduce:
var Trie = require('merkle-patricia-tree')
trie = new Trie()
var ops = [
{ type: 'put', key: 'do', value: 'verb' }
, { type: 'put', key: 'dog', value: 'puppy' }
]
trie.batch(ops,
() => {
trie.del('dog')
}
)
Output:
node test_trie.js
/home/local/tmp/node_modules/merkle-patricia-tree/baseTrie.js:631
var branchNodeKey = branchNode.key;
^TypeError: Cannot read property 'key' of undefined
at processBranchNode (/home/local/tmp/node_modules/merkle-patricia-tree/baseTrie.js:631:40)
at /home/local/tmp/node_modules/merkle-patricia-tree/baseTrie.js:730:19
at /home/local/tmp/node_modules/merkle-patricia-tree/baseTrie.js:212:11
at /home/local/tmp/node_modules/merkle-patricia-tree/util.js:87:7
at /home/local/tmp/node_modules/async/dist/async.js:473:16
at replenish (/home/local/tmp/node_modules/async/dist/async.js:1006:25)
at iterateeCallback (/home/local/tmp/node_modules/async/dist/async.js:995:17)
at /home/local/tmp/node_modules/async/dist/async.js:969:16
at /home/local/tmp/node_modules/merkle-patricia-tree/util.js:83:7
at /home/local/tmp/node_modules/merkle-patricia-tree/baseTrie.js:187:13
The reason is that when nodes in a branch are counted (baseTrie.js:573), the value of the node is taken as a reference to the node.
Next, there is a try to get the node (baseTrie.js:588) and access it (baseTrie.js:490).
Thanks for reporting, can you open a PR on this?
(optimally with an additional test case?)
Sorry! But I am writing in C ++, I tried to port the algorithm and came across errors. I ran the node.js to check if I introduced these errors. I just reported them.
Update: see https://github.com/ethereumjs/merkle-patricia-tree/issues/85 for a path to an updated example to retest this.
Will close this in favor of #85