socket reworking to reduce coupling
This commit is contained in:
parent
2f4533e057
commit
06b0e108d5
@ -8,6 +8,7 @@
|
||||
"no-multi-spaces": 0,
|
||||
"no-use-before-define": 1,
|
||||
"object-shorthand": 1,
|
||||
"key-spacing": 0
|
||||
"key-spacing": 0,
|
||||
"no-plusplus": 0
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
const Bcoin = require('./lib/node');
|
||||
const config = require('./config');
|
||||
const logger = require('./lib/logger');
|
||||
const Api = require('./lib/api').server;
|
||||
const Api = require('./lib/api');
|
||||
const db = require('./lib/db');
|
||||
|
||||
logger.log('debug',
|
||||
|
||||
@ -47,11 +47,5 @@ app.use((req, res) => {
|
||||
|
||||
// Socket server
|
||||
const server = require('http').Server(app);
|
||||
const io = require('socket.io')(server);
|
||||
|
||||
const SocketAPI = require('./socket')(io);
|
||||
|
||||
module.exports = {
|
||||
server,
|
||||
io,
|
||||
};
|
||||
module.exports = server;
|
||||
|
||||
@ -1,16 +1,68 @@
|
||||
// Change to have services push blocks/txs
|
||||
module.exports = function addressrouter(io) {
|
||||
io.on('connection', (socket) => {
|
||||
socket.on('subscribe', (data) => {
|
||||
});
|
||||
const server = require('.');
|
||||
const io = require('socket.io')(server);
|
||||
|
||||
socket.on('message', (data) => {
|
||||
});
|
||||
let refreshBlocks = false;
|
||||
const txInterval = 200;
|
||||
let txCounter = 0;
|
||||
|
||||
socket.on('unsubscribe', (data) => {
|
||||
});
|
||||
// Not quite debouncing
|
||||
setInterval(() => {
|
||||
refreshBlocks = true;
|
||||
}, 10000);
|
||||
|
||||
socket.on('disconnect', (data) => {
|
||||
});
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
socket.on('subscribe', (data) => {
|
||||
});
|
||||
|
||||
socket.on('message', (data) => {
|
||||
});
|
||||
|
||||
socket.on('unsubscribe', (data) => {
|
||||
});
|
||||
|
||||
socket.on('disconnect', (data) => {
|
||||
});
|
||||
});
|
||||
|
||||
// Emit block refresh and txs
|
||||
function processBlock(entry, block) {
|
||||
if (refreshBlocks) {
|
||||
refreshBlocks = false;
|
||||
emitBlock(entry);
|
||||
}
|
||||
block.txs.forEach((tx) => {
|
||||
txCounter++;
|
||||
if (txCounter % txInterval === 0) {
|
||||
txCounter = 0;
|
||||
emitTx(tx);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function emitBlock(block) {
|
||||
io.sockets.emit('block', {
|
||||
hash: block.toJSON().hash,
|
||||
});
|
||||
}
|
||||
|
||||
function emitTx(transaction) {
|
||||
const txJSON = transaction.toJSON();
|
||||
io.sockets.emit('tx', {
|
||||
txid: txJSON.hash,
|
||||
valueOut: transaction.outputs.reduce((sum, tx) => {
|
||||
tx = tx.toJSON();
|
||||
|
||||
const valB = (tx.value || tx.valueOut.value || 0) / 1e8;
|
||||
|
||||
return sum + valB;
|
||||
}, 0),
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
io,
|
||||
processBlock,
|
||||
emitBlock,
|
||||
emitTx,
|
||||
};
|
||||
|
||||
@ -1,22 +1,11 @@
|
||||
const FullNode = require('bcoin/lib/node/fullnode');
|
||||
const logger = require('../../lib/logger');
|
||||
const BlockParser = require('../parser').Block;
|
||||
const TxParser = require('../parser').Transaction;
|
||||
const addrParser = require('../parser').Address;
|
||||
const config = require('../../config');
|
||||
const io = require('../api').io;
|
||||
|
||||
// Reverse how sockets are working
|
||||
const socket = require('../../lib/api/socket');
|
||||
|
||||
const node = new FullNode(config.bcoin);
|
||||
|
||||
// Hacky move this to config
|
||||
let refreshBlocks = false;
|
||||
// Super Hacky but better than inline Maths.
|
||||
setInterval(() => {
|
||||
refreshBlocks = true;
|
||||
}, 10000); // Only refresh sockets after 5s passes
|
||||
|
||||
function start() {
|
||||
node.open()
|
||||
.then(() => {
|
||||
@ -27,21 +16,8 @@ function start() {
|
||||
});
|
||||
|
||||
node.chain.on('connect', (entry, block) => {
|
||||
|
||||
BlockParser.parse(entry, block);
|
||||
TxParser.parse(entry, block.txs);
|
||||
addrParser.parse(entry, block.txs);
|
||||
|
||||
if (refreshBlocks) {
|
||||
refreshBlocks = false;
|
||||
io.sockets.emit('block', {
|
||||
hash: block.toJSON().hash,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
node.pool.on('peer', (peer) => {
|
||||
|
||||
socket.processBlock(entry, block);
|
||||
});
|
||||
|
||||
node.on('error', (err) => {
|
||||
@ -50,7 +26,7 @@ function start() {
|
||||
});
|
||||
|
||||
node.mempool.on('tx', (tx) => {
|
||||
|
||||
socket.emitTx(tx);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -1,23 +0,0 @@
|
||||
const AddressModel = require('../../models/address');
|
||||
const InputModel = require('../../models/input');
|
||||
const OutputModel = require('../../models/output');
|
||||
const config = require('../../config');
|
||||
const util = require('../../lib/util');
|
||||
const logger = require('../logger');
|
||||
|
||||
function parse(entry, txs) {
|
||||
txs.forEach((tx) => {
|
||||
|
||||
tx.outputs.forEach((output) => {
|
||||
const outputJSON = output.toJSON();
|
||||
});
|
||||
|
||||
tx.inputs.forEach((input) => {
|
||||
const inputJSON = input.toJSON();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parse,
|
||||
};
|
||||
@ -1,9 +1,5 @@
|
||||
const Block = require('./block');
|
||||
const Transaction = require('./transaction');
|
||||
const Address = require('./address');
|
||||
|
||||
module.exports = {
|
||||
Block,
|
||||
Transaction,
|
||||
Address,
|
||||
};
|
||||
|
||||
@ -1,38 +0,0 @@
|
||||
const TxModel = require('../../models/transaction');
|
||||
const InputModel = require('../../models/input');
|
||||
const OutputModel = require('../../models/output');
|
||||
const config = require('../../config');
|
||||
const util = require('../../lib/util');
|
||||
const logger = require('../logger');
|
||||
const io = require('../api').io;
|
||||
|
||||
const socketThrottle = 250;
|
||||
let counter = 0;
|
||||
|
||||
function parse(entry, txs) {
|
||||
txs.forEach((tx) => {
|
||||
const txJSON = tx.toJSON();
|
||||
|
||||
counter++;
|
||||
|
||||
if (counter % socketThrottle === 0) {
|
||||
|
||||
counter = 0;
|
||||
|
||||
io.sockets.emit('tx', {
|
||||
txid: txJSON.hash,
|
||||
valueOut: tx.outputs.reduce((sum, tx) => {
|
||||
tx = tx.toJSON();
|
||||
|
||||
const valB = (tx.value || tx.valueOut.value || 0) / 1e8;
|
||||
|
||||
return sum + valB;
|
||||
}, 0),
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parse,
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user