From 1f260b6beed09b666b5bfa36518b930cd400c711 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Thu, 30 Nov 2017 15:44:12 +0100 Subject: [PATCH] Added some extra failing test cases for merkle proof generation/verification --- proof.js | 20 ++++++++++---------- test/proof.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/proof.js b/proof.js index 8f20c64..d474413 100644 --- a/proof.js +++ b/proof.js @@ -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.} `proof`) + * @param {Function} cb A callback `Function` (arguments {Error} `err`, {Array.} `proof`) */ exports.prove = function (trie, key, cb) { var nodes @@ -34,7 +34,7 @@ exports.prove = function (trie, key, cb) { * @param {Buffer} rootHash * @param {String} key * @param {Array.} 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) @@ -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) } @@ -59,15 +59,15 @@ 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 { @@ -75,13 +75,13 @@ exports.verifyProof = function (rootHash, key, proof, cb) { } } 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 { @@ -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')) } diff --git a/test/proof.js b/test/proof.js index 56464c7..28515c0 100644 --- a/test/proof.js +++ b/test/proof.js @@ -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)