From c9b038d4e05f05b889ab50d60db07efbad427298 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 24 Oct 2016 19:16:10 -0700 Subject: [PATCH] scanning: more bloom filter things. --- lib/chain/chaindb.js | 23 ++--------------------- lib/http/server.js | 39 +++++++++++++++++++++++++++++++++------ lib/net/pool.js | 2 +- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index c4ce22d4..9f9184e7 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -1108,7 +1108,6 @@ ChainDB.prototype.scan = co(function* scan(start, filter, iter) { var total = 0; var i, j, entry, hashes; var hash, tx, txs, block; - var input, prevout, found; if (start == null) start = this.network.genesis.hash; @@ -1127,7 +1126,7 @@ ChainDB.prototype.scan = co(function* scan(start, filter, iter) { throw new Error('Cannot rescan an alternate chain.'); while (entry) { - block = yield this.getBlock(entry.hash); + block = yield this.getFullBlock(entry.hash); txs = []; total++; @@ -1145,30 +1144,12 @@ ChainDB.prototype.scan = co(function* scan(start, filter, iter) { for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; - found = false; - - if (!tx.isCoinbase()) { - for (j = 0; j < tx.inputs.length; j++) { - input = tx.inputs[j]; - prevout = input.prevout; - if (filter.test(prevout.hash, 'hex')) { - txs.push(tx); - found = true; - break; - } - } - - if (found) - continue; - } - - hashes = tx.getOutputHashes(); + hashes = tx.getHashes(); for (j = 0; j < hashes.length; j++) { hash = hashes[j]; if (filter.test(hash)) { txs.push(tx); - filter.add(tx.hash()); break; } } diff --git a/lib/http/server.js b/lib/http/server.js index ca87df01..8275c17f 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -1561,7 +1561,30 @@ ClientSocket.prototype.testBlock = function testBlock(block) { }; ClientSocket.prototype.testFilter = function testFilter(tx) { - var i, hashes, hash, input, prevout; + if (this.chain.db.options.spv) + return this.testFilterSPV(tx); + return this.testFilterFull(tx); +}; + +ClientSocket.prototype.testFilterFull = function testFilterFull(tx) { + var i, hashes, hash; + + if (!this.filter) + return false; + + hashes = tx.getHashes(); + + for (i = 0; i < hashes.length; i++) { + hash = hashes[i]; + if (this.filter.test(hash)) + return true; + } + + return false; +}; + +ClientSocket.prototype.testFilterSPV = function testFilterSPV(tx) { + var i, hash, input, prevout, output, outpoint; if (!this.filter) return false; @@ -1570,17 +1593,21 @@ ClientSocket.prototype.testFilter = function testFilter(tx) { for (i = 0; i < tx.inputs.length; i++) { input = tx.inputs[i]; prevout = input.prevout; - if (this.filter.test(prevout.hash, 'hex')) + if (this.filter.test(prevout.toRaw())) return true; } } - hashes = tx.getOutputHashes(); + for (i = 0; i < tx.outputs.length; i++) { + output = tx.outputs[i]; + hash = output.getHash(); + + if (!hash) + continue; - for (i = 0; i < hashes.length; i++) { - hash = hashes[i]; if (this.filter.test(hash)) { - this.filter.add(tx.hash()); + outpoint = Outpoint.fromTX(tx, i); + this.filter.add(outpoint.toRaw()); return true; } } diff --git a/lib/net/pool.js b/lib/net/pool.js index a07ccf81..ab9a027f 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -230,7 +230,7 @@ Pool.prototype._initOptions = function _initOptions() { } if (this.options.spv) - this.spvFilter = Bloom.fromRate(10000, 0.001, constants.bloom.NONE); + this.spvFilter = Bloom.fromRate(10000, 0.001, constants.bloom.ALL); if (!this.options.mempool) this.txFilter = new Bloom.Rolling(50000, 0.000001);