Code cleanup.
This commit is contained in:
parent
4bef9f9b79
commit
7ade4c114b
@ -26,23 +26,23 @@ class MempoolBlocks {
|
|||||||
|
|
||||||
private calculateMempoolBlocks(transactionsSorted: TransactionExtended[]): MempoolBlock[] {
|
private calculateMempoolBlocks(transactionsSorted: TransactionExtended[]): MempoolBlock[] {
|
||||||
const mempoolBlocks: MempoolBlock[] = [];
|
const mempoolBlocks: MempoolBlock[] = [];
|
||||||
let blockWeight = 0;
|
let blockVSize = 0;
|
||||||
let blockSize = 0;
|
let blockSize = 0;
|
||||||
let transactions: TransactionExtended[] = [];
|
let transactions: TransactionExtended[] = [];
|
||||||
transactionsSorted.forEach((tx) => {
|
transactionsSorted.forEach((tx) => {
|
||||||
if (blockWeight + tx.vsize < 1000000 || mempoolBlocks.length === config.DEFAULT_PROJECTED_BLOCKS_AMOUNT - 1) {
|
if (blockVSize + tx.vsize <= 1000000 || mempoolBlocks.length === config.DEFAULT_PROJECTED_BLOCKS_AMOUNT - 1) {
|
||||||
blockWeight += tx.vsize;
|
blockVSize += tx.vsize;
|
||||||
blockSize += tx.size;
|
blockSize += tx.size;
|
||||||
transactions.push(tx);
|
transactions.push(tx);
|
||||||
} else {
|
} else {
|
||||||
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length));
|
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockVSize, mempoolBlocks.length));
|
||||||
blockWeight = 0;
|
blockVSize = 0;
|
||||||
blockSize = 0;
|
blockSize = 0;
|
||||||
transactions = [];
|
transactions = [];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (transactions.length) {
|
if (transactions.length) {
|
||||||
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length));
|
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockVSize, mempoolBlocks.length));
|
||||||
}
|
}
|
||||||
return mempoolBlocks;
|
return mempoolBlocks;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import { MempoolInfo, TransactionExtended, Transaction } from '../interfaces';
|
|||||||
|
|
||||||
class Mempool {
|
class Mempool {
|
||||||
private inSync: boolean = false;
|
private inSync: boolean = false;
|
||||||
private mempoolCache: any = {};
|
private mempoolCache: { [txId: string]: TransactionExtended } = {};
|
||||||
private mempoolInfo: MempoolInfo = { size: 0, bytes: 0 };
|
private mempoolInfo: MempoolInfo = { size: 0, bytes: 0 };
|
||||||
private mempoolChangedCallback: Function | undefined;
|
private mempoolChangedCallback: Function | undefined;
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ class Mempool {
|
|||||||
return this.mempoolCache;
|
return this.mempoolCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setMempool(mempoolData: any) {
|
public setMempool(mempoolData: { [txId: string]: TransactionExtended }) {
|
||||||
this.mempoolCache = mempoolData;
|
this.mempoolCache = mempoolData;
|
||||||
if (this.mempoolChangedCallback && mempoolData) {
|
if (this.mempoolChangedCallback && mempoolData) {
|
||||||
this.mempoolChangedCallback(mempoolData);
|
this.mempoolChangedCallback(mempoolData);
|
||||||
|
@ -25,7 +25,7 @@ class WebsocketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.wss.on('connection', (client: WebSocket) => {
|
this.wss.on('connection', (client: WebSocket) => {
|
||||||
client.on('message', (message: any) => {
|
client.on('message', (message: string) => {
|
||||||
try {
|
try {
|
||||||
const parsedMessage: WebsocketResponse = JSON.parse(message);
|
const parsedMessage: WebsocketResponse = JSON.parse(message);
|
||||||
const response = {};
|
const response = {};
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
const config = require('../mempool-config.json');
|
const config = require('../mempool-config.json');
|
||||||
|
import { Express, Request, Response, NextFunction } from 'express';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as express from 'express';
|
import * as express from 'express';
|
||||||
import * as compression from 'compression';
|
import * as compression from 'compression';
|
||||||
@ -17,13 +18,13 @@ import fiatConversion from './api/fiat-conversion';
|
|||||||
class Server {
|
class Server {
|
||||||
wss: WebSocket.Server;
|
wss: WebSocket.Server;
|
||||||
server: https.Server | http.Server;
|
server: https.Server | http.Server;
|
||||||
app: any;
|
app: Express;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.app = express();
|
this.app = express();
|
||||||
|
|
||||||
this.app
|
this.app
|
||||||
.use((req, res, next) => {
|
.use((req: Request, res: Response, next: NextFunction) => {
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
next();
|
next();
|
||||||
})
|
})
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { Request, Response } from 'express';
|
||||||
import statistics from './api/statistics';
|
import statistics from './api/statistics';
|
||||||
import feeApi from './api/fee-api';
|
import feeApi from './api/fee-api';
|
||||||
import backendInfo from './api/backend-info';
|
import backendInfo from './api/backend-info';
|
||||||
@ -22,60 +23,66 @@ class Routes {
|
|||||||
console.log('Statistics cache created');
|
console.log('Statistics cache created');
|
||||||
}
|
}
|
||||||
|
|
||||||
public async get2HStatistics(req, res) {
|
public async get2HStatistics(req: Request, res: Response) {
|
||||||
const result = await statistics.$list2H();
|
const result = await statistics.$list2H();
|
||||||
res.send(result);
|
res.send(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get24HStatistics(req, res) {
|
public get24HStatistics(req: Request, res: Response) {
|
||||||
res.send(this.cache['24h']);
|
res.send(this.cache['24h']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get1WHStatistics(req, res) {
|
public get1WHStatistics(req: Request, res: Response) {
|
||||||
res.send(this.cache['1w']);
|
res.send(this.cache['1w']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get1MStatistics(req, res) {
|
public get1MStatistics(req: Request, res: Response) {
|
||||||
res.send(this.cache['1m']);
|
res.send(this.cache['1m']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get3MStatistics(req, res) {
|
public get3MStatistics(req: Request, res: Response) {
|
||||||
res.send(this.cache['3m']);
|
res.send(this.cache['3m']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get6MStatistics(req, res) {
|
public get6MStatistics(req: Request, res: Response) {
|
||||||
res.send(this.cache['6m']);
|
res.send(this.cache['6m']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get1YStatistics(req, res) {
|
public get1YStatistics(req: Request, res: Response) {
|
||||||
res.send(this.cache['1y']);
|
res.send(this.cache['1y']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getRecommendedFees(req, res) {
|
public async getRecommendedFees(req: Request, res: Response) {
|
||||||
const result = feeApi.getRecommendedFee();
|
const result = feeApi.getRecommendedFee();
|
||||||
res.send(result);
|
res.send(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getMempoolBlocks(req, res) {
|
public getMempoolBlocks(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const result = await mempoolBlocks.getMempoolBlocks();
|
const result = mempoolBlocks.getMempoolBlocks();
|
||||||
res.send(result);
|
res.send(result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
res.status(500).send(e.message);
|
res.status(500).send(e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTransactionTimes(req, res) {
|
public getTransactionTimes(req: Request, res: Response) {
|
||||||
if (!Array.isArray(req.query.txId)) {
|
if (!Array.isArray(req.query.txId)) {
|
||||||
res.status(500).send('Not an array');
|
res.status(500).send('Not an array');
|
||||||
return;
|
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);
|
const times = mempool.getFirstSeenForTransactions(txIds);
|
||||||
res.send(times);
|
res.send(times);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getBackendInfo(req, res) {
|
public getBackendInfo(req: Request, res: Response) {
|
||||||
res.send(backendInfo.getBackendInfo());
|
res.send(backendInfo.getBackendInfo());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="block-size">{{ projectedBlock.blockSize | bytes: 2 }}</div>
|
<div class="block-size">{{ projectedBlock.blockSize | bytes: 2 }}</div>
|
||||||
<div class="transaction-count">{{ projectedBlock.nTx | number }} transactions</div>
|
<div class="transaction-count">{{ projectedBlock.nTx | number }} transactions</div>
|
||||||
<div class="time-difference" *ngIf="projectedBlock.blockVSize < 1000000">
|
<div class="time-difference" *ngIf="projectedBlock.blockVSize <= 1000000; else mergedBlock">
|
||||||
<ng-template [ngIf]="network === 'liquid'" [ngIfElse]="timeDiffMainnet">
|
<ng-template [ngIf]="network === 'liquid'" [ngIfElse]="timeDiffMainnet">
|
||||||
In < {{ 1 * i + 1 }} minute
|
In < {{ 1 * i + 1 }} minute
|
||||||
</ng-template>
|
</ng-template>
|
||||||
@ -17,8 +17,8 @@
|
|||||||
In ~{{ 10 * i + 10 }} minutes
|
In ~{{ 10 * i + 10 }} minutes
|
||||||
</ng-template>
|
</ng-template>
|
||||||
</div>
|
</div>
|
||||||
<ng-template [ngIf]="i === mempoolBlocks.length - 1 && projectedBlock.blockVSize >= 1000000">
|
<ng-template #mergedBlock>
|
||||||
<div class="time-difference">+{{ projectedBlock.blockVSize / 1000000 | ceil }} blocks</div>
|
<div class="time-difference"><b>({{ projectedBlock.blockVSize / 1000000 | ceil }} blocks)</b></div>
|
||||||
</ng-template>
|
</ng-template>
|
||||||
</div>
|
</div>
|
||||||
<span class="animated-border"></span>
|
<span class="animated-border"></span>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user