defensive coding

This commit is contained in:
Ben Wilson
2023-08-06 10:30:57 -04:00
parent a38c3de5b7
commit faac9ccfea
+80 -31
View File
@@ -128,8 +128,10 @@ export class StratumV1Client {
if (errors.length === 0) { if (errors.length === 0) {
this.clientSubscription = subscriptionMessage; this.clientSubscription = subscriptionMessage;
const success = await this.write(JSON.stringify(this.clientSubscription.response(this.extraNonceAndSessionId)) + '\n');
await this.promiseSocket.write(JSON.stringify(this.clientSubscription.response(this.extraNonceAndSessionId)) + '\n'); if (!success) {
return;
}
} else { } else {
console.error('Subscription validation error'); console.error('Subscription validation error');
const err = new StratumErrorMessage( const err = new StratumErrorMessage(
@@ -138,7 +140,10 @@ export class StratumV1Client {
'Subscription validation error', 'Subscription validation error',
errors).response(); errors).response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); const success = await this.write(err);
if (!success) {
return;
}
} }
break; break;
@@ -160,7 +165,11 @@ export class StratumV1Client {
if (errors.length === 0) { if (errors.length === 0) {
this.clientConfiguration = configurationMessage; this.clientConfiguration = configurationMessage;
//const response = this.buildSubscriptionResponse(configurationMessage.id); //const response = this.buildSubscriptionResponse(configurationMessage.id);
await this.promiseSocket.write(JSON.stringify(this.clientConfiguration.response()) + '\n'); const success = await this.write(JSON.stringify(this.clientConfiguration.response()) + '\n');
if (!success) {
return;
}
} else { } else {
console.error('Configuration validation error'); console.error('Configuration validation error');
const err = new StratumErrorMessage( const err = new StratumErrorMessage(
@@ -169,7 +178,10 @@ export class StratumV1Client {
'Configuration validation error', 'Configuration validation error',
errors).response(); errors).response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); const success = await this.write(err);
if (!success) {
return;
}
} }
break; break;
@@ -191,7 +203,10 @@ export class StratumV1Client {
this.clientAuthorization = authorizationMessage; this.clientAuthorization = authorizationMessage;
//const response = this.buildSubscriptionResponse(authorizationMessage.id); //const response = this.buildSubscriptionResponse(authorizationMessage.id);
await this.promiseSocket.write(JSON.stringify(this.clientAuthorization.response()) + '\n'); const success = await this.write(JSON.stringify(this.clientAuthorization.response()) + '\n');
if (!success) {
return;
}
} else { } else {
console.error('Authorization validation error'); console.error('Authorization validation error');
const err = new StratumErrorMessage( const err = new StratumErrorMessage(
@@ -200,7 +215,10 @@ export class StratumV1Client {
'Authorization validation error', 'Authorization validation error',
errors).response(); errors).response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); const success = await this.write(err);
if (!success) {
return;
}
} }
break; break;
@@ -226,7 +244,10 @@ export class StratumV1Client {
this.clientSuggestedDifficulty = suggestDifficultyMessage; this.clientSuggestedDifficulty = suggestDifficultyMessage;
this.sessionDifficulty = suggestDifficultyMessage.suggestedDifficulty; this.sessionDifficulty = suggestDifficultyMessage.suggestedDifficulty;
await this.promiseSocket.write(JSON.stringify(this.clientSuggestedDifficulty.response(this.sessionDifficulty)) + '\n'); const success = await this.write(JSON.stringify(this.clientSuggestedDifficulty.response(this.sessionDifficulty)) + '\n');
if (!success) {
return;
}
this.usedSuggestedDifficulty = true; this.usedSuggestedDifficulty = true;
} else { } else {
console.error('Suggest difficulty validation error'); console.error('Suggest difficulty validation error');
@@ -236,7 +257,10 @@ export class StratumV1Client {
'Suggest difficulty validation error', 'Suggest difficulty validation error',
errors).response(); errors).response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); const success = await this.write(err);
if (!success) {
return;
}
} }
break; break;
} }
@@ -256,7 +280,10 @@ export class StratumV1Client {
if (errors.length === 0 && this.stratumInitialized == true) { if (errors.length === 0 && this.stratumInitialized == true) {
const result = await this.handleMiningSubmission(miningSubmitMessage); const result = await this.handleMiningSubmission(miningSubmitMessage);
if (result == true) { if (result == true) {
await this.promiseSocket.write(JSON.stringify(miningSubmitMessage.response()) + '\n'); const success = await this.write(JSON.stringify(miningSubmitMessage.response()) + '\n');
if (!success) {
return;
}
} }
@@ -268,7 +295,10 @@ export class StratumV1Client {
'Mining Submit validation error', 'Mining Submit validation error',
errors).response(); errors).response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); const success = await this.write(err);
if (!success) {
return;
}
} }
break; break;
} }
@@ -291,7 +321,10 @@ export class StratumV1Client {
if (this.clientSuggestedDifficulty == null) { if (this.clientSuggestedDifficulty == null) {
console.log(`Setting difficulty to ${this.sessionDifficulty}`) console.log(`Setting difficulty to ${this.sessionDifficulty}`)
const setDifficulty = JSON.stringify(new SuggestDifficulty().response(this.sessionDifficulty)); const setDifficulty = JSON.stringify(new SuggestDifficulty().response(this.sessionDifficulty));
await this.promiseSocket.write(setDifficulty + '\n'); const success = await this.write(setDifficulty + '\n');
if (!success) {
return;
}
} }
@@ -367,10 +400,10 @@ export class StratumV1Client {
this.stratumV1JobsService.addJob(job); this.stratumV1JobsService.addJob(job);
try {
await this.promiseSocket.write(job.response(jobTemplate)); const success = await this.write(job.response(jobTemplate));
} catch (e) { if (!success) {
console.log(e); return;
} }
@@ -390,7 +423,10 @@ export class StratumV1Client {
eStratumErrorCode.JobNotFound, eStratumErrorCode.JobNotFound,
'Job not found').response(); 'Job not found').response();
console.log(err); console.log(err);
await this.promiseSocket.write(err); const success = await this.write(err);
if (!success) {
return false;
}
return false; return false;
} }
const jobTemplate = this.stratumV1JobsService.getJobTemplateById(job.jobTemplateId); const jobTemplate = this.stratumV1JobsService.getJobTemplateById(job.jobTemplateId);
@@ -439,7 +475,10 @@ export class StratumV1Client {
eStratumErrorCode.DuplicateShare, eStratumErrorCode.DuplicateShare,
'Duplicate share').response(); 'Duplicate share').response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); const success = await this.write(err);
if (!success) {
return false;
}
return false; return false;
} }
@@ -458,14 +497,12 @@ export class StratumV1Client {
submission.id, submission.id,
eStratumErrorCode.LowDifficultyShare, eStratumErrorCode.LowDifficultyShare,
'Difficulty too low').response(); 'Difficulty too low').response();
// console.error(err);
// console.log(`Header: ${header.toString('hex')}`); const success = await this.write(err);
try { if (!success) {
await this.promiseSocket.write(err); return false;
} catch (e) {
await this.promiseSocket.end();
console.error(e);
} }
return false; return false;
} }
@@ -490,12 +527,9 @@ export class StratumV1Client {
params: [targetDiff] params: [targetDiff]
}) + '\n'; }) + '\n';
try {
await this.promiseSocket.write(data); await this.promiseSocket.write(data);
} catch (e) {
await this.promiseSocket.end();
return;
}
// we need to clear the jobs so that the difficulty set takes effect. Otherwise the different miner implementations can cause issues // we need to clear the jobs so that the difficulty set takes effect. Otherwise the different miner implementations can cause issues
const jobTemplate = await firstValueFrom(this.stratumV1JobsService.newMiningJob$); const jobTemplate = await firstValueFrom(this.stratumV1JobsService.newMiningJob$);
@@ -526,4 +560,19 @@ export class StratumV1Client {
return number; return number;
} }
private async write(message: string): Promise<boolean> {
try {
if (this.promiseSocket.isPromiseWritable) {
await this.promiseSocket.write(message);
return true;
} else {
console.error('Error: Cannot write to closed or ended socket.');
}
} catch (error) {
await this.promiseSocket.end();
console.error('Error occurred while writing to socket:', error);
}
return false;
}
} }