From 358b32e02aa1d48d37d4aa6d1848becefe2c0eb7 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 15 Feb 2014 19:51:35 -0300 Subject: [PATCH] remove parsing txs from RPC --- app/controllers/transactions.js | 16 ++++------------ lib/Rpc.js | 21 ++++++++++----------- lib/TransactionDb.js | 4 ++-- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index a86f71e3..9b63bcac 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -10,6 +10,7 @@ var common = require('./common'); var TransactionDb = require('../../lib/TransactionDb').class(); var BlockDb = require('../../lib/BlockDb').class(); +var tDb = new TransactionDb(); var bdb = new BlockDb(); @@ -17,7 +18,6 @@ var bdb = new BlockDb(); * Find transaction by hash ... */ exports.transaction = function(req, res, next, txid) { - var tDb = new TransactionDb(); tDb.fromIdWithInfo(txid, function(err, tx) { if (err || ! tx) @@ -42,21 +42,13 @@ exports.show = function(req, res) { var getTransaction = function(txid, cb) { - var tDb = new TransactionDb(); tDb.fromIdWithInfo(txid, function(err, tx) { - if (err) { - console.log(err); - } + if (err) console.log(err); if (!tx || !tx.info) { -console.log('[transactions.js.48]:: TXid %s not found in RPC. CHECK THIS.', txid); //TODO - // not check this. no - tx = { - info: { - txid: txid - } - }; + console.log('[transactions.js.48]:: TXid %s not found in RPC. CHECK THIS.', txid); + return ({ txid: txid }); } return cb(null, tx.info); diff --git a/lib/Rpc.js b/lib/Rpc.js index fe8735b9..f465d2be 100644 --- a/lib/Rpc.js +++ b/lib/Rpc.js @@ -5,7 +5,6 @@ require('classtool'); function spec(b) { var RpcClient = require('bitcore/RpcClient').class(), - BitcoreTransaction = require('bitcore/Transaction').class(), BitcoreBlock = require('bitcore/Block').class(), bitcoreUtil = require('bitcore/util/util'), util = require('util'), @@ -16,25 +15,25 @@ function spec(b) { function Rpc() { } - Rpc._parseRpcResult = function(info) { + Rpc._parseTxResult = function(info) { var b = new Buffer(info.hex,'hex'); - var tx = new BitcoreTransaction(); - tx.parse(b); - // Inputs - if (tx.isCoinBase()) { - info.isCoinBase = true; - } + // remove fields we dont need, to speed, and adapt the information + delete info['hex']; + // Inputs => add index + coinBase flag var n =0; info.vin.forEach(function(i) { + if (i.coinbase) info.isCoinBase = true; i.n = n++; + delete i.scriptSig['hex']; }); - // Outputs + // Outputs => add total var valueOutSat = 0; info.vout.forEach( function(o) { valueOutSat += o.value * bitcoreUtil.COIN; + delete o.scriptPubKey['hex']; }); info.valueOut = parseInt(valueOutSat) / bitcoreUtil.COIN; info.size = b.length; @@ -54,7 +53,7 @@ function spec(b) { return e; }; - Rpc.getRpcInfo = function(txid, cb) { + Rpc.getTxInfo = function(txid, cb) { var self = this; bitcoreRpc.getRawTransaction(txid, 1, function(err, txInfo) { @@ -63,7 +62,7 @@ function spec(b) { if (err && err.code === -5) return cb(); if (err) return cb(self.errMsg(err)); - var info = self._parseRpcResult(txInfo.result); + var info = self._parseTxResult(txInfo.result); return cb(null,info); }); diff --git a/lib/TransactionDb.js b/lib/TransactionDb.js index 5eb7d7cc..8fc29900 100644 --- a/lib/TransactionDb.js +++ b/lib/TransactionDb.js @@ -246,7 +246,7 @@ function spec(b) { TransactionDb.prototype._getInfo = function(txid, next) { var self = this; - Rpc.getRpcInfo(txid, function(err, info) { + Rpc.getTxInfo(txid, function(err, info) { if (err) return next(err); self._fillOutpoints(info, function() { @@ -677,7 +677,7 @@ function spec(b) { // TODO: parse it from networks.genesisTX? if (t === genesisTXID) return each_cb(); - Rpc.getRpcInfo(t, function(err, inInfo) { + Rpc.getTxInfo(t, function(err, inInfo) { if (!inInfo) return each_cb(err); return self.add(inInfo, blockHash, each_cb);