From 3b554e3bb8184dc29caf9f25495b2ea1daa65314 Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Fri, 11 Oct 2024 00:42:30 +0200 Subject: [PATCH] Fix lint --- src/app/app.component.ts | 15 +- src/app/app.module.ts | 2 - .../services/daemon/daemon-data.service.ts | 6 +- src/app/core/services/daemon/daemon.client.ts | 667 ------------------ .../core/services/daemon/daemon.service.ts | 122 +++- src/app/pages/bans/bans.component.ts | 9 +- .../blockchain/blockchain.component.html | 14 +- .../pages/blockchain/blockchain.component.ts | 16 +- .../hard-fork-info.component.ts | 2 + src/app/pages/logs/logs.component.html | 620 ++++++++-------- src/app/pages/logs/logs.component.ts | 12 +- src/app/pages/logs/logs.service.ts | 5 +- src/app/pages/mining/mining.component.html | 12 +- src/app/pages/mining/mining.component.ts | 20 +- src/app/pages/network/network.component.html | 2 +- src/app/pages/network/network.component.ts | 2 +- src/app/pages/outputs/outputs.component.html | 12 +- src/app/pages/outputs/outputs.component.ts | 19 +- src/app/pages/peers/peers.component.html | 4 +- src/app/pages/peers/peers.component.ts | 5 +- .../pages/settings/settings.component.html | 8 +- src/app/pages/settings/settings.component.ts | 16 +- .../transactions/transactions.component.html | 4 +- .../transactions/transactions.component.ts | 10 +- src/app/pages/version/version.component.html | 2 +- src/app/pages/version/version.component.ts | 6 +- .../daemon-not-running.component.ts | 27 +- .../components/navbar/navbar.component.ts | 3 + .../components/navbar/navbar.service.ts | 3 + .../components/sidebar/sidebar.component.ts | 12 +- src/common/AuxPoW.ts | 9 +- src/common/Ban.ts | 12 +- src/common/Block.ts | 11 +- src/common/BlockCount.ts | 2 +- src/common/BlockDetails.ts | 12 +- src/common/BlockHeader.ts | 45 +- src/common/BlockTemplate.ts | 28 +- src/common/BlockchainPruneInfo.ts | 4 +- src/common/Chain.ts | 16 +- src/common/CoinbaseTxSum.ts | 14 +- src/common/Connection.ts | 44 +- src/common/DaemonInfo.ts | 389 +++++----- src/common/DaemonSettings.ts | 6 +- src/common/FeeEstimate.ts | 6 +- src/common/HardForkInfo.ts | 18 +- src/common/HistogramEntry.ts | 8 +- src/common/MinerData.ts | 23 +- src/common/MinerTx.ts | 20 +- src/common/MiningStatus.ts | 45 +- src/common/NetStats.ts | 8 +- src/common/OutKey.ts | 10 +- src/common/Output.ts | 5 +- src/common/OutputDistribution.ts | 10 +- src/common/PeerInfo.ts | 10 +- src/common/PublicNode.ts | 8 +- src/common/Span.ts | 2 +- src/common/SpentKeyImage.ts | 8 +- src/common/SyncInfo.ts | 8 +- src/common/TxBacklogEntry.ts | 4 +- src/common/TxInfo.ts | 35 +- src/common/UnconfirmedTx.ts | 28 +- src/common/UpdateInfo.ts | 12 +- src/common/request/CheckUpdateRequest.ts | 2 +- src/common/request/DownloadUpdateRequest.ts | 2 +- .../request/GetAltBlockHashesRequest.ts | 4 +- src/common/request/GetBlockRequest.ts | 2 +- src/common/request/GetNetStatsRequest.ts | 2 +- src/common/request/GetPeerListRequest.ts | 2 +- src/common/request/GetPublicNodesRequest.ts | 2 +- .../GetTransactionPoolHashesBinaryRequest.ts | 2 +- .../GetTransactionPoolHashesRequest.ts | 2 +- .../request/GetTransactionPoolRequest.ts | 2 +- src/common/request/InPeersRequest.ts | 4 +- src/common/request/IsKeyImageSpentRequest.ts | 4 +- src/common/request/MiningStatusRequest.ts | 4 +- src/common/request/OutPeersRequest.ts | 4 +- src/common/request/SaveBcRequest.ts | 4 +- .../request/SendRawTransactionRequest.ts | 4 +- .../request/SetBootstrapDaemonRequest.ts | 4 +- src/common/request/SetLimitRequest.ts | 4 +- src/common/request/SetLogCategoriesRequest.ts | 4 +- src/common/request/SetLogHashRateRequest.ts | 2 +- src/common/request/SetLogLevelRequest.ts | 2 +- src/common/request/StartMiningRequest.ts | 4 +- src/common/request/StopDaemonRequest.ts | 4 +- src/common/request/StopMiningRequest.ts | 4 +- src/common/request/SubmitBlockRequest.ts | 2 +- src/common/request/index.ts | 1 + src/polyfills.ts | 6 +- 89 files changed, 1021 insertions(+), 1579 deletions(-) delete mode 100644 src/app/core/services/daemon/daemon.client.ts diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 66aa1c9..b1283ab 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -43,15 +43,18 @@ export class AppComponent { this.load(); } - private async load(): Promise { + private load(): void { this.loading = true; - try { - this.daemonRunning = await this.daemonService.isRunning(); - } - catch(error) { + this.daemonService.isRunning().then((running: boolean) => { + this.daemonRunning = running; + this.loading; + }).catch((error) => { console.error(error); - } + this.daemonRunning = false; + }).finally(() => { + this.loading = false; + }); this.loading = false; } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 7241ec2..6b463b7 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -18,11 +18,9 @@ import { BlockchainModule } from './pages/blockchain/blockchain.module'; import { AppComponent } from './app.component'; import { LoadComponent } from "./shared/components/load/load.component"; import { BansModule } from './pages/bans/bans.module'; -import { NavbarComponent } from "./shared/components/navbar/navbar.component"; import { MiningModule } from './pages/mining/mining.module'; import { TransactionsModule } from './pages/transactions/transactions.module'; import { OutputsModule } from './pages/outputs/outputs.module'; -import { SidebarComponent } from './shared/components'; import { SettingsModule } from './pages/settings/settings.module'; import { LogsModule } from './pages/logs/logs.module'; import { VersionModule } from './pages/version/version.module'; diff --git a/src/app/core/services/daemon/daemon-data.service.ts b/src/app/core/services/daemon/daemon-data.service.ts index 0576067..99647ec 100644 --- a/src/app/core/services/daemon/daemon-data.service.ts +++ b/src/app/core/services/daemon/daemon-data.service.ts @@ -225,7 +225,9 @@ export class DaemonDataService { } this._firstRefresh = true; this.refreshInterval = setInterval(() => { - this.refresh(); + this.refresh().then().catch((error: any) => { + console.error(error); + }); },this.refreshTimeoutMs); } @@ -343,6 +345,8 @@ export class DaemonDataService { if (firstRefresh) { this.daemonService.pruneBlockchain(true).then((info) => { this._isBlockchainPruned = info.pruned; + }).catch((error) => { + console.error(error); }); } this._gettingIsBlockchainPruned = false; diff --git a/src/app/core/services/daemon/daemon.client.ts b/src/app/core/services/daemon/daemon.client.ts deleted file mode 100644 index 0b7d17f..0000000 --- a/src/app/core/services/daemon/daemon.client.ts +++ /dev/null @@ -1,667 +0,0 @@ -import { HttpClient, HttpErrorResponse } from "@angular/common/http"; -import { CoreIsBusyError, MethodNotFoundError, JsonRPCRequest, RPCRequest, AddAuxPoWRequest, AddedAuxPow, AuxPoW, Ban, BannedRequest, Block, BlockchainPruneInfo, BlockCount, BlockHeader, BlockTemplate, CalculatePoWHashRequest, Chain, CoinbaseTxSum, Connection, DaemonInfo, DaemonVersion, FeeEstimate, FlushCacheRequest, FlushTxPoolRequest, GenerateBlocksRequest, GeneratedBlocks, GetAltBlockHashesRequest, GetAlternateChainsRequest, GetBansRequest, GetBlockCountRequest, GetBlockHashRequest, GetBlockHeaderByHashRequest, GetBlockHeaderByHeightRequest, GetBlockHeadersRangeRequest, GetBlockRequest, GetBlockTemplateRequest, GetCoinbaseTxSumRequest, GetConnectionsRequest, GetFeeEstimateRequest, GetInfoRequest, GetLastBlockHeaderRequest, GetMinerDataRequest, GetNetStatsRequest, GetOutputDistributionRequest, GetOutputHistogramRequest, GetOutsRequest, GetPublicNodesRequest, GetTransactionPoolHashesBinaryRequest, GetTransactionPoolHashesRequest, GetTxPoolBacklogRequest, GetVersionRequest, HardForkInfo, HardForkInfoRequest, HistogramEntry, InPeersRequest, IsKeyImageSpentRequest, MinerData, MiningStatus, MiningStatusRequest, NetStats, OutKey, OutPeersRequest, Output, OutputDistribution, PopBlocksRequest, PruneBlockchainRequest, PublicNode, RelayTxRequest, SaveBcRequest, SendRawTransactionRequest, SetBansRequest, SetBootstrapDaemonRequest, SetLimitRequest, StartMiningRequest, StopMiningRequest, SubmitBlockRequest, SyncInfo, SyncInfoRequest, TxBacklogEntry, TxInfo, UpdateInfo, UpdateRequest, StopDaemonRequest, EmptyRpcRequest, DaemonSettings } from "../../../../common"; -import { firstValueFrom } from "rxjs"; -import { ElectronService } from "../electron/electron.service"; -import { IDBPDatabase, openDB } from "idb"; - -export class DaemonClient { - private electronService: ElectronService; - private httpClient: HttpClient; - private url: string; - - private readonly versionApiUrl: string = 'https://api.github.com/repos/monero-project/monero/releases/latest'; - private dbName = 'DaemonSettingsDB'; - private storeName = 'settingsStore'; - private openDbPromise: Promise; - - public daemonRunning: boolean; - public settings: DaemonSettings; - - - private readonly headers: { [key: string]: string } = { - "Access-Control-Allow-Headers": "*", // this will allow all CORS requests - "Access-Control-Allow-Methods": 'POST,GET' // this states the allowed methods - }; - - constructor(httpClient: HttpClient, url: string, electronService: ElectronService) { - this.httpClient = httpClient; - this.url = url; - this.electronService = electronService; - this.daemonRunning = false; - this.openDbPromise = this.openDatabase(); - this.settings = this.loadSettings(); - } - - private async openDatabase(): Promise { - return openDB(this.dbName, 1, { - upgrade(db) { - // Crea un archivio (store) per i settings se non esiste giĆ  - if (!db.objectStoreNames.contains('settingsStore')) { - db.createObjectStore('settingsStore', { - keyPath: 'id', - autoIncrement: true, - }); - } - }, - }); - } - - public async saveSettings(settings: DaemonSettings): Promise { - const db = await this.openDbPromise; - await db.put(this.storeName, { id: 1, ...settings }); - this.settings = settings; - } - - public async getSettings(): Promise { - const db = await this.openDbPromise; - const result = await db.get(this.storeName, 1); - if (result) { - this.settings = DaemonSettings.parse(result); - } - else - { - this.settings = new DaemonSettings(); - } - - return this.settings; - } - - public async deleteSettings(): Promise { - const db = await this.openDbPromise; - await db.delete(this.storeName, 1); - } - - private loadSettings(): DaemonSettings { - const settings = new DaemonSettings(); - settings.testnet = true; - settings.fastBlockSync = true; - settings.pruneBlockchain = true; - settings.syncPrunedBlocks = true; - settings.confirmExternalBind = true; - settings.logLevel = 1; - settings.rpcAccessControlOrigins = "*"; - return settings; - } - - private raiseRpcError(error: { code: number, message: string }): void { - if (error.code == -9) { - throw new CoreIsBusyError(); - } - else if (error.code == -32601) { - throw new MethodNotFoundError(); - } - else - { - throw new Error(error.message); - } - - } - - private async delay(ms: number = 0): Promise { - await new Promise(f => setTimeout(f, ms)); - } - - private async get(uri: string): Promise<{[key: string]: any}> { - return await firstValueFrom<{ [key: string]: any }>(this.httpClient.get(`${uri}`,this.headers)); - } - - private async post(uri: string, params: {[key: string]: any} = {}): Promise<{[key: string]: any}> { - return await firstValueFrom<{ [key: string]: any }>(this.httpClient.post(`${uri}`, params, this.headers)); - } - - private async callRpc(request: RPCRequest): Promise<{ [key: string]: any }> { - try { - let method: string = ''; - - if (request instanceof JsonRPCRequest) { - method = 'json_rpc'; - } - else { - method = request.method; - } - - const response = await this.post(`${this.url}/${method}`, request.toDictionary()); - - if (response.error) { - this.raiseRpcError(response.error); - } - - return response; - } - catch (error) { - if (error instanceof HttpErrorResponse && error.status == 0) { - const wasRunning = this.daemonRunning; - this.daemonRunning = false; - - if (wasRunning) { - //this.onDaemonStart.emit(false); - } - } - - throw error; - } - } - - - public async startDaemon(): Promise { - if (await this.isRunning()) { - console.warn("Daemon already running"); - return false; - } - - if (!this.electronService.isElectron) { - console.error("Could not start monero daemon: not electron app"); - return false; - } - - console.log("Starting daemon"); - const settings = await this.getSettings(); - this.electronService.ipcRenderer.send('start-monerod', settings.toCommandOptions()); - - await this.delay(3000); - - if (await this.isRunning(true)) { - console.log("Daemon started"); - return true; - } - else - { - console.log("Daemon not started"); - return false; - } - } - - public async isRunning(force: boolean = false): Promise { - try { - if (!force && this.daemonRunning != undefined) { - return this.daemonRunning; - } - - await this.callRpc(new EmptyRpcRequest()); - } - catch(error) { - if (error instanceof MethodNotFoundError) { - this.daemonRunning = true; - return this.daemonRunning; - } - - console.error(error); - } - - this.daemonRunning = false; - return this.daemonRunning; - - } - - public async stopDaemon(): Promise { - if (!this.daemonRunning) { - console.warn("Daemon not running"); - return false; - } - - const response = await this.callRpc(new StopDaemonRequest()); - console.log(response); - - if (typeof response.status == 'string' && response.status != 'OK') { - throw new Error(`Could not stop daemon: ${response.status}`); - } - - this.daemonRunning = false; - - return true; - } - - //#region Request - - public async getBlock(heightOrHash: number | string, fillPowHash: boolean = false): Promise { - const response = await this.callRpc(new GetBlockRequest(heightOrHash, fillPowHash)); - - if (response.error) { - this.raiseRpcError(response.error); - } - - return Block.parse(response.result); - } - - public async getBlockCount(): Promise { - const response = await this.callRpc(new GetBlockCountRequest()); - - return BlockCount.parse(response.result); - } - - public async getBlockHash(blockHeight: number): Promise { - const response = await this.callRpc(new GetBlockHashRequest(blockHeight)); - - return response.result; - } - - public async getBlockTemplate(walletAddress: string, reserveSize: number) { - const response = await this.callRpc(new GetBlockTemplateRequest(walletAddress, reserveSize)); - - return BlockTemplate.parse(response.result); - } - - public async submitBlock(... blockBlobData: string[]): Promise { - const response = await this.callRpc(new SubmitBlockRequest(blockBlobData)); - - if (response.error) { - if (!response.message) { - throw new Error(`Error code: ${response.code}`); - } - - throw new Error(response.message); - } - } - - public async generateBlocks(amountOfBlocks: number, walletAddress: string, prevBlock: string = '', startingNonce: number): Promise { - const response = await this.callRpc(new GenerateBlocksRequest(amountOfBlocks, walletAddress, prevBlock, startingNonce)); - - return GeneratedBlocks.parse(response.result); - } - - public async getLastBlockHeader(fillPowHash: boolean = false): Promise { - const response = await this.callRpc(new GetLastBlockHeaderRequest(fillPowHash)); - - return BlockHeader.parse(response.block_header); - } - - public async getBlockHeaderByHash(hash: string, fillPowHash: boolean = false): Promise { - const response = await this.callRpc(new GetBlockHeaderByHashRequest(hash, fillPowHash)); - - return BlockHeader.parse(response.block_header); - } - - public async getBlockHeaderByHeight(height: number, fillPowHash: boolean = false): Promise { - const response = await this.callRpc(new GetBlockHeaderByHeightRequest(height, fillPowHash)); - - return BlockHeader.parse(response.block_header); - } - - public async getBlockHeadersRange(startHeight: number, endHeight: number, fillPowHash: boolean = false): Promise { - const response = await this.callRpc(new GetBlockHeadersRangeRequest(startHeight, endHeight, fillPowHash)); - const block_headers: any[] = response.block_headers; - const result: BlockHeader[] = []; - - block_headers.forEach((block_header: any) => result.push(BlockHeader.parse(block_header))); - - return result; - } - - public async getConnections(): Promise { - const response = await this.callRpc(new GetConnectionsRequest()); - const connections: any[] = response.connections; - const result: Connection[] = []; - - connections.forEach((connection: any) => result.push(Connection.parse(connection))) - - return result; - } - - public async getInfo(): Promise { - const response = await this.callRpc(new GetInfoRequest()); - - return DaemonInfo.parse(response.result); - } - - public async hardForkInfo(): Promise { - const response = await this.callRpc(new HardForkInfoRequest()); - - return HardForkInfo.parse(response.result); - } - - public async setBans(...bans: Ban[]) { - const response = await this.callRpc(new SetBansRequest(bans)); - - if (response.status != 'OK') { - throw new Error(`Error code: ${response.status}`); - } - } - - public async getBans(): Promise { - const response = await this.callRpc(new GetBansRequest()); - - if (response.error) { - this.raiseRpcError(response.error); - } - - const bans: any[] = response.bans; - const result: Ban[] = []; - - if (bans) bans.forEach((ban: any) => result.push(Ban.parse(ban))); - - return result; - } - - public async banned(address: string): Promise { - const response = await this.callRpc(new BannedRequest(address)); - const result = response.result; - - if (result.status != 'OK') { - throw new Error(`Error code: ${result.response}`); - } - - return new Ban(address, 0, result.banned, result.seconds); - } - - public async flushTxPool(... txIds: string[]): Promise { - const response = await this.callRpc(new FlushTxPoolRequest(txIds)); - - if (response.status != 'OK') { - throw new Error(`Error code: ${response.status}`); - } - } - - public async getOuts(outputs: Output[], getTxId: boolean): Promise { - const response = await this.callRpc(new GetOutsRequest(outputs, getTxId)); - - if (response.error) { - this.raiseRpcError(response.error); - } - - const _outkeys: any[] | undefined = response.outs; - const outkeys: OutKey[] = []; - - if (_outkeys) _outkeys.forEach((outkey) => outkeys.push(OutKey.parse(outkey))); - - return outkeys; - } - - public async getOutputHistogram(amounts: number[], minCount: number, maxCount: number, unlocked: boolean, recentCutoff: number): Promise { - const response = await this.callRpc(new GetOutputHistogramRequest(amounts, minCount, maxCount, unlocked, recentCutoff)); - - if (response.error) { - this.raiseRpcError(response.error); - } - - const entries: any[] = response.result.histogram; - const result: HistogramEntry[] = []; - - if (entries) entries.forEach((entry: any) => result.push(HistogramEntry.parse(entry))); - - return result; - } - - public async getOutputDistribution(amounts: number[], cumulative: boolean, fromHeight: number, toHeight: number): Promise { - const response = await this.callRpc(new GetOutputDistributionRequest(amounts, cumulative, fromHeight, toHeight)); - - if (response.error) { - this.raiseRpcError(response.error); - } - - const entries: any[] = response.result.distributions; - const distributions: OutputDistribution[] = []; - - if (entries) entries.forEach((entry) => distributions.push(OutputDistribution.parse(entry))); - - return distributions; - } - - public async syncInfo(): Promise { - const response = await this.callRpc(new SyncInfoRequest()); - - return SyncInfo.parse(response.result); - } - - public async getLatestVersion(): Promise { - const response = await this.get(this.versionApiUrl); - - if (typeof response.tag_name != 'string') { - throw new Error("Could not get tag name version"); - } - - if (typeof response.name != 'string') { - throw new Error("Could not get name version"); - } - - const nameComponents = response.name.split(","); - - if (nameComponents.length == 0) { - throw new Error("Could not get name"); - } - - const name = nameComponents[0]; - - return new DaemonVersion(0, true, `Monero '${name}' (${response.tag_name}-release)`); - } - - public async getVersion(dontUseRpc: boolean = false): Promise { - if(!dontUseRpc && this.daemonRunning) { - const response = await this.callRpc(new GetVersionRequest()); - - return DaemonVersion.parse(response.result); - } - else if (dontUseRpc) { - const monerodPath: string = ''; // TO DO get local monerod path - - return new Promise((resolve, reject) => { - this.electronService.ipcRenderer.on('on-monerod-version', (event, version: string) => { - resolve(DaemonVersion.parse(version)); - }); - - this.electronService.ipcRenderer.on('on-monerod-version-error', (event, version: string) => { - reject(version); - }); - - this.electronService.ipcRenderer.send('get-monerod-version', monerodPath); - }); - } - - throw new Error("Daemon not running"); - } - - public async getFeeEstimate(): Promise { - const response = await this.callRpc(new GetFeeEstimateRequest()); - - return FeeEstimate.parse(response.result); - } - - public async getAlternateChains(): Promise { - const response = await this.callRpc(new GetAlternateChainsRequest()); - const chains: any[] = response.result.chains ? response.result.chains : []; - const result: Chain[] = []; - - chains.forEach((chain: any) => result.push(Chain.parse(chain))); - - return result; - } - - public async getCoinbaseTxSum(height: number, count: number): Promise { - const response = await this.callRpc(new GetCoinbaseTxSumRequest(height, count)); - - if (response.error) { - this.raiseRpcError(response.error); - } - - return CoinbaseTxSum.parse(response.result); - } - - public async relayTx(... txIds: string[]): Promise { - const response = await this.callRpc(new RelayTxRequest(txIds)); - - if (response.result.status != 'OK') { - throw new Error(`Error code: ${response.result.status}`); - } - } - - public async getTxPoolBacklog(): Promise { - const response = await this.callRpc(new GetTxPoolBacklogRequest()); - - if (typeof response.status == 'string' && response.status != 'OK') { - throw new Error(`Error code: ${response.status}`) - } - - if (!response.bakclog && !response.result) { - return []; - } - - if (response.backlog) { - return TxBacklogEntry.fromBinary(response.backlog); - } - else if (response.result.backlog) return TxBacklogEntry.fromBinary(response.result.backlog); - - return []; - } - - public async pruneBlockchain(check: boolean = false): Promise { - const response = await this.callRpc(new PruneBlockchainRequest(check)); - - return BlockchainPruneInfo.parse(response.result); - } - - public async calculatePoWHash(majorVersion: number, height: number, blockBlob: string, seedHash: string): Promise { - const response = await this.callRpc(new CalculatePoWHashRequest(majorVersion, height, blockBlob, seedHash)); - - return response.result; - } - - public async flushCache(badTxs: boolean = false, badBlocks: boolean = false): Promise { - const response = await this.callRpc(new FlushCacheRequest(badTxs, badBlocks)); - - if(response.result.status != 'OK') { - throw new Error(`Error code: ${response.result.status}`); - } - } - - public async getMinerData(): Promise { - const response = await this.callRpc(new GetMinerDataRequest()); - - return MinerData.parse(response.result); - } - - public async AddAuxPoW(blockTemplateBlob: string, auxPoW: AuxPoW[]): Promise { - const response = await this.callRpc(new AddAuxPoWRequest(blockTemplateBlob, auxPoW)); - - return AddedAuxPow.parse(response.result); - } - - public async setBootstrapDaemon(address: string, username: string = '', password: string = '', proxy: string = ''): Promise { - const response = await this.callRpc(new SetBootstrapDaemonRequest(address, username, password, proxy)); - - if (typeof response.status == 'string' && response.status != 'OK') { - throw new Error(`Could not set bootstrap daemon: ${response.status}`); - } - } - - public async saveBc(): Promise { - const response = await this.callRpc(new SaveBcRequest()); - - if (typeof response.status == 'string' && response.status != 'OK') { - throw new Error(`Could not save blockchain: ${response.status}`); - } - } - - public async getAltBlockHashes(): Promise { - const response = await this.callRpc(new GetAltBlockHashesRequest()); - - return response.blks_hashes; - } - - public async isKeyImageSpent(...keyImages: string[]): Promise { - const response = await this.callRpc(new IsKeyImageSpentRequest(keyImages)); - - return response.spent_status; - } - - public async sendRawTransaction(txAsHex: string, doNotRelay: boolean = false): Promise { - const response = await this.callRpc(new SendRawTransactionRequest(txAsHex, doNotRelay)); - - return TxInfo.parse(response); - } - - public async startMining(doBackgroundMining: boolean, ignoreBattery: boolean, minerAddress: string, threadsCount: number): Promise { - const response = await this.callRpc(new StartMiningRequest(doBackgroundMining, ignoreBattery, minerAddress, threadsCount)); - - if (typeof response.status == 'string' && response.status != 'OK') { - throw new Error(`Could not start mining: ${response.status}`); - } - } - - public async stopMining(): Promise { - const response = await this.callRpc(new StopMiningRequest()); - - if (typeof response.status == 'string' && response.status != 'OK') { - throw new Error(`Could not stop mining: ${response.status}`); - } - } - - public async miningStatus(): Promise { - const response = await this.callRpc(new MiningStatusRequest()); - - return MiningStatus.parse(response); - } - - public async setLimit(limitDown: number, limitUp: number): Promise<{ limitDown: number, limitUp: number }> { - const response = await this.callRpc(new SetLimitRequest(limitDown, limitUp)); - - return { - limitDown: response.limit_down, - limitUp: response.limit_up - }; - } - - public async inPeers(inPeers: number): Promise { - const response = await this.callRpc(new InPeersRequest(inPeers)); - - return response.in_peers; - } - - public async outPeers(outPeers: number): Promise { - const response = await this.callRpc(new OutPeersRequest(outPeers)); - - return response.out_peers; - } - - public async getNetStats(): Promise { - const response = await this.callRpc(new GetNetStatsRequest()); - - return NetStats.parse(response); - } - - public async getPublicNodes(whites: boolean = true, grays: boolean = false, includeBlocked: boolean = false): Promise { - const response = await this.callRpc(new GetPublicNodesRequest(whites, grays, includeBlocked)); - - const _whites: any[] | undefined = response.whites; - const _grays: any[] | undefined = response.grays; - const nodes: PublicNode[] = []; - - if (_whites) _whites.forEach((white) => nodes.push(PublicNode.parse(white, 'white'))); - if (_grays) _grays.forEach((gray) => nodes.push(PublicNode.parse(gray, 'gray'))); - - return nodes; - } - - public async getTransactionPoolHashes(): Promise { - const response = await this.callRpc(new GetTransactionPoolHashesRequest()); - - return response.tx_hashes; - } - - public async getTransactionPoolHashesBinary(): Promise { - const response = await this.callRpc(new GetTransactionPoolHashesBinaryRequest()); - - return response.tx_hashes; - } - - public async popBlocks(nBlocks: number): Promise { - const response = await this.callRpc(new PopBlocksRequest(nBlocks)); - - return response.height; - } - - public async update(command: 'check' | 'download', path: string = ''): Promise { - const response = await this.callRpc(new UpdateRequest(command, path)); - - return UpdateInfo.parse(response); - } - - public async checkUpdate(): Promise { - return await this.update('check'); - } - - public async downloadUpdate(path: string = ''): Promise { - return await this.update('download', path); - } - - - //#endregion -} \ No newline at end of file diff --git a/src/app/core/services/daemon/daemon.service.ts b/src/app/core/services/daemon/daemon.service.ts index 0987029..e5f0740 100644 --- a/src/app/core/services/daemon/daemon.service.ts +++ b/src/app/core/services/daemon/daemon.service.ts @@ -118,11 +118,15 @@ export class DaemonService { if (this.electronService.isElectron) { this.electronService.ipcRenderer.on('monero-close', (event, code: number | null) => { + console.log(event); + console.log(code); this.onClose(); }); } else if (wdw.electronAPI && wdw.electronAPI.onMoneroClose) { wdw.electronAPI.onMoneroClose((event: any, code: number) => { + console.log(event); + console.log(code); this.onClose(); }); } @@ -213,7 +217,7 @@ export class DaemonService { return settings; } - private raiseRpcError(error: { code: number, message: string }): void { + private raiseRpcError(error: RpcError): void { if (error.code == -9) { throw new CoreIsBusyError(); @@ -254,7 +258,7 @@ export class DaemonService { const response = await this.post(`${this.url}/${method}`, request.toDictionary()); if (response.error) { - this.raiseRpcError(response.error); + this.raiseRpcError(response.error); } return response; @@ -372,7 +376,7 @@ export class DaemonService { const response = await this.callRpc(new GetBlockRequest(heightOrHash, fillPowHash)); if (response.error) { - this.raiseRpcError(response.error); + this.raiseRpcError(response.error); } return Block.parse(response.result); @@ -387,6 +391,10 @@ export class DaemonService { public async getBlockHash(blockHeight: number): Promise { const response = await this.callRpc(new GetBlockHashRequest(blockHeight)); + if (typeof response.result != 'string') { + throw new Error("Could not parse block hash"); + } + return response.result; } @@ -404,19 +412,19 @@ export class DaemonService { throw new CoreIsBusyError(); } - throw new Error(response.result.status); + throw new Error(response.result.status); } } public async generateBlocks(amountOfBlocks: number, walletAddress: string, prevBlock: string = '', startingNonce: number): Promise { const response = await this.callRpc(new GenerateBlocksRequest(amountOfBlocks, walletAddress, prevBlock, startingNonce)); - if(response.result && response.result.status != 'OK') { + if(response.result && typeof response.result.status == 'string' && response.result.status != 'OK') { if (response.result.status == 'BUSY') { throw new CoreIsBusyError(); } - throw new Error(response.result.status); + throw new Error(response.result.status); } return GeneratedBlocks.parse(response.result); @@ -491,10 +499,6 @@ export class DaemonService { public async getBans(): Promise { const response = await this.callRpc(new GetBansRequest()); - - if (response.error) { - this.raiseRpcError(response.error); - } if (!response.result) { return []; @@ -516,7 +520,10 @@ export class DaemonService { throw new Error(`Error code: ${result.response}`); } - return new Ban(address, 0, result.banned, result.seconds); + const banned: boolean = result.banned; + const seconds: number = result.seconds; + + return new Ban(address, 0, banned, seconds); } public async flushTxPool(... txIds: string[]): Promise { @@ -530,8 +537,8 @@ export class DaemonService { public async getOuts(outputs: Output[], getTxId: boolean): Promise { const response = await this.callRpc(new GetOutsRequest(outputs, getTxId)); - if (response.error) { - this.raiseRpcError(response.error); + if (typeof response.status == 'string' && response.status != 'OK') { + throw new Error(response.status); } const _outkeys: any[] | undefined = response.outs; @@ -545,10 +552,6 @@ export class DaemonService { public async getOutputHistogram(amounts: number[], minCount: number, maxCount: number, unlocked: boolean, recentCutoff: number): Promise { const response = await this.callRpc(new GetOutputHistogramRequest(amounts, minCount, maxCount, unlocked, recentCutoff)); - if (response.error) { - this.raiseRpcError(response.error); - } - const entries: any[] = response.result.histogram; const result: HistogramEntry[] = []; @@ -560,10 +563,6 @@ export class DaemonService { public async getOutputDistribution(amounts: number[], cumulative: boolean, fromHeight: number, toHeight: number): Promise { const response = await this.callRpc(new GetOutputDistributionRequest(amounts, cumulative, fromHeight, toHeight)); - if (response.error) { - this.raiseRpcError(response.error); - } - const entries: any[] = response.result.distributions; const distributions: OutputDistribution[] = []; @@ -660,10 +659,6 @@ export class DaemonService { public async getCoinbaseTxSum(height: number, count: number): Promise { const response = await this.callRpc(new GetCoinbaseTxSumRequest(height, count)); - if (response.error) { - this.raiseRpcError(response.error); - } - return CoinbaseTxSum.parse(response.result); } @@ -682,14 +677,14 @@ export class DaemonService { throw new Error(`Error code: ${response.status}`) } - if (!response.bakclog && !response.result) { + if (!response.backlog && !response.result) { return []; } - if (response.backlog) { + if (response.backlog && typeof response.backlog == 'string') { return TxBacklogEntry.fromBinary(response.backlog); } - else if (response.result.backlog) return TxBacklogEntry.fromBinary(response.result.backlog); + else if (response.result.backlog && typeof response.result.backlog == 'string') return TxBacklogEntry.fromBinary(response.result.backlog); return []; } @@ -703,6 +698,10 @@ export class DaemonService { public async calculatePoWHash(majorVersion: number, height: number, blockBlob: string, seedHash: string): Promise { const response = await this.callRpc(new CalculatePoWHashRequest(majorVersion, height, blockBlob, seedHash)); + if (typeof response.result != 'string') { + throw new Error("Unexpected result type") + } + return response.result; } @@ -744,25 +743,48 @@ export class DaemonService { public async getAltBlockHashes(): Promise { const response = await this.callRpc(new GetAltBlockHashesRequest()); + const altBlockHashes: string[] = response.blks_hashes; - return response.blks_hashes; + if (!Array.isArray(altBlockHashes)) { + return []; + } + + altBlockHashes.forEach((blockHash: string) => { + if(typeof blockHash != 'string') { + throw new Error("Could not parse alt block hashes"); + } + }) + + return altBlockHashes; } public async isKeyImageSpent(...keyImages: string[]): Promise { const response = await this.callRpc(new IsKeyImageSpentRequest(keyImages)); - if (response.status != 'OK') { + if (typeof response.status == 'string' && response.status != 'OK') { throw new Error(response.status); } - return response.spent_status; + const spentStatus: number[] = response.spent_status; + + if (!Array.isArray(spentStatus)) { + throw new Error("Could not parse spent list result"); + } + + return spentStatus; } public async sendRawTransaction(txAsHex: string, doNotRelay: boolean = false): Promise { const response = await this.callRpc(new SendRawTransactionRequest(txAsHex, doNotRelay)); if (typeof response.status == 'string' && response.status != 'OK') { - throw new Error(response.reason); + if (typeof response.reason == 'string') + { + throw new Error(response.reason); + } + else { + throw new Error(response.status); + } } return TxInfo.parse(response); @@ -848,13 +870,21 @@ export class DaemonService { public async inPeers(inPeers: number): Promise { const response = await this.callRpc(new InPeersRequest(inPeers)); - + + if (typeof response.in_peers != 'number') { + throw new Error("Could not parse in peers count"); + } + return response.in_peers; } public async outPeers(outPeers: number): Promise { const response = await this.callRpc(new OutPeersRequest(outPeers)); + if (typeof response.out_peers != 'number') { + throw new Error("Could not parse out peers count"); + } + return response.out_peers; } @@ -867,7 +897,7 @@ export class DaemonService { public async getPeerList(): Promise { const response = await this.callRpc(new GetPeerListRequest()); - if (response.status != 'OK') { + if (typeof response.status == 'string' && response.status != 'OK') { throw new Error(response.status); } @@ -897,7 +927,7 @@ export class DaemonService { public async getTransactionPool(): Promise { const response = await this.callRpc(new GetTransactionPoolRequest()); - if (response.status != 'OK') { + if (typeof response.status == 'string' && response.status != 'OK') { throw new Error(response.status); } @@ -906,19 +936,32 @@ export class DaemonService { public async getTransactionPoolHashes(): Promise { const response = await this.callRpc(new GetTransactionPoolHashesRequest()); + const txHashes: string[] = response.tx_hashes; - return response.tx_hashes; + if (!Array.isArray(txHashes)) { + throw new Error("Could not parse txHashses"); + } + + return txHashes; } public async getTransactionPoolHashesBinary(): Promise { const response = await this.callRpc(new GetTransactionPoolHashesBinaryRequest()); + if (typeof response.tx_hashes != 'string') { + throw new Error("Could not parse binary"); + } + return response.tx_hashes; } public async popBlocks(nBlocks: number): Promise { const response = await this.callRpc(new PopBlocksRequest(nBlocks)); + if (typeof response.height != 'number') { + throw new Error(""); + } + return response.height; } @@ -939,7 +982,7 @@ export class DaemonService { public async setLogLevel(level: number): Promise { const response = await this.callRpc(new SetLogLevelRequest(level)); - if (response.status != 'OK') { + if (typeof response.status == 'string' && response.status != 'OK') { throw new Error(response.status); } } @@ -947,7 +990,7 @@ export class DaemonService { public async setLogCategories(cateogories: string): Promise { const response = await this.callRpc(new SetLogCategoriesRequest(cateogories)); - if (response.status != 'OK') { + if (typeof response.status == 'string' && response.status != 'OK') { throw new Error(response.status); } } @@ -955,7 +998,7 @@ export class DaemonService { public async setLogHashRate(visible: boolean): Promise { const response = await this.callRpc(new SetLogHashRateRequest(visible)); - if (response.status != 'OK') { + if (typeof response.status == 'string' && response.status != 'OK') { throw new Error(response.status); } } @@ -966,3 +1009,4 @@ export class DaemonService { } +export interface RpcError { code: number, message: string } \ No newline at end of file diff --git a/src/app/pages/bans/bans.component.ts b/src/app/pages/bans/bans.component.ts index 481ad2c..f88a7df 100644 --- a/src/app/pages/bans/bans.component.ts +++ b/src/app/pages/bans/bans.component.ts @@ -71,15 +71,16 @@ export class BansComponent implements AfterViewInit { public ngAfterViewInit(): void { this.navbarService.setLinks(this.navbarLinks); - this.ngZone.run(() => { + this.ngZone.run(async () => { const $table = $('#bansTable'); $table.bootstrapTable({}); $table.bootstrapTable('refreshOptions', { classes: 'table table-bordered table-hover table-dark table-striped' }); $table.bootstrapTable('showLoading'); - this.refreshBansTable(); - + await this.refreshBansTable(); + }).then().catch((error: any) => { + console.error(error); }); } @@ -115,7 +116,7 @@ export class BansComponent implements AfterViewInit { this.setBansError = ''; this.setBansSuccess = true; } - catch (error) { + catch (error: any) { console.error(error); this.setBansSuccess = false; this.setBansError = `${error}`; diff --git a/src/app/pages/blockchain/blockchain.component.html b/src/app/pages/blockchain/blockchain.component.html index 40a9d88..7d65a19 100644 --- a/src/app/pages/blockchain/blockchain.component.html +++ b/src/app/pages/blockchain/blockchain.component.html @@ -14,13 +14,13 @@
-