Skip to content

Commit

Permalink
Improve KAS family job distribution and efficiency for RUST kaspad node
Browse files Browse the repository at this point in the history
  • Loading branch information
ceedii committed Oct 26, 2024
1 parent 947a91a commit 1669d3f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/Miningcore/Blockchain/Kaspa/KaspaConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public static class KaspaConstants

public static class KarlsencoinConstants
{
public const long FishHashForkHeightTestnet = 0;
public const long FishHashPlusForkHeightTestnet = 43200;
public const long FishHashPlusForkHeightMainnet = 26962009;
public const ulong FishHashForkHeightTestnet = 0;
public const ulong FishHashPlusForkHeightTestnet = 43200;
public const ulong FishHashPlusForkHeightMainnet = 26962009;
}

// Pyrin is definitely a scam, use at your own risk
public static class PyrinConstants
{
public const long Blake3ForkHeight = 1484741;
public const ulong Blake3ForkHeight = 1484741;
}

public static class SpectreConstants
Expand Down
3 changes: 2 additions & 1 deletion src/Miningcore/Blockchain/Kaspa/KaspaJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ public virtual Share ProcessShare(StratumConnection worker, string nonce)
public virtual void Init(kaspad.RpcBlock blockTemplate, string jobId, double shareMultiplier)
{
Contract.RequiresNonNull(blockTemplate);
Contract.RequiresNonNull(jobId);
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(jobId));
Contract.RequiresNonNull(shareMultiplier);

JobId = jobId;
this.shareMultiplier = shareMultiplier;
Expand Down
40 changes: 24 additions & 16 deletions src/Miningcore/Blockchain/Kaspa/KaspaJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public KaspaJobManager(
private kaspad.KaspadRPC.KaspadRPCClient rpc;
private kaspaWalletd.KaspaWalletdRPC.KaspaWalletdRPCClient walletRpc;
private string network;
private readonly List<KaspaJob> validJobs = new();
private readonly IExtraNonceProvider extraNonceProvider;
private readonly IMasterClock clock;
private KaspaPoolConfigExtra extraPoolConfig;
Expand Down Expand Up @@ -142,7 +141,7 @@ public KaspaJobManager(
catch(NullReferenceException)
{
// The following is weird but correct, when all data has been received `streamNotifyNewBlockTemplate.ResponseStream.ReadAllAsync()` will return a `NullReferenceException`
logger.Debug(() => $"Waiting for data...");
logger.Info(() => $"Waiting for `NewBlockTemplate` data...");
goto retry_blocktemplate;
}

Expand Down Expand Up @@ -213,7 +212,7 @@ private void SetupJobUpdates(CancellationToken ct)
.RefCount();
}

private KaspaJob CreateJob(long blockHeight)
private KaspaJob CreateJob(ulong blockHeight)
{
switch(coin.Symbol)
{
Expand Down Expand Up @@ -370,18 +369,9 @@ private async Task<bool> UpdateJob(CancellationToken ct, string via = null, kasp

if(isNew)
{
job = CreateJob((long) blockTemplate.Header.DaaScore);
job = CreateJob(blockTemplate.Header.DaaScore);

job.Init(blockTemplate, NextJobId(), ShareMultiplier);

lock(jobLock)
{
validJobs.Insert(0, job);

// trim active jobs
while(validJobs.Count > maxActiveJobs)
validJobs.RemoveAt(validJobs.Count - 1);
}
job.Init(blockTemplate, NextJobId("D"), ShareMultiplier);

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

KaspaJob job;

lock(jobLock)
lock(context)
{
job = validJobs.FirstOrDefault(x => x.JobId == jobId);
job = context.validJobs.FirstOrDefault(x => x.JobId == jobId);

if(job == null)
{
// stupid hack for busted ass IceRiver/Bitmain ASICs. Need to loop
// through job history because they submit jobs with incorrect IDs
// https://github.com/rdugan/kaspa-stratum-bridge/blob/main/src/kaspastratum/share_handler.go#L216
if(ValidateIsGodMiner(context.UserAgent) || ValidateIsIceRiverMiner(context.UserAgent))
job = context.validJobs.FirstOrDefault(x => Int64.Parse(x.JobId) < Int64.Parse(jobId));
}

if(job == null)
logger.Warn(() => $"[{context.Miner}] => jobId: {jobId} - Last known job: {context.validJobs.FirstOrDefault()?.JobId}");
}

if(job == null)
Expand Down Expand Up @@ -967,5 +969,11 @@ private object[] GetJobParamsForStratum()
return job?.GetJobParams();
}

public KaspaJob GetJobForStratum()
{
var job = currentJob;
return job;
}

#endregion // Overrides
}
35 changes: 26 additions & 9 deletions src/Miningcore/Blockchain/Kaspa/KaspaPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ protected virtual async Task OnAuthorizeAsync(StratumConnection connection, Time
logger.Warn(() => $"[{connection.ConnectionId}] Requesting static difficulty of {staticDiff.Value} (Request has been ignored and instead used as 'initial difficulty' for varDiff)");
}

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

// send intial job
await SendJob(connection, context, currentJobParams);
// send intial update
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });
await SendJob(connection, context, minerJobParams);
}

else
Expand All @@ -214,6 +214,21 @@ protected virtual async Task OnAuthorizeAsync(StratumConnection connection, Time
}
}

private object[] CreateWorkerJob(StratumConnection connection)
{
var context = connection.ContextAs<KaspaWorkerContext>();
var maxActiveJobs = extraPoolConfig?.MaxActiveJobs ?? 8;
var job = manager.GetJobForStratum();

// update context
lock(context)
{
context.AddJob(job, maxActiveJobs);
}

return job.GetJobParams();
}

protected virtual async Task OnSubmitAsync(StratumConnection connection, Timestamped<JsonRpcRequest> tsRequest, CancellationToken ct)
{
var request = tsRequest.Value;
Expand Down Expand Up @@ -303,11 +318,13 @@ await Guard(() => ForEachMinerAsync(async (connection, ct) =>
{
var context = connection.ContextAs<KaspaWorkerContext>();

var minerJobParams = CreateWorkerJob(connection);

// varDiff: if the client has a pending difficulty change, apply it now
if(context.ApplyPendingDifficulty())
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });

await SendJob(connection, context, currentJobParams);
await SendJob(connection, context, minerJobParams);
}));
}

Expand Down Expand Up @@ -466,11 +483,11 @@ protected override async Task OnVarDiffUpdateAsync(StratumConnection connection,

if(context.ApplyPendingDifficulty())
{
// send difficulty
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });
var minerJobParams = CreateWorkerJob(connection);

// send job
await SendJob(connection, context, currentJobParams);
// send varDiff update
await connection.NotifyAsync(KaspaStratumMethods.SetDifficulty, new object[] { context.Difficulty });
await SendJob(connection, context, minerJobParams);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Miningcore/Blockchain/Kaspa/KaspaWorkerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ public class KaspaWorkerContext : WorkerContextBase
/// Default: false
/// </summary>
public bool IsLargeJob { get; set; } = false;

public List<KaspaJob> validJobs { get; set; } = new();

public void AddJob(KaspaJob job, int maxActiveJobs)
{
validJobs.Insert(0, job);

while(validJobs.Count > maxActiveJobs)
validJobs.RemoveAt(validJobs.Count - 1);
}
}

0 comments on commit 1669d3f

Please sign in to comment.