Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Commit 1669d3f

Browse files
author
ceedii
committed
Improve KAS family job distribution and efficiency for RUST kaspad node
1 parent 947a91a commit 1669d3f

File tree

5 files changed

+66
-30
lines changed

5 files changed

+66
-30
lines changed

src/Miningcore/Blockchain/Kaspa/KaspaConstants.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public static class KaspaConstants
5151

5252
public static class KarlsencoinConstants
5353
{
54-
public const long FishHashForkHeightTestnet = 0;
55-
public const long FishHashPlusForkHeightTestnet = 43200;
56-
public const long FishHashPlusForkHeightMainnet = 26962009;
54+
public const ulong FishHashForkHeightTestnet = 0;
55+
public const ulong FishHashPlusForkHeightTestnet = 43200;
56+
public const ulong FishHashPlusForkHeightMainnet = 26962009;
5757
}
5858

5959
// Pyrin is definitely a scam, use at your own risk
6060
public static class PyrinConstants
6161
{
62-
public const long Blake3ForkHeight = 1484741;
62+
public const ulong Blake3ForkHeight = 1484741;
6363
}
6464

6565
public static class SpectreConstants

src/Miningcore/Blockchain/Kaspa/KaspaJob.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ public virtual Share ProcessShare(StratumConnection worker, string nonce)
367367
public virtual void Init(kaspad.RpcBlock blockTemplate, string jobId, double shareMultiplier)
368368
{
369369
Contract.RequiresNonNull(blockTemplate);
370-
Contract.RequiresNonNull(jobId);
370+
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(jobId));
371+
Contract.RequiresNonNull(shareMultiplier);
371372

372373
JobId = jobId;
373374
this.shareMultiplier = shareMultiplier;

src/Miningcore/Blockchain/Kaspa/KaspaJobManager.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public KaspaJobManager(
5757
private kaspad.KaspadRPC.KaspadRPCClient rpc;
5858
private kaspaWalletd.KaspaWalletdRPC.KaspaWalletdRPCClient walletRpc;
5959
private string network;
60-
private readonly List<KaspaJob> validJobs = new();
6160
private readonly IExtraNonceProvider extraNonceProvider;
6261
private readonly IMasterClock clock;
6362
private KaspaPoolConfigExtra extraPoolConfig;
@@ -142,7 +141,7 @@ public KaspaJobManager(
142141
catch(NullReferenceException)
143142
{
144143
// The following is weird but correct, when all data has been received `streamNotifyNewBlockTemplate.ResponseStream.ReadAllAsync()` will return a `NullReferenceException`
145-
logger.Debug(() => $"Waiting for data...");
144+
logger.Info(() => $"Waiting for `NewBlockTemplate` data...");
146145
goto retry_blocktemplate;
147146
}
148147

@@ -213,7 +212,7 @@ private void SetupJobUpdates(CancellationToken ct)
213212
.RefCount();
214213
}
215214

216-
private KaspaJob CreateJob(long blockHeight)
215+
private KaspaJob CreateJob(ulong blockHeight)
217216
{
218217
switch(coin.Symbol)
219218
{
@@ -370,18 +369,9 @@ private async Task<bool> UpdateJob(CancellationToken ct, string via = null, kasp
370369

371370
if(isNew)
372371
{
373-
job = CreateJob((long) blockTemplate.Header.DaaScore);
372+
job = CreateJob(blockTemplate.Header.DaaScore);
374373

375-
job.Init(blockTemplate, NextJobId(), ShareMultiplier);
376-
377-
lock(jobLock)
378-
{
379-
validJobs.Insert(0, job);
380-
381-
// trim active jobs
382-
while(validJobs.Count > maxActiveJobs)
383-
validJobs.RemoveAt(validJobs.Count - 1);
384-
}
374+
job.Init(blockTemplate, NextJobId("D"), ShareMultiplier);
385375

386376
logger.Debug(() => $"blockTargetValue: {job.blockTargetValue}");
387377
logger.Debug(() => $"Difficulty: {job.Difficulty}");
@@ -584,9 +574,21 @@ public virtual async ValueTask<Share> SubmitShareAsync(StratumConnection worker,
584574

585575
KaspaJob job;
586576

587-
lock(jobLock)
577+
lock(context)
588578
{
589-
job = validJobs.FirstOrDefault(x => x.JobId == jobId);
579+
job = context.validJobs.FirstOrDefault(x => x.JobId == jobId);
580+
581+
if(job == null)
582+
{
583+
// stupid hack for busted ass IceRiver/Bitmain ASICs. Need to loop
584+
// through job history because they submit jobs with incorrect IDs
585+
// https://github.com/rdugan/kaspa-stratum-bridge/blob/main/src/kaspastratum/share_handler.go#L216
586+
if(ValidateIsGodMiner(context.UserAgent) || ValidateIsIceRiverMiner(context.UserAgent))
587+
job = context.validJobs.FirstOrDefault(x => Int64.Parse(x.JobId) < Int64.Parse(jobId));
588+
}
589+
590+
if(job == null)
591+
logger.Warn(() => $"[{context.Miner}] => jobId: {jobId} - Last known job: {context.validJobs.FirstOrDefault()?.JobId}");
590592
}
591593

592594
if(job == null)
@@ -967,5 +969,11 @@ private object[] GetJobParamsForStratum()
967969
return job?.GetJobParams();
968970
}
969971

972+
public KaspaJob GetJobForStratum()
973+
{
974+
var job = currentJob;
975+
return job;
976+
}
977+
970978
#endregion // Overrides
971979
}

src/Miningcore/Blockchain/Kaspa/KaspaPool.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ protected virtual async Task OnAuthorizeAsync(StratumConnection connection, Time
191191
logger.Warn(() => $"[{connection.ConnectionId}] Requesting static difficulty of {staticDiff.Value} (Request has been ignored and instead used as 'initial difficulty' for varDiff)");
192192
}
193193

194-
// send initial difficulty
195-
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });
194+
var minerJobParams = CreateWorkerJob(connection);
196195

197-
// send intial job
198-
await SendJob(connection, context, currentJobParams);
196+
// send intial update
197+
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });
198+
await SendJob(connection, context, minerJobParams);
199199
}
200200

201201
else
@@ -214,6 +214,21 @@ protected virtual async Task OnAuthorizeAsync(StratumConnection connection, Time
214214
}
215215
}
216216

217+
private object[] CreateWorkerJob(StratumConnection connection)
218+
{
219+
var context = connection.ContextAs<KaspaWorkerContext>();
220+
var maxActiveJobs = extraPoolConfig?.MaxActiveJobs ?? 8;
221+
var job = manager.GetJobForStratum();
222+
223+
// update context
224+
lock(context)
225+
{
226+
context.AddJob(job, maxActiveJobs);
227+
}
228+
229+
return job.GetJobParams();
230+
}
231+
217232
protected virtual async Task OnSubmitAsync(StratumConnection connection, Timestamped<JsonRpcRequest> tsRequest, CancellationToken ct)
218233
{
219234
var request = tsRequest.Value;
@@ -303,11 +318,13 @@ await Guard(() => ForEachMinerAsync(async (connection, ct) =>
303318
{
304319
var context = connection.ContextAs<KaspaWorkerContext>();
305320

321+
var minerJobParams = CreateWorkerJob(connection);
322+
306323
// varDiff: if the client has a pending difficulty change, apply it now
307324
if(context.ApplyPendingDifficulty())
308325
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });
309326

310-
await SendJob(connection, context, currentJobParams);
327+
await SendJob(connection, context, minerJobParams);
311328
}));
312329
}
313330

@@ -466,11 +483,11 @@ protected override async Task OnVarDiffUpdateAsync(StratumConnection connection,
466483

467484
if(context.ApplyPendingDifficulty())
468485
{
469-
// send difficulty
470-
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });
486+
var minerJobParams = CreateWorkerJob(connection);
471487

472-
// send job
473-
await SendJob(connection, context, currentJobParams);
488+
// send varDiff update
489+
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });
490+
await SendJob(connection, context, minerJobParams);
474491
}
475492
}
476493

src/Miningcore/Blockchain/Kaspa/KaspaWorkerContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ public class KaspaWorkerContext : WorkerContextBase
2424
/// Default: false
2525
/// </summary>
2626
public bool IsLargeJob { get; set; } = false;
27+
28+
public List<KaspaJob> validJobs { get; set; } = new();
29+
30+
public void AddJob(KaspaJob job, int maxActiveJobs)
31+
{
32+
validJobs.Insert(0, job);
33+
34+
while(validJobs.Count > maxActiveJobs)
35+
validJobs.RemoveAt(validJobs.Count - 1);
36+
}
2737
}

0 commit comments

Comments
 (0)