diff --git a/.gitignore b/.gitignore index f11f951..f6f0c7d 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ db/testnet/blocks README.html public + +package-lock.json diff --git a/lib/addresses.js b/lib/addresses.js index ed1db73..d1ce2ad 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -24,7 +24,7 @@ AddressController.prototype.show = function(req, res) { options.to = parseInt(req.query.to); } - this.getAddressSummary(req.addr, options, function(err, data) { + this._address.getAddressSummary(req.addr, options, function(err, data) { if(err) { return self.common.handleErrors(err, res); } @@ -139,14 +139,29 @@ AddressController.prototype.utxo = function(req, res) { AddressController.prototype.multiutxo = function(req, res) { var self = this; - this.node.getAddressUnspentOutputs(req.addrs, true, function(err, utxos) { - if(err && err.code === -5) { - return res.jsonp([]); - } else if(err) { + + var finalUtxos = []; + + async.eachLimit(req.addrs, 4, function(addr, next) { + + self.node.getAddressUnspentOutputs(addr, {}, function(err, utxos) { + + if (err) { + return next(err); + } + + finalUtxos = finalUtxos.concat(utxos); + next(); + }); + + }, function(err) { + + if (err) { return self.common.handleErrors(err, res); } - res.jsonp(utxos.map(self.transformUtxo.bind(self))); + res.jsonp(finalUtxos.map(self.transformUtxo.bind(self))); + }); }; @@ -190,6 +205,7 @@ AddressController.prototype.multitxs = function(req, res) { options.to = parseInt(req.query.to) || parseInt(req.body.to) || parseInt(options.from) + 10; self.node.getAddressHistory(req.addrs, options, function(err, result) { + if(err) { return self.common.handleErrors(err, res); } @@ -211,24 +227,16 @@ AddressController.prototype.multitxs = function(req, res) { }); }; -AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfos, options, callback) { +AddressController.prototype.transformAddressHistoryForMultiTxs = function(txs, options, callback) { var self = this; - var items = txinfos.map(function(txinfo) { - return txinfo.tx; - }).filter(function(value, index, self) { - return self.indexOf(value) === index; - }); - async.map( - items, - function(item, next) { - self.txController.transformTransaction(item, options, next); + txs, + function(tx, next) { + self.txController.transformTransaction(tx, options, next); }, callback ); }; - - module.exports = AddressController; diff --git a/lib/blocks.js b/lib/blocks.js index a7ff812..4bb4ede 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -140,13 +140,13 @@ BlockController.prototype.transformBlock = function(block, info) { nonce: block.nonce, bits: block.bits, difficulty: null, - chainwork: null, + chainwork: info.chainwork, confirmations: null, previousblockhash: bcoin.util.revHex(block.prevBlock), nextblockhash: null, reward: null, isMainChain: null, - poolInfo: null + poolInfo: this.getPoolInfo(block) }; }; @@ -200,7 +200,7 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next) var br = new bitcore.encoding.BufferReader(blockBuffer); - // take a shortcut to get number of transactions and the blocksize. + // TODO: take a shortcut to get number of transactions and the blocksize. // Also reads the coinbase transaction and only that. // Old code parsed all transactions in every block _and_ then encoded // them all back together to get the binary size of the block. @@ -320,7 +320,7 @@ BlockController.prototype.list = function(req, res) { }; BlockController.prototype.getPoolInfo = function(block) { - var coinbaseBuffer = block.transactions[0].inputs[0]._scriptBuffer; + var coinbaseBuffer = block.txs[0].inputs[0].script.raw; for(var k in this.poolStrings) { if (coinbaseBuffer.toString('utf-8').match(k)) { diff --git a/lib/transactions.js b/lib/transactions.js index 0fb7f4b..c9c5313 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -2,7 +2,6 @@ var bitcore = require('bitcore-lib'); var _ = bitcore.deps._; -var bcoin = require('bcoin'); var $ = bitcore.util.preconditions; var Common = require('./common'); var async = require('async'); @@ -49,14 +48,18 @@ TxController.prototype.transaction = function(req, res, next) { }; TxController.prototype.transformTransaction = function(transaction, options, callback) { + if (_.isFunction(options)) { callback = options; options = {}; } + $.checkArgument(_.isFunction(callback)); + var confirmations = 0; + if(transaction.__height >= 0) { - var height = this._block.height; + var height = this._block.getTip().height; confirmations = height - transaction.__height + 1; } @@ -75,7 +78,7 @@ TxController.prototype.transformTransaction = function(transaction, options, cal } ]; } else { - options.inputiValues = transaction.__inputValues; + options.inputValues = transaction.__inputValues; transformed.vin = transaction.inputs.map(this.transformInput.bind(this, options)); } @@ -84,7 +87,7 @@ TxController.prototype.transformTransaction = function(transaction, options, cal transformed.blockhash = transaction.blockHash; transformed.blockheight = transaction.__height; transformed.confirmations = transaction.confirmations; - // TODO consider mempool txs with receivedTime? + var time = transaction.__timestamp ? transaction.__timestamp : Math.round(Date.now() / 1000); transformed.time = time; if (transformed.confirmations) { @@ -201,6 +204,7 @@ TxController.prototype.transformInvTransaction = function(transaction) { }; TxController.prototype.rawTransaction = function(req, res, next) { + var self = this; var txid = req.params.txid; @@ -212,10 +216,11 @@ TxController.prototype.rawTransaction = function(req, res, next) { } req.rawTransaction = { - 'rawtx': transaction.toBuffer().toString('hex') + 'rawtx': transaction.toRaw().toString('hex') }; next(); + }); };