insight-ui-zcash/app/models/Block.js

140 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-01-06 18:50:29 -08:00
'use strict';
/**
* Module dependencies.
*/
2014-01-07 11:49:42 -08:00
var mongoose = require('mongoose'),
2014-01-08 11:29:39 -08:00
Schema = mongoose.Schema,
RpcClient = require('bitcore/RpcClient').class(),
2014-01-14 07:58:45 -08:00
util = require('bitcore/util/util'),
BitcoreBlock= require('bitcore/Block').class(),
2014-01-15 12:36:49 -08:00
Transaction = require('./Transaction'),
2014-01-08 11:29:39 -08:00
config = require('../../config/config')
;
2014-01-06 18:50:29 -08:00
/**
* 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
2014-01-07 04:48:31 -08:00
hash: {
type: String,
2014-01-06 18:50:29 -08:00
index: true,
unique: true,
},
2014-01-08 12:45:37 -08:00
time: Number,
2014-01-16 08:01:32 -08:00
nextBlockHash: String,
2014-01-18 13:28:24 -08:00
isOrphan: Boolean,
2014-01-06 18:50:29 -08:00
});
/**
* Validations
*/
/*
BlockSchema.path('title').validate(function(title) {
return title.length;
},'Title cannot be blank');
*/
/**
* Statics
*/
2014-01-16 05:51:39 -08:00
BlockSchema.statics.customCreate = function(block, cb) {
2014-01-15 12:36:49 -08:00
2014-01-15 13:11:18 -08:00
var That= this;
2014-01-15 12:36:49 -08:00
var BlockSchema = mongoose.model('Block', BlockSchema);
2014-01-16 05:51:39 -08:00
2014-01-15 13:11:18 -08:00
var newBlock = new That();
2014-01-16 05:51:39 -08:00
newBlock.time = block.time ? block.time : Math.round(new Date().getTime() / 1000);
2014-01-16 06:01:01 -08:00
newBlock.hash = block.hash;
2014-01-16 08:01:32 -08:00
newBlock.nextBlockHash = block.nextBlockHash;
2014-01-15 12:36:49 -08:00
2014-01-17 11:36:34 -08:00
2014-01-16 05:54:59 -08:00
Transaction.createFromArray(block.tx, newBlock.time, function(err, inserted_txs) {
2014-01-15 12:36:49 -08:00
if (err) return cb(err);
2014-01-16 06:01:01 -08:00
2014-01-15 13:11:18 -08:00
newBlock.save(function(err) {
2014-01-16 08:01:32 -08:00
return cb(err, newBlock, inserted_txs);
2014-01-15 13:11:18 -08:00
});
2014-01-15 12:36:49 -08:00
});
2014-01-15 05:32:18 -08:00
};
2014-01-06 18:50:29 -08:00
BlockSchema.statics.load = function(id, cb) {
2014-01-07 04:48:31 -08:00
this.findOne({
_id: id
}).exec(cb);
2014-01-06 18:50:29 -08:00
};
BlockSchema.statics.fromHash = function(hash, cb) {
2014-01-07 04:48:31 -08:00
this.findOne({
hash: hash,
}).exec(cb);
2014-01-06 18:50:29 -08:00
};
2014-01-08 11:29:39 -08:00
BlockSchema.statics.fromHashWithInfo = function(hash, cb) {
2014-01-20 10:51:23 -08:00
var That = this;
2014-01-08 11:29:39 -08:00
this.fromHash(hash, function(err, block) {
if (err) return cb(err);
2014-01-20 10:51:23 -08:00
if (!block) {
// No in mongo...but maybe in bitcoind... lets query it
block = new That();
block.hash = hash;
block.getInfo(function(err, blockInfo) {
if (err) return cb(err);
if (!blockInfo) return cb();
block.save(function(err) {
return cb(err,block);
});
});
}
else {
block.getInfo(function(err) {
return cb(err,block);
});
}
2014-01-08 11:29:39 -08:00
});
};
// TODO: Can we store the rpc instance in the Block object?
BlockSchema.methods.getInfo = function (next) {
var that = this;
var rpc = new RpcClient(config.bitcoind);
rpc.getBlock(this.hash, function(err, blockInfo) {
2014-01-20 10:51:23 -08:00
// Not found?
if (err && err.code === -5) return next();
2014-01-08 11:29:39 -08:00
if (err) return next(err);
/*
* Not sure this is the right way to do it.
* Any other way to lazy load a property in a mongoose object?
*/
that.info = blockInfo.result;
2014-01-14 07:58:45 -08:00
that.info.reward = BitcoreBlock.getBlockValue(that.info.height) / util.COIN ;
2014-01-08 11:29:39 -08:00
//console.log("THAT", that);
return next(null, that.info);
});
};
2014-01-08 06:05:19 -08:00
module.exports = mongoose.model('Block', BlockSchema);