sync working for the thing version of the models
This commit is contained in:
parent
8fc7001e8e
commit
5aa508ae8a
|
@ -13,59 +13,17 @@ var Transaction = require('./Transaction');
|
|||
* Block Schema
|
||||
*/
|
||||
var BlockSchema = new Schema({
|
||||
|
||||
// For now we keep this as short as possible
|
||||
// More fields will be propably added as we move
|
||||
// forward with the UX
|
||||
hash: {
|
||||
type: String,
|
||||
index: true,
|
||||
unique: true,
|
||||
},
|
||||
size: Number,
|
||||
height: Number,
|
||||
confirmations: Number,
|
||||
version: Number,
|
||||
merkleroot: String,
|
||||
tx: [ String ],
|
||||
time: Date,
|
||||
nonce: Number,
|
||||
bits: String,
|
||||
difficulty: Number,
|
||||
chainwork: String,
|
||||
previousblockhash: {
|
||||
type: String,
|
||||
index: true,
|
||||
unique: true,
|
||||
},
|
||||
nextblockhash: {
|
||||
type: String,
|
||||
index: true,
|
||||
unique: true,
|
||||
},
|
||||
});
|
||||
|
||||
BlockSchema.methods.explodeTransactions = function(next) {
|
||||
|
||||
// console.log('exploding %s', this.hash, typeof this.tx);
|
||||
|
||||
async.forEach( this.tx,
|
||||
function(tx, callback) {
|
||||
// console.log('procesing TX %s', tx);
|
||||
Transaction.create({ txid: tx }, function(err) {
|
||||
if (err && ! err.toString().match(/E11000/)) {
|
||||
return callback();
|
||||
}
|
||||
if (err) {
|
||||
|
||||
return callback(err);
|
||||
}
|
||||
return callback();
|
||||
|
||||
});
|
||||
},
|
||||
function(err) {
|
||||
if (err) return next(err);
|
||||
return next();
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Validations
|
||||
|
|
|
@ -4,35 +4,20 @@
|
|||
* Module dependencies.
|
||||
*/
|
||||
var mongoose = require('mongoose'),
|
||||
Schema = mongoose.Schema;
|
||||
|
||||
Schema = mongoose.Schema,
|
||||
async = require('async');
|
||||
|
||||
/**
|
||||
*/
|
||||
var TransactionSchema = new Schema({
|
||||
// For now we keep this as short as possible
|
||||
// More fields will be propably added as we move
|
||||
// forward with the UX
|
||||
txid: {
|
||||
type: String,
|
||||
index: true,
|
||||
unique: true,
|
||||
},
|
||||
version: Number,
|
||||
locktime: Number,
|
||||
vin: {
|
||||
type: Array,
|
||||
default: [],
|
||||
},
|
||||
vout: {
|
||||
type: Array,
|
||||
default: [],
|
||||
},
|
||||
blockhash: {
|
||||
type: String,
|
||||
index: true,
|
||||
default: null,
|
||||
},
|
||||
confirmations: Number,
|
||||
time: Number,
|
||||
blocktime: Number,
|
||||
});
|
||||
|
||||
/**
|
||||
|
@ -52,13 +37,44 @@ TransactionSchema.statics.fromID = function(txid, cb) {
|
|||
}).exec(cb);
|
||||
};
|
||||
|
||||
TransactionSchema.statics.createFromArray = function(txs, next) {
|
||||
|
||||
var that = this;
|
||||
|
||||
if (!txs) return next();
|
||||
|
||||
// console.log('exploding ', txs);
|
||||
|
||||
async.forEach( txs,
|
||||
function(tx, callback) {
|
||||
// console.log('procesing TX %s', tx);
|
||||
that.create({ txid: tx }, function(err) {
|
||||
if (err && ! err.toString().match(/E11000/)) {
|
||||
return callback();
|
||||
}
|
||||
if (err) {
|
||||
|
||||
return callback(err);
|
||||
}
|
||||
return callback();
|
||||
|
||||
});
|
||||
},
|
||||
function(err) {
|
||||
if (err) return next(err);
|
||||
return next();
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* virtual
|
||||
*/
|
||||
|
||||
// ugly? new object every call?
|
||||
TransactionSchema.virtual('date').get(function () {
|
||||
return new Date(this.time);
|
||||
TransactionSchema.virtual('info').get(function () {
|
||||
|
||||
});
|
||||
|
||||
module.exports = mongoose.model('Transaction', TransactionSchema);
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
require('classtool');
|
||||
|
||||
|
||||
/* We dont sync any contents from TXs, only their IDs are stored */
|
||||
|
||||
var isSyncTxEnabled = 0;
|
||||
|
||||
function spec(b) {
|
||||
var mongoose = require('mongoose');
|
||||
var util = require('util');
|
||||
|
@ -8,9 +13,9 @@ function spec(b) {
|
|||
var networks = require('bitcore/networks');
|
||||
var async = require('async');
|
||||
|
||||
var config = require('./config/config');
|
||||
var Block = require('./app/models/Block');
|
||||
var Transaction=require('./app/models/Transaction');
|
||||
var config = require('../config/config');
|
||||
var Block = require('../app/models/Block');
|
||||
var Transaction=require('../app/models/Transaction');
|
||||
|
||||
function Sync(config) {
|
||||
this.network = config.networkName == 'testnet' ? networks.testnet : networks.livenet;
|
||||
|
@ -46,7 +51,7 @@ function spec(b) {
|
|||
}
|
||||
|
||||
if (inBlock) {
|
||||
inBlock.explodeTransactions(function (err) {
|
||||
Transaction.createFromArray( blockInfo.result.tx,function (err) {
|
||||
return that.getNextBlock(blockInfo.result.nextblockhash, cb);
|
||||
});
|
||||
}
|
||||
|
@ -82,6 +87,8 @@ function spec(b) {
|
|||
}
|
||||
|
||||
|
||||
// This is not currently used. Transactions are represented by txid only
|
||||
// in mongodb
|
||||
Sync.prototype.syncTXs = function (reindex, cb) {
|
||||
|
||||
var that = this;
|
||||
|
@ -157,14 +164,14 @@ function spec(b) {
|
|||
function(cb){
|
||||
if (opts.destroy) {
|
||||
console.log("Deleting Blocks...");
|
||||
return Block.remove().exec(cb);
|
||||
return db.collections['blocks'].drop(cb);
|
||||
}
|
||||
return cb();
|
||||
},
|
||||
function(cb){
|
||||
if (opts.destroy) {
|
||||
console.log("Deleting TXs...");
|
||||
return Transaction.remove().exec(cb);
|
||||
return db.collections['transactions'].drop(cb);
|
||||
}
|
||||
return cb();
|
||||
},
|
||||
|
@ -186,7 +193,7 @@ function spec(b) {
|
|||
}
|
||||
},
|
||||
function(cb) {
|
||||
if (! opts.skip_txs) {
|
||||
if ( isSyncTxEnabled && ! opts.skip_txs) {
|
||||
that.syncTXs(opts.reindex, function(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
|
@ -6,7 +6,7 @@ require('buffertools').extend();
|
|||
|
||||
var SYNC_VERSION = '0.1';
|
||||
var program = require('commander');
|
||||
var Sync = require('../Sync').class();
|
||||
var Sync = require('../lib/Sync').class();
|
||||
|
||||
program
|
||||
.version(SYNC_VERSION)
|
||||
|
|
Loading…
Reference in New Issue