fix coinbase TXs in inputs

This commit is contained in:
Matias Alejo Garcia 2014-01-09 18:18:47 -03:00
parent e754d12129
commit 50485ce18e
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,14 +158,17 @@ TransactionSchema.methods.queryInfo = function (next) {
var valueIn = bignum(0);
var valueOut = bignum(0);
if ( tx.isCoinBase() ) {
that.info.isCoinBase = true;
}
else {
tx.ins.forEach(function(i) {
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);
@ -166,6 +179,7 @@ TransactionSchema.methods.queryInfo = function (next) {
c++;
});
}
tx.outs.forEach( function(i) {

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