1- import { EventEmitter , Injectable } from '@angular/core' ;
1+ import { EventEmitter , inject , Injectable } from '@angular/core' ;
22import { MiningStatus , XmrigSettings } from '../../../../common' ;
33import { IDBPDatabase , openDB } from 'idb' ;
4+ import { P2poolService } from '../p2pool/p2pool.service' ;
45
56type Status = 'running' | 'stopped' | 'starting' | 'restarting' | 'stopping' ;
67
@@ -16,6 +17,8 @@ export class XmrigService {
1617
1718 // #region Attributes
1819
20+ private readonly p2poolService = inject ( P2poolService ) ;
21+
1922 private readonly openDbPromise : Promise < IDBPDatabase > ;
2023 private readonly dbName = 'XmrigSettingsDB' ;
2124 private readonly storeName = 'settingsStore' ;
@@ -149,18 +152,22 @@ export class XmrigService {
149152 this . _logs = [ ] ;
150153 }
151154
152- public async refreshMiningStatus ( networkDifficulty : number ) : Promise < void > {
153- const _config = this . _settings ;
154-
155- await new Promise < void > ( ( resolve , reject ) => {
156- window . electronAPI . xmrigCmd ( 'h' , ( result : { error ?: string } ) => {
155+ private async sendCommand ( cmd : 'h' | 'p' | 'r' | 'c' ) : Promise < void > {
156+ return await new Promise < void > ( ( resolve , reject ) => {
157+ window . electronAPI . xmrigCmd ( cmd , ( result : { error ?: string } ) => {
157158 if ( result . error ) reject ( new Error ( result . error ) ) ;
158159 else resolve ( ) ;
159160 } ) ;
160161 } ) ;
162+ }
163+
164+ public async refreshMiningStatus ( networkDifficulty : number ) : Promise < void > {
165+ const _config = this . _settings ;
166+
167+ await this . sendCommand ( 'h' ) ;
161168
162169 const lastHash = this . _lastHashStatus ;
163- const info = lastHash . split ( '\n' ) [ 4 ]
170+ const info = lastHash . split ( '\n' ) . find ( ( line ) => line . includes ( 'speed' ) ) ;
164171 if ( info ) {
165172 const c = info . split ( ' ' ) . filter ( x => x !== '' ) ;
166173 const hash10s = c [ 5 ] ;
@@ -194,7 +201,7 @@ export class XmrigService {
194201 '' ) ;
195202 }
196203
197- public async start ( config ?: XmrigSettings ) : Promise < void > {
204+ public async start ( config ?: XmrigSettings , waitForP2Pool : boolean = false ) : Promise < void > {
198205 const _config = config ? config : this . _settings ;
199206 if ( _config . path === '' ) throw new Error ( "Xmrig not configured. Go to Settings -> Mining" ) ;
200207 if ( this . running ) throw new Error ( "Already running xmrig" ) ;
@@ -204,12 +211,14 @@ export class XmrigService {
204211 this . _status = 'starting' ;
205212
206213 try {
214+ if ( waitForP2Pool ) await this . p2poolService . waitForSidechainSync ( ) ;
215+
207216 const promise = new Promise < void > ( ( resolve , reject ) => {
208217
209218 window . electronAPI . onXmrigOutput ( ( { stdout, stderr} : { stdout ?: string , stderr ?: string } ) => {
210219 if ( stdout ) {
211220 stdout = this . cleanLog ( stdout ) ;
212- if ( stdout . includes ( "CPU" ) && stdout . includes ( "AFFINITY ") ) {
221+ if ( stdout . includes ( "speed " ) ) {
213222 this . _lastHashStatus = stdout ;
214223 } else {
215224 this . _logs . push ( stdout ) ;
@@ -226,6 +235,9 @@ export class XmrigService {
226235 console . log ( code ) ;
227236 this . _status = 'stopped' ;
228237 this . _startedAt = 0 ;
238+ this . _lastHash10s = 0 ;
239+ this . _lastHash60s = 0 ;
240+ this . _lastHash15m = 0 ;
229241 this . onStop . emit ( ) ;
230242 } ) ;
231243
0 commit comments