diff --git a/app/models/Block.js b/app/models/Block.js index b176e7e..8705978 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -4,10 +4,11 @@ * Module dependencies. */ var mongoose = require('mongoose'), - Schema = mongoose.Schema; - -var async = require('async'); -var Transaction = require('./Transaction'); + Schema = mongoose.Schema, + async = require('async'), + RpcClient = require('bitcore/RpcClient').class(), + config = require('../../config/config') + ; /** * Block Schema @@ -52,4 +53,38 @@ BlockSchema.statics.fromHash = function(hash, cb) { }).exec(cb); }; + +BlockSchema.statics.fromHashWithInfo = function(hash, cb) { + this.fromHash(hash, function(err, block) { + if (err) return cb(err); + + 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; + + //console.log("THAT", that); + return next(null, that.info); + }); +}; + + + module.exports = mongoose.model('Block', BlockSchema); diff --git a/app/models/Transaction.js b/app/models/Transaction.js index f0f24c4..5d2a6ee 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -37,6 +37,14 @@ TransactionSchema.statics.fromID = function(txid, cb) { }).exec(cb); }; +TransactionSchema.statics.fromIDWithInfo = function(txid, cb) { + this.fromHash(hash, function(err, tx) { + if (err) return cb(err); + + tx.getInfo(function(err) { return cb(err,tx); } ); + }); +}; + TransactionSchema.statics.createFromArray = function(txs, next) { var that = this; @@ -68,13 +76,22 @@ TransactionSchema.statics.createFromArray = function(txs, next) { }; -/* - * virtual - */ -// ugly? new object every call? -TransactionSchema.virtual('info').get(function () { - -}); +TransactionSchema.methods.getInfo = function (next) { + + var that = this; + var rpc = new RpcClient(config.bitcoind); + + rpc.getRawTransaction(this.txid, function(err, txInfo) { + if (err) return next(err); + that.info = txInfo.result; + + //console.log("THAT", that); + return next(null, that.info); + }); +}; + + + module.exports = mongoose.model('Transaction', TransactionSchema); diff --git a/lib/Sync.js b/lib/Sync.js index e8be9b2..8254cd8 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -152,7 +152,7 @@ function spec(b) { mongoose.connect(config.db); var db = mongoose.connection; - this.rpc = new RpcClient(config.bitcoind); + this.rpc = new RpcClient(config.bitcoind); var that = this; diff --git a/test/model/block.js b/test/model/block.js new file mode 100644 index 0000000..88af38e --- /dev/null +++ b/test/model/block.js @@ -0,0 +1,41 @@ +#!/usr/bin/env node + +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + + +var TESTING_BLOCK = '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; + +var + mongoose= require('mongoose'), + assert = require('assert'), + config = require('../../config/config'), + Block = require('../../app/models/Block'); + +mongoose.connect(config.db); + +var db = mongoose.connection; + +describe('getInfo', function(){ + + var block_hash = TESTING_BLOCK; + + + db.on('error', console.error.bind(console, 'connection error:')); + + db.once('open', function (){ + + + var block2 = Block.fromHashWithInfo(block_hash, function(err, b2) { + if (err) done(err); + + console.log("Block obj:"); + console.log(b2); + console.log("Block.info:"); + console.log(b2.info); + db.close(); + done(); + }); + + }); +}); +