From cd4432652d9e7fcb053e35cf51dbdc9e3e6f6ea9 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 13 May 2016 18:51:01 -0400 Subject: [PATCH] main: remove transaction with populate methods The methods populateInputs and populateSpentInfo are nolonger necessary or used now that there is is getDetailedTransaction. --- index.js | 1 - lib/services/bitcoind.js | 2 +- lib/transaction.js | 74 ---------------- regtest/node.js | 2 +- test/services/bitcoind.unit.js | 2 +- test/transaction.unit.js | 152 --------------------------------- 6 files changed, 3 insertions(+), 230 deletions(-) delete mode 100644 lib/transaction.js delete mode 100644 test/transaction.unit.js diff --git a/index.js b/index.js index 0a210849..6626fe18 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,6 @@ module.exports = require('./lib'); module.exports.Node = require('./lib/node'); -module.exports.Transaction = require('./lib/transaction'); module.exports.Service = require('./lib/service'); module.exports.errors = require('./lib/errors'); diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 89b9cf58..7f4c68a9 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -11,12 +11,12 @@ var LRU = require('lru-cache'); var BitcoinRPC = require('bitcoind-rpc'); var $ = bitcore.util.preconditions; var _ = bitcore.deps._; +var Transaction = bitcore.Transaction; var index = require('../'); var errors = index.errors; var log = index.log; var Service = require('../service'); -var Transaction = require('../transaction'); /** * Provides a friendly event driven API to bitcoind in Node.js. Manages starting and diff --git a/lib/transaction.js b/lib/transaction.js deleted file mode 100644 index 66042a16..00000000 --- a/lib/transaction.js +++ /dev/null @@ -1,74 +0,0 @@ -'use strict'; - -var async = require('async'); -var bitcore = require('bitcore-lib'); -var Transaction = bitcore.Transaction; - -var MAX_TRANSACTION_LIMIT = 5; - -Transaction.prototype.populateSpentInfo = function(db, options, callback) { - var self = this; - var txid = self.hash; - - async.eachLimit( - Object.keys(self.outputs), - db.maxTransactionlimit || MAX_TRANSACTION_LIMIT, - function(outputIndex, next) { - db.getSpentInfo({ - txid: txid, - index: parseInt(outputIndex) - }, function(err, info) { - if (err) { - return next(err); - } - self.outputs[outputIndex].__spentTxId = info.txid; - self.outputs[outputIndex].__spentIndex = info.index; - self.outputs[outputIndex].__spentHeight = info.height; - next(); - }); - }, - callback - ); -}; - -Transaction.prototype.populateInputs = function(db, poolTransactions, callback) { - var self = this; - - if(this.isCoinbase()) { - return setImmediate(callback); - } - - async.eachLimit( - this.inputs, - db.maxTransactionLimit || MAX_TRANSACTION_LIMIT, - function(input, next) { - self._populateInput(db, input, poolTransactions, next); - }, - callback - ); -}; - -Transaction.prototype._populateInput = function(db, input, poolTransactions, callback) { - if (!input.prevTxId || !Buffer.isBuffer(input.prevTxId)) { - return callback(new TypeError('Input is expected to have prevTxId as a buffer')); - } - var txid = input.prevTxId.toString('hex'); - db.getTransaction(txid, function(err, prevTx) { - if(err) { - return callback(err); - } else if (!prevTx) { - // Check the pool for transaction - for(var i = 0; i < poolTransactions.length; i++) { - if(txid === poolTransactions[i].hash) { - input.output = poolTransactions[i].outputs[input.outputIndex]; - return callback(); - } - } - return callback(new Error('Previous tx ' + input.prevTxId.toString('hex') + ' not found')); - } - input.output = prevTx.outputs[input.outputIndex]; - callback(); - }); -}; - -module.exports = Transaction; diff --git a/regtest/node.js b/regtest/node.js index 9593e4fd..4f22e43f 100644 --- a/regtest/node.js +++ b/regtest/node.js @@ -17,7 +17,7 @@ var should = chai.should(); var BitcoinRPC = require('bitcoind-rpc'); var index = require('..'); -var Transaction = index.Transaction; +var Transaction = bitcore.Transaction; var BitcoreNode = index.Node; var BitcoinService = index.services.Bitcoin; var testWIF = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG'; diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 28d70494..49c0e679 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -15,7 +15,7 @@ var index = require('../../lib'); var log = index.log; var errors = index.errors; -var Transaction = require('../../lib/transaction'); +var Transaction = bitcore.Transaction; var readFileSync = sinon.stub().returns(fs.readFileSync(path.resolve(__dirname, '../data/bitcoin.conf'))); var BitcoinService = proxyquire('../../lib/services/bitcoind', { fs: { diff --git a/test/transaction.unit.js b/test/transaction.unit.js deleted file mode 100644 index 0a78c8c5..00000000 --- a/test/transaction.unit.js +++ /dev/null @@ -1,152 +0,0 @@ -'use strict'; - -var should = require('chai').should(); -var sinon = require('sinon'); -var bitcoinlib = require('../'); -var Transaction = bitcoinlib.Transaction; - -describe('Bitcoin Transaction', function() { - - describe('#populateSpentInfo', function() { - it('will call db.getSpentInfo with correct arguments', function(done) { - var tx = new Transaction(); - tx.to('1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', 1000); - tx.to('3CMNFxN1oHBc4R1EpboAL5yzHGgE611Xou', 2000); - var expectedHash = tx.hash; - var expectedIndex = 2; - var expectedHeight = 300000; - var db = { - getSpentInfo: sinon.stub().callsArgWith(1, null, { - txid: expectedHash, - index: expectedIndex, - height: expectedHeight - }) - }; - tx.populateSpentInfo(db, {}, function(err) { - if (err) { - return done(err); - } - db.getSpentInfo.args[0][0].txid.should.equal(tx.hash); - db.getSpentInfo.args[0][0].index.should.equal(0); - tx.outputs[0].__spentTxId.should.equal(expectedHash); - tx.outputs[0].__spentIndex.should.equal(expectedIndex); - tx.outputs[0].__spentHeight.should.equal(expectedHeight); - - db.getSpentInfo.args[1][0].txid.should.equal(tx.hash); - db.getSpentInfo.args[1][0].index.should.equal(1); - tx.outputs[1].__spentTxId.should.equal(expectedHash); - tx.outputs[1].__spentIndex.should.equal(expectedIndex); - tx.outputs[1].__spentHeight.should.equal(expectedHeight); - done(); - }); - }); - }); - - describe('#populateInputs', function() { - it('will call _populateInput with transactions', function() { - var tx = new Transaction(); - tx.isCoinbase = sinon.stub().returns(false); - tx._populateInput = sinon.stub().callsArg(3); - tx.inputs = ['input']; - var transactions = []; - var db = {}; - tx.populateInputs(db, transactions, function(err) { - tx._populateInput.callCount.should.equal(1); - tx._populateInput.args[0][0].should.equal(db); - tx._populateInput.args[0][1].should.equal('input'); - tx._populateInput.args[0][2].should.equal(transactions); - }); - }); - it('will skip coinbase transactions', function() { - var tx = new Transaction(); - tx.isCoinbase = sinon.stub().returns(true); - tx._populateInput = sinon.stub().callsArg(3); - tx.inputs = ['input']; - var transactions = []; - var db = {}; - tx.populateInputs(db, transactions, function(err) { - tx._populateInput.callCount.should.equal(0); - }); - }); - }); - - describe('#_populateInput', function() { - var input = { - prevTxId: new Buffer('d6cffbb343a6a41eeaa199478c985493843bfe6a59d674a5c188787416cbcda3', 'hex'), - outputIndex: 0 - }; - it('should give an error if the input does not have a prevTxId', function(done) { - var badInput = {}; - var tx = new Transaction(); - tx._populateInput({}, badInput, [], function(err) { - should.exist(err); - err.message.should.equal('Input is expected to have prevTxId as a buffer'); - done(); - }); - }); - it('should give an error if the input does not have a valid prevTxId', function(done) { - var badInput = { - prevTxId: 'bad' - }; - var tx = new Transaction(); - tx._populateInput({}, badInput, [], function(err) { - should.exist(err); - err.message.should.equal('Input is expected to have prevTxId as a buffer'); - done(); - }); - }); - it('if an error happened it should pass it along', function(done) { - var tx = new Transaction(); - var db = { - getTransaction: sinon.stub().callsArgWith(1, new Error('error')) - }; - tx._populateInput(db, input, [], function(err) { - should.exist(err); - err.message.should.equal('error'); - done(); - }); - }); - it('should return an error if the transaction for the input does not exist', function(done) { - var tx = new Transaction(); - var db = { - getTransaction: sinon.stub().callsArgWith(1, null, null) - }; - tx._populateInput(db, input, [], function(err) { - should.exist(err); - err.message.should.equal('Previous tx ' + input.prevTxId.toString('hex') + ' not found'); - done(); - }); - }); - it('should look through poolTransactions if database does not have transaction', function(done) { - var tx = new Transaction(); - var db = { - getTransaction: sinon.stub().callsArgWith(1, null, null) - }; - var transactions = [ - { - hash: 'd6cffbb343a6a41eeaa199478c985493843bfe6a59d674a5c188787416cbcda3', - outputs: ['output'] - } - ]; - tx._populateInput(db, input, transactions, function(err) { - should.not.exist(err); - input.output.should.equal('output'); - done(); - }); - }); - it('should set the output on the input', function(done) { - var prevTx = new Transaction(); - prevTx.outputs = ['output']; - var tx = new Transaction(); - var db = { - getTransaction: sinon.stub().callsArgWith(1, null, prevTx) - }; - tx._populateInput(db, input, [], function(err) { - should.not.exist(err); - input.output.should.equal('output'); - done(); - }); - }); - }); - -});