From c3a3289fcfdb05ab19230eb3058c351449a89cc6 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Fri, 1 Apr 2022 12:41:25 +0900 Subject: [PATCH] Mining stats does not depends on the websocket blocks number anymore --- backend/src/repositories/BlocksRepository.ts | 10 ++-- .../reward-stats/reward-stats.component.ts | 46 ++++++++++++------- .../src/app/interfaces/node-api.interface.ts | 2 + 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index 5b253d3a0..b426e77d2 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -386,10 +386,14 @@ class BlocksRepository { connection = await DB.getConnection(); // We need to use a subquery - const query = `SELECT SUM(reward) as totalReward, SUM(fees) as totalFee, SUM(tx_count) as totalTx - FROM (SELECT reward, fees, tx_count FROM blocks ORDER by height DESC LIMIT ${blockCount}) as sub`; + const query = ` + SELECT MIN(height) as startBlock, MAX(height) as endBlock, SUM(reward) as totalReward, SUM(fees) as totalFee, SUM(tx_count) as totalTx + FROM + (SELECT height, reward, fees, tx_count FROM blocks + ORDER by height DESC + LIMIT ?) as sub`; - const [rows]: any = await connection.query(query); + const [rows]: any = await connection.query(query, [blockCount]); connection.release(); return rows[0]; diff --git a/frontend/src/app/components/reward-stats/reward-stats.component.ts b/frontend/src/app/components/reward-stats/reward-stats.component.ts index dd466985e..582796fa1 100644 --- a/frontend/src/app/components/reward-stats/reward-stats.component.ts +++ b/frontend/src/app/components/reward-stats/reward-stats.component.ts @@ -1,6 +1,6 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; -import { Observable } from 'rxjs'; -import { map, skip, switchMap } from 'rxjs/operators'; +import { concat, Observable } from 'rxjs'; +import { map, switchMap, tap } from 'rxjs/operators'; import { ApiService } from 'src/app/services/api.service'; import { StateService } from 'src/app/services/state.service'; @@ -12,25 +12,39 @@ import { StateService } from 'src/app/services/state.service'; }) export class RewardStatsComponent implements OnInit { public $rewardStats: Observable; + private lastBlockHeight: number; constructor(private apiService: ApiService, private stateService: StateService) { } ngOnInit(): void { - this.$rewardStats = this.stateService.blocks$ + this.$rewardStats = concat( + // We fetch the latest reward stats when the page load and + // wait for the API response before listening to websocket blocks + this.apiService.getRewardStats$() + .pipe( + tap((stats) => { + this.lastBlockHeight = stats.endBlock; + }) + ), + // Or when we receive a newer block, newer than the latest reward stats api call + this.stateService.blocks$ + .pipe( + switchMap((block) => { + if (block[0].height <= this.lastBlockHeight) { + return []; // Return an empty stream so the last pipe is not executed + } + this.lastBlockHeight = block[0].height; + return this.apiService.getRewardStats$(); + }) + ) + ) .pipe( - // (we always receives some blocks at start so only trigger for the last one) - skip(this.stateService.env.MEMPOOL_BLOCKS_AMOUNT - 1), - switchMap(() => { - return this.apiService.getRewardStats$() - .pipe( - map((stats) => { - return { - totalReward: stats.totalReward, - rewardPerTx: stats.totalReward / stats.totalTx, - feePerTx: stats.totalFee / stats.totalTx, - }; - }) - ); + map((stats) => { + return { + totalReward: stats.totalReward, + rewardPerTx: stats.totalReward / stats.totalTx, + feePerTx: stats.totalFee / stats.totalTx, + }; }) ); } diff --git a/frontend/src/app/interfaces/node-api.interface.ts b/frontend/src/app/interfaces/node-api.interface.ts index 022e215d0..bcda5ff4c 100644 --- a/frontend/src/app/interfaces/node-api.interface.ts +++ b/frontend/src/app/interfaces/node-api.interface.ts @@ -119,6 +119,8 @@ export interface BlockExtended extends Block { } export interface RewardStats { + startBlock: number; + endBlock: number; totalReward: number; totalFee: number; totalTx: number;