Bugfix: Change mempool block time precision. (#650)

* Fix time precision.

* Fix rounding numbers only for minutes range.
Fix reflected avg time to ETA transactions.

* Fix now variable update.
This commit is contained in:
Miguel Medeiros 2021-07-24 19:26:29 -03:00 committed by GitHub
parent 11f5e99187
commit f6a889298c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 9 deletions

View file

@ -77,7 +77,6 @@ export class MempoolBlocksComponent implements OnInit, OnDestroy {
this.mempoolBlocks = this.reduceMempoolBlocksToFitScreen(JSON.parse(stringifiedBlocks)); this.mempoolBlocks = this.reduceMempoolBlocksToFitScreen(JSON.parse(stringifiedBlocks));
this.updateMempoolBlockStyles(); this.updateMempoolBlockStyles();
this.calculateTransactionPosition(); this.calculateTransactionPosition();
this.now = new Date().getTime();
return this.mempoolBlocks; return this.mempoolBlocks;
}) })
); );
@ -90,6 +89,7 @@ export class MempoolBlocksComponent implements OnInit, OnDestroy {
this.stateService.lastDifficultyAdjustment$ this.stateService.lastDifficultyAdjustment$
])), ])),
map(([block, DATime]) => { map(([block, DATime]) => {
this.now = new Date().getTime();
const now = new Date().getTime() / 1000; const now = new Date().getTime() / 1000;
const diff = now - DATime; const diff = now - DATime;
const blocksInEpoch = block.height % 2016; const blocksInEpoch = block.height % 2016;

View file

@ -56,7 +56,7 @@ export class TimeSinceComponent implements OnInit, OnChanges, OnDestroy {
if (seconds < 60) { if (seconds < 60) {
return $localize`:@@date-base.just-now:Just now`; return $localize`:@@date-base.just-now:Just now`;
} }
let counter; let counter: number;
for (const i in this.intervals) { for (const i in this.intervals) {
if (this.intervals.hasOwnProperty(i)) { if (this.intervals.hasOwnProperty(i)) {
counter = Math.floor(seconds / this.intervals[i]); counter = Math.floor(seconds / this.intervals[i]);

View file

@ -56,7 +56,7 @@ export class TimeSpanComponent implements OnInit, OnChanges, OnDestroy {
if (seconds < 60) { if (seconds < 60) {
return $localize`:@@date-base.just-now:Just now`; return $localize`:@@date-base.just-now:Just now`;
} }
let counter; let counter: number;
for (const i in this.intervals) { for (const i in this.intervals) {
if (this.intervals.hasOwnProperty(i)) { if (this.intervals.hasOwnProperty(i)) {
counter = Math.floor(seconds / this.intervals[i]); counter = Math.floor(seconds / this.intervals[i]);

View file

@ -58,16 +58,20 @@ export class TimeUntilComponent implements OnInit, OnChanges, OnDestroy {
} }
calculate() { calculate() {
const seconds = Math.floor((+new Date(this.time) - +new Date()) / 1000); const seconds = (+new Date(this.time) - +new Date()) / 1000;
if (seconds < 60) { if (seconds < 60) {
const dateStrings = dates(1); const dateStrings = dates(1);
return $localize`:@@time-until:In ~${dateStrings.i18nMinute}:DATE:`; return $localize`:@@time-until:In ~${dateStrings.i18nMinute}:DATE:`;
} }
let counter; let counter: number;
for (const i in this.intervals) { for (const i in this.intervals) {
if (this.intervals.hasOwnProperty(i)) { if (this.intervals.hasOwnProperty(i)) {
if (i === 'minute') {
counter = Math.round(seconds / this.intervals[i]); counter = Math.round(seconds / this.intervals[i]);
} else {
counter = Math.floor(seconds / this.intervals[i]);
}
const dateStrings = dates(counter); const dateStrings = dates(counter);
if (counter > 0) { if (counter > 0) {
if (counter === 1) { if (counter === 1) {

View file

@ -117,10 +117,10 @@
</ng-template> </ng-template>
<ng-template #belowBlockLimit> <ng-template #belowBlockLimit>
<ng-template [ngIf]="network === 'liquid'" [ngIfElse]="timeEstimateDefault"> <ng-template [ngIf]="network === 'liquid'" [ngIfElse]="timeEstimateDefault">
<ng-container *ngTemplateOutlet="1 * txInBlockIndex + 1 === 1 ? nextBlockEta : nextBlockEtaPlural; context:{ $implicit: 1 * txInBlockIndex + 1 }"></ng-container>&nbsp;<i><span class="symbol">(<ng-container *ngTemplateOutlet="txInBlockIndex === 0 ? blocksSingular : blocksPlural; context: {$implicit: txInBlockIndex + 1 }"></ng-container>)</span></i> <app-time-until [time]="(60 * 1000 * txInBlockIndex) + now" [fastRender]="false" [fixedRender]="true"></app-time-until>
</ng-template> </ng-template>
<ng-template #timeEstimateDefault> <ng-template #timeEstimateDefault>
<ng-container *ngTemplateOutlet="nextBlockEtaPlural; context:{ $implicit: 10 * txInBlockIndex + 10 }"></ng-container>&nbsp;<i><span class="symbol">(<ng-container *ngTemplateOutlet="txInBlockIndex === 0 ? blocksSingular : blocksPlural; context: {$implicit: txInBlockIndex + 1 }"></ng-container>)</span></i> <app-time-until *ngIf="(timeAvg$ | async) as timeAvg;" [time]="(timeAvg * txInBlockIndex) + now + timeAvg" [fastRender]="false" [fixedRender]="true"></app-time-until>
</ng-template> </ng-template>
</ng-template> </ng-template>
</ng-template> </ng-template>

View file

@ -7,9 +7,10 @@ import {
catchError, catchError,
retryWhen, retryWhen,
delay, delay,
map
} from 'rxjs/operators'; } from 'rxjs/operators';
import { Transaction, Block } from '../../interfaces/electrs.interface'; import { Transaction, Block } from '../../interfaces/electrs.interface';
import { of, merge, Subscription, Observable, Subject } from 'rxjs'; import { of, merge, Subscription, Observable, Subject, timer, combineLatest, } from 'rxjs';
import { StateService } from '../../services/state.service'; import { StateService } from '../../services/state.service';
import { WebsocketService } from '../../services/websocket.service'; import { WebsocketService } from '../../services/websocket.service';
import { AudioService } from 'src/app/services/audio.service'; import { AudioService } from 'src/app/services/audio.service';
@ -40,6 +41,8 @@ export class TransactionComponent implements OnInit, OnDestroy {
showCpfpDetails = false; showCpfpDetails = false;
fetchCpfp$ = new Subject<string>(); fetchCpfp$ = new Subject<string>();
commitments: Map<any, any>; commitments: Map<any, any>;
now = new Date().getTime();
timeAvg$: Observable<number>;
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
@ -57,6 +60,33 @@ export class TransactionComponent implements OnInit, OnDestroy {
(network) => (this.network = network) (network) => (this.network = network)
); );
this.timeAvg$ = timer(0, 1000)
.pipe(
switchMap(() => combineLatest([
this.stateService.blocks$.pipe(map(([block]) => block)),
this.stateService.lastDifficultyAdjustment$
])),
map(([block, DATime]) => {
this.now = new Date().getTime();
const now = new Date().getTime() / 1000;
const diff = now - DATime;
const blocksInEpoch = block.height % 2016;
let difficultyChange = 0;
if (blocksInEpoch > 0) {
difficultyChange = (600 / (diff / blocksInEpoch ) - 1) * 100;
}
const timeAvgDiff = difficultyChange * 0.1;
let timeAvgMins = 10;
if (timeAvgDiff > 0 ){
timeAvgMins -= Math.abs(timeAvgDiff);
} else {
timeAvgMins += Math.abs(timeAvgDiff);
}
return timeAvgMins * 60 * 1000;
})
);
this.fetchCpfpSubscription = this.fetchCpfp$ this.fetchCpfpSubscription = this.fetchCpfp$
.pipe( .pipe(
switchMap((txId) => switchMap((txId) =>