misc improvements from blitzpool

This commit is contained in:
Ben
2026-05-06 19:05:10 -04:00
parent 355437df3c
commit 216fa74d99
14 changed files with 526 additions and 101 deletions
+67 -18
View File
@@ -29,6 +29,21 @@ import { EXTRANONCE1_SIZE_BYTES } from './stratum.constants';
import { SuggestDifficulty } from './stratum-messages/SuggestDifficultyMessage';
import { StratumV1ClientStatistics } from './StratumV1ClientStatistics';
export function effectiveJobDifficulty(
jobIdInt: number,
currentDiff: number,
oldDiff: number,
diffChangeJobId: number | null,
): number {
if (diffChangeJobId == null || !Number.isFinite(jobIdInt)) {
return currentDiff;
}
if (jobIdInt < diffChangeJobId) {
return Math.min(currentDiff, oldDiff);
}
return currentDiff;
}
export class StratumV1Client {
@@ -43,6 +58,8 @@ export class StratumV1Client {
private stratumInitialized = false;
private usedSuggestedDifficulty = false;
private sessionDifficulty: number = 100000;
private oldSessionDifficulty: number = 100000;
private diffChangeJobId: number | null = null;
private clientEntity: ClientEntity;
private creatingEntity: Promise<void>;
@@ -73,16 +90,16 @@ export class StratumV1Client {
let lines = this.buffer.split('\n');
this.buffer = lines.pop() || ''; // Save the last part of the data (incomplete line) to the buffer
lines
.filter(m => m.length > 0)
.forEach(async (m) => {
(async () => {
for (const m of lines.filter(l => l.length > 0)) {
try {
await this.handleMessage(m);
} catch (e) {
await this.socket.end();
console.error(e);
}
});
}
})();
});
@@ -224,6 +241,7 @@ export class StratumV1Client {
this.clientAuthorization = authorizationMessage;
if (this.clientSuggestedDifficulty == null && this.clientAuthorization.startingDiff != null && this.clientAuthorization.startingDiff > this.sessionDifficulty) {
this.sessionDifficulty = this.clientAuthorization.startingDiff;
this.oldSessionDifficulty = this.sessionDifficulty;
}
const success = await this.write(JSON.stringify(this.clientAuthorization.response()) + '\n');
if (!success) {
@@ -265,6 +283,7 @@ export class StratumV1Client {
this.clientSuggestedDifficulty = suggestDifficultyMessage;
this.sessionDifficulty = suggestDifficultyMessage.suggestedDifficulty;
this.oldSessionDifficulty = this.sessionDifficulty;
const success = await this.write(JSON.stringify(this.clientSuggestedDifficulty.response(this.sessionDifficulty)) + '\n');
if (!success) {
return;
@@ -361,6 +380,7 @@ export class StratumV1Client {
switch (this.clientSubscription.userAgent) {
case 'cpuminer': {
this.sessionDifficulty = 0.1;
this.oldSessionDifficulty = this.sessionDifficulty;
}
}
@@ -443,7 +463,8 @@ export class StratumV1Client {
network,
this.stratumV1JobsService.getNextId(),
payoutInformation,
jobTemplate
jobTemplate,
this.configService.get('POOL_IDENTIFIER') || 'Public-Pool'
);
this.stratumV1JobsService.addJob(job);
@@ -531,6 +552,19 @@ export class StratumV1Client {
}
return false;
}
const classification = this.stratumV1JobsService.classifyJobForShare(job);
if (classification === 'stale-rejected') {
const err = new StratumErrorMessage(
submission.id,
eStratumErrorCode.JobNotFound,
'stale').response();
const success = await this.write(err);
if (!success) {
return false;
}
return false;
}
const updatedJobBlock = job.copyAndUpdateBlock(
@@ -545,30 +579,37 @@ export class StratumV1Client {
const { submissionDifficulty } = this.calculateDifficulty(header);
//console.log(`DIFF: ${submissionDifficulty} of ${this.sessionDifficulty} from ${this.clientAuthorization.worker + '.' + this.extraNonceAndSessionId}`);
const effectiveDiff = effectiveJobDifficulty(
parseInt(submission.jobId, 16),
this.sessionDifficulty,
this.oldSessionDifficulty,
this.diffChangeJobId,
);
if (submissionDifficulty >= this.sessionDifficulty) {
if (submissionDifficulty >= effectiveDiff) {
if (submissionDifficulty >= jobTemplate.blockData.networkDifficulty) {
console.log('!!! BLOCK FOUND !!!');
const blockHex = updatedJobBlock.toHex(false);
const result = await this.bitcoinRpcService.SUBMIT_BLOCK(blockHex);
await this.blocksService.save({
height: jobTemplate.blockData.height,
minerAddress: this.clientAuthorization.address,
worker: this.clientAuthorization.worker,
sessionId: this.extraNonceAndSessionId,
blockData: blockHex
});
if (result === 'SUCCESS!') {
await this.blocksService.save({
height: jobTemplate.blockData.height,
minerAddress: this.clientAuthorization.address,
worker: this.clientAuthorization.worker,
sessionId: this.extraNonceAndSessionId,
blockData: blockHex
});
await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address, jobTemplate.blockData.height, updatedJobBlock, result);
//success
if (result == null) {
await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address, jobTemplate.blockData.height, updatedJobBlock, result);
await this.addressSettingsService.resetBestDifficultyAndShares();
} else {
console.warn(`[Block submit rejected at height ${jobTemplate.blockData.height}]: ${result}`);
}
}
try {
await this.statistics.addShares(this.clientEntity, this.sessionDifficulty);
await this.statistics.addShares(this.clientEntity, effectiveDiff);
const now = new Date();
// only update every minute
//if (this.clientEntity.updatedAt == null || now.getTime() - this.clientEntity.updatedAt.getTime() > 1000 * 60) {
@@ -617,6 +658,11 @@ export class StratumV1Client {
if (targetDiff != this.sessionDifficulty) {
//console.log(`Adjusting ${this.extraNonceAndSessionId} difficulty from ${this.sessionDifficulty} to ${targetDiff}`);
if (!Number.isFinite(targetDiff)) {
return;
}
this.oldSessionDifficulty = this.sessionDifficulty;
this.diffChangeJobId = parseInt(this.stratumV1JobsService.getNextId(), 16);
this.sessionDifficulty = targetDiff;
const data = JSON.stringify({
@@ -626,7 +672,10 @@ export class StratumV1Client {
}) + '\n';
await this.socket.write(data);
const success = await this.write(data);
if (!success) {
return;
}
const jobTemplate = await firstValueFrom(this.stratumV1JobsService.newMiningJob$);
// we need to clear the jobs so that the difficulty set takes effect. Otherwise the different miner implementations can cause issues