Browse Source

move txitem creation to transaction model

generic-ui
Matias Alejo Garcia 11 years ago
parent
commit
28e082b53b
  1. 63
      app/models/Transaction.js
  2. 88
      lib/Sync.js

63
app/models/Transaction.js

@ -13,7 +13,8 @@ var mongoose = require('mongoose'),
networks = require('bitcore/networks'),
util = require('bitcore/util/util'),
bignum = require('bignum'),
config = require('../../config/config');
config = require('../../config/config'),
TransactionItem = require('./TransactionItem');
/**
@ -30,6 +31,7 @@ var TransactionSchema = new Schema({
processed: {
type: Boolean,
default: false,
index: true,
},
orphaned: {
type: Boolean,
@ -67,6 +69,8 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
});
};
TransactionSchema.statics.createFromArray = function(txs, next) {
var that = this;
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) {
if (tx.isCoinBase()) return next();

88
lib/Sync.js

@ -156,20 +156,19 @@ function spec() {
var filter = reindex ? {} : { processed: false } ;
Transaction.find(filter,
function(err, txs) {
if (err) return cb(err);
Transaction.find(filter, function(err, txs) {
if (err) return cb(err);
var read = 0,
pull = 0,
write = 0,
total = txs.length;
var read = 0,
pull = 0,
proc = 0,
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 (!tx.txid) {
@ -177,73 +176,14 @@ function spec() {
return next();
}
// This will trigger an RPC call
Transaction.fromIdWithInfo( tx.txid, function(err,t) {
if (pull++ % 1000 === 0) progress_bar('\tpull', pull, total);
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();
Transaction.explodeTransactionItems( tx.txid, function(err) {
if (proc++ % 1000 === 0) progress_bar('\tproc', pull, total);
next(err);
});
},
function(err) {
return cb(err);
});
});
cb);
});
};
Sync.prototype.init = function(opts) {

Loading…
Cancel
Save