getSuggestedDifficulty bug

This commit is contained in:
Ben Wilson
2023-08-06 22:41:25 -04:00
parent 0a9c254e84
commit 1172ab5971
5 changed files with 38 additions and 36 deletions
+20 -7
View File
@@ -75,13 +75,13 @@ export class StratumV1Client {
})
});
this.sessionStart = new Date();
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService);
this.extraNonceAndSessionId = this.getRandomHexString();
console.log(`New client ID: : ${this.extraNonceAndSessionId}`);
}
public destroy() {
public async destroy() {
await this.clientService.delete(this.extraNonceAndSessionId);
if (this.stratumSubscription != null) {
this.stratumSubscription.unsubscribe();
}
@@ -99,18 +99,24 @@ export class StratumV1Client {
private async handleMessage(message: string) {
// console.log(`Received from ${this.extraNonceAndSessionId}`, message);
console.log(`Received from ${this.extraNonceAndSessionId}`, message);
// Parse the message and check if it's the initial subscription message
let parsedMessage = null;
try {
parsedMessage = JSON.parse(message);
} catch (e) {
console.log("Invalid JSON");
//console.log("Invalid JSON");
await this.socket.end();
return;
}
if (this.sessionStart == null) {
this.sessionStart = new Date();
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService);
this.extraNonceAndSessionId = this.getRandomHexString();
console.log(`New client ID: : ${this.extraNonceAndSessionId}`);
}
switch (parsedMessage.method) {
case eRequestMethod.SUBSCRIBE: {
@@ -264,6 +270,13 @@ export class StratumV1Client {
break;
}
case eRequestMethod.SUBMIT: {
if (this.stratumInitialized == false) {
console.log('Submit before initalized');
await this.socket.end();
}
const miningSubmitMessage = plainToInstance(
MiningSubmitMessage,
parsedMessage,
+6 -6
View File
@@ -49,14 +49,14 @@ export class StratumV1ClientStatistics {
public getSuggestedDifficulty(clientDifficulty: number) {
// miner hasn't submitted shares in one minute
if (this.submissionCache.length == 0 || (new Date().getTime() - this.submissionCacheStart.getTime()) / 1000 > 60) {
return this.nearestPowerOfTwo(clientDifficulty >> 1);
if (this.submissionCache.length == 0) {
if ((new Date().getTime() - this.submissionCacheStart.getTime()) / 1000 > 60) {
return this.nearestPowerOfTwo(clientDifficulty >> 1);
} else {
return null;
}
}
// if (this.submissionCache.length < CACHE_SIZE) {
// return null;
// }
const sum = this.submissionCache.reduce((pre, cur) => {
pre += cur.difficulty;
return pre;