From 9f4ebfb1f9cecc991b7526a5f6bca770d6c32ba1 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 20 Oct 2017 07:56:48 -0400 Subject: [PATCH] Fixed mempool search by address. --- : | 65 -------------------------------- lib/services/mempool/encoding.js | 8 ++++ lib/services/mempool/index.js | 40 ++++++++++++++------ package-lock.json | 16 ++++---- 4 files changed, 45 insertions(+), 84 deletions(-) delete mode 100644 : diff --git a/: b/: deleted file mode 100644 index 7febdb6b..00000000 --- a/: +++ /dev/null @@ -1,65 +0,0 @@ -'use strict'; -var should = require('chai').should(); -var Tx = require('bcoin').tx; - -var Encoding = require('../../../lib/services/transaction/encoding'); - -describe('Transaction service encoding', function() { - - var servicePrefix = new Buffer('0000', 'hex'); - - var encoding = new Encoding(servicePrefix); - var txid = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; - var blockHash = txid; - var txHex = '0100000001cc3ffe0638792c8b39328bb490caaefe2cf418f2ce0144956e0c22515f29724d010000006a473044022030ce9fa68d1a32abf0cd4adecf90fb998375b64fe887c6987278452b068ae74c022036a7d00d1c8af19e298e04f14294c807ebda51a20389ad751b4ff3c032cf8990012103acfcb348abb526526a9f63214639d79183871311c05b2eebc727adfdd016514fffffffff02f6ae7d04000000001976a9144455183e407ee4d3423858c8a3275918aedcd18e88aca99b9b08010000001976a9140beceae2c29bfde08d2b6d80b33067451c5887be88ac00000000'; - var tx = Tx.fromRaw(txHex, 'hex'); - var txEncoded = Buffer.concat([new Buffer('00000002', 'hex'), new Buffer('00000001', 'hex'), new Buffer('0002', 'hex'), new Buffer('40000000000000004008000000000000', 'hex'), tx.toRaw()]); - var indexBuf = new Buffer(4); - indexBuf.writeUInt32BE(3); - - it('should encode transaction key' , function() { - var txBuf = new Buffer(txid, 'hex'); - encoding.encodeTransactionKey(txid).should.deep.equal(Buffer.concat([servicePrefix, new Buffer('00', 'hex'), txBuf])); - }); - - it('should decode transaction key', function() { - encoding.decodeTransactionKey(Buffer.concat([servicePrefix, new Buffer('00', 'hex'), new Buffer(txid, 'hex')])) - .should.equal(txid); - }); - - it('should encode transaction value', function() { - tx.__height = 2; - tx.__blockHash = blockHash; - tx.__timestamp = 1; - tx.__inputValues = [ 2, 3 ]; - - encoding.encodeTransactionValue(tx).should.deep.equal(txEncoded); - }); - - it('should decode transaction value', function() { - var tx = encoding.decodeTransactionValue(txEncoded); - tx.__height.should.equal(2); - - tx.__timestamp.should.equal(1); - tx.__inputValues.should.deep.equal([2,3]); - tx.toRaw().toString('hex').should.equal(txHex); - }); - - it('should encode spent key', function() { - encoding.encodeSpentKey(txid, 3).should.deep.equal(Buffer.concat([servicePrefix, - new Buffer('01', 'hex'), new Buffer(txid, 'hex'), indexBuf])); - }); - - it('should decode spent key', function() { - encoding.decodeSpentKey(Buffer.concat([servicePrefix, - new Buffer('01', 'hex'), new Buffer(txid, 'hex'), indexBuf])).should.deep.equal({ txid: txid, outputIndex: 3 }); - }); - - it('should encode spent value', function() { - encoding.encodeSpentValue(txid, 3).should.deep.equal(Buffer.concat([new Buffer(txid, 'hex'), indexBuf])); - }); - - it('should decode spent value', function() { - encoding.decodeSpentValue(Buffer.concat([new Buffer(txid, 'hex'), indexBuf])).should.deep.equal({ txid: txid, inputIndex: 3 }); - }); -}); diff --git a/lib/services/mempool/encoding.js b/lib/services/mempool/encoding.js index ad327da2..b0aae44e 100644 --- a/lib/services/mempool/encoding.js +++ b/lib/services/mempool/encoding.js @@ -73,5 +73,13 @@ Encoding.prototype.decodeMempoolAddressKey = function(buffer) { }; +Encoding.prototype.encodeMempoolAddressValue = function(transaction) { + return transaction.toRaw(); +}; + +Encoding.prototype.decodeMempoolAddressValue = function(buffer) { + return tx.fromRaw(buffer); +}; + module.exports = Encoding; diff --git a/lib/services/mempool/index.js b/lib/services/mempool/index.js index 925b4d38..2ee6c33c 100644 --- a/lib/services/mempool/index.js +++ b/lib/services/mempool/index.js @@ -3,6 +3,7 @@ var BaseService = require('../../service'); var util = require('util'); var Encoding = require('./encoding'); var log = require('../..').log; +var utils = require('../../utils'); var MempoolService = function(options) { BaseService.call(this, options); @@ -162,13 +163,14 @@ MempoolService.prototype.onBlock = function(block, callback) { MempoolService.prototype._getAddressOperations = function(tx, reverse) { var ops = []; + var address; var action = reverse ? 'put' : 'del'; for(var i = 0; i < tx.outputs.length; i++) { var output = tx.outputs[i]; - var address = utils.getAddress(output, this._network); + address = utils.getAddress(output, this._network); if (!address) { continue; @@ -176,13 +178,14 @@ MempoolService.prototype._getAddressOperations = function(tx, reverse) { ops.push({ type: action, - key: this.encoding.encodeMempoolAddressKey(address, tx.txid(), i, 0) + key: this._encoding.encodeMempoolAddressKey(address, tx.txid(), i, 0), + value: this._encoding.encodeMempoolAddressValue(tx) }); } - for(i = 0; i < .length; i++) { + for(i = 0; i < tx.inputs.length; i++) { var input = tx.inputs[i]; - var address = utils.getAddress(input, this._network); + address = utils.getAddress(input, this._network); if (!address) { continue; @@ -190,7 +193,8 @@ MempoolService.prototype._getAddressOperations = function(tx, reverse) { ops.push({ type: action, - key: this.encoding.encodeMempoolAddressKey(address, tx.txid(), i, 1) + key: this._encoding.encodeMempoolAddressKey(address, tx.txid(), i, 1), + value: this._encoding.encodeMempoolAddressValue(tx) }); } @@ -200,8 +204,22 @@ MempoolService.prototype._getAddressOperations = function(tx, reverse) { }; MempoolService.prototype._onTransaction = function(tx) { - this._db.put(this._encoding.encodeMempoolTransactionKey(tx.txid()), - this._encoding.encodeMempoolTransactionValue(tx)); + + var self = this; + var ops = [{ + type: 'put', + key: self._encoding.encodeMempoolTransactionKey(tx.txid()), + value: self._encoding.encodeMempoolTransactionValue(tx) + }]; + + ops.concat(self._getAddressOperations(tx, true)); + self._db.batch(ops, function(err) { + if(err) { + log.error(err); + self.node.stop(); + } + }); + }; MempoolService.prototype.getMempoolTransaction = function(txid, callback) { @@ -236,25 +254,25 @@ MempoolService.prototype.getTxsByAddress = function(address, type, callback) { lte: end }; - var stream = self._db.createKeyStream(criteria); + var stream = self._db.createReadStream(criteria); stream.on('error', function() { return []; }); stream.on('end', function() { - + callback(null, results); }); stream.on('data', function(data) { - var addressInfo = self._encoding.decodeMempoolAddressKey(data); + var addressInfo = self._encoding.decodeMempoolAddressKey(data.key); if (type === 'input') { type = 1; } else if (type === 'output') { type = 0; } if (type === 'both' || type === addressInfo.input) { - results.push(addressInfo.txid); + results.push(self._encoding.decodeMempoolAddressValue(data.value)); } }); diff --git a/package-lock.json b/package-lock.json index c53f6bd5..ad4492f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4295,6 +4295,14 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz", "integrity": "sha1-+vUbnrdKrvOzrPStX2Gr8ky3uT4=" }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "requires": { + "safe-buffer": "5.1.1" + } + }, "string-length": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/string-length/-/string-length-1.0.1.tgz", @@ -4314,14 +4322,6 @@ "strip-ansi": "3.0.1" } }, - "string_decoder": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", - "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", - "requires": { - "safe-buffer": "5.1.1" - } - }, "stringstream": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz",