Skip to content

Commit

Permalink
Wide implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
everoddandeven committed Oct 3, 2024
1 parent 1311991 commit 91b38d1
Show file tree
Hide file tree
Showing 17 changed files with 798 additions and 598 deletions.
10 changes: 8 additions & 2 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ function getMonerodVersion(monerodFilePath: string): void {
}

function startMoneroDaemon(commandOptions: string[]): ChildProcessWithoutNullStreams {
const monerodPath = getMonerodPath();

//const monerodPath = getMonerodPath();
const monerodPath = commandOptions.shift();

if (!monerodPath) {
win?.webContents.send('monero-sterr', `Invalid monerod path provided: ${monerodPath}`);
throw Error("Invalid monerod path provided");
}

console.log("Starting monerod daemon with options: " + commandOptions.join(" "));

// Avvia il processo usando spawn
Expand Down
15 changes: 0 additions & 15 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,6 @@ export class AppComponent {
private async load(): Promise<void> {
this.loading = true;

if (!window.indexedDB) {
console.log("Il tuo browser non supporta indexedDB");
}
else {
console.log("Browser supports IndexedDB");
var request = window.indexedDB.open("dati", 1);
console.log(request);

request.onsuccess = function(event: Event) {
if(event.target instanceof IDBOpenDBRequest) {
console.log(event.target.result)
}
};
}

try {
this.daemonRunning = await this.daemonService.isRunning();
}
Expand Down
27 changes: 26 additions & 1 deletion src/app/core/services/daemon/daemon-data.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter, Injectable } from '@angular/core';
import { DaemonService } from './daemon.service';
import { BlockCount, BlockHeader, DaemonInfo, SyncInfo } from '../../../../common';
import { BlockCount, BlockHeader, Chain, DaemonInfo, SyncInfo } from '../../../../common';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -31,6 +31,9 @@ export class DaemonDataService {
private _lastBlockHeader?: BlockHeader;
private _gettingLastBlockHeader: boolean = false;

private _altChains: Chain[] = [];
private _gettingAltChains: boolean = false;

public readonly syncStart: EventEmitter<void> = new EventEmitter<void>();
public readonly syncEnd: EventEmitter<void> = new EventEmitter<void>();
public readonly syncError: EventEmitter<Error> = new EventEmitter<Error>();
Expand Down Expand Up @@ -115,6 +118,14 @@ export class DaemonDataService {
return this._gettingLastBlockHeader;
}

public get AltChains(): Chain[] {
return this._altChains;
}

public get gettingAltChains(): boolean {
return this._gettingAltChains;
}

public setRefreshTimeout(ms: number = 5000): void {
this.refreshTimeoutMs = ms;
}
Expand Down Expand Up @@ -144,6 +155,10 @@ export class DaemonDataService {
return Date.now() - this._lastRefresh <= this.refreshTimeoutMs;
}

private async getInfo(): Promise<void> {

}

private async refresh(): Promise<void> {
if (this.refreshing || this.tooEarlyForRefresh) {
return;
Expand All @@ -157,6 +172,11 @@ export class DaemonDataService {
this._daemonRunning = await this.daemonService.isRunning();
this._firstRefresh = false;

if (!this._daemonRunning) {
this.syncEnd.emit();
return;
}

this._gettingDaemonInfo = true;
this._daemonInfo = await this.daemonService.getInfo();
this._gettingDaemonInfo = false;
Expand All @@ -179,6 +199,10 @@ export class DaemonDataService {
if (firstRefresh) this._isBlockchainPruned = (await this.daemonService.pruneBlockchain(true)).pruned;
this._gettingIsBlockchainPruned = false;

this._gettingAltChains = true;
this._altChains = await this.daemonService.getAlternateChains();
this._gettingAltChains = false;

this._lastRefresh = Date.now();
} catch(error) {
console.error(error);
Expand All @@ -187,6 +211,7 @@ export class DaemonDataService {
this._gettingBlockCount = false;
this._gettingLastBlockHeader = false;
this._gettingIsBlockchainPruned = false;
this._gettingAltChains = false;

this.syncError.emit(<Error>error);

Expand Down
53 changes: 37 additions & 16 deletions src/app/core/services/daemon/daemon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export class DaemonService {
public readonly onDaemonStopStart: EventEmitter<void> = new EventEmitter<void>();
public readonly onDaemonStopEnd: EventEmitter<void> = new EventEmitter<void>();

private isRunningPromise?: Promise<boolean>;

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
Expand Down Expand Up @@ -237,13 +239,17 @@ export class DaemonService {
return response;
}
catch (error) {
if (error instanceof HttpErrorResponse && error.status == 0) {
const wasRunning = this.daemonRunning;
this.daemonRunning = false;

if (wasRunning) {
this.onDaemonStatusChanged.emit(false);
if (error instanceof HttpErrorResponse) {
if (error.status == 0) {
const wasRunning = this.daemonRunning;
this.daemonRunning = false;

if (wasRunning) {
this.onDaemonStatusChanged.emit(false);
}
}

throw new Error(error.message);
}

throw error;
Expand Down Expand Up @@ -282,26 +288,37 @@ export class DaemonService {

}

public async isRunning(force: boolean = false): Promise<boolean> {
private async checkDaemonIsRunning(): Promise<boolean> {
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;
return true;
}

console.error(error);
}

return false;
}

public async isRunning(force: boolean = false): Promise<boolean> {
if (this.isRunningPromise) {
return await this.isRunningPromise;
}

this.daemonRunning = false;
return this.daemonRunning;
if (!force && this.daemonRunning != undefined) {
return this.daemonRunning;
}

this.isRunningPromise = this.checkDaemonIsRunning();

this.daemonRunning = await this.isRunningPromise;

this.isRunningPromise = undefined;

return this.daemonRunning;
}

public async getBlock(heightOrHash: number | string, fillPowHash: boolean = false): Promise<Block> {
Expand Down Expand Up @@ -678,6 +695,10 @@ export class DaemonService {
public async isKeyImageSpent(...keyImages: string[]): Promise<number[]> {
const response = await this.callRpc(new IsKeyImageSpentRequest(keyImages));

if (response.status != 'OK') {
throw new Error(response.status);
}

return response.spent_status;
}

Expand Down
26 changes: 26 additions & 0 deletions src/app/pages/blockchain/blockchain.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,32 @@ <h4><i class="bi bi-check-circle m-2"></i></h4>&nbsp;&nbsp;

</div>

<div class="tab-pane fade" id="pills-prune-blockchain" role="tabpanel" aria-labelledby="pills-prune-blockchain-tab" tabindex="0">
<div *ngIf="pruneBlockchainError != ''" class="alert alert-danger d-flex align-items-center justify-content-center text-center" role="alert">
<h4><i class="bi bi-exclamation-triangle m-2"></i></h4>&nbsp;&nbsp;
<div>
{{pruneBlockchainError}}
</div>
</div>

<div *ngIf="blockchainPruned" class="alert alert-success d-flex align-items-center justify-content-center text-center" role="alert">
<h4><i class="bi bi-check-circle m-2"></i></h4>&nbsp;&nbsp;
<div>
Successfully initiated blockchain pruning
</div>
</div>

<div class="alert alert-info d-flex text-center" role="alert">
<div>
'Pruning' allows node operators to save 2/3 of storage space while keeping the full transaction history </div>
</div>

<hr class="my-4">

<button *ngIf="!pruningBlockchain" class="w-100 btn btn-primary btn-lg" type="button" (click)="pruneBlockchain()">Prune Blockchain</button>
<button *ngIf="pruningBlockchain" class="w-100 btn btn-primary btn-lg" type="button" disabled>Pruning Blockchain ...</button>

</div>

<div class="tab-pane fade" id="pills-save-bc" role="tabpanel" aria-labelledby="pills-save-bc-tab" tabindex="0">
<div *ngIf="saveBlockchainError != ''" class="alert alert-danger d-flex align-items-center justify-content-center text-center" role="alert">
Expand Down
29 changes: 24 additions & 5 deletions src/app/pages/blockchain/blockchain.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ export class BlockchainComponent implements AfterViewInit {
public saveBlockchainError: string = '';
public blockchainSaved: boolean = false;

public pruningBlockchain: boolean = false;
public pruneBlockchainError: string = '';
public blockchainPruned: boolean = false;

constructor(private daemonService: DaemonService, private ngZone: NgZone) {
this.navbarLinks = [
new NavbarLink('pills-last-block-header-tab', '#pills-last-block-header', 'pills-last-block-header', true, 'Last Block Header', true),
new NavbarLink('pills-get-block-tab', '#pills-get-block', 'pills-get-block', false, 'Get Block', true),
new NavbarLink('pills-get-block-header-tab', '#pills-get-block-header', 'pills-get-block-header', false, 'Get Block Header', true),
new NavbarLink('pills-pop-blocks-tab', '#pills-pop-blocks', 'pills-pop-blocks', false, 'Pop Blocks', true),
new NavbarLink('pills-save-bc-tab', '#pills-save-bc', 'pills-save-bc', false, 'Save Blockchain', true)
new NavbarLink('pills-last-block-header-tab', '#pills-last-block-header', 'pills-last-block-header', true, 'Last Block Header'),
new NavbarLink('pills-get-block-tab', '#pills-get-block', 'pills-get-block', false, 'Get Block'),
new NavbarLink('pills-get-block-header-tab', '#pills-get-block-header', 'pills-get-block-header', false, 'Get Block Header'),
new NavbarLink('pills-pop-blocks-tab', '#pills-pop-blocks', 'pills-pop-blocks', false, 'Pop Blocks'),
new NavbarLink('pills-prune-blockchain-tab', '#pills-prune-blockchain', 'pills-prune-blockchain', false, 'Prune'),
new NavbarLink('pills-save-bc-tab', '#pills-save-bc', 'pills-save-bc', false, 'Save')
];

this.daemonService.onDaemonStatusChanged.subscribe((running) => {
Expand Down Expand Up @@ -148,4 +153,18 @@ export class BlockchainComponent implements AfterViewInit {

this.savingBlockchain = false;
}

public async pruneBlockchain(): Promise<void> {
this.pruningBlockchain = true;

try {
await this.daemonService.pruneBlockchain(false);
this.blockchainPruned = true;
} catch(error) {
this.pruneBlockchainError = `${error}`;
this.blockchainPruned = false;
}

this.pruningBlockchain = false;
}
}
14 changes: 12 additions & 2 deletions src/app/pages/detail/detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ export class DetailComponent implements AfterViewInit {
}

private get targetHeight(): number {
return this.daemonData.syncInfo ? this.daemonData.syncInfo.targetHeight : 0;
const value = this.daemonData.syncInfo ? this.daemonData.syncInfo.targetHeight : 0;

if (value == 0 && this.height > 0) {
return this.height;
}

return value;
}

private get nextNeededPruningSeed(): number {
Expand Down Expand Up @@ -90,7 +96,11 @@ export class DetailComponent implements AfterViewInit {
}

private get syncProgress(): string {
return `${(this.height*100/this.targetHeight).toFixed(2)} %`;
const targetHeight = this.targetHeight;
const height = this.height;
console.log(`Sync progress, height ${height},targetHeight ${targetHeight}`)

return `${(height*100/targetHeight).toFixed(2)} %`;
}

//#endregion
Expand Down
Loading

0 comments on commit 91b38d1

Please sign in to comment.