Code cleanup.

This commit is contained in:
softsimon
2020-06-07 17:30:32 +07:00
parent c28e2786b9
commit 33cd77eba6
6 changed files with 35 additions and 27 deletions

View File

@@ -26,23 +26,23 @@ class MempoolBlocks {
private calculateMempoolBlocks(transactionsSorted: TransactionExtended[]): MempoolBlock[] {
const mempoolBlocks: MempoolBlock[] = [];
let blockWeight = 0;
let blockVSize = 0;
let blockSize = 0;
let transactions: TransactionExtended[] = [];
transactionsSorted.forEach((tx) => {
if (blockWeight + tx.vsize < 1000000 || mempoolBlocks.length === config.DEFAULT_PROJECTED_BLOCKS_AMOUNT - 1) {
blockWeight += tx.vsize;
if (blockVSize + tx.vsize <= 1000000 || mempoolBlocks.length === config.DEFAULT_PROJECTED_BLOCKS_AMOUNT - 1) {
blockVSize += tx.vsize;
blockSize += tx.size;
transactions.push(tx);
} else {
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length));
blockWeight = 0;
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockVSize, mempoolBlocks.length));
blockVSize = 0;
blockSize = 0;
transactions = [];
}
});
if (transactions.length) {
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length));
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockVSize, mempoolBlocks.length));
}
return mempoolBlocks;
}

View File

@@ -4,7 +4,7 @@ import { MempoolInfo, TransactionExtended, Transaction } from '../interfaces';
class Mempool {
private inSync: boolean = false;
private mempoolCache: any = {};
private mempoolCache: { [txId: string]: TransactionExtended } = {};
private mempoolInfo: MempoolInfo = { size: 0, bytes: 0 };
private mempoolChangedCallback: Function | undefined;
@@ -30,7 +30,7 @@ class Mempool {
return this.mempoolCache;
}
public setMempool(mempoolData: any) {
public setMempool(mempoolData: { [txId: string]: TransactionExtended }) {
this.mempoolCache = mempoolData;
if (this.mempoolChangedCallback && mempoolData) {
this.mempoolChangedCallback(mempoolData);

View File

@@ -25,7 +25,7 @@ class WebsocketHandler {
}
this.wss.on('connection', (client: WebSocket) => {
client.on('message', (message: any) => {
client.on('message', (message: string) => {
try {
const parsedMessage: WebsocketResponse = JSON.parse(message);
const response = {};

View File

@@ -1,4 +1,5 @@
const config = require('../mempool-config.json');
import { Express, Request, Response, NextFunction } from 'express';
import * as fs from 'fs';
import * as express from 'express';
import * as compression from 'compression';
@@ -17,13 +18,13 @@ import fiatConversion from './api/fiat-conversion';
class Server {
wss: WebSocket.Server;
server: https.Server | http.Server;
app: any;
app: Express;
constructor() {
this.app = express();
this.app
.use((req, res, next) => {
.use((req: Request, res: Response, next: NextFunction) => {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
})

View File

@@ -1,3 +1,4 @@
import { Request, Response } from 'express';
import statistics from './api/statistics';
import feeApi from './api/fee-api';
import backendInfo from './api/backend-info';
@@ -22,60 +23,66 @@ class Routes {
console.log('Statistics cache created');
}
public async get2HStatistics(req, res) {
public async get2HStatistics(req: Request, res: Response) {
const result = await statistics.$list2H();
res.send(result);
}
public get24HStatistics(req, res) {
public get24HStatistics(req: Request, res: Response) {
res.send(this.cache['24h']);
}
public get1WHStatistics(req, res) {
public get1WHStatistics(req: Request, res: Response) {
res.send(this.cache['1w']);
}
public get1MStatistics(req, res) {
public get1MStatistics(req: Request, res: Response) {
res.send(this.cache['1m']);
}
public get3MStatistics(req, res) {
public get3MStatistics(req: Request, res: Response) {
res.send(this.cache['3m']);
}
public get6MStatistics(req, res) {
public get6MStatistics(req: Request, res: Response) {
res.send(this.cache['6m']);
}
public get1YStatistics(req, res) {
public get1YStatistics(req: Request, res: Response) {
res.send(this.cache['1y']);
}
public async getRecommendedFees(req, res) {
public async getRecommendedFees(req: Request, res: Response) {
const result = feeApi.getRecommendedFee();
res.send(result);
}
public async getMempoolBlocks(req, res) {
public getMempoolBlocks(req: Request, res: Response) {
try {
const result = await mempoolBlocks.getMempoolBlocks();
const result = mempoolBlocks.getMempoolBlocks();
res.send(result);
} catch (e) {
res.status(500).send(e.message);
}
}
public getTransactionTimes(req, res) {
public getTransactionTimes(req: Request, res: Response) {
if (!Array.isArray(req.query.txId)) {
res.status(500).send('Not an array');
return;
}
const txIds = req.query.txId;
const txIds: string[] = [];
for (const _txId in req.query.txId) {
if (typeof req.query.txId[_txId] === 'string') {
txIds.push(req.query.txId[_txId].toString());
}
}
const times = mempool.getFirstSeenForTransactions(txIds);
res.send(times);
}
public getBackendInfo(req, res) {
public getBackendInfo(req: Request, res: Response) {
res.send(backendInfo.getBackendInfo());
}
}