From 50485ce18ecfc1e23495d2b8d7955352d128a5e2 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 9 Jan 2014 18:18:47 -0300 Subject: [PATCH] fix coinbase TXs in inputs --- app/models/Transaction.js | 44 ++++++++++++++++++++++++++------------- test/model/transaction.js | 22 +++++++++++++++----- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/app/models/Transaction.js b/app/models/Transaction.js index 8d26c70..58a0707 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -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; diff --git a/test/model/transaction.js b/test/model/transaction.js index fe3aef0..ee9f16c 100644 --- a/test/model/transaction.js +++ b/test/model/transaction.js @@ -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(); + }); + }); + });