block & tx throttling through the sockets. Blocks emits every 10s. Tx every 100 tx

This commit is contained in:
tenthirtyone 2017-08-10 14:20:13 -04:00
parent bdbb086e10
commit eb7e5d10c9
2 changed files with 26 additions and 12 deletions

View File

@ -13,8 +13,7 @@ let refreshBlocks = false;
// Super Hacky but better than inline Maths. // Super Hacky but better than inline Maths.
setInterval(() => { setInterval(() => {
refreshBlocks = true; refreshBlocks = true;
}, 5000); // Only refresh sockets after 5s passes }, 10000); // Only refresh sockets after 5s passes
function start() { function start() {
node.open() node.open()
@ -26,8 +25,7 @@ function start() {
}); });
node.chain.on('connect', (entry, block) => { node.chain.on('connect', (entry, block) => {
logger.log('debug',
'New Block & Ledger Entry');
BlockParser.parse(entry, block); BlockParser.parse(entry, block);
TxParser.parse(entry, block.txs); TxParser.parse(entry, block.txs);
addrParser.parse(entry, block.txs); addrParser.parse(entry, block.txs);
@ -49,14 +47,9 @@ function start() {
`${err}`); `${err}`);
}); });
// node.mempool.on('tx' ...) node.mempool.on('tx', (tx) => {
} console.log(tx)
});
// Super Hack
function setSocket(client) {
console.log('setting socket for node');
//socket = client;
} }
module.exports = { module.exports = {

View File

@ -4,11 +4,32 @@ const OutputModel = require('../../models/output');
const config = require('../../config'); const config = require('../../config');
const util = require('../../lib/util'); const util = require('../../lib/util');
const logger = require('../logger'); const logger = require('../logger');
const io = require('../api').io;
const socketThrottle = 100;
let counter = 0;
function parse(entry, txs) { function parse(entry, txs) {
txs.forEach((tx) => { txs.forEach((tx) => {
const txJSON = tx.toJSON(); const txJSON = tx.toJSON();
counter++;
if (counter % socketThrottle === 0) {
io.sockets.emit('tx', {
txid: txJSON.hash,
valueOut: tx.outputs.reduce((a, b) => {
a = a.toJSON();
b = b.toJSON();
const valA = (a.value || a.valueOut.value || 0) / 1e8;
const valB = (b.value || b.valueOut.value || 0) / 1e8;
return valA + valB;
}),
}, 0);
}
const t = new TxModel({ const t = new TxModel({
hash: txJSON.hash, hash: txJSON.hash,
witnessHash: txJSON.witnessHash, witnessHash: txJSON.witnessHash,