fix coinbase TXs in inputs
This commit is contained in:
parent
e754d12129
commit
50485ce18e
|
@ -91,6 +91,8 @@ TransactionSchema.statics.createFromArray = function(txs, next) {
|
||||||
|
|
||||||
TransactionSchema.methods.fillInputValues = function (tx, next) {
|
TransactionSchema.methods.fillInputValues = function (tx, next) {
|
||||||
|
|
||||||
|
if (tx.isCoinBase()) return next();
|
||||||
|
|
||||||
if (! this.rpc) this.rpc = new RpcClient(config.bitcoind);
|
if (! this.rpc) this.rpc = new RpcClient(config.bitcoind);
|
||||||
|
|
||||||
var that = this;
|
var that = this;
|
||||||
|
@ -103,9 +105,17 @@ TransactionSchema.methods.fillInputValues = function (tx, next) {
|
||||||
var c=0;
|
var c=0;
|
||||||
that.rpc.getRawTransaction(outHashBase64, function(err, txdata) {
|
that.rpc.getRawTransaction(outHashBase64, function(err, txdata) {
|
||||||
var txin = new Transaction();
|
var txin = new Transaction();
|
||||||
|
|
||||||
|
if (err || ! txdata.result) return cb( new Error('Input TX '+outHashBase64+' not found'));
|
||||||
|
|
||||||
var b = new Buffer(txdata.result,'hex');
|
var b = new Buffer(txdata.result,'hex');
|
||||||
txin.parse(b);
|
txin.parse(b);
|
||||||
|
|
||||||
|
|
||||||
|
if ( txin.isCoinBase() ) {
|
||||||
|
return cb();
|
||||||
|
}
|
||||||
|
|
||||||
txin.outs.forEach( function(j) {
|
txin.outs.forEach( function(j) {
|
||||||
// console.log( c + ': ' + util.formatValue(j.v) );
|
// console.log( c + ': ' + util.formatValue(j.v) );
|
||||||
if (c === outIndex) {
|
if (c === outIndex) {
|
||||||
|
@ -148,14 +158,17 @@ TransactionSchema.methods.queryInfo = function (next) {
|
||||||
|
|
||||||
var valueIn = bignum(0);
|
var valueIn = bignum(0);
|
||||||
var valueOut = bignum(0);
|
var valueOut = bignum(0);
|
||||||
|
|
||||||
|
if ( tx.isCoinBase() ) {
|
||||||
|
that.info.isCoinBase = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
tx.ins.forEach(function(i) {
|
tx.ins.forEach(function(i) {
|
||||||
|
|
||||||
that.info.vin[c].value = util.formatValue(i.value);
|
that.info.vin[c].value = util.formatValue(i.value);
|
||||||
|
|
||||||
var n = util.valueToBigInt(i.value).toNumber();
|
var n = util.valueToBigInt(i.value).toNumber();
|
||||||
valueIn = valueIn.add( n );
|
valueIn = valueIn.add( n );
|
||||||
|
|
||||||
|
|
||||||
var scriptSig = i.getScript();
|
var scriptSig = i.getScript();
|
||||||
var pubKey = scriptSig.simpleInPubKey();
|
var pubKey = scriptSig.simpleInPubKey();
|
||||||
var pubKeyHash = util.sha256ripe160(pubKey);
|
var pubKeyHash = util.sha256ripe160(pubKey);
|
||||||
|
@ -166,6 +179,7 @@ TransactionSchema.methods.queryInfo = function (next) {
|
||||||
|
|
||||||
c++;
|
c++;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
tx.outs.forEach( function(i) {
|
tx.outs.forEach( function(i) {
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||||
|
|
||||||
|
|
||||||
var TESTING_TX = '21798ddc9664ac0ef618f52b151dda82dafaf2e26d2bbef6cdaf55a6957ca237';
|
|
||||||
|
|
||||||
var
|
var
|
||||||
mongoose= require('mongoose'),
|
mongoose= require('mongoose'),
|
||||||
|
@ -27,17 +26,19 @@ describe('Transaction fromIdWithInfo', function(){
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pool tx\'s object from mongoose', function(done) {
|
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);
|
if (err) done(err);
|
||||||
assert.equal(tx.txid, TESTING_TX);
|
assert.equal(tx.txid, test_txid);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pool tx\'s info from bitcoind', function(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);
|
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.blockhash, '000000000185678d3d7ecc9962c96418174431f93fe20bf216d5565272423f74');
|
||||||
assert.equal(tx.info.valueOut, 1.66174);
|
assert.equal(tx.info.valueOut, 1.66174);
|
||||||
assert.equal(tx.info.feeds, 0.0005 );
|
assert.equal(tx.info.feeds, 0.0005 );
|
||||||
|
@ -45,5 +46,16 @@ describe('Transaction fromIdWithInfo', function(){
|
||||||
done();
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue