Start/stop mining implementation

This commit is contained in:
everoddandeven 2024-10-07 00:31:16 +02:00
parent 47f7665ba6
commit e6c5fbdc15
3 changed files with 161 additions and 7 deletions

View file

@ -1,6 +1,6 @@
import { EventEmitter, Injectable } from '@angular/core';
import { DaemonService } from './daemon.service';
import { BlockCount, BlockHeader, Chain, DaemonInfo, NetStats, SyncInfo } from '../../../../common';
import { BlockCount, BlockHeader, Chain, DaemonInfo, MinerData, MiningStatus, NetStats, SyncInfo } from '../../../../common';
@Injectable({
providedIn: 'root'
@ -37,6 +37,12 @@ export class DaemonDataService {
private _netStats?: NetStats;
private _gettingNetStats: boolean = false;
private _miningStatus?: MiningStatus;
private _gettingMiningStatus: boolean = false;
private _minerData?: MinerData;
private _gettingMinerData: 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>();
@ -137,6 +143,18 @@ export class DaemonDataService {
return this._gettingNetStats;
}
public get miningStatus(): MiningStatus | undefined {
return this._miningStatus;
}
public get gettingMiningStatus(): boolean {
return this._gettingMiningStatus;
}
public get gettingMinerData(): boolean {
return this._gettingMinerData;
}
public setRefreshTimeout(ms: number = 5000): void {
this.refreshTimeoutMs = ms;
}
@ -170,6 +188,46 @@ export class DaemonDataService {
}
private async refreshMiningStatus(): Promise<void> {
this._gettingMiningStatus = true;
try {
this._miningStatus = await this.daemonService.miningStatus();
}
catch(error) {
console.error(error);
}
this._gettingMiningStatus = false;
}
private async refreshMinerData(): Promise<void> {
this._gettingMinerData = true;
try {
this._minerData = await this.daemonService.getMinerData();
}
catch (error) {
console.error(error);
}
this._gettingMinerData = false;
}
private async refreshAltChains(): Promise<void> {
this._gettingAltChains = true;
try {
this._altChains = await this.daemonService.getAlternateChains();
}
catch (error) {
console.error(error);
}
this._gettingAltChains = false;
}
private async refresh(): Promise<void> {
if (this.refreshing || this.tooEarlyForRefresh) {
return;
@ -210,14 +268,16 @@ 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;
await this.refreshAltChains();
this._gettingNetStats = true;
this._netStats = await this.daemonService.getNetStats();
this._gettingNetStats = false;
await this.refreshMiningStatus();
await this.refreshMinerData();
this._lastRefresh = Date.now();
} catch(error) {
console.error(error);

View file

@ -11,7 +11,7 @@
</div>
</div>
<div *ngIf="daemonRunning" class="tab-content" id="pills-tabContent">
<div *ngIf="daemonRunning && !daemonStopping" class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-mining-status" role="tabpanel" aria-labelledby="pills-mining-status-tab" tabindex="0">
<div *ngIf="miningStatusLoading">
<!-- Placeholder per il caricamento -->
@ -31,7 +31,7 @@
</div>
</div>
<div *ngIf="miningStatus" class="card">
<div *ngIf="miningStatus && miningStatus.active" class="card">
<div class="card-header bg-primary text-white">
<h4>Mining Status</h4>
</div>
@ -78,6 +78,48 @@
</div>
</div>
<div *ngIf="miningStatus && !miningStatus.active" class="row g-5 p-2">
<div class="cold-md-7 col-lg-12">
<div class="row gy-3">
<div class="col-sm-12">
<label for="start-mining-miner-address" class="form-label">Miner Address</label>
<input type="text" class="form-control" id="start-mining-miner-address" placeholder="" [(ngModel)]="startMiningMinerAddress" [ngModelOptions]="{standalone: true}">
<small class="text-body-secondary">Account address to mine to</small>
</div>
<div class="form-check form-switch col-md-6">
<label for="start-mining-do-background-mining" class="form-check-label">Do Background Mining</label>
<input class="form-control form-check-input" type="checkbox" role="switch" id="start-mining-do-background-mining" [checked]="startMiningDoBackgroundMining" [(ngModel)]="startMiningDoBackgroundMining" [ngModelOptions]="{standalone: true}">
<br>
<small class="text-body-secondary">States if the mining should run in background (true) or foreground (false)</small>
</div>
<div class="form-check form-switch col-md-6">
<label for="start-mining-ignore-battery" class="form-check-label">Ignore Battery</label>
<input class="form-control form-check-input" type="checkbox" role="switch" id="start-mining-ignore-battery" [checked]="startMiningIgnoreBattery" [(ngModel)]="startMiningIgnoreBattery" [ngModelOptions]="{standalone: true}">
<br>
<small class="text-body-secondary">States if battery state (on laptop) should be ignored (true) or not (false)</small>
</div>
<div class="col-sm-3">
<label for="start-mining-threads-count" class="form-label">Threads Count</label>
<input type="number" class="form-control" id="start-mining-threads-count" placeholder="" [(ngModel)]="startMiningThreadsCount" [ngModelOptions]="{standalone: true}">
<small class="text-body-secondary">Number of mining thread to run</small>
</div>
</div>
</div>
</div>
<hr class="my-4">
<button *ngIf="!startingMining && (!miningStatus || !miningStatus.active)" class="w-100 btn btn-primary btn-lg" type="button" (click)="startMining()" [disabled]="!validStartMiningMinerAddress">Start Mining</button>
<button *ngIf="startingMining && (!miningStatus || !miningStatus.active)" class="w-100 btn btn-primary btn-lg" type="button" disabled>Starting Mining ...</button>
<button *ngIf="!stoppingMining && miningStatus && miningStatus.active" class="w-100 btn btn-primary btn-lg" type="button" (click)="stopMining()">Stop Mining</button>
<button *ngIf="stoppingMining && miningStatus && miningStatus.active" class="w-100 btn btn-primary btn-lg" type="button" disabled>Stopping Mining ...</button>
</div>
<div class="tab-pane fade" id="pills-miner-data" role="tabpanel" aria-labelledby="pills-miner-data-tab" tabindex="0">

View file

@ -64,6 +64,25 @@ export class MiningComponent implements AfterViewInit {
//private txBacklog: MineableTxBacklog[]
public cards: Card[];
public daemonRunning: boolean;
public get daemonStopping(): boolean {
return this.daemonService.stopping;
}
public startMiningDoBackgroundMining: boolean = false;
public startMiningIgnoreBattery: boolean = false;
public startMiningMinerAddress: string = '';
public startMiningThreadsCount: number = 0;
public startingMining: boolean = false;
public startMiningSuccess: boolean = false;
public startMiningError: string = '';
public stoppingMining: boolean = false;
public stopMiningError: string = '';
public stopMiningSuccess: boolean = false;
public get validStartMiningMinerAddress(): boolean {
return this.startMiningMinerAddress != '';
}
constructor(private router: Router, private daemonService: DaemonService, private navbarService: NavbarService, private ngZone: NgZone) {
@ -109,7 +128,7 @@ export class MiningComponent implements AfterViewInit {
});
}
ngAfterViewInit(): void {
public ngAfterViewInit(): void {
console.log('DetailComponent AFTER VIEW INIT');
this.navbarService.setLinks(this.navbarLinks);
@ -289,6 +308,39 @@ export class MiningComponent implements AfterViewInit {
this.generatingBlocks = false;
}
public async startMining(): Promise<void> {
this.startingMining = true;
try {
await this.daemonService.startMining(this.startMiningDoBackgroundMining, this.startMiningIgnoreBattery, this.startMiningMinerAddress, this.startMiningThreadsCount)
this.startMiningError = '';
this.startMiningSuccess = true;
}
catch(error) {
this.startMiningSuccess = false;
this.startMiningError = `${error}`;
}
this.startingMining = false;
}
public async stopMining(): Promise<void> {
this.stoppingMining = true;
try {
await this.daemonService.stopMining();
this.stopMiningSuccess = true;
this.stopMiningError = '';
}
catch(error) {
console.error(error);
this.stopMiningSuccess = false;
this.stopMiningError = `${error};`
}
this.stoppingMining = false;
}
}
class Card {