-
Notifications
You must be signed in to change notification settings - Fork 89
Support for proofs of null/absence. Dried up prove/verify. #82
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,39 @@ module.exports = class Trie { | |
this.root = root | ||
} | ||
|
||
static fromProof (proofNodes, cb, proofTrie) { | ||
let opStack = proofNodes.map((nodeValue) => { | ||
return {type: 'put', key: ethUtil.keccak(nodeValue), value: ethUtil.toBuffer(nodeValue)} | ||
}) | ||
|
||
if (!proofTrie) { | ||
proofTrie = new Trie() | ||
if (opStack[0]) { proofTrie.root = opStack[0].key } | ||
} | ||
|
||
proofTrie.db.batch(opStack, (e) => { | ||
cb(e, proofTrie) | ||
}) | ||
} | ||
|
||
static prove (trie, key, cb) { | ||
trie.findPath(key, function (err, node, remaining, stack) { | ||
if (err) return cb(err) | ||
let p = stack.map((stackElem) => { return stackElem.serialize() }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're including embedded nodes (specifically those with length < 32) in the proof in comparison with the previous |
||
cb(null, p) | ||
}) | ||
} | ||
|
||
static verifyProof (rootHash, key, proofNodes, cb) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't root of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually yeah that's true. It's hard to say who's job this is. But if it's going to take the argument
|
||
let proofTrie = new Trie(null, rootHash) | ||
Trie.fromProof(proofNodes, (error, proofTrie) => { | ||
if (error) cb(new Error('Invalid proof nodes given'), null) | ||
proofTrie.get(key, (e, r) => { | ||
return cb(e, r) | ||
}) | ||
}, proofTrie) | ||
} | ||
|
||
/** | ||
* Gets a value given a `key` | ||
* @method get | ||
|
@@ -135,16 +168,11 @@ module.exports = class Trie { | |
cb(null, new TrieNode(node)) | ||
} else { | ||
this.db.get(node, (err, value) => { | ||
if (err) { | ||
throw err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
if (value) { | ||
value = new TrieNode(rlp.decode(value)) | ||
} else { | ||
err = new Error('Missing node in DB') | ||
} | ||
|
||
cb(err, value) | ||
}) | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,18 +6,17 @@ const ethUtil = require('ethereumjs-util') | |
|
||
tape('simple save and retrive', function (tester) { | ||
var it = tester.test | ||
|
||
it('should not crash if given a non-existant root', function (t) { | ||
var root = new Buffer('3f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d', 'hex') | ||
var trie = new Trie(null, root) | ||
|
||
trie.get('test', function (err, value) { | ||
t.equal(value, null) | ||
|
||
t.notEqual(err, null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note The behavior here has changed slightly, but reading the objective of the test I think it's ok |
||
t.end() | ||
}) | ||
}) | ||
|
||
var trie = new Trie() | ||
|
||
it('save a value', function (t) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its kindof like the verification is done in this step because when you traverse the tree you already know each key is the hash of the node it's storing