Skip to content

Commit

Permalink
Basic electrum stat tracking on /admin (as is available for rpc)
Browse files Browse the repository at this point in the history
  • Loading branch information
janoside committed Jun 28, 2020
1 parent abd03a7 commit cb506b1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
34 changes: 33 additions & 1 deletion app/api/electrumAddressApi.js
Expand Up @@ -16,6 +16,9 @@ const ElectrumClient = require('rn-electrum-client');

var electrumClients = [];

global.electrumStats = {};


function connectToServers() {
return new Promise(function(resolve, reject) {
var promises = [];
Expand Down Expand Up @@ -167,12 +170,16 @@ function getAddressDetails(address, scriptPubkey, sort, limit, offset) {

function getAddressTxids(addrScripthash) {
return new Promise(function(resolve, reject) {
var startTime = new Date().getTime();

runOnAllServers(function(electrumClient) {
return electrumClient.blockchainScripthash_getHistory(addrScripthash);

}).then(function(results) {
debugLog(`getAddressTxids=${utils.ellipsize(JSON.stringify(results), 200)}`);

logStats("blockchainScripthash_getHistory", new Date().getTime() - startTime, true);

if (addrScripthash == coinConfig.genesisCoinbaseOutputAddressScripthash) {
for (var i = 0; i < results.length; i++) {
results[i].result.unshift({tx_hash:coinConfig.genesisCoinbaseTransactionIdsByNetwork[global.activeBlockchain], height:0});
Expand All @@ -194,19 +201,25 @@ function getAddressTxids(addrScripthash) {
resolve(results[0]);
}
}).catch(function(err) {
logStats("blockchainScripthash_getHistory", new Date().getTime() - startTime, false);

reject(err);
});
});
}

function getAddressBalance(addrScripthash) {
return new Promise(function(resolve, reject) {
var startTime = new Date().getTime();

runOnAllServers(function(electrumClient) {
return electrumClient.blockchainScripthash_getBalance(addrScripthash);

}).then(function(results) {
debugLog(`getAddressBalance=${JSON.stringify(results)}`);

logStats("blockchainScripthash_getBalance", new Date().getTime() - startTime, true);

if (addrScripthash == coinConfig.genesisCoinbaseOutputAddressScripthash) {
for (var i = 0; i < results.length; i++) {
var coinbaseBlockReward = coinConfig.blockRewardFunction(0, global.activeBlockchain);
Expand All @@ -230,12 +243,31 @@ function getAddressBalance(addrScripthash) {
resolve(results[0]);
}
}).catch(function(err) {
logStats("blockchainScripthash_getBalance", new Date().getTime() - startTime, false);

reject(err);
});
});
}

function logStats(cmd, dt, success) {
if (!global.electrumStats[cmd]) {
global.electrumStats[cmd] = {count:0, time:0, successes:0, failures:0};
}

global.electrumStats[cmd].count++;
global.electrumStats[cmd].time += dt;

if (success) {
global.electrumStats[cmd].successes++;

} else {
global.electrumStats[cmd].failures++;
}
}

module.exports = {
connectToServers: connectToServers,
getAddressDetails: getAddressDetails
};
};

1 change: 1 addition & 0 deletions routes/baseActionsRouter.js
Expand Up @@ -1498,6 +1498,7 @@ router.get("/admin", function(req, res, next) {
res.locals.appStartTime = global.appStartTime;
res.locals.memstats = v8.getHeapStatistics();
res.locals.rpcStats = global.rpcStats;
res.locals.electrumStats = global.electrumStats;
res.locals.cacheStats = global.cacheStats;
res.locals.errorStats = global.errorStats;

Expand Down
35 changes: 35 additions & 0 deletions views/admin.pug
Expand Up @@ -130,6 +130,41 @@ block content
span #{new Decimal(item.successes).dividedBy(new Decimal(item.successes + item.failures)).times(100).toDP(1)}
small %

if (electrumStats)
div.card.shadow-sm.mb-3
div.card-body
h3.h6 Electrum Stats
hr

div.table-responsive
table.table.table-hover.table-striped
thead
tr
th Method
th.text-right Count
th.text-right Time
small (s)
th.text-right Avg Time
small (ms)
th.text-right Successes / Failures
th.text-right Success Rate

tbody
each item, itemName in electrumStats
tr.text-monospace
td #{itemName}
td.text-right #{item.count.toLocaleString()}
td.text-right #{(item.time / 1000).toLocaleString()}
td.text-right #{(item.time / item.count).toLocaleString()}
td.text-right
span.text-success #{item.successes.toLocaleString()}
span.mx-1 /
span.text-danger #{item.failures.toLocaleString()}

td.text-right
span #{new Decimal(item.successes).dividedBy(new Decimal(item.successes + item.failures)).times(100).toDP(1)}
small %

div.card.shadow-sm.mb-3
div.card-body
h3.h6 Error Stats
Expand Down

0 comments on commit cb506b1

Please sign in to comment.