Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
added network views;added network settings;added peers script;
Browse files Browse the repository at this point in the history
  • Loading branch information
iquidus committed Jul 18, 2016
1 parent 3e9feb1 commit b063839
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 39 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ An open source block explorer written in node.js.

* [Jumbucks](http://explorer.getjumbucks.com)
* [Sphere](http://sphere.iquidus.io)
* [Gambit](http://explorer.gambitcrypto.com)
* [SAR](http://explorer.sarcoin.info)
* [Vanillacoin](https://blockchain.vanillacoin.net/)
* [Coven](http://cov.iquidus.io)
* [OKCash](http://okcash.explorer.rocks/)
* [Neoscoin](http://explorer.infernopool.com/)
* [C2Chain](http://c2chain.info/)

Expand Down Expand Up @@ -73,16 +71,16 @@ To stop the cluster you can use
sync.js (located in scripts/) is used for updating the local databases. This script must be called from the explorers root directory.

Usage: node scripts/sync.js [database] [mode]

database: (required)
index [mode] Main index: coin info/stats, transactions & addresses
market Market data: summaries, orderbooks, trade history & chartdata

mode: (required for index database only)
update Updates index from last sync to current block
check checks index for (and adds) any missing transactions/addresses
reindex Clears index then resyncs from genesis to current block

notes:
* 'current block' is the latest created block when script is executed.
* The market database only supports (& defaults to) reindex mode.
Expand All @@ -96,8 +94,9 @@ sync.js (located in scripts/) is used for updating the local databases. This scr

*Example crontab; update index every minute and market data every 2 minutes*

*/1 * * * * cd /path/to/explorer && /usr/bin/node scripts/sync.js index update > /dev/null 2>&1
*/2 * * * * cd /path/to/explorer && /usr/bin/node scripts/sync.js market > /dev/null 2>&1
*/1 * * * * cd /path/to/explorer && /usr/bin/nodejs scripts/sync.js index update > /dev/null 2>&1
*/2 * * * * cd /path/to/explorer && /usr/bin/nodejs scripts/sync.js market > /dev/null 2>&1
*/5 * * * * cd /path/to/explorer && /usr/bin/nodejs scripts/peers.js > /dev/null 2>&1

### Wallet

Expand Down Expand Up @@ -132,7 +131,7 @@ To run sync.js with a larger stack size launch with

node --stack-size=[SIZE] scripts/sync.js index update

Where [SIZE] is an integer higher than the default.
Where [SIZE] is an integer higher than the default.

*note: SIZE will depend on which blockchain you are using, you may need to play around a bit to find an optimal setting*

Expand Down Expand Up @@ -166,4 +165,3 @@ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

9 changes: 8 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var express = require('express')
, routes = require('./routes/index')
, lib = require('./lib/explorer')
, db = require('./lib/database')
, locale = require('./lib/locale');
, locale = require('./lib/locale')
, request = require('request');

var app = express();

Expand Down Expand Up @@ -99,6 +100,12 @@ app.use('/ext/getlasttxs/:min', function(req,res){
});
});

app.use('/ext/connections', function(req,res){
db.get_peers(function(peers){
res.send({data: peers});
});
});

// locals
app.set('title', settings.title);
app.set('symbol', settings.symbol);
Expand Down
91 changes: 64 additions & 27 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var mongoose = require('mongoose')
, Address = require('../models/address')
, Tx = require('../models/tx')
, Richlist = require('../models/richlist')
, Peers = require('../models/peers')
, Heavy = require('../models/heavy')
, lib = require('./explorer')
, settings = require('./settings')
Expand Down Expand Up @@ -60,8 +61,8 @@ function update_address(hash, txid, amount, type, cb) {
} else {
received = received + amount;
}
if (unique == true) {
tx_array.push({addresses: txid, type: type});
if (unique == true) {
tx_array.push({addresses: txid, type: type});
if ( tx_array.length > settings.txcount ) {
tx_array.shift();
}
Expand All @@ -74,9 +75,9 @@ function update_address(hash, txid, amount, type, cb) {
return cb();
});
} else {
if (type == tx_array[index].type) {
if (type == tx_array[index].type) {
return cb(); //duplicate
} else {
} else {
Address.update({a_id:hash}, {
txs: tx_array,
received: received,
Expand All @@ -85,7 +86,7 @@ function update_address(hash, txid, amount, type, cb) {
}, function() {
return cb();
});
}
}
}
});
}
Expand All @@ -106,7 +107,7 @@ function update_address(hash, txid, amount, type, cb) {
balance: amount,
});
}

newAddress.save(function(err) {
if (err) {
return cb(err);
Expand All @@ -130,8 +131,8 @@ function find_tx(txid, cb) {
});
}

function save_tx(txid, cb) {
//var s_timer = new Date().getTime();
function save_tx(txid, cb) {
//var s_timer = new Date().getTime();
lib.get_rawtransaction(txid, function(tx){
if (tx != 'There was an error. Check your console.') {
lib.get_block(tx.blockhash, function(block){
Expand All @@ -142,14 +143,14 @@ function save_tx(txid, cb) {
var i = loop.iteration();
update_address(nvin[i].addresses, txid, nvin[i].amount, 'vin', function(){
loop.next();
});
}, function(){
});
}, function(){
lib.syncLoop(vout.length, function (subloop) {
var t = subloop.iteration();
if (vout[t].addresses) {
update_address(vout[t].addresses, txid, vout[t].amount, 'vout', function(){
subloop.next();
});
});
} else {
subloop.next();
}
Expand All @@ -176,7 +177,7 @@ function save_tx(txid, cb) {
});
});
});
});
});
} else {
return cb('block not found: ' + tx.blockhash);
}
Expand Down Expand Up @@ -351,7 +352,7 @@ module.exports = {
}
});
},

create_txs: function(block, cb) {
lib.syncLoop(block.tx.length, function (loop) {
var i = loop.iteration();
Expand All @@ -367,7 +368,7 @@ module.exports = {
return cb();
});
},

get_last_txs: function(count, min, cb) {
Tx.find({'total': {$gt: min}}).sort({_id: 'desc'}).limit(count).exec(function(err, txs){
if (err) {
Expand Down Expand Up @@ -396,7 +397,7 @@ module.exports = {
}
});
},

// checks market data exists for given market
check_market: function(market, cb) {
Markets.findOne({market: market}, function(err, exists) {
Expand Down Expand Up @@ -445,7 +446,7 @@ module.exports = {
}
});
},

create_heavy: function(coin, cb) {
var newHeavy = new Heavy({
coin: coin,
Expand All @@ -461,7 +462,7 @@ module.exports = {
}
});
},

check_heavy: function(coin, cb) {
Heavy.findOne({coin: coin}, function(err, exists) {
if(exists) {
Expand Down Expand Up @@ -527,7 +528,7 @@ module.exports = {
},
// updates heavy stats for coin
// height: current block height, count: amount of votes to store
update_heavy: function(coin, height, count, cb) {
update_heavy: function(coin, height, count, cb) {
var newVotes = [];
lib.get_maxmoney( function (maxmoney) {
lib.get_maxvote( function (maxvote) {
Expand All @@ -544,7 +545,7 @@ module.exports = {
newVotes.push({count:height-i,reward:block.reward,vote:block.vote});
loop.next();
});
});
});
}, function(){
console.log(newVotes);
Heavy.update({coin: coin}, {
Expand All @@ -569,7 +570,7 @@ module.exports = {
});
});
});
});
});
},

// updates market data for given market; called by sync.js
Expand Down Expand Up @@ -598,17 +599,17 @@ module.exports = {
}
});
},

// updates stats data for given coin; called by sync.js
update_db: function(coin, cb) {
lib.get_blockcount( function (count) {
if (!count){
console.log('Unable to connect to explorer API');
return cb(false);
}
lib.get_supply( function (supply){
lib.get_supply( function (supply){
lib.get_connectioncount(function (connections) {
Stats.update({coin: coin}, {
Stats.update({coin: coin}, {
coin: coin,
count : count,
supply: supply,
Expand All @@ -624,11 +625,11 @@ module.exports = {
// updates tx, address & richlist db's; called by sync.js
update_tx_db: function(coin, start, end, timeout, cb) {
var complete = false;
lib.syncLoop((end - start) + 1, function (loop) {
lib.syncLoop((end - start) + 1, function (loop) {
var x = loop.iteration();
if (x % 5000 === 0) {
Tx.find({}).where('blockindex').lt(start + x).sort({timestamp: 'desc'}).limit(settings.index.last_txs).exec(function(err, txs){
Stats.update({coin: coin}, {
Stats.update({coin: coin}, {
last: start + x - 1,
last_txs: '' //not used anymore left to clear out existing objects
}, function() {});
Expand Down Expand Up @@ -674,13 +675,49 @@ module.exports = {
});
}, function(){
Tx.find({}).sort({timestamp: 'desc'}).limit(settings.index.last_txs).exec(function(err, txs){
Stats.update({coin: coin}, {
Stats.update({coin: coin}, {
last: end,
last_txs: '' //not used anymore left to clear out existing objects
}, function() {
return cb();
});
});
});
});
},

create_peer: function(params, cb) {
var newPeer = new Peers(params);
newPeer.save(function(err) {
if (err) {
console.log(err);
return cb();
} else {
return cb();
}
});
},

find_peer: function(address, cb) {
Peers.findOne({address: address}, function(err, peer) {
if (err) {
return cb(null);
} else {
if (peer) {
return cb(peer);
} else {
return cb (null)
}
}
})
},

get_peers: function(cb) {
Peers.find({}, function(err, peers) {
if (err) {
return cb([]);
} else {
return cb(peers);
}
});
}
};
7 changes: 7 additions & 0 deletions lib/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exports.menu_richlist = "Rich List",
exports.menu_reward = "Reward",
exports.menu_movement = "Movement",
exports.menu_node = "Nodes",
exports.menu_network = "Network"

exports.ex_title = "Block Explorer",
exports.ex_search_title = "Search",
Expand Down Expand Up @@ -70,6 +71,12 @@ exports.rl_top75 = "Top 51-75",
exports.rl_top100 = "Top 76-100",
exports.rl_hundredplus = "101+",

exports.net_connections = "Connections",
exports.net_address = "Address",
exports.net_protocol = "Protocol",
exports.net_subversion = "Sub-version",
exports.net_country = "Country",
exports.net_warning = "This is simply a sub sample of the network based on wallets connected to this node.",

exports.api_title = "API Documentation",
exports.api_message = "The block explorer provides an API allowing users and/or applications to retrieve information from the network without the need for a local wallet.",
Expand Down
3 changes: 2 additions & 1 deletion lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ exports.display = {
"twitter": true,
"search": true,
"richlist": true,
"movement": true
"movement": true,
"network": true
};


Expand Down
8 changes: 8 additions & 0 deletions locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"menu_reward": "Reward",
"menu_movement": "Movement",
"menu_node": "Nodes",
"menu_network": "Network",

// explorer view
"ex_title": "Block Explorer",
Expand Down Expand Up @@ -74,6 +75,13 @@
"rl_top100": "Top 76-100",
"rl_hundredplus": "101+",

"net_connections": "Connections",
"net_address": "Address",
"net_protocol": "Protocol",
"net_subversion": "Sub-version",
"net_country": "Country",
"net_warning": "This is sub sample of the network based on wallets that have connected to this node in the last 24hours.",

// api view
"api_title": "API Documentation",
"api_message": "The block explorer provides an API allowing users and/or applications to retrieve information from the network without the need for a local wallet.",
Expand Down
12 changes: 12 additions & 0 deletions models/peers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var mongoose = require('mongoose')
, Schema = mongoose.Schema;

var PeersSchema = new Schema({
createdAt: { type: Date, expires: 86400, default: Date.now()},
address: { type: String, default: "" },
protocol: { type: String, default: "" },
version: { type: String, default: "" },
country: { type: String, default: "" }
});

module.exports = mongoose.model('Peers', PeersSchema);
Loading

0 comments on commit b063839

Please sign in to comment.