Merge pull request #10 from matiu/bug/tx-api-crashing-with-someids

fix coinbase TXs in inputs -- it is a fix for show transaction pages
This commit is contained in:
Gustavo Maximiliano Cortez 2014-01-09 13:21:03 -08:00
commit f539b0fa74
2 changed files with 46 additions and 20 deletions

View File

@ -91,6 +91,8 @@ TransactionSchema.statics.createFromArray = function(txs, next) {
TransactionSchema.methods.fillInputValues = function (tx, next) {
if (tx.isCoinBase()) return next();
if (! this.rpc) this.rpc = new RpcClient(config.bitcoind);
var that = this;
@ -103,9 +105,17 @@ TransactionSchema.methods.fillInputValues = function (tx, next) {
var c=0;
that.rpc.getRawTransaction(outHashBase64, function(err, txdata) {
var txin = new Transaction();
if (err || ! txdata.result) return cb( new Error('Input TX '+outHashBase64+' not found'));
var b = new Buffer(txdata.result,'hex');
txin.parse(b);
if ( txin.isCoinBase() ) {
return cb();
}
txin.outs.forEach( function(j) {
// console.log( c + ': ' + util.formatValue(j.v) );
if (c === outIndex) {
@ -148,24 +158,28 @@ TransactionSchema.methods.queryInfo = function (next) {
var valueIn = bignum(0);
var valueOut = bignum(0);
tx.ins.forEach(function(i) {
that.info.vin[c].value = util.formatValue(i.value);
if ( tx.isCoinBase() ) {
that.info.isCoinBase = true;
}
else {
tx.ins.forEach(function(i) {
var n = util.valueToBigInt(i.value).toNumber();
valueIn = valueIn.add( n );
that.info.vin[c].value = util.formatValue(i.value);
var n = util.valueToBigInt(i.value).toNumber();
valueIn = valueIn.add( n );
var scriptSig = i.getScript();
var pubKey = scriptSig.simpleInPubKey();
var pubKeyHash = util.sha256ripe160(pubKey);
var addr = new Address(network.addressPubkey, pubKeyHash);
var addrStr = addr.toString();
var scriptSig = i.getScript();
var pubKey = scriptSig.simpleInPubKey();
var pubKeyHash = util.sha256ripe160(pubKey);
var addr = new Address(network.addressPubkey, pubKeyHash);
var addrStr = addr.toString();
that.info.vin[c].addr = addrStr;
that.info.vin[c].addr = addrStr;
c++;
});
c++;
});
}
tx.outs.forEach( function(i) {
@ -173,8 +187,8 @@ TransactionSchema.methods.queryInfo = function (next) {
valueOut = valueOut.add(n);
});
that.info.valueIn = valueIn / util.COIN;
that.info.valueOut = valueOut / util.COIN;
that.info.valueIn = valueIn / util.COIN;
that.info.valueOut = valueOut / util.COIN;
that.info.feeds = (valueIn - valueOut) / util.COIN;
that.info.size = b.length;

View File

@ -3,7 +3,6 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var TESTING_TX = '21798ddc9664ac0ef618f52b151dda82dafaf2e26d2bbef6cdaf55a6957ca237';
var
mongoose= require('mongoose'),
@ -27,17 +26,19 @@ describe('Transaction fromIdWithInfo', function(){
});
it('should pool tx\'s object from mongoose', function(done) {
Transaction.fromIdWithInfo(TESTING_TX, function(err, tx) {
var test_txid = '21798ddc9664ac0ef618f52b151dda82dafaf2e26d2bbef6cdaf55a6957ca237';
Transaction.fromIdWithInfo(test_txid, function(err, tx) {
if (err) done(err);
assert.equal(tx.txid, TESTING_TX);
assert.equal(tx.txid, test_txid);
done();
});
});
it('should pool tx\'s info from bitcoind', function(done) {
Transaction.fromIdWithInfo(TESTING_TX, function(err, tx) {
var test_txid = '21798ddc9664ac0ef618f52b151dda82dafaf2e26d2bbef6cdaf55a6957ca237';
Transaction.fromIdWithInfo(test_txid, function(err, tx) {
if (err) done(err);
assert.equal(tx.info.txid, TESTING_TX);
assert.equal(tx.info.txid, test_txid);
assert.equal(tx.info.blockhash, '000000000185678d3d7ecc9962c96418174431f93fe20bf216d5565272423f74');
assert.equal(tx.info.valueOut, 1.66174);
assert.equal(tx.info.feeds, 0.0005 );
@ -45,5 +46,16 @@ describe('Transaction fromIdWithInfo', function(){
done();
});
});
it('test a coinbase TX', function(done) {
var test_txid2 = '2a104bab1782e9b6445583296d4a0ecc8af304e4769ceb64b890e8219c562399';
Transaction.fromIdWithInfo(test_txid2, function(err, tx) {
if (err) done(err);
assert(tx.info.isCoinBase);
assert.equal(tx.info.txid, test_txid2);
done();
});
});
});