move txitem creation to transaction model

This commit is contained in:
Matias Alejo Garcia 2014-01-11 22:58:00 -03:00
parent 7bf54aadf6
commit 28e082b53b
2 changed files with 76 additions and 75 deletions

View File

@ -13,7 +13,8 @@ var mongoose = require('mongoose'),
networks = require('bitcore/networks'), networks = require('bitcore/networks'),
util = require('bitcore/util/util'), util = require('bitcore/util/util'),
bignum = require('bignum'), bignum = require('bignum'),
config = require('../../config/config'); config = require('../../config/config'),
TransactionItem = require('./TransactionItem');
/** /**
@ -30,6 +31,7 @@ var TransactionSchema = new Schema({
processed: { processed: {
type: Boolean, type: Boolean,
default: false, default: false,
index: true,
}, },
orphaned: { orphaned: {
type: Boolean, type: Boolean,
@ -67,6 +69,8 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
}); });
}; };
TransactionSchema.statics.createFromArray = function(txs, next) { TransactionSchema.statics.createFromArray = function(txs, next) {
var that = this; var that = this;
if (!txs) return next(); if (!txs) return next();
@ -87,6 +91,63 @@ TransactionSchema.statics.createFromArray = function(txs, next) {
}; };
TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
this.fromIdWithInfo(txid, function(err, t) {
if (err || !t) return cb(err);
async.each(t.info.vin, function(i, next_in) {
/*
* TODO Support multisigs???
*/
if (i.addr && i.value) {
TransactionItem.create({
txid : t.txid,
value : -1 * i.value,
addr : i.addr,
index : i.n,
}, next_in);
}
else {
if ( !i.coinbase )
console.log ("TX: %s seems to be multisig IN. Skipping... ", t.txid);
return next_in();
}
},
function (err) {
if (err) console.log (err);
async.each(t.info.vout, function(o, next_out) {
/*
* TODO Support multisigs
*/
if (o.value && o.scriptPubKey
&& o.scriptPubKey.addresses
&& o.scriptPubKey.addresses[0]
) {
TransactionItem.create({
txid : t.txid,
value : o.value,
addr : o.scriptPubKey.addresses[0],
index : o.n,
}, next_out);
}
else {
console.log ("TX: %s,%d seems to be multisig OUT. Skipping... ", t.txid, o.n);
return next_out();
}
},
function (err) {
return cb(err);
});
});
});
};
TransactionSchema.methods.fillInputValues = function (tx, next) { TransactionSchema.methods.fillInputValues = function (tx, next) {
if (tx.isCoinBase()) return next(); if (tx.isCoinBase()) return next();

View File

@ -156,20 +156,19 @@ function spec() {
var filter = reindex ? {} : { processed: false } ; var filter = reindex ? {} : { processed: false } ;
Transaction.find(filter, Transaction.find(filter, function(err, txs) {
function(err, txs) { if (err) return cb(err);
if (err) return cb(err);
var read = 0, var read = 0,
pull = 0, pull = 0,
write = 0, proc = 0,
total = txs.length; total = txs.length;
console.log('\tneed to pull %d txs', total); console.log('\tneed to pull %d txs', total);
if (!total) return cb(); if (!total) return cb();
async.each(txs, function(tx, next) { async.each(txs, function(tx, next) {
if (read++ % 1000 === 0) progress_bar('read', read, total); if (read++ % 1000 === 0) progress_bar('read', read, total);
if (!tx.txid) { if (!tx.txid) {
@ -177,73 +176,14 @@ function spec() {
return next(); return next();
} }
// This will trigger an RPC call // This will trigger an RPC call
Transaction.fromIdWithInfo( tx.txid, function(err,t) { Transaction.explodeTransactionItems( tx.txid, function(err) {
if (pull++ % 1000 === 0) progress_bar('\tpull', pull, total); if (proc++ % 1000 === 0) progress_bar('\tproc', pull, total);
next(err);
if (!err && t) {
var index = 0;
async.each(t.info.vin, function(i, next_in) {
/*
* TODO Support multisigs???
* how??
*/
if (i.addr && i.value) {
TransactionItem.create({
txid : t.txid,
value : -1 * i.value,
addr : i.addr,
index : i.n,
}, next_in);
}
else {
if ( !i.coinbase )
console.log ("TX: %s seems to be multisig IN. Skipping... ", t.txid);
return next_in();
}
},
function (err) {
if (err) console.log (err);
index = 0;
async.each(t.info.vout, function(o, next_out) {
/*
* TODO Support multisigs
*/
if (o.value && o.scriptPubKey
&& o.scriptPubKey.addresses
&& o.scriptPubKey.addresses[0]
) {
TransactionItem.create({
txid : t.txid,
value : o.value,
addr : o.scriptPubKey.addresses[0],
index : o.n,
}, next_out);
}
else {
console.log ("TX: %s,%d seems to be multisig OUT. Skipping... ", t.txid, o.n);
return next_out();
}
},
function (err) {
if (err) console.log (err);
if (write++ % 1000 === 0) progress_bar('\t\twrite', write, total);
return next();
});
});
}
else return next();
}); });
}, },
function(err) { cb);
return cb(err); });
});
});
}; };
Sync.prototype.init = function(opts) { Sync.prototype.init = function(opts) {