From 4f46947516831e8ecab4205cb4bcb0bf798171fa Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 20 Oct 2017 10:04:33 -0400 Subject: [PATCH] Fixed tx api call. --- lib/services/address/index.js | 111 +++++++++++++----------------- lib/services/header/index.js | 5 +- lib/services/mempool/index.js | 5 +- lib/services/transaction/index.js | 36 ++-------- 4 files changed, 56 insertions(+), 101 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 2ccbcb30..7633396c 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -70,7 +70,6 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba return callback(err); } - // TODO: these should already be ordered correctly, please check txList = utils.dedupByTxid(txList); txList = utils.orderByConfirmations(txList); @@ -130,6 +129,48 @@ AddressService.prototype.getAddressSummary = function(address, options, callback }; +AddressService.prototype._setOutputResults = function(tx, address, result) { + + for(var j = 0; j < tx.outputs.length; j++) { + + var output = tx.outputs[j]; + + if (utils.getAddress(output, this._network) !== address) { + continue; + } + + result.txApperances++; + result.totalReceivedSat += output.value; + result.balanceSat += output.value; + + if (tx.confirmations === 0) { + result.unconfirmedTxApperances++; + result.unconfirmedBalanceSat += output.value; + } + + } + return result; + +}; + +AddressService.prototype._setInputResults = function(tx, address, result) { + for(var i = 0; i < tx.inputs.length; i++) { + + var input = tx.inputs[i]; + if (utils.getAddress(input, this._network) !== address) { + continue; + } + + result.totalSentSat += tx.__inputValues[i]; + result.balanceSat -= tx.__inputValues[i]; + + if (tx.confirmations === 0) { + result.unconfirmedBalanceSat -= tx.__inputValues[i]; + } + + } +}; + AddressService.prototype._getAddressSummaryResult = function(txs, address, result, options) { var self = this; @@ -138,40 +179,8 @@ AddressService.prototype._getAddressSummaryResult = function(txs, address, resul var tx = txs[i]; - for(var j = 0; j < tx.outputs.length; j++) { - - var output = tx.outputs[j]; - - if (utils.getAddress(output, self._network) !== address) { - continue; - } - - result.txApperances++; - result.totalReceivedSat += output.value; - result.balanceSat += output.value; - - if (tx.confirmations === 0) { - result.unconfirmedTxApperances++; - result.unconfirmedBalanceSat += output.value; - } - - } - - for(j = 0; j < tx.inputs.length; j++) { - - var input = tx.inputs[j]; - if (utils.getAddress(input, self._network) !== address) { - continue; - } - - result.totalSentSat += tx.__inputValues[j]; - result.balanceSat -= tx.__inputValues[j]; - - if (tx.confirmations === 0) { - result.unconfirmedBalanceSat -= tx.__inputValues[j]; - } - - } + self._setOutputResults(tx, address, result); + self._setInputResults(tx, address, result); if (!options.noTxList) { if (!result.transactions) { @@ -690,6 +699,9 @@ AddressService.prototype._processInput = function(tx, input, index, opts) { AddressService.prototype._processOutput = function(tx, output, index, opts) { + // TODO: if the output is pay to public key, we are reporting this as p2pkh + // this leads to the spending tx not being properly indexed. Txs that + // spend p2pk outputs, will not have the public key as part of their input script sig var address = output.getAddress(); if(!address) { @@ -759,33 +771,4 @@ AddressService.prototype._processTransaction = function(tx, opts) { }; -// TODO seems spendy -AddressService.prototype._getInputInfoForAddress = function(address) { - - var self = this; - var inputs = []; - var start = self._encoding.encodeAddressIndexKey(address); - - var criteria = { - gte: start, - lt: Buffer.concat(start.slice(0, address.length + 4), new Buffer(new Array(address.length + 5).join('f'), 'hex')) - }; - - // all the info about this address - var stream = self._db.createKeyStream(criteria); - - stream.on('data', function(data) { - var info = self._encoding.decodeAddressIndexKey(data); - //only returning - if (info.input) { - inputs.push(info); - } - }); - - stream.on('end', function() { - return inputs; - }); - -}; - module.exports = AddressService; diff --git a/lib/services/header/index.js b/lib/services/header/index.js index 59d302e5..db20892d 100644 --- a/lib/services/header/index.js +++ b/lib/services/header/index.js @@ -422,9 +422,6 @@ HeaderService.prototype._onHeaders = function(headers) { var self = this; - // TODO we could be reaching this because either our headers list is a multiple of 2000 - // or because our peer returned us an empty list because our checkpoint hash is not on - // its mainchain. if (headers.length === 0) { self._onHeadersSave(function(err) { if (err) { @@ -702,6 +699,7 @@ HeaderService.prototype._removeAllSubscriptions = function() { this._bus.removeAllListeners(); }; +// this should fire in edge cases where a new peer is not quite synced HeaderService.prototype._findReorgConditionInNewPeer = function(callback) { var self = this; @@ -709,7 +707,6 @@ HeaderService.prototype._findReorgConditionInNewPeer = function(callback) { var newPeerHeaders = new utils.SimpleMap(); var headerCount = 0; - // TODO: these 2 list could be incredibly long lists and could cause a memory crash self.getAllHeaders(function(err, allHeaders) { if (err) { diff --git a/lib/services/mempool/index.js b/lib/services/mempool/index.js index 2ee6c33c..2fdb2183 100644 --- a/lib/services/mempool/index.js +++ b/lib/services/mempool/index.js @@ -54,7 +54,9 @@ MempoolService.prototype.start = function(callback) { MempoolService.prototype._flushMempool = function(callback) { var self = this; + log.warn('Mempool Service: flushing mempool, this could take a minute.'); // TODO: just handle the txindex for now, later handle both txindex and addressindex + // TODO: plan a migration system for upgrades to the indexes var ops = []; var criteria = { @@ -72,10 +74,11 @@ MempoolService.prototype._flushMempool = function(callback) { }); stream.on('end', function() { - ops.batch(ops, function(err) { + self._db.batch(ops, function(err) { if (err) { return callback(err); } + log.info('Mempool Service: completed flushing: ' + ops.length + ' mempool records.'); callback(); }); }); diff --git a/lib/services/transaction/index.js b/lib/services/transaction/index.js index a36e4591..3e78acf8 100644 --- a/lib/services/transaction/index.js +++ b/lib/services/transaction/index.js @@ -125,7 +125,6 @@ TransactionService.prototype.getTransaction = function(txid, options, callback) async.waterfall([ self._getTransaction.bind(self, txid, options), - self._getSupplementaryTransactionInfo.bind(self), self._getMempoolTransaction.bind(self), self.setTxMetaInfo.bind(self) ], function(err, tx) { @@ -140,33 +139,6 @@ TransactionService.prototype.getTransaction = function(txid, options, callback) }; -TransactionService.prototype._getSupplementaryTransactionInfo = function(txid, tx, options, callback) { - - if (!tx) { - return callback(null, txid, tx, options); - } - - var self = this; - tx.confirmations = self._block.getTip().height - tx.__height + 1; - - // TODO maybe we should index the block hash along with the height on tx - self._header.getBlockHeader(tx.__height, function(err, header) { - - if (err) { - return callback(err); - } - - if (header) { - // Do we need both of these? - tx.blockhash = header.hash; - tx.__blockhash = header.hash; - } - - callback(null, txid, tx, options); - - }); -}; - TransactionService.prototype.setTxMetaInfo = function(tx, options, callback) { var self = this; @@ -292,6 +264,9 @@ TransactionService.prototype._getTransaction = function(txid, options, callback) } tx = self._encoding.decodeTransactionValue(tx); + tx.confirmations = self._block.getTip().height - tx.__height + 1; + tx.blockhash = tx.__blockhash; + callback(null, txid, tx, options); }); @@ -489,10 +464,6 @@ TransactionService.prototype._getSpentTxOperations = function(tx, callback) { TransactionService.prototype._processTransaction = function(tx, opts, callback) { - // this index is very simple txid -> tx, but we also need to find each - // input's prev output value, the adjusted timestamp for the block and - // the tx's block height - var self = this; self._getInputValues(tx, opts, function(err, inputValues) { @@ -517,6 +488,7 @@ TransactionService.prototype._processTransaction = function(tx, opts, callback) // block hash tx.__blockhash = opts.block.rhash(); + var operations = [{ key: self._encoding.encodeTransactionKey(tx.txid()), value: self._encoding.encodeTransactionValue(tx)