Browse Source

- move more functionality into rpcApi

- new /blocks page
fix-133-memory-crash
Dan Janosik 7 years ago
parent
commit
7efe9385c3
  1. 55
      app/rpcApi.js
  2. 67
      routes/baseActionsRouter.js
  3. 32
      views/blocks.pug
  4. 17
      views/includes/blocks-list.pug
  5. 23
      views/index.pug

55
app/rpcApi.js

@ -33,6 +33,18 @@ var genesisCoinbaseTransaction = {
"blocktime": 1230988505 "blocktime": 1230988505
}; };
function getInfo() {
return new Promise(function(resolve, reject) {
client.cmd('getinfo', function(err, result, resHeaders) {
if (err) {
return console.log("Error 3207fh0f: " + err);
}
resolve(result);
});
});
}
function getBlockByHeight(blockHeight) { function getBlockByHeight(blockHeight) {
console.log("getBlockByHeight: " + blockHeight); console.log("getBlockByHeight: " + blockHeight);
@ -55,6 +67,47 @@ function getBlockByHeight(blockHeight) {
}); });
} }
function getBlocksByHeight(blockHeights) {
console.log("getBlocksByHeight: " + blockHeights);
return new Promise(function(resolve, reject) {
var batch = [];
for (var i = 0; i < blockHeights.length; i++) {
batch.push({
method: 'getblockhash',
params: [ blockHeights[i] ]
});
}
var blockHashes = [];
client.cmd(batch, function(err, result, resHeaders) {
blockHashes.push(result);
if (blockHashes.length == batch.length) {
var batch2 = [];
for (var i = 0; i < blockHashes.length; i++) {
batch2.push({
method: 'getblock',
params: [ blockHashes[i] ]
});
}
var blocks = [];
client.cmd(batch2, function(err2, result2, resHeaders2) {
if (err2) {
console.log("Error 138ryweufdf: " + err2);
}
blocks.push(result2);
if (blocks.length == batch2.length) {
resolve(blocks);
}
});
}
});
});
}
function getTransactionInputs(rpcClient, transaction) { function getTransactionInputs(rpcClient, transaction) {
console.log("getTransactionInputs: " + transaction.txid); console.log("getTransactionInputs: " + transaction.txid);
@ -190,7 +243,9 @@ function getBlockData(rpcClient, blockHash, txLimit, txOffset) {
} }
module.exports = { module.exports = {
getInfo: getInfo,
getBlockByHeight: getBlockByHeight, getBlockByHeight: getBlockByHeight,
getBlocksByHeight: getBlocksByHeight,
getTransactionInputs: getTransactionInputs, getTransactionInputs: getTransactionInputs,
getBlockData: getBlockData, getBlockData: getBlockData,
getRawTransaction: getRawTransaction, getRawTransaction: getRawTransaction,

67
routes/baseActionsRouter.js

@ -6,7 +6,7 @@ var utils = require('./../app/utils');
var md5 = require("md5"); var md5 = require("md5");
var env = require("./../app/env"); var env = require("./../app/env");
var bitcoin = require("bitcoin"); var bitcoin = require("bitcoin");
var rpcApi = require("./../app/rpcApi") var rpcApi = require("./../app/rpcApi");
router.get("/", function(req, res) { router.get("/", function(req, res) {
if (!req.session.host) { if (!req.session.host) {
@ -30,22 +30,18 @@ router.get("/", function(req, res) {
var client = global.client; var client = global.client;
client.cmd('getinfo', function(err, result, resHeaders) { rpcApi.getInfo().then(function(getinfo) {
if (err) { res.locals.result = getinfo;
return console.log(err);
}
res.locals.result = result;
var promises = []; var blockHeights = [];
if (result.blocks) { if (getinfo.blocks) {
for (var i = 0; i < 10; i++) { for (var i = 0; i < 10; i++) {
promises.push(rpcApi.getBlockByHeight(result.blocks - i)); blockHeights.push(getinfo.blocks - i);
} }
} }
Promise.all(promises).then(function() { rpcApi.getBlocksByHeight(blockHeights).then(function(latestBlocks) {
res.locals.latestBlocks = arguments[0]; res.locals.latestBlocks = latestBlocks;
res.render("index"); res.render("index");
}); });
@ -84,6 +80,51 @@ router.post("/connect", function(req, res) {
res.redirect("/"); res.redirect("/");
}); });
router.get("/blocks", function(req, res) {
var limit = 20;
var offset = 0;
var sort = "desc";
if (req.query.limit) {
limit = parseInt(req.query.limit);
}
if (req.query.offset) {
offset = parseInt(req.query.offset);
}
if (req.query.sort) {
sort = req.query.sort;
}
res.locals.limit = limit;
res.locals.offset = offset;
res.locals.sort = sort;
res.locals.paginationBaseUrl = "/blocks";
rpcApi.getInfo().then(function(getinfo) {
res.locals.blockCount = getinfo.blocks;
res.locals.blockOffset = offset;
var blockHeights = [];
if (sort == "desc") {
for (var i = (getinfo.blocks - offset); i > (getinfo.blocks - offset - limit); i--) {
blockHeights.push(i);
}
} else {
for (var i = offset; i < (offset + limit); i++) {
blockHeights.push(i);
}
}
rpcApi.getBlocksByHeight(blockHeights).then(function(blocks) {
res.locals.blocks = blocks;
res.render("blocks");
});
});
});
router.get("/block-height/:blockHeight", function(req, res) { router.get("/block-height/:blockHeight", function(req, res) {
var client = global.client; var client = global.client;
@ -181,7 +222,7 @@ router.get("/tx/:transactionId", function(req, res) {
txids.push(rawTxResult.vin[i].txid); txids.push(rawTxResult.vin[i].txid);
} }
} }
rpcApi.getRawTransactions(txids).then(function(txInputs) { rpcApi.getRawTransactions(txids).then(function(txInputs) {
res.locals.result.txInputs = txInputs; res.locals.result.txInputs = txInputs;

32
views/blocks.pug

@ -0,0 +1,32 @@
extends layout
block content
h1(class="h2") Blocks
hr
nav(aria-label="Page navigation")
ul(class="pagination justify-content-center")
li(class="page-item", class=(sort == "asc" ? "active" : false))
a(class="page-link", href=(sort == "asc" ? "javascript:void(0)" : "/blocks?limit=" + limit + "&offset=" + offset + "&sort=asc"))
span(aria-hidden="true") Oldest blocks first
li(class="page-item", class=(sort == "desc" ? "active" : false))
a(class="page-link", href=(sort == "desc" ? "javascript:void(0)" : "/blocks?limit=" + limit + "&offset=" + offset + "&sort=desc"))
span(aria-hidden="true") Newest blocks first
include includes/blocks-list.pug
hr
if (blockCount > limit)
- var pageNumber = offset / limit + 1;
- var pageCount = Math.floor(blockCount / limit);
- if (pageCount * limit < blockCount) {
- pageCount++;
- }
- var paginationUrlFunction = function(x) {
- return paginationBaseUrl + "?limit=" + limit + "&offset=" + ((x - 1) * limit + "&sort=" + sort);
- }
hr
include includes/pagination.pug

17
views/includes/blocks-list.pug

@ -0,0 +1,17 @@
table(class="table table-striped")
thead
tr
th
th Height
th Timestamp (utc)
th Transactions
th Size (bytes)
tbody
each block, blockIndex in blocks
tr
th #{blockIndex + blockOffset + 1}
td
a(href=("/block-height/" + block.height)) #{block.height}
td #{moment.utc(new Date(parseInt(block.time) * 1000)).format("Y-MM-DD HH:mm:ss")}
td #{block.tx.length.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}
td #{block.size.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}

23
views/index.pug

@ -24,21 +24,14 @@ block content
div(class="tab-content") div(class="tab-content")
div(id="tab-latest-tx", class="tab-pane active", role="tabpanel") div(id="tab-latest-tx", class="tab-pane active", role="tabpanel")
h3 Latest Blocks h3 Latest Blocks
table(class="table table-striped")
thead - var blocks = latestBlocks;
tr - var blockOffset = 0;
th Height
th Timestamp (utc) include includes/blocks-list.pug
th Transactions
th Size (bytes) hr
tbody a(href="/blocks", class="btn btn-primary btn-block") See more
each block in latestBlocks
tr
td
a(href=("/block-height/" + block.getblock.height)) #{block.getblock.height}
td #{moment.utc(new Date(parseInt(block.getblock.time) * 1000)).format("Y-MM-DD HH:mm:ss")}
td #{block.getblock.tx.length.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}
td #{block.getblock.size.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}
div(id="tab-getinfo", class="tab-pane", role="tabpanel") div(id="tab-getinfo", class="tab-pane", role="tabpanel")
h3 Node Info (getinfo) h3 Node Info (getinfo)

Loading…
Cancel
Save