Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
argonmining committed Aug 22, 2024
1 parent afe0bd1 commit 967c7c5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
14 changes: 11 additions & 3 deletions src/monitoring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ interface LogJobData {

export default class Monitoring {
private logQueue: PQueue;
private debugEnabled: boolean;

constructor() {
constructor(debugEnabled: boolean = false) {
this.logQueue = new PQueue({ concurrency: 1 });
this.debugEnabled = debugEnabled;
}

setDebugEnabled(enabled: boolean) {
this.debugEnabled = enabled;
}

log(message: string) {
this.logQueue.add(() => this.processLog({ level: 'LOG', message }));
}

debug(message: string) {
this.logQueue.add(() => this.processLog({ level: 'DEBUG', message }));
if (this.debugEnabled) {
this.logQueue.add(() => this.processLog({ level: 'DEBUG', message }));
}
}

error(message: string) {
Expand All @@ -38,4 +46,4 @@ export default class Monitoring {
async waitForQueueToDrain() {
await this.logQueue.onIdle();
}
}
}
5 changes: 4 additions & 1 deletion src/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class Pool {
private sharesManager: SharesManager; // Add SharesManager property
private pushMetrics: PushMetrics; // Add PushMetrics property
private lastProcessedTimestamp = 0; // Add timestamp check
private duplicateEventCount = 0;

constructor(treasury: Treasury, stratum: Stratum, sharesManager: SharesManager) {
this.treasury = treasury;
Expand All @@ -34,10 +35,12 @@ export default class Pool {
this.treasury.on('coinbase', (minerReward: bigint, poolFee: bigint) => {
const currentTimestamp = Date.now();
if (currentTimestamp - this.lastProcessedTimestamp < 1000) { // 1 second cooldown
this.monitoring.debug(`Pool: Skipping duplicate coinbase event. Last processed: ${this.lastProcessedTimestamp}, Current: ${currentTimestamp}`);
this.duplicateEventCount++;
this.monitoring.debug(`Pool: Skipping duplicate coinbase event. Last processed: ${this.lastProcessedTimestamp}, Current: ${currentTimestamp}, Duplicate count: ${this.duplicateEventCount}`);
return;
}
this.lastProcessedTimestamp = currentTimestamp;
this.duplicateEventCount = 0;
this.monitoring.log(`Pool: Processing coinbase event. Timestamp: ${currentTimestamp}`);
this.allocate(minerReward, poolFee).catch(console.error)
});
Expand Down
45 changes: 40 additions & 5 deletions src/stratum/sharesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,41 @@ export class SharesManager {

const share = { minerId, address, difficulty, timestamp: Date.now() };
this.shareWindow.push(share);

// Implement variable difficulty
this.updateDifficulty(minerId);
}

private updateDifficulty(minerId: string): void {
const workerStats = this.miners.get(minerId)?.workerStats;
if (!workerStats) return;

const now = Date.now();
const elapsedMs = now - workerStats.varDiffStartTime;

if (elapsedMs >= 120000) { // 120000ms = 2 minutes
const shareRate = workerStats.varDiffSharesFound / (elapsedMs / 1000);
const targetShareRate = 60 / 60; // 60 shares per minute

let newDifficulty = workerStats.minDiff;

if (shareRate > targetShareRate * 1.1) {
newDifficulty *= 1.1;
} else if (shareRate < targetShareRate * 0.9) {
newDifficulty /= 1.1;
}

newDifficulty = Math.max(newDifficulty, 1);

if (newDifficulty !== workerStats.minDiff) {
workerStats.minDiff = newDifficulty;
this.monitoring.log(`SharesManager: Updated difficulty for ${minerId} to ${newDifficulty}`);
varDiff.labels(minerId).set(newDifficulty);
}

workerStats.varDiffStartTime = now;
workerStats.varDiffSharesFound = 0;
}
}

startStatsThread() {
Expand Down Expand Up @@ -297,7 +332,7 @@ export class SharesManager {
const shareRate = (sharesFound / elapsedSeconds) * 60; // Convert to per minute
const targetRate = sharesPerMin;

if (DEBUG) this.monitoring.debug(`shareManager - VarDiff for ${stats.workerName}: sharesFound: ${sharesFound}, elapsedSeconds: ${elapsedSeconds}, shareRate: ${shareRate}, targetRate: ${targetRate}, currentDiff: ${stats.minDiff}`);
if (DEBUG) this.monitoring.debug(`SharesManager - VarDiff for ${stats.workerName}: sharesFound: ${sharesFound}, elapsedSeconds: ${elapsedSeconds}, shareRate: ${shareRate}, targetRate: ${targetRate}, currentDiff: ${stats.minDiff}`);

let newDiff = stats.minDiff;

Expand All @@ -314,19 +349,19 @@ export class SharesManager {
newDiff = Math.max(newDiff, minDifficulty);

if (newDiff !== stats.minDiff) {
this.monitoring.debug(`shareManager: VarDiff - Adjusting difficulty for ${stats.workerName} from ${stats.minDiff} to ${newDiff}`);
this.monitoring.debug(`SharesManager: VarDiff - Adjusting difficulty for ${stats.workerName} from ${stats.minDiff} to ${newDiff}`);
stats.minDiff = newDiff;
this.updateSocketDifficulty(address, newDiff);
varDiff.labels(stats.workerName).set(newDiff);
} else {
this.monitoring.debug(`shareManager: VarDiff - No change in difficulty for ${stats.workerName} (current difficulty: ${stats.minDiff})`);
this.monitoring.debug(`SharesManager: VarDiff - No change in difficulty for ${stats.workerName} (current difficulty: ${stats.minDiff})`);
}

stats.varDiffSharesFound = 0;
stats.varDiffStartTime = now;

if (varDiffStats) {
this.monitoring.log(`shareManager: VarDiff for ${stats.workerName}: sharesFound: ${sharesFound}, elapsed: ${elapsedSeconds.toFixed(2)}, shareRate: ${shareRate.toFixed(2)}, newDiff: ${stats.minDiff}`);
metrics.updateGaugeValue(varDiff, [stats.workerName], stats.minDiff);
this.monitoring.log(`SharesManager: VarDiff for ${stats.workerName}: sharesFound: ${sharesFound}, elapsed: ${elapsedSeconds.toFixed(2)}, shareRate: ${shareRate.toFixed(2)}, newDiff: ${stats.minDiff}`);
}
});
}, intervalMs);
Expand Down
2 changes: 1 addition & 1 deletion src/stratum/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ export default class Templates {

await this.rpc.subscribeNewBlockTemplate()
}
}
}

0 comments on commit 967c7c5

Please sign in to comment.