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

Commit

Permalink
Added some extra failing test cases for merkle proof generation/verif…
Browse files Browse the repository at this point in the history
…ication
  • Loading branch information
holgerd77 committed Nov 30, 2017
1 parent 394ed84 commit 1f260b6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
20 changes: 10 additions & 10 deletions proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const matchingNibbleLength = require('./util').matchingNibbleLength
* @method Trie.prove
* @param {Trie} trie
* @param {String} key
* @param {Function} cb A callback `Function` (arguments {Error} `err`, {Array.<TrieNode>} `proof`)
* @param {Function} cb A callback `Function` (arguments {Error} `err`, {Array.<TrieNode>} `proof`)
*/
exports.prove = function (trie, key, cb) {
var nodes
Expand All @@ -34,7 +34,7 @@ exports.prove = function (trie, key, cb) {
* @param {Buffer} rootHash
* @param {String} key
* @param {Array.<TrieNode>} proof
* @param {Function} cb A callback `Function` (arguments {Error} `err`, {String} `val`)
* @param {Function} cb A callback `Function` (arguments {Error} `err`, {String} `val`)
*/
exports.verifyProof = function (rootHash, key, proof, cb) {
key = TrieNode.stringToNibbles(key)
Expand All @@ -43,14 +43,14 @@ exports.verifyProof = function (rootHash, key, proof, cb) {
var p = ethUtil.toBuffer(proof[i])
var hash = ethUtil.sha3(proof[i])
if (Buffer.compare(hash, wantHash)) {
return cb(new Error('bad proof node ' + i + ': hash mismatch'))
return cb(new Error('Bad proof node ' + i + ': hash mismatch'))
}
var node = new TrieNode(ethUtil.rlp.decode(p))
var cld
if (node.type === 'branch') {
if (key.length === 0) {
if (i !== proof.length - 1) {
return cb(new Error('additional nodes at end of proof'))
return cb(new Error('Additional nodes at end of proof (branch)'))
}
return cb(null, node.value)
}
Expand All @@ -59,29 +59,29 @@ exports.verifyProof = function (rootHash, key, proof, cb) {
if (cld.length === 2) {
var embeddedNode = new TrieNode(cld)
if (i !== proof.length - 1) {
return cb(new Error('Key does not match with the proof one'))
return cb(new Error('Additional nodes at end of proof (embeddedNode)'))
}

if (matchingNibbleLength(embeddedNode.key, key) !== embeddedNode.key.length) {
return cb(new Error('Key does not match with the proof one'))
return cb(new Error('Key length does not match with the proof one (embeddedNode)'))
}
key = key.slice(embeddedNode.key.length)
if (key.length !== 0) {
return cb(new Error('Key does not match with the proof one'))
return cb(new Error('Key does not match with the proof one (embeddedNode)'))
}
return cb(null, embeddedNode.value)
} else {
wantHash = cld
}
} else if ((node.type === 'extention') || (node.type === 'leaf')) {
if (matchingNibbleLength(node.key, key) !== node.key.length) {
return cb(new Error('Key does not match with the proof one'))
return cb(new Error('Key does not match with the proof one (extention|leaf)'))
}
cld = node.value
key = key.slice(node.key.length)
if (key.length === 0) {
if (i !== proof.length - 1) {
return cb(new Error('Key does not match with the proof one'))
return cb(new Error('Additional nodes at end of proof (extention|leaf)'))
}
return cb(null, cld)
} else {
Expand All @@ -91,5 +91,5 @@ exports.verifyProof = function (rootHash, key, proof, cb) {
return cb(new Error('Invalid node type'))
}
}
cb(new Error('unexpected end of proof'))
cb(new Error('Unexpected end of proof'))
}
28 changes: 28 additions & 0 deletions test/proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ tape('simple merkle proofs generation and verification', function (tester) {
cb()
})
})
},
function (cb) {
Trie.prove(trie, 'key2bb', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'randomkey', prove, function (err, val) {
t.notEqual(err, null, 'Expected error: ' + err.message)
cb()
})
})
},
function (cb) {
Trie.prove(trie, 'key2bb', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key2b', prove, function (err, val) {
t.notEqual(err, null, 'Expected error: ' + err.message)
cb()
})
})
},
function (cb) {
Trie.prove(trie, 'key2bb', function (err, prove) {
if (err) return cb(err)
prove.push(Buffer.from('123456'))
Trie.verifyProof(trie.root, 'key2b', prove, function (err, val) {
t.notEqual(err, null, 'Expected error: ' + err.message)
cb()
})
})
}
], function (err) {
t.end(err)
Expand Down

0 comments on commit 1f260b6

Please sign in to comment.