mempool/frontend/src/app/services/websocket.service.ts

399 lines
12 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
import { WebsocketResponse } from '../interfaces/websocket.interface';
import { StateService } from './state.service';
import { Transaction } from '../interfaces/electrs.interface';
2020-03-06 13:47:04 +07:00
import { Subscription } from 'rxjs';
2020-11-07 04:30:52 +07:00
import { ApiService } from './api.service';
import { take } from 'rxjs/operators';
import { TransferState, makeStateKey } from '@angular/platform-browser';
import { CacheService } from './cache.service';
2023-05-01 18:01:07 -06:00
const OFFLINE_RETRY_AFTER_MS = 2000;
const OFFLINE_PING_CHECK_AFTER_MS = 30000;
const EXPECT_PING_RESPONSE_AFTER_MS = 5000;
const initData = makeStateKey('/api/v1/init-data');
@Injectable({
providedIn: 'root'
})
export class WebsocketService {
private webSocketProtocol = (document.location.protocol === 'https:') ? 'wss:' : 'ws:';
private webSocketUrl = this.webSocketProtocol + '//' + document.location.hostname + ':' + document.location.port + '{network}/api/v1/ws';
private websocketSubject: WebSocketSubject<WebsocketResponse>;
private goneOffline = false;
private lastWant: string | null = null;
private isTrackingTx = false;
private trackingTxId: string;
private isTrackingMempoolBlock = false;
2022-12-14 16:51:53 -06:00
private isTrackingRbf = false;
2023-07-14 16:08:57 +09:00
private isTrackingRbfSummary = false;
private trackingMempoolBlock: number;
private latestGitCommit = '';
private onlineCheckTimeout: number;
private onlineCheckTimeoutTwo: number;
2020-03-06 13:47:04 +07:00
private subscription: Subscription;
2020-05-10 12:31:57 +07:00
private network = '';
constructor(
private stateService: StateService,
2020-11-07 04:30:52 +07:00
private apiService: ApiService,
private transferState: TransferState,
private cacheService: CacheService,
) {
if (!this.stateService.isBrowser) {
// @ts-ignore
this.websocketSubject = { next: () => {}};
2020-11-07 04:30:52 +07:00
this.stateService.isLoadingWebSocket$.next(false);
this.apiService.getInitData$()
.pipe(take(1))
.subscribe((response) => this.handleResponse(response));
} else {
this.network = this.stateService.network === 'bisq' && !this.stateService.env.BISQ_SEPARATE_BACKEND ? '' : this.stateService.network;
this.websocketSubject = webSocket<WebsocketResponse>(this.webSocketUrl.replace('{network}', this.network ? '/' + this.network : ''));
const theInitData = this.transferState.get<any>(initData, null);
if (theInitData) {
this.handleResponse(theInitData.body);
this.startSubscription(false, true);
} else {
this.startSubscription();
}
2020-11-07 04:30:52 +07:00
this.stateService.networkChanged$.subscribe((network) => {
if (network === 'bisq' && !this.stateService.env.BISQ_SEPARATE_BACKEND) {
2020-11-07 04:30:52 +07:00
network = '';
}
if (network === this.network) {
return;
}
this.network = network;
clearTimeout(this.onlineCheckTimeout);
clearTimeout(this.onlineCheckTimeoutTwo);
this.stateService.resetChainTip();
2020-11-07 04:30:52 +07:00
this.websocketSubject.complete();
this.subscription.unsubscribe();
this.websocketSubject = webSocket<WebsocketResponse>(
this.webSocketUrl.replace('{network}', this.network ? '/' + this.network : '')
);
2020-11-07 04:30:52 +07:00
this.startSubscription();
});
}
}
startSubscription(retrying = false, hasInitData = false) {
if (!hasInitData) {
this.stateService.isLoadingWebSocket$.next(true);
this.websocketSubject.next({'action': 'init'});
}
if (retrying) {
this.stateService.connectionState$.next(1);
}
2020-03-06 13:47:04 +07:00
this.subscription = this.websocketSubject
.subscribe((response: WebsocketResponse) => {
this.stateService.isLoadingWebSocket$.next(false);
2020-11-07 04:30:52 +07:00
this.handleResponse(response);
if (this.goneOffline === true) {
this.goneOffline = false;
if (this.lastWant) {
this.want(JSON.parse(this.lastWant), true);
}
if (this.isTrackingTx) {
this.startMultiTrackTransaction(this.trackingTxId);
}
if (this.isTrackingMempoolBlock) {
this.startTrackMempoolBlock(this.trackingMempoolBlock);
}
this.stateService.connectionState$.next(2);
}
if (this.stateService.connectionState$.value !== 2) {
this.stateService.connectionState$.next(2);
}
this.startOnlineCheck();
},
(err: Error) => {
console.log(err);
2023-05-01 18:01:07 -06:00
console.log(`WebSocket error`);
this.goOffline();
});
}
startTrackTransaction(txId: string) {
if (this.isTrackingTx) {
return;
}
this.websocketSubject.next({ 'track-tx': txId });
this.isTrackingTx = true;
this.trackingTxId = txId;
}
startMultiTrackTransaction(txId: string) {
this.websocketSubject.next({ 'track-tx': txId, 'watch-mempool': true });
this.isTrackingTx = true;
this.trackingTxId = txId;
}
stopTrackingTransaction() {
if (!this.isTrackingTx) {
return;
}
this.websocketSubject.next({ 'track-tx': 'stop' });
this.isTrackingTx = false;
}
startTrackAddress(address: string) {
this.websocketSubject.next({ 'track-address': address });
}
stopTrackingAddress() {
this.websocketSubject.next({ 'track-address': 'stop' });
}
2020-04-28 17:10:31 +07:00
startTrackAsset(asset: string) {
this.websocketSubject.next({ 'track-asset': asset });
}
stopTrackingAsset() {
this.websocketSubject.next({ 'track-asset': 'stop' });
}
startTrackMempoolBlock(block: number) {
this.websocketSubject.next({ 'track-mempool-block': block });
this.isTrackingMempoolBlock = true
this.trackingMempoolBlock = block
}
stopTrackMempoolBlock() {
this.websocketSubject.next({ 'track-mempool-block': -1 });
this.isTrackingMempoolBlock = false
}
2022-12-14 16:51:53 -06:00
startTrackRbf(mode: 'all' | 'fullRbf') {
this.websocketSubject.next({ 'track-rbf': mode });
this.isTrackingRbf = true;
}
stopTrackRbf() {
this.websocketSubject.next({ 'track-rbf': 'stop' });
this.isTrackingRbf = false;
}
2023-07-14 16:08:57 +09:00
startTrackRbfSummary() {
this.websocketSubject.next({ 'track-rbf-summary': true });
this.isTrackingRbfSummary = true;
}
stopTrackRbfSummary() {
this.websocketSubject.next({ 'track-rbf-summary': false });
this.isTrackingRbfSummary = false;
}
startTrackBisqMarket(market: string) {
this.websocketSubject.next({ 'track-bisq-market': market });
}
stopTrackingBisqMarket() {
this.websocketSubject.next({ 'track-bisq-market': 'stop' });
}
fetchStatistics(historicalDate: string) {
this.websocketSubject.next({ historicalDate });
}
want(data: string[], force = false) {
if (!this.stateService.isBrowser) {
2020-11-07 04:30:52 +07:00
return;
}
if (JSON.stringify(data) === this.lastWant && !force) {
2020-09-26 22:46:26 +07:00
return;
}
2020-02-17 20:39:20 +07:00
this.websocketSubject.next({action: 'want', data: data});
this.lastWant = JSON.stringify(data);
}
2020-03-13 18:00:34 +07:00
goOffline() {
2023-05-01 18:01:07 -06:00
const retryDelay = OFFLINE_RETRY_AFTER_MS + (Math.random() * OFFLINE_RETRY_AFTER_MS);
console.log(`trying to reconnect websocket in ${retryDelay} seconds`);
this.goneOffline = true;
this.stateService.connectionState$.next(0);
window.setTimeout(() => {
this.startSubscription(true);
2023-05-01 18:01:07 -06:00
}, retryDelay);
}
startOnlineCheck() {
clearTimeout(this.onlineCheckTimeout);
clearTimeout(this.onlineCheckTimeoutTwo);
this.onlineCheckTimeout = window.setTimeout(() => {
this.websocketSubject.next({action: 'ping'});
this.onlineCheckTimeoutTwo = window.setTimeout(() => {
if (!this.goneOffline) {
2023-05-01 18:01:07 -06:00
console.log('WebSocket response timeout, force closing');
this.websocketSubject.complete();
2020-03-06 13:47:04 +07:00
this.subscription.unsubscribe();
this.goOffline();
}
}, EXPECT_PING_RESPONSE_AFTER_MS);
}, OFFLINE_PING_CHECK_AFTER_MS);
}
2020-11-07 04:30:52 +07:00
handleResponse(response: WebsocketResponse) {
let reinitBlocks = false;
2020-11-07 04:30:52 +07:00
if (response.blocks && response.blocks.length) {
const blocks = response.blocks;
2023-07-08 01:07:06 -04:00
this.stateService.resetBlocks(blocks);
const maxHeight = blocks.reduce((max, block) => Math.max(max, block.height), this.stateService.latestBlockHeight);
this.stateService.updateChainTip(maxHeight);
2020-11-07 04:30:52 +07:00
}
if (response.tx) {
this.stateService.mempoolTransactions$.next(response.tx);
}
if (response['txPosition']) {
this.stateService.mempoolTxPosition$.next(response['txPosition']);
}
2020-11-07 04:30:52 +07:00
if (response.block) {
if (response.block.height === this.stateService.latestBlockHeight + 1) {
this.stateService.updateChainTip(response.block.height);
2023-07-08 01:07:06 -04:00
this.stateService.addBlock(response.block);
this.stateService.txConfirmed$.next([response.txConfirmed, response.block]);
} else if (response.block.height > this.stateService.latestBlockHeight + 1) {
reinitBlocks = true;
2020-11-07 04:30:52 +07:00
}
if (response.txConfirmed) {
this.isTrackingTx = false;
}
}
if (response.conversions) {
this.stateService.conversions$.next(response.conversions);
}
if (response.rbfTransaction) {
this.stateService.txReplaced$.next(response.rbfTransaction);
}
2022-12-14 08:49:35 -06:00
if (response.rbfInfo) {
this.stateService.txRbfInfo$.next(response.rbfInfo);
}
2022-12-14 16:51:53 -06:00
if (response.rbfLatest) {
this.stateService.rbfLatest$.next(response.rbfLatest);
}
2023-07-14 16:08:57 +09:00
if (response.rbfLatestSummary) {
this.stateService.rbfLatestSummary$.next(response.rbfLatestSummary);
}
2022-03-08 14:49:25 +01:00
if (response.txReplaced) {
this.stateService.txReplaced$.next(response.txReplaced);
}
2020-11-07 04:30:52 +07:00
if (response['mempool-blocks']) {
this.stateService.mempoolBlocks$.next(response['mempool-blocks']);
}
if (response.transactions) {
response.transactions.forEach((tx) => this.stateService.transactions$.next(tx));
}
if (response['bsq-price']) {
this.stateService.bsqPrice$.next(response['bsq-price']);
}
2022-03-06 18:27:13 +01:00
if (response.utxoSpent) {
this.stateService.utxoSpent$.next(response.utxoSpent);
2022-03-06 18:27:13 +01:00
}
if (response.da) {
this.stateService.difficultyAdjustment$.next(response.da);
}
if (response.fees) {
this.stateService.recommendedFees$.next(response.fees);
}
if (response.backendInfo) {
this.stateService.backendInfo$.next(response.backendInfo);
2020-11-07 04:30:52 +07:00
if (!this.latestGitCommit) {
this.latestGitCommit = response.backendInfo.gitCommit;
2020-11-07 04:30:52 +07:00
} else {
if (this.latestGitCommit !== response.backendInfo.gitCommit) {
2020-11-07 04:30:52 +07:00
setTimeout(() => {
window.location.reload();
}, Math.floor(Math.random() * 60000) + 60000);
}
}
}
if (response['address-transactions']) {
response['address-transactions'].forEach((addressTransaction: Transaction) => {
this.stateService.mempoolTransactions$.next(addressTransaction);
});
}
if (response['block-transactions']) {
response['block-transactions'].forEach((addressTransaction: Transaction) => {
this.stateService.blockTransactions$.next(addressTransaction);
});
}
if (response['projected-block-transactions']) {
if (response['projected-block-transactions'].index == this.trackingMempoolBlock) {
if (response['projected-block-transactions'].blockTransactions) {
this.stateService.mempoolBlockTransactions$.next(response['projected-block-transactions'].blockTransactions);
} else if (response['projected-block-transactions'].delta) {
this.stateService.mempoolBlockDelta$.next(response['projected-block-transactions'].delta);
}
}
}
2020-11-07 04:30:52 +07:00
if (response['live-2h-chart']) {
this.stateService.live2Chart$.next(response['live-2h-chart']);
}
if (response.loadingIndicators) {
this.stateService.loadingIndicators$.next(response.loadingIndicators);
2023-07-24 16:22:35 +09:00
if (response.loadingIndicators.mempool != null && response.loadingIndicators.mempool < 100) {
this.stateService.isLoadingMempool$.next(true);
} else {
this.stateService.isLoadingMempool$.next(false);
}
}
2020-11-07 04:30:52 +07:00
if (response.mempoolInfo) {
this.stateService.mempoolInfo$.next(response.mempoolInfo);
}
if (response.vBytesPerSecond !== undefined) {
this.stateService.vbytesPerSecond$.next(response.vBytesPerSecond);
}
if (response.previousRetarget !== undefined) {
this.stateService.previousRetarget$.next(response.previousRetarget);
}
2020-11-07 04:30:52 +07:00
if (response['git-commit']) {
this.stateService.backendInfo$.next(response['git-commit']);
2020-11-07 04:30:52 +07:00
}
if (reinitBlocks) {
this.websocketSubject.next({'refresh-blocks': true});
}
2020-11-07 04:30:52 +07:00
}
}