From 11234788f2dc08d64d7e4384bd23fc88610cefad Mon Sep 17 00:00:00 2001 From: hackyminer Date: Sun, 17 Mar 2019 02:31:09 +0900 Subject: [PATCH 1/3] make account type searchable --- public/js/controllers/AccountsController.js | 6 +- routes/richlist.js | 97 +++++++++++++++++---- 2 files changed, 85 insertions(+), 18 deletions(-) diff --git a/public/js/controllers/AccountsController.js b/public/js/controllers/AccountsController.js index 38363b0e1..f19a1f45b 100755 --- a/public/js/controllers/AccountsController.js +++ b/public/js/controllers/AccountsController.js @@ -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(); @@ -69,6 +66,9 @@ angular.module('BlocksApp').controller('AccountsController', function($statePara } return "Genesis Alloc"; } + if (data & 0x1) { + return "Contract"; + } return "Account"; }, targets: [2] diff --git a/routes/richlist.js b/routes/richlist.js index 6772f1757..0b2e1f43e 100644 --- a/routes/richlist.js +++ b/routes/richlist.js @@ -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 { @@ -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 { @@ -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) { From aee9a12d7916a014edfc57b43e4faad64f7b306d Mon Sep 17 00:00:00 2001 From: hackyminer Date: Sun, 17 Mar 2019 04:05:47 +0900 Subject: [PATCH 2/3] read genesis.json file correctly --- tools/richlist.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/richlist.js b/tools/richlist.js index 5f1f08f44..d2776f488 100644 --- a/tools/richlist.js +++ b/tools/richlist.js @@ -455,12 +455,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) => { From 939438201a4c295af2e6f798cde7dda4f83926f7 Mon Sep 17 00:00:00 2001 From: hackyminer Date: Sun, 17 Mar 2019 04:07:02 +0900 Subject: [PATCH 3/3] update some accounts with a given json accounts --- tools/richlist.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tools/richlist.js b/tools/richlist.js index d2776f488..0ac537d6f 100644 --- a/tools/richlist.js +++ b/tools/richlist.js @@ -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'); @@ -545,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;