Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make account type searchable #289

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
97 changes: 82 additions & 15 deletions routes/richlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,115 @@

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

require('../db.js');

const Account = mongoose.model('Account');

// load config.json
let config = {};
try {
config = require('../config.json');
console.log('config.json found.');
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
config = require('../config.example.json');
console.log('No config file found. Using default configuration... (config.example.json)');
} else {
throw error;
process.exit(1);
}
}

var getAccounts = function (req, res) {
const 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) {
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
let count = req.body.recordsTotal || 0;
let count = self.count[searchType] || 0;
count = parseInt(count);
if (count < 0) {
count = 0;
}

// get totalSupply only once
const queryTotalSupply = self.totalSupply || req.body.totalSupply || null;
const 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((err, docs) => {
Account.aggregate(aggregateArg).exec((err, docs) => {
if (err) {
callbck(err);
return;
}

if (docs.length == 0) {
callback(true);
return;
}
const { totalSupply } = docs[0];

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

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

Account.find({}).lean(true).sort(sortOrder).skip(start)
Account.find(searchArg).lean(true).sort(sortOrder).skip(start)
.limit(limit)
.exec((err, accounts) => {
if (err) {
Expand Down
31 changes: 28 additions & 3 deletions tools/richlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require("@babel/register")({
const _ = require('lodash');
const Web3 = require('web3');
const asyncL = require('async');
const fs = require('fs');
const BigNumber = require('bignumber.js');
const mongoose = require('mongoose');

Expand Down Expand Up @@ -455,12 +456,13 @@ var bulkInsert = function (bulk) {

function prepareJsonAddress(json, defaultType = 0) {
const accounts = {};
if (json.accounts) {
if (json.accounts || json.alloc) {
// genesis.json style
Object.keys(json.accounts).forEach((account) => {
let jsonAccounts = json.accounts || json.alloc;
Object.keys(jsonAccounts).forEach((account) => {
let key = account.toLowerCase();
key = `0x${key.replace(/^0x/, '')}`;
accounts[key] = { address: key, type };
accounts[key] = { address: key, defaultType };
});
} else if (typeof json === 'object') {
Object.keys(json).forEach((account) => {
Expand Down Expand Up @@ -544,6 +546,29 @@ async function startSync(isParity) {
}
}

// only update some accounts and exit
if (process.argv[2]) {
console.log("Update accounts ...");

let addrs = process.argv[2];
try {
if (fs.existsSync(addrs)) {
let content = fs.readFileSync(addrs);
let json = JSON.parse(content);
web3.eth.getBlockNumber((err, latestBlock) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`* latestBlock = ${latestBlock}`);
readJsonAccounts(json, latestBlock, updateAccounts);
});
}
} catch(err) {
console.error(err);
process.exit(1);
}
} else
web3.eth.getNodeInfo((err, nodeInfo) => {
let isParity = false;

Expand Down