Merge branch 'master' of github.com:bitpay/mystery into test/test-p2p-sync

Conflicts:
	lib/PeerSync.js
	lib/Sync.js
This commit is contained in:
Manuel Araoz 2014-01-17 10:22:49 -03:00
commit 717b1e930f
11 changed files with 133 additions and 146 deletions

23
app/controllers/socket.js Normal file
View File

@ -0,0 +1,23 @@
'use strict';
// server-side socket behaviour
// io is a variable already taken in express
var ios = null;
module.exports.init = function(app, io_ext) {
ios = io_ext;
ios.set('log level', 1); // reduce logging
ios.sockets.on('connection', function() {
});
};
module.exports.broadcast_tx = function(tx) {
ios.sockets.emit('tx', tx);
};
module.exports.broadcast_block = function(block) {
ios.sockets.emit('block', block);
};

View File

@ -1,25 +0,0 @@
'use strict';
var Transaction = require('../../models/Transaction');
// server-side socket behaviour
var io = null;
module.exports.init = function(app, io_ext) {
io = io_ext;
io.set('log level', 1); // reduce logging
io.sockets.on('connection', function(socket) {
});
};
module.exports.broadcast_tx = function(tx) {
io.sockets.emit('tx', tx);
};
module.exports.broadcast_block = function(block) {
io.sockets.emit('block', block);
};

View File

@ -14,4 +14,6 @@ module.exports = {
disableAgent: true, disableAgent: true,
}, },
network: 'testnet', network: 'testnet',
disableP2pSync: false,
disableHistoricSync: false,
} }

View File

@ -4,19 +4,13 @@ require('classtool');
function spec() { function spec() {
var mongoose = require('mongoose');
var util = require('util'); var util = require('util');
var RpcClient = require('bitcore/RpcClient').class(); var RpcClient = require('bitcore/RpcClient').class();
var networks = require('bitcore/networks'); var networks = require('bitcore/networks');
var async = require('async'); var async = require('async');
var config = require('../config/config'); var config = require('../config/config');
var Block = require('../app/models/Block'); var Block = require('../app/models/Block');
var Transaction = require('../app/models/Transaction');
var TransactionItem = require('../app/models/TransactionItem');
var Sync = require('./Sync').class(); var Sync = require('./Sync').class();
var sockets = require('../app/views/sockets/main.js');
var CONCURRENCY = 5;
function HistoricSync(opts) { function HistoricSync(opts) {
this.block_count= 0; this.block_count= 0;
@ -26,10 +20,12 @@ function spec() {
} }
function p() { function p() {
var params = Array.prototype.slice.call(arguments); var args = [];
Array.prototype.push.apply( args, arguments );
params.unshift('[historic_sync]'); args.unshift('[historic_sync]');
console.log.apply(this,params); /*jshint validthis:true */
console.log.apply(this, args);
} }
var progress_bar = function(string, current, total) { var progress_bar = function(string, current, total) {
@ -63,7 +59,7 @@ function spec() {
// Already got it? // Already got it?
function(c) { function(c) {
Block.findOne({hash:blockHash}, function(err,block){ Block.findOne({hash:blockHash}, function(err,block){
if (err) { p(err); return c(err); }; if (err) { p(err); return c(err); }
if (block) { if (block) {
existed = 1; existed = 1;
blockObj = block; blockObj = block;
@ -96,7 +92,7 @@ function spec() {
function(c) { function(c) {
if (existed) return c(); if (existed) return c();
that.sync.storeBlock(blockInfo.result, function(err, block) { that.sync.storeBlock(blockInfo.result, function(err) {
existed = err && err.toString().match(/E11000/); existed = err && err.toString().match(/E11000/);
if (err && ! existed) return c(err); if (err && ! existed) return c(err);
return c(); return c();
@ -133,8 +129,9 @@ function spec() {
HistoricSync.prototype.syncBlocks = function(start, end, isForward, cb) { HistoricSync.prototype.syncBlocks = function(start, end, isForward, cb) {
var that = this; var that = this;
p('Syncing Blocks, starting from: %s end: %s isForward:', p('Starting from: ', start);
start, end, isForward); p(' to : ', end);
p(' isForward: ', isForward);
return that.getPrevNextBlock( start, end, return that.getPrevNextBlock( start, end,
@ -224,7 +221,7 @@ function spec() {
if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){ if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){
setTimeout(function() { setTimeout(function() {
p("Retrying in %d secs ", retry_secs); p('Retrying in %d secs', retry_secs);
sync(); sync();
}, retry_secs * 1000); }, retry_secs * 1000);
} }
@ -232,9 +229,12 @@ function spec() {
return next(err, that.block_count); return next(err, that.block_count);
}); });
} }
if (!err)
sync(); sync();
else
return next(err, 0);
}); });
} };
HistoricSync.prototype.import_history = function(opts, next) { HistoricSync.prototype.import_history = function(opts, next) {
var that = this; var that = this;

View File

@ -5,18 +5,13 @@ require('classtool');
function spec() { function spec() {
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var util = require('util');
var RpcClient = require('bitcore/RpcClient').class();
var async = require('async');
var config = require('../config/config'); var config = require('../config/config');
var Block = require('../app/models/Block'); var Block = require('../app/models/Block');
var Transaction = require('../app/models/Transaction'); var Transaction = require('../app/models/Transaction');
var TransactionItem = require('../app/models/TransactionItem'); var sockets = require('../app/controllers/socket.js');
var sockets = require('../app/views/sockets/main.js');
var CONCURRENCY = 5;
function Sync(config) { function Sync() {
this.tx_count = 0; this.tx_count = 0;
} }

View File

@ -19,6 +19,14 @@
{ {
"name": "Mario Colque", "name": "Mario Colque",
"email": "colquemario@gmail.com" "email": "colquemario@gmail.com"
},
{
"name": "Gustavo Cortez",
"email": "cmgustavo83@gmail.com"
},
{
"name": "Juan Sosa",
"email": "bechilandia@gmail.com"
} }
], ],
"bugs": { "bugs": {

View File

@ -40,26 +40,6 @@
<div data-ng-controller="transactionsController" data-ng-init="byAddress(params.addrStr)"> <div data-ng-controller="transactionsController" data-ng-init="byAddress(params.addrStr)">
<h2>Transactions <small>Transactions contained within this block</small></h2> <h2>Transactions <small>Transactions contained within this block</small></h2>
<table class="table table-striped"> <div data-ng-include src="'/views/transaction/list.html'"></div>
<thead>
<tr>
<th>Transaction Hash</th>
<th>Datetime</th>
<th>Fee</th>
<th>Transacted Amount</th>
</tr>
</thead>
<tbody>
<tr data-ng-show="!txs.length">
<td colspan="4" class="text-center">Loading...</td>
</tr>
<tr data-ng-repeat="tx in txs">
<td><a href="/#!/tx/{{tx.txid}}">{{tx.txid}}</a></td>
<td>{{tx.time * 1000 | date:'medium'}}</td>
<td>{{tx.feeds}}</td>
<td>{{tx.valueOut}}</td>
</tr>
</tbody>
</table>
</div> </div>
</section> </section>

View File

@ -50,18 +50,6 @@
<td> <strong> Number Of Transactions </strong> </td> <td> <strong> Number Of Transactions </strong> </td>
<td class="text-right text-muted">{{block.tx.length}}<td> <td class="text-right text-muted">{{block.tx.length}}<td>
</tr> </tr>
<tr>
<td> <strong> Output Total</strong></td>
<td class="text-right text-muted">--</td>
</tr>
<tr>
<td> <strong> Estimated Transaction Volume </strong></td>
<td class="text-right text-muted">--</td>
</tr>
<tr>
<td> <strong> Transaction Fees </strong></td>
<td class="text-right text-muted">--</td>
</tr>
<tr> <tr>
<td> <strong> Height </strong></td> <td> <strong> Height </strong></td>
<td class="text-right text-muted">{{block.height}}</td> <td class="text-right text-muted">{{block.height}}</td>
@ -76,10 +64,6 @@
<div class="col-md-6"> <div class="col-md-6">
<table class="table"> <table class="table">
<tbody> <tbody>
<tr>
<td> <strong> Relayed By </strong></td>
<td class="text-right text-muted">--</td>
</tr>
<tr> <tr>
<td> <strong> Difficulty </strong></td> <td> <strong> Difficulty </strong></td>
<td class="text-right text-muted">{{block.difficulty}}</td> <td class="text-right text-muted">{{block.difficulty}}</td>
@ -107,42 +91,7 @@
<div data-ng-controller="transactionsController" data-ng-init="byBlock(params.blockHash)"> <div data-ng-controller="transactionsController" data-ng-init="byBlock(params.blockHash)">
<h2>Transactions <small >Transactions contained within this block</small></h2> <h2>Transactions <small >Transactions contained within this block</small></h2>
<div data-ng-include src="'/views/transaction/list.html'"></div>
<div class="block-tx" data-ng-repeat="tx in txs">
<div class="line-bot">
<a href="/#!/tx/{{tx.txid}}">{{tx.txid}}</a>
<span class="pull-right">{{tx.time * 1000 | date:'medium'}}</span>
</div>
<div class="row">
<div class="col-md-5">
Transmitter
</div>
<div class="col-md-2">
<span class="glyphicon glyphicon-arrow-right lead"></span>
</div>
<div class="col-md-5">
<div class="row">
<div class="col-md-6">
<p>Receptor</p>
<p>Receptor</p>
</div>
<div class="col-md-6 text-right">
<p>Bitcoin of Receptor</p>
<p>Bitcoin of Receptor</p>
</div>
</div>
</div>
</div>
<div class="line-top">
<small class="text-muted">Feeds: {{tx.feeds}}</small>
<div class="btn btn-primary pull-right">
{{tx.valueOut}}
</div>
</div>
</div><!-- END OF BLOCK-TX -->
</div> </div>
</div> </div>
</div> </div>

View File

@ -0,0 +1,48 @@
<div data-ng-show="!txs || txs.lenght">Loading...</div>
<div class="block-tx" data-ng-repeat="tx in txs">
<div class="line-bot">
<a href="/#!/tx/{{tx.txid}}">{{tx.txid}}</a>
<span class="pull-right">{{tx.time * 1000 | date:'medium'}}</span>
</div>
<div class="row">
<div class="col-md-5">
<span data-ng-show="tx.isCoinBase">No Inputs (Newly Generated isCoinBasens)</span>
<div data-ng-show="!tx.isCoinBase">
<div data-ng-repeat="vin in tx.vin">
<a href="/#!/address/{{vin.addr}}" class="ellipsis">{{vin.addr}}</a>
<span class="badge">{{vin.value}} BTC</span>
<p>
Outpoint:
<a href="/#!/tx/{{vin.txid}}" class="ellipsis">{{vin.txid}}</a>
</p>
</div>
</div>
</div>
<div class="col-md-2">
<span class="glyphicon glyphicon-arrow-right lead"></span>
</div>
<div class="col-md-5">
<div class="row">
<div class="col-md-6">
<div data-ng-repeat="vout in tx.vout">
<span data-ng-repeat="address in vout.scriptPubKey.addresses">{{vout.scriptPubKey.type}}</span>
</div>
</div>
<div class="col-md-6 text-right">
<div data-ng-repeat="vout in tx.vout">
<a href="/#!/address/{{address}}" data-ng-repeat="address in vout.scriptPubKey.addresses" class="ellipsis">{{address}}</a>
<span class="badge">{{vout.value}} BTC</span>
</div>
</div>
</div>
</div>
</div>
<div class="line-top">
<small data-ng-show="!tx.isCoinBase" class="text-muted">Feeds: {{tx.feeds}}</small>
<div class="btn btn-primary pull-right">
{{tx.valueOut}}
</div>
</div>
</div>

View File

@ -4,6 +4,7 @@
//Set the node enviornment variable if not set before //Set the node enviornment variable if not set before
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; process.env.NODE_ENV = process.env.NODE_ENV || 'development';
/** /**
* Module dependencies. * Module dependencies.
*/ */
@ -13,6 +14,7 @@ var express = require('express'),
HistoricSync = require('./lib/HistoricSync').class(), HistoricSync = require('./lib/HistoricSync').class(),
mongoose = require('mongoose'); mongoose = require('mongoose');
/** /**
* Main application entry file. * Main application entry file.
*/ */
@ -42,6 +44,7 @@ var walk = function(path) {
walk(models_path); walk(models_path);
// historic_sync process // historic_sync process
if (!config.disableHistoricSync) {
var hs = new HistoricSync(); var hs = new HistoricSync();
hs.init({ hs.init({
skip_db_connection: true, skip_db_connection: true,
@ -53,9 +56,11 @@ hs.init({
console.log('historic_sync finished!'); console.log('historic_sync finished!');
}); });
}); });
}
// p2p_sync process // p2p_sync process
if (!config.disableP2pSync) {
var ps = new PeerSync(); var ps = new PeerSync();
ps.init({ ps.init({
skip_db_connection: true, skip_db_connection: true,
@ -64,8 +69,10 @@ ps.init({
}, function() { }, function() {
ps.run(); ps.run();
}); });
}
// express app // express app
/*global app: true*/
var app = express(); var app = express();
//express settings //express settings
@ -76,8 +83,8 @@ require('./config/routes')(app);
// socket.io // socket.io
var server = require('http').createServer(app); var server = require('http').createServer(app);
var io = require('socket.io').listen(server); var ios = require('socket.io').listen(server);
require('./app/views/sockets/main.js').init(app,io); require('./app/controllers/socket.js').init(app,ios);
//Start the app by listening on <port> //Start the app by listening on <port>
var port = process.env.PORT || config.port; var port = process.env.PORT || config.port;