Handle stale smoothed mempool block updates

This commit is contained in:
Mononaut 2024-01-08 14:42:00 +00:00
parent 5f66a95402
commit fc8eca4c26
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E

View File

@ -35,6 +35,7 @@ export class MempoolBlockOverviewComponent implements OnInit, OnDestroy, OnChang
blockSub: Subscription; blockSub: Subscription;
rateLimit = 1000; rateLimit = 1000;
private lastEventTime = Date.now() - this.rateLimit; private lastEventTime = Date.now() - this.rateLimit;
private subId = 0;
firstLoad: boolean = true; firstLoad: boolean = true;
@ -59,23 +60,30 @@ export class MempoolBlockOverviewComponent implements OnInit, OnDestroy, OnChang
this.stateService.mempoolBlockTransactions$, this.stateService.mempoolBlockTransactions$,
this.stateService.mempoolBlockDelta$, this.stateService.mempoolBlockDelta$,
).pipe( ).pipe(
concatMap(event => { concatMap(update => {
const now = Date.now(); const now = Date.now();
const timeSinceLastEvent = now - this.lastEventTime; const timeSinceLastEvent = now - this.lastEventTime;
this.lastEventTime = Math.max(now, this.lastEventTime + this.rateLimit); this.lastEventTime = Math.max(now, this.lastEventTime + this.rateLimit);
const subId = this.subId;
// If time since last event is less than X seconds, delay this event // If time since last event is less than X seconds, delay this event
if (timeSinceLastEvent < this.rateLimit) { if (timeSinceLastEvent < this.rateLimit) {
return timer(this.rateLimit - timeSinceLastEvent).pipe( return timer(this.rateLimit - timeSinceLastEvent).pipe(
// Emit the event after the timer // Emit the event after the timer
map(() => event) map(() => ({ update, subId }))
); );
} else { } else {
// If enough time has passed, emit the event immediately // If enough time has passed, emit the event immediately
return of(event); return of({ update, subId });
} }
}) })
).subscribe((update) => { ).subscribe(({ update, subId }) => {
// discard stale updates after a block transition
if (subId !== this.subId) {
return;
}
// process update
if (update['added']) { if (update['added']) {
// delta // delta
this.updateBlock(update as MempoolBlockDelta); this.updateBlock(update as MempoolBlockDelta);
@ -122,6 +130,7 @@ export class MempoolBlockOverviewComponent implements OnInit, OnDestroy, OnChang
ngOnChanges(changes): void { ngOnChanges(changes): void {
if (changes.index) { if (changes.index) {
this.subId++;
this.firstLoad = true; this.firstLoad = true;
if (this.blockGraph) { if (this.blockGraph) {
this.blockGraph.clear(changes.index.currentValue > changes.index.previousValue ? this.chainDirection : this.poolDirection); this.blockGraph.clear(changes.index.currentValue > changes.index.previousValue ? this.chainDirection : this.poolDirection);