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

115 lines
2.3 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-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);
newBlock.hash = block.blockHash;
2014-01-15 12:36:49 -08:00
Transaction.createFromArray(block.tx, function(err, inserted_txs) {
if (err) return cb(err);
2014-01-15 13:11:18 -08:00
newBlock.save(function(err) {
return cb(err, inserted_txs);
});
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) {
this.fromHash(hash, function(err, block) {
if (err) return cb(err);
if (!block) { return cb(new Error('Block not found')); }
2014-01-08 11:29:39 -08:00
block.getInfo(function(err) { return cb(err,block); } );
});
};
// 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) {
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);