resolve merge

This commit is contained in:
Matias Alejo Garcia 2014-01-20 16:47:46 -03:00
commit 05f2cf426c
17 changed files with 222 additions and 92 deletions

View File

@ -4,30 +4,32 @@
* Module dependencies.
*/
var Address = require('../models/Address');
var Address = require('../models/Address'),
common = require('./common');
/**
* Find block by hash ...
*/
exports.address = function(req, res, next, addr) {
var a = Address.new(addr);
var a;
try {
a = Address.new(addr);
} catch (e) {
return common.handleErrors({message: 'Invalid address:' + e.message, code: 1}, res, next);
}
a.update(function(err) {
if (err && !a.totalReceivedSat) {
console.log(err);
res.status(404).send('Invalid address');
return next();
}
req.address = a;
return next();
});
if (err)
return common.handleErrors(err, res);
else {
req.address = a;
return next();
}
});
};
/**
* Show block
*/
exports.show = function(req, res) {
if (req.address) {

View File

@ -4,7 +4,8 @@
* Module dependencies.
*/
var mongoose = require('mongoose'),
Block = mongoose.model('Block');
Block = mongoose.model('Block'),
common = require('./common');
/**
@ -12,14 +13,12 @@ var mongoose = require('mongoose'),
*/
exports.block = function(req, res, next, hash) {
Block.fromHashWithInfo(hash, function(err, block) {
if (err && !block) {
console.log(err);
res.status(404).send('Not found');
if (err || ! block)
return common.handleErrors(err, res, next);
else {
req.block = block.info;
return next();
}
req.block = block.info;
return next();
});
};

16
app/controllers/common.js Normal file
View File

@ -0,0 +1,16 @@
'use strict';
exports.handleErrors = function (err, res) {
if (err) {
if (err.code) {
res.status(400).send(err.message + '. Code:' + err.code);
}
else {
res.status(503).send(err.message);
}
}
else {
res.status(404).send('Not found');
}
};

View File

@ -27,5 +27,5 @@ module.exports.broadcast_address_tx = function(address, tx) {
};
module.exports.broadcastSyncInfo = function(syncInfo) {
ios.sockets.emit('status', syncInfo);
ios.sockets.emit('sync', syncInfo);
};

View File

@ -4,7 +4,8 @@
* Module dependencies.
*/
var Status = require('../models/Status');
var Status = require('../models/Status'),
common = require('./common');
/**
* Status
@ -19,8 +20,11 @@ exports.show = function(req, res, next) {
var statusObject = Status.new();
var returnJsonp = function (err) {
if(err) return next(err);
res.jsonp(statusObject);
if (err || ! statusObject)
return common.handleErrors(err, res);
else {
res.jsonp(statusObject);
}
};
switch(option) {

View File

@ -7,6 +7,7 @@ var Transaction = require('../models/Transaction');
var Block = require('../models/Block');
var Address = require('../models/Address');
var async = require('async');
var common = require('./common');
/**
@ -14,15 +15,12 @@ var async = require('async');
*/
exports.transaction = function(req, res, next, txid) {
Transaction.fromIdWithInfo(txid, function(err, tx) {
if (err) {
console.log(err);
res.status(404).send('Not found');
if (err || ! tx)
return common.handleErrors(err, res);
else {
req.transaction = tx.info;
return next();
}
if (!tx) return next(new Error('Failed to load TX ' + txid));
req.transaction = tx.info;
next();
});
};

View File

@ -19,11 +19,8 @@ function spec() {
this.transactions = [];
var a = new BitcoreAddress(addrStr);
try {
a.validate();
this.addrStr = addrStr;
} catch(e){
}
a.validate();
this.addrStr = addrStr;
Object.defineProperty(this, 'totalSent', {
@ -58,11 +55,6 @@ function spec() {
}
Address.prototype.update = function(next) {
if (! this.addrStr) {
return next(new Error('Invalid or undefined address string'));
}
var that = this;
async.series([
// TODO TXout!

View File

@ -81,16 +81,33 @@ BlockSchema.statics.fromHash = function(hash, cb) {
BlockSchema.statics.fromHashWithInfo = function(hash, cb) {
var That = this;
this.fromHash(hash, function(err, block) {
if (err) return cb(err);
if (!block) { return cb(new Error('Block not found')); }
block.getInfo(function(err) { return cb(err,block); } );
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);
});
}
});
};
// TODO: Can we store the rpc instance in the Block object?
BlockSchema.methods.getInfo = function (next) {
@ -98,6 +115,9 @@ BlockSchema.methods.getInfo = function (next) {
var rpc = new RpcClient(config.bitcoind);
rpc.getBlock(this.hash, function(err, blockInfo) {
// Not found?
if (err && err.code === -5) return next();
if (err) return next(err);
/*

View File

@ -9,11 +9,6 @@ function spec() {
var rpc = new RpcClient(config.bitcoind);
function Status() {
this.info = {};
this.difficulty = {};
this.txoutsetinfo = {};
this.bestblockhash = {};
this.lastblockhash = {};
}
Status.prototype.getInfo = function(next) {

View File

@ -74,8 +74,8 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
tx.txid = txid;
tx.fillInfo(function(err, txInfo) {
if (!txInfo)
return cb(new Error('TX not found'));
if (err) return cb(err);
if (!txInfo) return cb();
tx.save(function(err) {
return cb(err,tx);
@ -251,6 +251,10 @@ TransactionSchema.statics.queryInfo = function(txid, cb) {
var rpc = new RpcClient(config.bitcoind);
rpc.getRawTransaction(txid, 1, function(err, txInfo) {
// Not found?
if (err && err.code === -5) return cb();
if (err) return cb(err);
var info = txInfo.result;

View File

@ -142,13 +142,17 @@ function spec() {
}
if (opts.upToExisting && existed ) {
if (self.syncInfo.blocksToSync <= self.syncInfo.syncedBlocks) {
var diff = self.syncInfo.blocksToSync - self.syncInfo.syncedBlocks;
if (diff <= 0) {
self.status = 'finished';
p('DONE. Found existing block: ', blockHash);
return cb(err);
}
else {
p('WARN found target block\n\tbut blockChain Height is still higher that ours. Previous light sync must be interrupted.\n\tWill keep syncing.', self.syncInfo.syncedBlocks);
self.syncInfo.skipped_blocks = self.syncInfo.skipped_blocks || 1;
if ((self.syncInfo.skipped_blocks++ % 1000) === 1 ) {
p('WARN found target block\n\tbut blockChain Height is still higher that ours. Previous light sync must be interrupted.\n\tWill keep syncing.', self.syncInfo.syncedBlocks, self.syncInfo.blocksToSync, self.syncInfo.skipped_blocks);
}
}
}
@ -161,7 +165,7 @@ function spec() {
// Continue
if (blockInfo && blockInfo.result) {
self.syncInfo.syncedBlocks++;
if (! existed) self.syncInfo.syncedBlocks++;
if (opts.prev && blockInfo.result.previousblockhash) {
return self.getPrevNextBlock(blockInfo.result.previousblockhash, blockEnd, opts, cb);
}

View File

@ -16,8 +16,16 @@ angular.module('insight.address').controller('AddressController',
addrStr: $routeParams.addrStr
}, function(address) {
$scope.address = address;
}, function() {
$rootScope.flashMessage = 'Address Not Found';
}, function(e) {
if (e.status === 400) {
$rootScope.flashMessage = 'Invalid Address: ' + $routeParams.addrStr;
}
else if (e.status === 503) {
$rootScope.flashMessage = 'Backend Error. ' + e.data;
}
else {
$rootScope.flashMessage = 'Address Not Found';
}
$location.path('/');
});
};

View File

@ -17,8 +17,16 @@ angular.module('insight.blocks').controller('BlocksController', ['$scope', '$roo
blockHash: $routeParams.blockHash
}, function(block) {
$scope.block = block;
}, function() {
$rootScope.flashMessage = 'Block Not Found';
}, function(e) {
if (e.status === 400) {
$rootScope.flashMessage = 'Invalid Transaction ID: ' + $routeParams.txId;
}
else if (e.status === 503) {
$rootScope.flashMessage = 'Backend Error. ' + e.data;
}
else {
$rootScope.flashMessage = 'Block Not Found';
}
$location.path('/');
});
};

View File

@ -1,30 +1,31 @@
'use strict';
angular.module('insight.status').controller('StatusController', ['$scope', '$routeParams', '$location', 'Global', 'Status', function ($scope, $routeParams, $location, Global, Status) {
angular.module('insight.status').controller('StatusController', ['$scope', '$routeParams', '$location', '$rootScope', 'Global', 'Status', 'Sync', function ($scope, $routeParams, $location, $rootScope, Global, Status, Sync) {
$scope.global = Global;
$scope.getData = function(q) {
$scope.getStatus = function(q) {
Status.get({
q: q
q: 'get' + q
}, function(d) {
if (q === 'getInfo') {
$scope.info = d.info;
$rootScope.infoError = null;
angular.extend($scope, d);
}, function(e) {
if (e.status === 503) {
$rootScope.infoError = 'Backend Error. ' + e.data;
}
if (q === 'getDifficulty') {
$scope.difficulty = d.difficulty;
else {
$rootScope.infoError = 'Unknown error:' + e.data;
}
if (q === 'getTxOutSetInfo') {
$scope.txoutsetinfo = d.txoutsetinfo;
}
if (q === 'getBestBlockHash') {
$scope.bestblockhash = d.bestblockhash;
}
if (q === 'getLastBlockHash') {
$scope.lastblockhash = d.lastblockhash;
}
});
};
$scope.getSync = function() {
Sync.get({}, function(sync) {
$rootScope.syncError = null;
$scope.sync = sync;
}, function(e) {
$rootScope.syncError = 'Could not get sync information' + e;
});
};
}]);

View File

@ -22,10 +22,23 @@ angular.module('insight.transactions').controller('transactionsController',
txId: txid
}, function(tx) {
$scope.tx = tx;
<<<<<<< HEAD
console.log(console.log(tx));
$scope.txs.push(tx);
}, function() {
$rootScope.flashMessage = 'Transaction Not Found';
=======
}, function(e) {
if (e.status === 400) {
$rootScope.flashMessage = 'Invalid Transaction ID: ' + $routeParams.txId;
}
else if (e.status === 503) {
$rootScope.flashMessage = 'Backend Error. ' + e.data;
}
else {
$rootScope.flashMessage = 'Transaction Not Found';
}
>>>>>>> matiu/feature/11showsyncstatus
$location.path('/');
});
};

View File

@ -6,3 +6,7 @@ angular.module('insight.status').factory('Status', ['$resource', function($resou
});
}]);
angular.module('insight.status').factory('Sync', ['$resource', function($resource) {
return $resource('/api/sync');
}]);

View File

@ -7,11 +7,12 @@
<div class="row">
<div class="col-lg-6">
<h3>getInfo</h3>
<table class="table table-striped" data-ng-init="getData('getInfo')">
<table class="table table-striped" data-ng-init="getStatus('Info')">
<tbody>
<tr data-ng-show="!info">
<td colspan="2" class="text-center">Loading...</td>
</tr>
<tr data-ng-show="!info &amp;&amp; !infoError">
<td colspan="2" class="text-center">Loading...
<tr data-ng-show="infoError">
<td colspan="2" class="text-danger">{{infoError}}
<tr>
<td>Version</td>
<td>{{info.version}}</td>
@ -65,19 +66,72 @@
<td>{{info.paytxfee}}</td>
</tr>
<tr>
<td>errors</td>
<td>{{info.errors}}</td>
<td>infoErrors</td>
<td>{{info.infoErrors}}</td>
</tr>
</tbody>
</table>
</div>
<div class="col-lg-6">
<h3>getTxOutSetInfo</h3>
<table class="table table-striped" data-ng-init="getData('getTxOutSetInfo')">
<h3>sync status</h3>
<table class="table table-striped" data-ng-init="getSync()">
<tbody>
<tr data-ng-show="!txoutsetinfo">
<tr data-ng-show="syncError">
<td colspan="2"> <span class="text-danger"> {{ syncError }} </span>
<tr data-ng-show="sync.error">
<td colspan="2"> <span class="text-danger"> {{ sync.err }} </span>
</tr>
<tr>
<td>Sync Progress</td>
<td> {{(100 * sync.syncedBlocks/sync.blocksToSync)| number:2}}%
</tr>
<tr>
<td>blocksToSync</td>
<td>{{sync.blocksToSync}}</td>
</tr>
<tr>
<td>syncedBlocks</td>
<td>{{sync.syncedBlocks}}</td>
</tr>
<tr>
<td>start</td>
<td>{{sync.start}}
<span data-ng-show="sync.isStartGenesis"> (genesisBlock) </span>
</td>
</tr>
<tr>
<td>end</td>
<td>{{sync.end}}
<span data-ng-show="sync.isEndGenesis"> (genesisBlock) </span>
</td>
</tr>
<tr>
<td>Sync Type</td>
<td>
<ul>
<li data-ng-show="sync.upToExisting"> <span> Stops at existing block </span>
<li>
<span data-ng-show="sync.scanningBackward"> scanningBackward </span>
<span data-ng-hide="sync.scanningBackward"> scanningForward </span>
</ul>
</td>
</tr>
</tbody>
</table>
<h3>getTxOutSetInfo</h3>
<table class="table table-striped" data-ng-init="getStatus('TxOutSetInfo')">
<tbody>
<tr data-ng-show="!txoutsetinfo &amp;&amp; !infoError">
<td colspan="2" class="text-center">Loading...</td>
</tr>
<tr data-ng-show="infoError">
<td colspan="2" class="text-danger">{{infoError}}</td>
</tr>
<tr>
<td>Height</td>
<td>{{txoutsetinfo.height}}</td>
@ -114,11 +168,15 @@
<div class="row">
<div class="col-lg-6">
<h3>getDifficulty</h3>
<table class="table table-striped" data-ng-init="getData('getDifficulty')">
<table class="table table-striped" data-ng-init="getStatus('Difficulty')">
<tbody>
<tr data-ng-show="!difficulty">
<tr data-ng-show="!difficulty &amp;&amp; !infoError">
<td colspan="2" class="text-center">Loading...</td>
</tr>
<tr data-ng-show="infoError">
<td colspan="2" class="text-danger">{{infoError}}</td>
</tr>
<tr>
<td>Difficulty</td>
<td>{{difficulty}}</td>
@ -128,12 +186,16 @@
</div>
<div class="col-lg-6">
<h3>getLastBlockHash</h3>
<table class="table table-striped" data-ng-init="getData('getLastBlockHash')">
<table class="table table-striped" data-ng-init="getStatus('LastBlockHash')">
<tbody>
<tr data-ng-show="!lastblockhash">
<td colspan="1" class="text-center">Loading...</td>
<tr data-ng-show="!lastblockhash &amp;&amp; !infoError">
<td colspan="2" class="text-center">Loading...</td>
</tr>
<tr data-ng-show="infoError">
<td colspan="2" class="text-danger">{{infoError}}</td>
</tr>
<tr>
<td>Last block hash</td>
<td>{{lastblockhash}}</td>
</tr>
</tbody>