Skip to content

Commit

Permalink
make account type searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
hackmod committed Mar 16, 2019
1 parent 31e5bf0 commit 156112d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
6 changes: 3 additions & 3 deletions public/js/controllers/AccountsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ angular.module('BlocksApp').controller('AccountsController', function($statePara
{
render:
function(data, type, row) {
if (data & 0x1) {
return "Contract";
}
if (data & 0x4) { // user defined account type
var accountType = data >> 3;
accountType = accountType.toString();
Expand All @@ -69,6 +66,9 @@ angular.module('BlocksApp').controller('AccountsController', function($statePara
}
return "Genesis Alloc";
}
if (data & 0x1) {
return "Contract";
}
return "Account";
},
targets: [2]
Expand Down
95 changes: 80 additions & 15 deletions routes/richlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,114 @@
* Endpoint for richlist
*/

var _ = require('lodash');
var async = require('async');
var mongoose = require('mongoose');

require( '../db.js' );
var Account = mongoose.model('Account');

// load config.json
var config = { nodeAddr: 'localhost', gethPort: 8545 };
try {
var local = require('../config.json');
_.extend(config, local);
console.log('config.json found.');
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
var local = require('../config.example.json');
_.extend(config, local);
console.log('No config file found. Using default configuration... (config.example.json)');
} else {
throw error;
process.exit(1);
}
}

var getAccounts = function(req, res) {
var self = getAccounts;
if (!self.totalSupply) {
self.totalSupply = -1;
self.timestamp = 0;
self.totalSupply = {};
self.timestamp = {};
self.count = {};
self.cacheTime = config.settings.cacheTime || 30*60;
}

// search word
var search = req.body.search && req.body.search.value;
var searchArg = {};
var searchType = -1;
if (search) {
// check user defined accountTypes
if (config.settings.accountTypes) {
try {
var searchre = new RegExp(search, 'i');
_.forEach(config.settings.accountTypes, function(v, k) {
console.log(v, k, search);
if (v.match(searchre)) {
searchType = (k << 3) | 0x4; // user defined type
return false;
}
});
} catch (e) {
// ignore
}
}

// check default account types
if (searchType < 0) {
try {
var searchre = new RegExp(search, 'i');
if ('account'.match(searchre)) {
searchType = 0; // normal account
} else if ('contract'.match(searchre)) {
searchType = 1; // contract
}
} catch (e) {
// ignore
}
}
if (searchType >= 0) {
searchArg = { type: searchType };
}
}

// check cached totalSupply
if (new Date() - self.timestamp > 30*60*1000) {
self.totalSupply = -1;
self.timestamp = 0;
// check cached totalSupply and count
if (!self.timestamp[searchType] || new Date() - self.timestamp[searchType] > self.cacheTime*1000) {
self.totalSupply[searchType] = -1;
self.timestamp[searchType] = 0;
self.count[searchType] = 0;
}

// count accounts only once
var count = req.body.recordsTotal || 0;
var count = self.count[searchType] || 0;
count = parseInt(count);
if (count < 0) {
count = 0;
}

// get totalSupply only once
var queryTotalSupply = self.totalSupply || req.body.totalSupply || null;
var queryTotalSupply = self.totalSupply[searchType] || -1;

var aggregateArg = [];
if (searchType >= 0) {
aggregateArg.push({ $match: searchArg });
}
aggregateArg.push({ $group: { _id: null, totalSupply: { $sum: '$balance' } } });

async.waterfall([
function(callback) {
if (queryTotalSupply < 0) {
Account.aggregate([
{ $group: { _id: null, totalSupply: { $sum: '$balance' } } }
]).exec(function(err, docs) {
Account.aggregate(aggregateArg).exec(function(err, docs) {
if (err) {
callbck(err);
return;
}

var totalSupply = docs[0].totalSupply;
// update cache
self.timestamp = new Date();
self.totalSupply = totalSupply;
self.timestamp[searchType] = new Date();
self.totalSupply[searchType] = totalSupply;
callback(null, totalSupply);
});
} else {
Expand All @@ -56,13 +120,14 @@ var getAccounts = function(req, res) {
function(totalSupply, callback) {
if (!count) {
// get the number of all accounts
Account.count({}, function(err, count) {
Account.count(searchArg, function(err, count) {
if (err) {
callbck(err);
return;
}

count = parseInt(count);
self.count[searchType] = count;
callback(null, totalSupply, count);
});
} else {
Expand Down Expand Up @@ -102,7 +167,7 @@ var getAccounts = function(req, res) {
data.totalSupply = totalSupply;
}

Account.find({}).lean(true).sort(sortOrder).skip(start).limit(limit)
Account.find(searchArg).lean(true).sort(sortOrder).skip(start).limit(limit)
.exec(function (err, accounts) {
if (err) {
res.write(JSON.stringify({"error": true}));
Expand Down

0 comments on commit 156112d

Please sign in to comment.