main: remove transaction with populate methods

The methods populateInputs and populateSpentInfo are nolonger necessary or used
now that there is is getDetailedTransaction.
This commit is contained in:
Braydon Fuller 2016-05-13 18:51:01 -04:00
parent 8bddf4f0d6
commit cd4432652d
6 changed files with 3 additions and 230 deletions

View File

@ -2,7 +2,6 @@
module.exports = require('./lib'); module.exports = require('./lib');
module.exports.Node = require('./lib/node'); module.exports.Node = require('./lib/node');
module.exports.Transaction = require('./lib/transaction');
module.exports.Service = require('./lib/service'); module.exports.Service = require('./lib/service');
module.exports.errors = require('./lib/errors'); module.exports.errors = require('./lib/errors');

View File

@ -11,12 +11,12 @@ var LRU = require('lru-cache');
var BitcoinRPC = require('bitcoind-rpc'); var BitcoinRPC = require('bitcoind-rpc');
var $ = bitcore.util.preconditions; var $ = bitcore.util.preconditions;
var _ = bitcore.deps._; var _ = bitcore.deps._;
var Transaction = bitcore.Transaction;
var index = require('../'); var index = require('../');
var errors = index.errors; var errors = index.errors;
var log = index.log; var log = index.log;
var Service = require('../service'); var Service = require('../service');
var Transaction = require('../transaction');
/** /**
* Provides a friendly event driven API to bitcoind in Node.js. Manages starting and * Provides a friendly event driven API to bitcoind in Node.js. Manages starting and

View File

@ -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;

View File

@ -17,7 +17,7 @@ var should = chai.should();
var BitcoinRPC = require('bitcoind-rpc'); var BitcoinRPC = require('bitcoind-rpc');
var index = require('..'); var index = require('..');
var Transaction = index.Transaction; var Transaction = bitcore.Transaction;
var BitcoreNode = index.Node; var BitcoreNode = index.Node;
var BitcoinService = index.services.Bitcoin; var BitcoinService = index.services.Bitcoin;
var testWIF = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG'; var testWIF = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG';

View File

@ -15,7 +15,7 @@ var index = require('../../lib');
var log = index.log; var log = index.log;
var errors = index.errors; 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 readFileSync = sinon.stub().returns(fs.readFileSync(path.resolve(__dirname, '../data/bitcoin.conf')));
var BitcoinService = proxyquire('../../lib/services/bitcoind', { var BitcoinService = proxyquire('../../lib/services/bitcoind', {
fs: { fs: {

View File

@ -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();
});
});
});
});