Compare commits

3 Commits
Author SHA1 Message Date
Ben c5169e105e add stratum worker backpressure 2026-05-07 23:56:55 -04:00
Ben 2d6d2afacb dead code 2026-05-07 23:52:56 -04:00
Ben 327be48ade update postgresql branch 2026-05-07 23:45:07 -04:00
15 changed files with 332 additions and 441 deletions
@@ -15,17 +15,14 @@ export class AddressSettingsService {
}
public async getSettings(address: string, createIfNotFound: boolean) {
let settings = await this.addressSettingsRepository.findOne({ where: { address } });
if (createIfNotFound === true && settings == null) {
await this.addressSettingsRepository
.createQueryBuilder()
.insert()
.into(AddressSettingsEntity)
.values({ address })
.orIgnore()
.execute();
settings = await this.addressSettingsRepository.findOne({ where: { address } });
const settings = await this.addressSettingsRepository.findOne({ where: { address } });
if (createIfNotFound == true && settings == null) {
// It's possible to have a race condition here so if we get a PK violation, fetch it
try {
return await this.createNew(address);
} catch (e) {
return await this.addressSettingsRepository.findOne({ where: { address } });
}
}
return settings;
}
@@ -84,4 +81,4 @@ export class AddressSettingsService {
.limit(10)
.execute();
}
}
}
-38
View File
@@ -1,38 +0,0 @@
import { DateTimeTransformer } from './DateTimeTransformer';
describe('DateTimeTransformer', () => {
const transformer = new DateTimeTransformer();
it('should pass Date values through as Date instances', () => {
const date = new Date('2026-05-06T12:00:00.000Z');
expect(transformer.to(date)).toBe(date);
expect(transformer.from(date)).toBe(date);
});
it('should parse ISO strings from the database', () => {
const date = transformer.from('2026-05-06T12:00:00.000Z');
expect(date).toBeInstanceOf(Date);
expect((date as Date).toISOString()).toBe('2026-05-06T12:00:00.000Z');
});
it('should parse legacy locale strings', () => {
const usDate = transformer.from('5/6/2026, 1:02:03 PM') as Date;
const europeanDate = transformer.from('6.5.2026, 13:02:03') as Date;
expect(usDate.getFullYear()).toBe(2026);
expect(usDate.getMonth()).toBe(4);
expect(usDate.getDate()).toBe(6);
expect(usDate.getHours()).toBe(13);
expect(europeanDate.getFullYear()).toBe(2026);
expect(europeanDate.getMonth()).toBe(4);
expect(europeanDate.getDate()).toBe(6);
expect(europeanDate.getHours()).toBe(13);
});
it('should preserve null and undefined values', () => {
expect(transformer.to(null)).toBeNull();
expect(transformer.from(undefined)).toBeUndefined();
});
});
+8 -107
View File
@@ -1,113 +1,14 @@
import { ValueTransformer } from 'typeorm';
const EUROPEAN_LOCALE_REGEX =
/^(\d{1,2})\.(\d{1,2})\.(\d{4}),\s*(\d{1,2}):(\d{2})(?::(\d{2}))?(?:\s*(AM|PM))?$/i;
const US_LOCALE_REGEX =
/^(\d{1,2})\/(\d{1,2})\/(\d{4}),\s*(\d{1,2}):(\d{2})(?::(\d{2}))?\s*(AM|PM)$/i;
function parseLegacyLocaleString(value: string): Date | null {
const trimmed = value.trim();
const matchEuropean = trimmed.match(EUROPEAN_LOCALE_REGEX);
if (matchEuropean) {
const [, day, month, year, hours, minutes, seconds = '0', meridiem] = matchEuropean;
return buildDateFromParts({
year,
month,
day,
hours,
minutes,
seconds,
meridiem,
});
export class DateTimeTransformer implements ValueTransformer {
to(value: Date): any {
// Convert the local time to UTC before saving to the database
const utcTime = value?.toLocaleString();
return utcTime;
}
const matchUs = trimmed.match(US_LOCALE_REGEX);
if (matchUs) {
const [, month, day, year, hours, minutes, seconds = '0', meridiem] = matchUs;
return buildDateFromParts({
year,
month,
day,
hours,
minutes,
seconds,
meridiem,
});
}
return null;
}
function buildDateFromParts({
year,
month,
day,
hours,
minutes,
seconds,
meridiem,
}: {
year: string;
month: string;
day: string;
hours: string;
minutes: string;
seconds: string;
meridiem?: string;
}): Date {
let hourValue = Number(hours);
if (meridiem) {
const normalized = meridiem.toUpperCase();
if (normalized === 'PM' && hourValue < 12) {
hourValue += 12;
} else if (normalized === 'AM' && hourValue === 12) {
hourValue = 0;
}
}
return new Date(
Number(year),
Number(month) - 1,
Number(day),
hourValue,
Number(minutes),
Number(seconds),
);
}
function normalizeDate(value: Date | string): Date {
if (value instanceof Date) {
from(value: any): Date {
// Convert the UTC time from the database to the local time zone
return value;
}
const legacyDate = parseLegacyLocaleString(value);
if (legacyDate) {
return legacyDate;
}
const date = new Date(value);
if (!Number.isNaN(date.getTime())) {
return date;
}
throw new Error(`Invalid date value received: ${value}`);
}
export class DateTimeTransformer implements ValueTransformer {
to(value: Date | string | null | undefined): Date | null | undefined {
if (value === null || value === undefined) {
return value === undefined ? undefined : null;
}
return normalizeDate(value);
}
from(value: Date | string | null | undefined): Date | null | undefined {
if (value === null || value === undefined) {
return value === undefined ? undefined : null;
}
return normalizeDate(value);
}
}
}
-28
View File
@@ -109,32 +109,4 @@ describe('MiningJob', () => {
expect(fastHeader.equals(updatedBlock.toBuffer(true))).toBe(true);
});
it('should expose coinbase helpers for downstream validation', () => {
const notify = JSON.parse(job.response(jobTemplate));
expect(job.getCoinbaseTxHex()).toContain(notify.params[2]);
expect(job.getCoinbasePrefixBuffer().toString('hex')).toBe(notify.params[2]);
expect(job.getCoinbaseSuffixBuffer().toString('hex')).toBe(notify.params[3]);
const clone = job.cloneCoinbaseTransaction();
const nonWitnessCoinbase = bitcoinjs.Transaction.fromHex(job.getCoinbaseTxHex());
expect(clone.ins[0].script.toString('hex')).toBe(nonWitnessCoinbase.ins[0].script.toString('hex'));
expect(clone.outs).toHaveLength(nonWitnessCoinbase.outs.length);
expect(clone.ins[0].witness[0]).toHaveLength(32);
});
it('should omit oversized pool identifiers from the coinbase script', () => {
jest.spyOn(console, 'warn').mockImplementation(() => undefined);
const oversizedIdentifier = 'x'.repeat(120);
const oversizedJob = new MiningJob(
bitcoinjs.networks.testnet,
'2',
[{ address: 'tb1qumezefzdeqqwn5zfvgdrhxjzc5ylr39uhuxcz4', percent: 100 }],
jobTemplate,
oversizedIdentifier
);
const coinbase = bitcoinjs.Transaction.fromHex(oversizedJob.getCoinbaseTxHex());
expect(coinbase.ins[0].script.toString()).not.toContain(oversizedIdentifier);
});
});
+22 -41
View File
@@ -3,10 +3,9 @@ import * as bitcoinjs from 'bitcoinjs-lib';
import { IJobTemplate } from '../services/stratum-v1-jobs.service';
import { eResponseMethod } from './enums/eResponseMethod';
import { IMiningNotify } from './stratum-messages/IMiningNotify';
import { TOTAL_EXTRANONCE_SIZE_BYTES } from './stratum.constants';
const MAX_BLOCK_WEIGHT = 4000000;
const MAX_SCRIPT_SIZE = 100;
interface AddressObject {
address: string;
@@ -20,7 +19,6 @@ export class MiningJob {
private coinbasePart1Buffer: Buffer;
private coinbasePart2Buffer: Buffer;
private merkleBranchBuffers: Buffer[];
private notifyStaticParams: string;
public jobTemplateId: string;
public networkDifficulty: number;
@@ -30,8 +28,7 @@ export class MiningJob {
private network: bitcoinjs.networks.Network,
public jobId: string,
payoutInformation: AddressObject[],
jobTemplate: IJobTemplate,
poolIdentifier: string = process.env.POOL_IDENTIFIER || 'Public-Pool'
jobTemplate: IJobTemplate
) {
this.creation = new Date().getTime();
@@ -48,7 +45,7 @@ export class MiningJob {
// 32-byte - Commitment hash: Double-SHA256(witness root hash|witness reserved value)
// 39th byte onwards: Optional data with no consensus meaning
const extra = Buffer.from(poolIdentifier);
const extra = Buffer.from('Public-Pool');
// Encode the block height
// https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki
@@ -61,21 +58,10 @@ export class MiningJob {
const padding = Buffer.alloc(TOTAL_EXTRANONCE_SIZE_BYTES + (3 - blockHeightEncoded.length), 0)
// build the script
let script = Buffer.concat([blockHeightLengthByte, blockHeightEncoded, extra, padding]);
if (script.length > MAX_SCRIPT_SIZE) {
console.warn('Pool identifier is too long, removing the pool identifier');
script = Buffer.concat([blockHeightLengthByte, blockHeightEncoded, padding]);
}
this.coinbaseTransaction.ins[0].script = script;
this.coinbaseTransaction.ins[0].script = Buffer.concat([blockHeightLengthByte, blockHeightEncoded, extra, padding])
this.coinbaseTransaction.addOutput(bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.concat([segwitMagicBits, jobTemplate.block.witnessCommit])]), 0);
if ((this.coinbaseTransaction.weight() + jobTemplate.block.weight()) > MAX_BLOCK_WEIGHT) {
console.warn('Block weight exceeds the maximum allowed weight, removing the pool identifier');
this.coinbaseTransaction.ins[0].script = Buffer.concat([blockHeightLengthByte, blockHeightEncoded, padding]);
}
// get the non-witness coinbase tx
//@ts-ignore
const serializedCoinbaseTx = this.coinbaseTransaction.__toBuffer().toString('hex');
@@ -88,30 +74,8 @@ export class MiningJob {
this.coinbasePart2 = serializedCoinbaseTx.slice(partOneIndex);
this.coinbasePart1Buffer = Buffer.from(this.coinbasePart1, 'hex');
this.coinbasePart2Buffer = Buffer.from(this.coinbasePart2, 'hex');
this.notifyStaticParams = JSON.stringify([
this.swapEndianWords(jobTemplate.block.prevHash).toString('hex'),
this.coinbasePart1,
this.coinbasePart2,
jobTemplate.merkle_branch,
jobTemplate.block.version.toString(16),
jobTemplate.block.bits.toString(16),
jobTemplate.block.timestamp.toString(16),
jobTemplate.blockData.clearJobs
]).slice(1);
}
public getCoinbaseTxHex(): string {
//@ts-ignore
return this.coinbaseTransaction.__toBuffer().toString('hex');
}
public getCoinbasePrefixBuffer(): Buffer {
return Buffer.from(this.coinbasePart1Buffer);
}
public getCoinbaseSuffixBuffer(): Buffer {
return Buffer.from(this.coinbasePart2Buffer);
}
public cloneCoinbaseTransaction(): bitcoinjs.Transaction {
@@ -245,7 +209,24 @@ export class MiningJob {
}
public response(jobTemplate: IJobTemplate): string {
return `{"id":null,"method":"${eResponseMethod.MINING_NOTIFY}","params":[${JSON.stringify(this.jobId)},${this.notifyStaticParams}}\n`;
const job: IMiningNotify = {
id: null,
method: eResponseMethod.MINING_NOTIFY,
params: [
this.jobId,
this.swapEndianWords(jobTemplate.block.prevHash).toString('hex'),
this.coinbasePart1,
this.coinbasePart2,
jobTemplate.merkle_branch,
jobTemplate.block.version.toString(16),
jobTemplate.block.bits.toString(16),
jobTemplate.block.timestamp.toString(16),
jobTemplate.blockData.clearJobs
]
};
return JSON.stringify(job) + '\n';
}
+2 -62
View File
@@ -21,7 +21,7 @@ import { NotificationService } from '../services/notification.service';
import { StratumV1JobsService } from '../services/stratum-v1-jobs.service';
import { IBlockTemplate } from './bitcoin-rpc/IBlockTemplate';
import { MiningJob } from './MiningJob';
import { effectiveJobDifficulty, StratumV1Client } from './StratumV1Client';
import { StratumV1Client } from './StratumV1Client';
import { MiningSubmitMessage } from './stratum-messages/MiningSubmitMessage';
@@ -199,27 +199,6 @@ describe('StratumV1Client', () => {
expect(socket.on).toHaveBeenCalled();
});
it('should process batched socket messages sequentially', async () => {
const calls: string[] = [];
jest.spyOn(client as any, 'handleMessage').mockImplementation(async (message: string) => {
calls.push(`start:${message}`);
await Promise.resolve();
calls.push(`end:${message}`);
});
socketEmitter(Buffer.from('first\nsecond\n'));
await Promise.resolve();
await Promise.resolve();
await Promise.resolve();
expect(calls).toEqual([
'start:first',
'end:first',
'start:second',
'end:second',
]);
});
it('should respond to mining.subscribe', async () => {
jest.spyOn(socket, 'write').mockImplementation((data) => true);
@@ -597,14 +576,12 @@ describe('StratumV1Client', () => {
it('should submit and persist found blocks', async () => {
jest.spyOn(client as any, 'write').mockImplementation((data) => Promise.resolve(true));
(bitcoinRpcService.SUBMIT_BLOCK as jest.Mock).mockResolvedValue('SUCCESS!');
jest.spyOn(client as any, 'calculateDifficulty').mockReturnValue({
submissionDifficulty: Number.MAX_SAFE_INTEGER,
submissionHash: 'block-share'
});
const addressSettings = moduleRef.get<AddressSettingsService>(AddressSettingsService);
const resetSpy = jest.spyOn(addressSettings, 'resetBestDifficultyAndShares').mockResolvedValue(undefined);
resetSpy.mockClear();
jest.spyOn(addressSettings, 'resetBestDifficultyAndShares').mockResolvedValue(undefined);
emitMessage(MockRecording1.MINING_SUBSCRIBE);
emitMessage(`{"id": 4, "method": "mining.suggest_difficulty", "params": [0]}`);
@@ -628,43 +605,6 @@ describe('StratumV1Client', () => {
expect((client as any).write).lastCalledWith(`{"id":5,"error":null,"result":true}\n`);
});
it('should not persist or notify rejected block submissions', async () => {
jest.spyOn(client as any, 'write').mockImplementation((data) => Promise.resolve(true));
(bitcoinRpcService.SUBMIT_BLOCK as jest.Mock).mockResolvedValue('bad-prevblk');
jest.spyOn(client as any, 'calculateDifficulty').mockReturnValue({
submissionDifficulty: Number.MAX_SAFE_INTEGER,
submissionHash: 'block-share'
});
const addressSettings = moduleRef.get<AddressSettingsService>(AddressSettingsService);
const resetSpy = jest.spyOn(addressSettings, 'resetBestDifficultyAndShares').mockResolvedValue(undefined);
resetSpy.mockClear();
emitMessage(MockRecording1.MINING_SUBSCRIBE);
emitMessage(`{"id": 4, "method": "mining.suggest_difficulty", "params": [0]}`);
emitMessage(MockRecording1.MINING_AUTHORIZE);
await new Promise((r) => setTimeout(r, 100));
emitMessage(MockRecording1.MINING_SUBMIT);
jest.useRealTimers();
await new Promise((r) => setTimeout(r, 1000));
expect(bitcoinRpcService.SUBMIT_BLOCK).toHaveBeenCalledWith(expect.any(String));
expect(blocksService.save).not.toHaveBeenCalled();
expect(notificationService.notifySubscribersBlockFound).not.toHaveBeenCalled();
expect(resetSpy).not.toHaveBeenCalled();
expect((client as any).write).lastCalledWith(`{"id":5,"error":null,"result":true}\n`);
});
});
describe('effectiveJobDifficulty', () => {
it('should use the old difficulty for jobs issued before a difficulty change', () => {
expect(effectiveJobDifficulty(9, 128, 32, 10)).toBe(32);
});
it('should use the current difficulty for jobs issued after a difficulty change', () => {
expect(effectiveJobDifficulty(10, 128, 32, 10)).toBe(128);
});
});
+14 -51
View File
@@ -32,22 +32,6 @@ const TRUE_DIFF_ONE = 2.695953529101131e67;
const BLOCKED_USER_AGENT_LOG_INTERVAL_MS = 60 * 1000;
const VALIDATION_ERROR_LOG_INTERVAL_MS = 60 * 1000;
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 {
private static blockedUserAgentLogState = new Map<string, { nextLogAt: number, suppressed: number }>();
private static validationErrorLogState = new Map<string, { nextLogAt: number, suppressed: number, sample: string }>();
@@ -63,8 +47,6 @@ 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>;
@@ -255,7 +237,6 @@ 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) {
@@ -297,7 +278,6 @@ 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;
@@ -395,7 +375,6 @@ export class StratumV1Client {
switch (this.clientSubscription.userAgent) {
case 'cpuminer': {
this.sessionDifficulty = 0.1;
this.oldSessionDifficulty = this.sessionDifficulty;
}
}
@@ -478,8 +457,7 @@ export class StratumV1Client {
network,
this.stratumV1JobsService.getNextId(),
payoutInformation,
jobTemplate,
this.configService.get('POOL_IDENTIFIER') || 'Public-Pool'
jobTemplate
);
this.stratumV1JobsService.addJob(job);
@@ -586,15 +564,9 @@ 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 >= effectiveDiff) {
if (submissionDifficulty >= this.sessionDifficulty) {
const success = await this.write(JSON.stringify(submission.response()) + '\n');
if (!success) {
return false;
@@ -612,24 +584,23 @@ export class StratumV1Client {
);
const blockHex = updatedJobBlock.toHex(false);
const result = await this.bitcoinRpcService.SUBMIT_BLOCK(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.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);
await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address, jobTemplate.blockData.height, updatedJobBlock, result);
//success
if (result == null) {
await this.addressSettingsService.resetBestDifficultyAndShares();
} else {
console.warn(`[Block submit rejected at height ${jobTemplate.blockData.height}]: ${result}`);
}
}
await this.ensureClientEntity();
try {
await this.statistics.addShares(this.clientEntity, effectiveDiff);
await this.statistics.addShares(this.clientEntity, this.sessionDifficulty);
const now = new Date();
// only update every minute
//if (this.clientEntity.updatedAt == null || now.getTime() - this.clientEntity.updatedAt.getTime() > 1000 * 60) {
@@ -676,11 +647,6 @@ 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({
@@ -690,10 +656,7 @@ export class StratumV1Client {
}) + '\n';
const success = await this.write(data);
if (!success) {
return;
}
await this.socket.write(data);
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
+3 -3
View File
@@ -81,10 +81,10 @@ describe('StratumV1ClientStatistics', () => {
expect(statistics.getSuggestedDifficulty(64)).toBeNull();
});
it('should lower difficulty when a miner has not submitted shares for more than a minute', () => {
jest.setSystemTime(new Date('2026-05-06T12:01:01Z'));
it('should lower difficulty when a miner has not submitted shares for several minutes', () => {
jest.setSystemTime(new Date('2026-05-06T12:06:00Z'));
expect(statistics.getSuggestedDifficulty(64)).toBe(12);
expect(statistics.getSuggestedDifficulty(64)).toBe(8);
});
it('should increase difficulty for rapid submissions', async () => {
+19 -25
View File
@@ -104,8 +104,8 @@ export class StratumV1ClientStatistics {
// miner hasn't submitted shares in one minute
if (this.submissionCache.length < 5) {
if ((new Date().getTime() - this.submissionCacheStart.getTime()) / 1000 > 60) {
return this.nearestDifficultyStep(clientDifficulty / 6);
if ((new Date().getTime() - this.submissionCacheStart.getTime()) / 5000 > 60) {
return this.nearestPowerOfTwo(clientDifficulty / 6);
} else {
return null;
}
@@ -116,45 +116,39 @@ export class StratumV1ClientStatistics {
return pre;
}, 0);
const diffSeconds = (this.submissionCache[this.submissionCache.length - 1].time.getTime() - this.submissionCache[0].time.getTime()) / 1000;
if (diffSeconds <= 0) {
return null;
}
const difficultyPerSecond = sum / diffSeconds;
const targetDifficulty = difficultyPerSecond * this.targetSubmitShareEveryNSeconds;
if (!Number.isFinite(difficultyPerSecond) || !Number.isFinite(targetDifficulty)) {
return null;
}
if ((clientDifficulty * 2) < targetDifficulty || (clientDifficulty / 2) > targetDifficulty) {
return this.nearestDifficultyStep(targetDifficulty)
return this.nearestPowerOfTwo(targetDifficulty)
}
return null;
}
private nearestDifficultyStep(val: number): number {
private nearestPowerOfTwo(val): number {
if (val === 0) {
return null;
}
if (val < MIN_DIFF) {
return MIN_DIFF;
}
const exponent = Math.floor(Math.log2(val));
const lower = 2 ** exponent;
const middle = lower + lower / 2;
const upper = lower * 2;
const distances = [
{ value: lower, diff: Math.abs(val - lower) },
{ value: middle, diff: Math.abs(val - middle) },
{ value: upper, diff: Math.abs(val - upper) },
];
distances.sort((a, b) => a.diff - b.diff);
return distances[0].value;
let x = val | (val >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
x = x | (x >> 32);
const res = x - (x >> 1);
if (res == 0 && val * 100 < MIN_DIFF) {
return MIN_DIFF;
}
if (res == 0) {
return this.nearestPowerOfTwo(val * 100) / 100;
}
return res;
}
}
}
@@ -58,29 +58,6 @@ describe('MiningSubmitMessage', () => {
expect(errors.some(error => error.property === 'extraNonce2')).toBe(true);
});
it('should hash submit fields without concatenation ambiguity', () => {
const first = plainToInstance(
MiningSubmitMessage,
{
id: 5,
method: 'mining.submit',
params: ['worker', 'e', '9902000000000000', 'd', 'bc', 'a']
},
);
const second = plainToInstance(
MiningSubmitMessage,
{
id: 5,
method: 'mining.submit',
params: ['worker', 'e', '9902000000000000', 'd', 'c', 'ab']
},
);
expect(first.versionMask + first.nonce + first.extraNonce2 + first.ntime + first.jobId)
.toBe(second.versionMask + second.nonce + second.extraNonce2 + second.ntime + second.jobId);
expect(first.hash()).not.toBe(second.hash());
});
});
@@ -68,14 +68,8 @@ export class MiningSubmitMessage extends StratumBaseMessage {
}
public hash(): string{
const canonical = JSON.stringify({
versionMask: this.versionMask ?? '',
nonce: this.nonce,
extraNonce2: this.extraNonce2,
ntime: this.ntime,
jobId: this.jobId,
});
return bitcoinjs.crypto.hash256(Buffer.from(canonical)).toString('base64');
const buffer = Buffer.from(this.versionMask + this.nonce + this.extraNonce2 + this.ntime + this.jobId);
return bitcoinjs.crypto.hash256(buffer).toString('base64');
}
+2 -10
View File
@@ -8,7 +8,6 @@ import * as zmq from 'zeromq';
import { IBlockTemplate } from '../models/bitcoin-rpc/IBlockTemplate';
import { IMiningInfo } from '../models/bitcoin-rpc/IMiningInfo';
import * as PGPubsub from 'pg-pubsub';
import * as fs from 'node:fs';
@Injectable()
export class BitcoinRpcService implements OnModuleInit {
@@ -36,17 +35,10 @@ export class BitcoinRpcService implements OnModuleInit {
const url = this.configService.get('BITCOIN_RPC_URL');
let user = this.configService.get('BITCOIN_RPC_USER');
let pass = this.configService.get('BITCOIN_RPC_PASSWORD');
const user = this.configService.get('BITCOIN_RPC_USER');
const pass = this.configService.get('BITCOIN_RPC_PASSWORD');
const port = parseInt(this.configService.get('BITCOIN_RPC_PORT'));
const timeout = parseInt(this.configService.get('BITCOIN_RPC_TIMEOUT'));
const cookiefile = this.configService.get('BITCOIN_RPC_COOKIEFILE');
if (cookiefile != null && cookiefile !== '') {
const [cookieUser, cookiePass] = fs.readFileSync(cookiefile).toString().trim().split(':');
user = cookieUser;
pass = cookiePass;
}
const baseURL = this.buildRpcUrl(url, port);
this.client = axios.create({
+23 -27
View File
@@ -109,7 +109,29 @@ export class StratumV1JobsService {
}
}),
tap((data) => {
this.cleanup(data.blockData.clearJobs);
if (data.blockData.clearJobs) {
this.blocks = {};
this.jobs = {};
}else{
let templatesDeleted = 0;
let jobsDeleted = 0;
const now = new Date().getTime();
// Delete old templates (5 minutes)
for(const templateId in this.blocks){
if(now - this.blocks[templateId].blockData.creation > (1000 * 60 * 5)){
delete this.blocks[templateId];
templatesDeleted++;
}
}
// Delete old jobs (5 minutes)
for (const jobId in this.jobs) {
if(now - this.jobs[jobId].creation > (1000 * 60 * 5)){
delete this.jobs[jobId];
jobsDeleted++;
}
}
//console.log(`Deleted ${templatesDeleted} templates and ${jobsDeleted} jobs.`)
}
this.blocks[data.blockData.id] = data;
}),
shareReplay({ refCount: true, bufferSize: 1 })
@@ -140,32 +162,6 @@ export class StratumV1JobsService {
return this.blocks[jobTemplateId];
}
public cleanup(clearJobs: boolean, now: number = Date.now()) {
if (clearJobs) {
this.blocks = {};
this.jobs = {};
return;
}
let templatesDeleted = 0;
let jobsDeleted = 0;
for (const templateId in this.blocks) {
if (now - this.blocks[templateId].blockData.creation > (1000 * 60 * 5)) {
delete this.blocks[templateId];
templatesDeleted++;
}
}
for (const jobId in this.jobs) {
if (now - this.jobs[jobId].creation > (1000 * 60 * 5)) {
delete this.jobs[jobId];
jobsDeleted++;
}
}
//console.log(`Deleted ${templatesDeleted} templates and ${jobsDeleted} jobs.`)
}
public addJob(job: MiningJob) {
this.jobs[job.jobId] = job;
this.latestJobId++;
+51
View File
@@ -5,10 +5,12 @@ describe('StratumV1Service', () => {
const originalStratumPorts = process.env.STRATUM_PORTS;
const originalStratumSecure = process.env.STRATUM_SECURE;
const originalSecureStratumPorts = process.env.SECURE_STRATUM_PORTS;
const originalBackpressureEnabled = process.env.STRATUM_BACKPRESSURE_ENABLED;
let service: StratumV1Service;
let clientService;
let consoleLogSpy: jest.SpyInstance;
let consoleWarnSpy: jest.SpyInstance;
beforeEach(() => {
jest.useFakeTimers();
@@ -26,6 +28,7 @@ describe('StratumV1Service', () => {
{} as any
);
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined);
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
});
afterEach(() => {
@@ -33,7 +36,9 @@ describe('StratumV1Service', () => {
restoreEnv('STRATUM_PORTS', originalStratumPorts);
restoreEnv('STRATUM_SECURE', originalStratumSecure);
restoreEnv('SECURE_STRATUM_PORTS', originalSecureStratumPorts);
restoreEnv('STRATUM_BACKPRESSURE_ENABLED', originalBackpressureEnabled);
consoleLogSpy.mockRestore();
consoleWarnSpy.mockRestore();
jest.useRealTimers();
});
@@ -68,6 +73,52 @@ describe('StratumV1Service', () => {
expect(startSecureSocketServerSpy).toHaveBeenCalledWith(4333);
});
it('should pause listeners when worker backpressure is high', () => {
const close = jest.fn((callback?: (error?: Error) => void) => callback?.());
(service as any).listeners.push({
port: 3333,
secure: false,
server: { close },
paused: false
});
jest.spyOn(service as any, 'getEventLoopP95Ms').mockReturnValue(5000);
jest.spyOn(service as any, 'getBackpressureEventLoopP95Ms').mockReturnValue(2000);
(service as any).checkBackpressure();
expect(close).toHaveBeenCalled();
expect((service as any).listeners[0].paused).toBe(true);
expect((service as any).listeners[0].server).toBeNull();
expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('Pausing Stratum accepts'));
});
it('should resume listeners after consecutive healthy backpressure checks', () => {
(service as any).listeners.push({
port: 3333,
secure: false,
server: null,
paused: true
});
jest.spyOn(service as any, 'getEventLoopP95Ms').mockReturnValue(50);
jest.spyOn(service as any, 'getBackpressureEventLoopP95Ms').mockReturnValue(2000);
jest.spyOn(service as any, 'getBackpressureResumeEventLoopP95Ms').mockReturnValue(250);
jest.spyOn(service as any, 'getBackpressureResumeRssMb').mockReturnValue(Number.MAX_SAFE_INTEGER);
jest.spyOn(service as any, 'getBackpressureHealthyChecks').mockReturnValue(2);
const listenSpy = jest.spyOn(service as any, 'listen').mockImplementation((listener: any) => {
listener.server = {};
listener.paused = false;
});
(service as any).checkBackpressure();
expect(listenSpy).not.toHaveBeenCalled();
(service as any).checkBackpressure();
expect(listenSpy).toHaveBeenCalledWith((service as any).listeners[0]);
expect((service as any).listeners[0].paused).toBe(false);
expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('Resuming Stratum accepts'));
});
function restoreEnv(key: string, value: string | undefined) {
if (value == null) {
delete process.env[key];
+177 -6
View File
@@ -1,6 +1,7 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Server, Socket } from 'net';
import { monitorEventLoopDelay } from 'perf_hooks';
import { StratumV1Client } from '../models/StratumV1Client';
import { AddressSettingsService } from '../ORM/address-settings/address-settings.service';
@@ -15,6 +16,20 @@ import { readFileSync } from 'fs';
import { TlsOptions, TLSSocket, createServer } from 'tls';
import * as path from 'path';
interface StratumListenerState {
port: number;
secure: boolean;
server: Server | null;
paused: boolean;
}
const DEFAULT_BACKPRESSURE_CHECK_INTERVAL_MS = 5000;
const DEFAULT_BACKPRESSURE_EVENT_LOOP_P95_MS = 2000;
const DEFAULT_BACKPRESSURE_EVENT_LOOP_RESUME_P95_MS = 250;
const DEFAULT_BACKPRESSURE_RSS_MB = 2500;
const DEFAULT_BACKPRESSURE_RESUME_RSS_MB = 2000;
const DEFAULT_BACKPRESSURE_HEALTHY_CHECKS = 3;
@Injectable()
@@ -24,6 +39,10 @@ export class StratumV1Service implements OnModuleInit {
private emptySocket = 0;
private normalClosure = 0;
private errorClosure = 0;
private readonly listeners: StratumListenerState[] = [];
private readonly eventLoopDelay = monitorEventLoopDelay({ resolution: 20 });
private backpressureMonitor: NodeJS.Timeout | null = null;
private healthyBackpressureChecks = 0;
constructor(
private readonly bitcoinRpcService: BitcoinRpcService,
@@ -66,9 +85,22 @@ export class StratumV1Service implements OnModuleInit {
this.errorClosure = 0;
}, 1000 * 60);
this.startBackpressureMonitor();
}
private startSocketServer(port: number) {
const listener: StratumListenerState = {
port,
secure: false,
server: null,
paused: false
};
this.listeners.push(listener);
this.listen(listener);
}
private createSocketServer(): Server {
const server = new Server(async (socket: Socket) => {
// Set 15-minute timeout
socket.setTimeout(1000 * 60 * 15);
@@ -131,13 +163,21 @@ export class StratumV1Service implements OnModuleInit {
console.error(`Server error: ${err.message}`);
});
server.listen(port, () => {
console.log(`Stratum server is listening on port ${port}`);
});
return server;
}
private startSecureSocketServer(port: number) {
const listener: StratumListenerState = {
port,
secure: true,
server: null,
paused: false
};
this.listeners.push(listener);
this.listen(listener);
}
private createSecureSocketServer(): Server {
const currentDirectory = process.cwd();
const keyPath = path.join(currentDirectory, 'secrets', 'key.pem');
@@ -203,9 +243,140 @@ export class StratumV1Service implements OnModuleInit {
console.error(`Server error: ${err.message}`);
});
server.listen(port, () => {
console.log(`Stratum TLS server is listening on port ${port}`);
return server;
}
private listen(listener: StratumListenerState) {
if (listener.server != null) {
return;
}
const server = listener.secure ? this.createSecureSocketServer() : this.createSocketServer();
listener.server = server;
listener.paused = false;
server.listen(listener.port, () => {
console.log(`${listener.secure ? 'Stratum TLS' : 'Stratum'} server is listening on port ${listener.port}`);
});
}
private startBackpressureMonitor() {
if (this.isBackpressureDisabled() || this.backpressureMonitor != null) {
return;
}
this.eventLoopDelay.enable();
this.backpressureMonitor = setInterval(() => {
this.checkBackpressure();
}, this.getBackpressureCheckIntervalMs());
}
private checkBackpressure() {
const eventLoopP95Ms = this.getEventLoopP95Ms();
const rssMb = Math.round(process.memoryUsage().rss / 1024 / 1024);
const overloaded = eventLoopP95Ms >= this.getBackpressureEventLoopP95Ms()
|| rssMb >= this.getBackpressureRssMb();
const paused = this.listeners.some(listener => listener.paused);
if (overloaded) {
this.healthyBackpressureChecks = 0;
if (!paused) {
this.pauseAccepting(eventLoopP95Ms, rssMb);
}
this.eventLoopDelay.reset();
return;
}
if (!paused) {
this.eventLoopDelay.reset();
return;
}
const healthy = eventLoopP95Ms <= this.getBackpressureResumeEventLoopP95Ms()
&& rssMb <= this.getBackpressureResumeRssMb();
if (!healthy) {
this.healthyBackpressureChecks = 0;
this.eventLoopDelay.reset();
return;
}
this.healthyBackpressureChecks++;
if (this.healthyBackpressureChecks >= this.getBackpressureHealthyChecks()) {
this.resumeAccepting(eventLoopP95Ms, rssMb);
this.healthyBackpressureChecks = 0;
}
this.eventLoopDelay.reset();
}
private pauseAccepting(eventLoopP95Ms: number, rssMb: number) {
console.warn(`Pausing Stratum accepts: eventLoopP95Ms=${eventLoopP95Ms}, rssMb=${rssMb}`);
for (const listener of this.listeners) {
if (listener.paused || listener.server == null) {
continue;
}
const server = listener.server;
listener.server = null;
listener.paused = true;
server.close((error) => {
if (error != null) {
console.error(`Error while pausing Stratum listener on port ${listener.port}: ${error.message}`);
}
});
}
}
private resumeAccepting(eventLoopP95Ms: number, rssMb: number) {
console.warn(`Resuming Stratum accepts: eventLoopP95Ms=${eventLoopP95Ms}, rssMb=${rssMb}`);
for (const listener of this.listeners) {
if (!listener.paused || listener.server != null) {
continue;
}
this.listen(listener);
}
}
private getEventLoopP95Ms() {
return Math.round(this.eventLoopDelay.percentile(95) / 1e6);
}
private isBackpressureDisabled() {
return process.env.STRATUM_BACKPRESSURE_ENABLED?.toLowerCase() === 'false';
}
private getBackpressureCheckIntervalMs() {
return this.getPositiveIntegerEnv('STRATUM_BACKPRESSURE_CHECK_INTERVAL_MS', DEFAULT_BACKPRESSURE_CHECK_INTERVAL_MS);
}
private getBackpressureEventLoopP95Ms() {
return this.getPositiveIntegerEnv('STRATUM_BACKPRESSURE_EVENT_LOOP_P95_MS', DEFAULT_BACKPRESSURE_EVENT_LOOP_P95_MS);
}
private getBackpressureResumeEventLoopP95Ms() {
return this.getPositiveIntegerEnv('STRATUM_BACKPRESSURE_EVENT_LOOP_RESUME_P95_MS', DEFAULT_BACKPRESSURE_EVENT_LOOP_RESUME_P95_MS);
}
private getBackpressureRssMb() {
return this.getPositiveIntegerEnv('STRATUM_BACKPRESSURE_RSS_MB', DEFAULT_BACKPRESSURE_RSS_MB);
}
private getBackpressureResumeRssMb() {
return this.getPositiveIntegerEnv('STRATUM_BACKPRESSURE_RESUME_RSS_MB', DEFAULT_BACKPRESSURE_RESUME_RSS_MB);
}
private getBackpressureHealthyChecks() {
return this.getPositiveIntegerEnv('STRATUM_BACKPRESSURE_HEALTHY_CHECKS', DEFAULT_BACKPRESSURE_HEALTHY_CHECKS);
}
private getPositiveIntegerEnv(key: string, fallback: number) {
const configured = parseInt(process.env[key], 10);
if (Number.isFinite(configured) && configured > 0) {
return configured;
}
return fallback;
}