Merge pull request #1496 from mempool/nymkappa/bugfix/dont-assume-init-block-count
Mining stats does not depends on the websocket blocks number anymore
This commit is contained in:
@@ -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<any>;
|
||||
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,
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -119,6 +119,8 @@ export interface BlockExtended extends Block {
|
||||
}
|
||||
|
||||
export interface RewardStats {
|
||||
startBlock: number;
|
||||
endBlock: number;
|
||||
totalReward: number;
|
||||
totalFee: number;
|
||||
totalTx: number;
|
||||
|
||||
Reference in New Issue
Block a user