Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #841 from coinfoundry/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Oliver Weichhold authored Jun 15, 2021
2 parents 474b5ea + ca91790 commit 858a1fe
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 47 deletions.
6 changes: 5 additions & 1 deletion src/Miningcore/Blockchain/Bitcoin/BitcoinJobManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,11 @@ protected override async Task PostStartInitAsync(CancellationToken ct)
if(results.Any(x => x.Error != null))
{
var resultList = results.ToList();
var errors = results.Where(x => x.Error != null && commands[resultList.IndexOf(x)].Method != BitcoinCommands.SubmitBlock)

// filter out optional RPCs
var errors = results
.Where(x => x.Error != null && commands[resultList.IndexOf(x)].Method != BitcoinCommands.SubmitBlock)
.Where(x => x.Error != null && commands[resultList.IndexOf(x)].Method != BitcoinCommands.GetAddressInfo)
.ToArray();

if(errors.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class CryptonotePoolConfigExtra
/// </summary>
public JToken RandomXFlagsOverride { get; set; }

/// <summary>
/// Optional additive value for RandomX VM Flags (see Native/LibRandomX.cs)
/// </summary>
public JToken RandomXFlagsAdd { get; set; }

/// <summary>
/// Optional value for number of RandomX VMs allocated per generation (new seed hash)
/// Set to -1 to scale to number of cores
Expand Down
2 changes: 0 additions & 2 deletions src/Miningcore/Blockchain/Cryptonote/CryptonoteConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public class CryptonoteConstants
public const string DaemonRpcLocation = "json_rpc";
public const string DaemonRpcDigestAuthRealm = "monero_rpc";
public const int MoneroRpcMethodNotFound = -32601;
public const char MainNetAddressPrefix = '4';
public const char TestNetAddressPrefix = '9';
public const int PaymentIdHexLength = 64;
public static readonly Regex RegexValidNonce = new("^[0-9a-f]{8}$", RegexOptions.Compiled);

Expand Down
6 changes: 4 additions & 2 deletions src/Miningcore/Blockchain/Cryptonote/CryptonoteJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public class CryptonoteJob
{
public CryptonoteJob(GetBlockTemplateResponse blockTemplate, byte[] instanceId, string jobId,
PoolConfig poolConfig, ClusterConfig clusterConfig, string prevHash,
LibRandomX.randomx_flags? randomXFlagsOverride, int randomXvmCount)
LibRandomX.randomx_flags? randomXFlagsOverride,
LibRandomX.randomx_flags? randomXFlagsAdd,
int randomXvmCount)
{
Contract.RequiresNonNull(blockTemplate, nameof(blockTemplate));
Contract.RequiresNonNull(poolConfig, nameof(poolConfig));
Expand All @@ -57,7 +59,7 @@ public CryptonoteJob(GetBlockTemplateResponse blockTemplate, byte[] instanceId,
case CryptonightHashType.RandomX:
hashFunc = ((key, data, result, height) =>
{
LibRandomX.CalculateHash(key, data, result, randomXFlagsOverride, randomXvmCount);
LibRandomX.CalculateHash(key, data, result, randomXFlagsOverride, randomXFlagsAdd, randomXvmCount);
});
break;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Miningcore/Blockchain/Cryptonote/CryptonoteJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public CryptonoteJobManager(
private CryptonoteNetworkType networkType;
private CryptonotePoolConfigExtra extraPoolConfig;
private LibRandomX.randomx_flags? randomXFlagsOverride;
private LibRandomX.randomx_flags? randomXFlagsAdd;
private ulong poolAddressBase58Prefix;
private DaemonEndpointConfig[] walletDaemonEndpoints;

Expand Down Expand Up @@ -114,7 +115,7 @@ protected async Task<bool> UpdateJob(string via = null, string json = null)
else
logger.Info(() => $"Detected new block {blockTemplate.Height}");

job = new CryptonoteJob(blockTemplate, instanceId, NextJobId(), poolConfig, clusterConfig, newHash, randomXFlagsOverride, extraPoolConfig.RandomXVMCount);
job = new CryptonoteJob(blockTemplate, instanceId, NextJobId(), poolConfig, clusterConfig, newHash, randomXFlagsOverride, randomXFlagsAdd, extraPoolConfig.RandomXVMCount);
currentJob = job;

// update stats
Expand Down Expand Up @@ -243,6 +244,7 @@ public override void Configure(PoolConfig poolConfig, ClusterConfig clusterConfi
extraPoolConfig = poolConfig.Extra.SafeExtensionDataAs<CryptonotePoolConfigExtra>();

randomXFlagsOverride = MakeRandomXFlags(extraPoolConfig.RandomXFlagsOverride);
randomXFlagsAdd = MakeRandomXFlags(extraPoolConfig.RandomXFlagsAdd);

// extract standard daemon endpoints
daemonEndpoints = poolConfig.Daemons
Expand Down
90 changes: 49 additions & 41 deletions src/Miningcore/Native/LibRandomX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static unsafe class LibRandomX
{
#region Context managment

private static readonly ConcurrentDictionary<string, Tuple<GenContext, BlockingCollection<RxVm>>> generations = new();
private static readonly Dictionary<string, Tuple<GenContext, BlockingCollection<RxVm>>> generations = new();
private static readonly Thread gcThread;
private static readonly byte[] empty = new byte[32];

Expand All @@ -51,36 +51,39 @@ static LibRandomX()
{
Thread.Sleep(TimeSpan.FromMinutes(1));

var list = new List<KeyValuePair<string, Tuple<GenContext, BlockingCollection<RxVm>>>>();

foreach(var pair in generations)
lock(generations)
{
if(DateTime.Now - pair.Value.Item1.LastAccess > TimeSpan.FromMinutes(5))
list.Add(pair);
}
var list = new List<KeyValuePair<string, Tuple<GenContext, BlockingCollection<RxVm>>>>();

foreach(var pair in list.OrderBy(x=> DateTime.Now - x.Value.Item1.LastAccess, OrderByDirection.Descending))
{
// don't dispose remaining VM
if(generations.Count <= 1)
break;
foreach(var pair in generations)
{
if(DateTime.Now - pair.Value.Item1.LastAccess > TimeSpan.FromMinutes(5))
list.Add(pair);
}

if(generations.TryRemove(pair.Key, out var item))
foreach(var pair in list.OrderBy(x => DateTime.Now - x.Value.Item1.LastAccess, OrderByDirection.Descending))
{
// remove all associated VMs
var remaining = item.Item1.VmCount;
var col = item.Item2;
// don't dispose remaining VM
if(generations.Count <= 1)
break;

while(remaining > 0)
if(generations.Remove(pair.Key, out var item))
{
var vm = col.Take();
// remove all associated VMs
var remaining = item.Item1.VmCount;
var col = item.Item2;

logger.Info($"Disposing VM {item.Item1.VmCount - remaining} for seed hash {pair.Key}");
vm.Dispose();
remaining--;
}
while(remaining > 0)
{
var vm = col.Take();

col.Dispose();
logger.Info($"Disposing VM {item.Item1.VmCount - remaining} for seed hash {pair.Key}");
vm.Dispose();
remaining--;
}

col.Dispose();
}
}
}
}
Expand Down Expand Up @@ -184,10 +187,6 @@ private class RxVm : IDisposable
private IntPtr cache = IntPtr.Zero;
private IntPtr vm = IntPtr.Zero;
private RxDataSet ds;
private randomx_flags flags;

public IntPtr Handle => vm;
public randomx_flags Flags => flags;

public void Dispose()
{
Expand All @@ -206,10 +205,8 @@ public void Dispose()
}
}

public void Init(ReadOnlySpan<byte> key, randomx_flags? flags_override = null)
public void Init(ReadOnlySpan<byte> key, randomx_flags flags)
{
flags = flags_override ?? randomx_get_flags();

cache = randomx_alloc_cache(flags);

fixed(byte* key_ptr = key)
Expand All @@ -235,7 +232,8 @@ public void CalculateHash(ReadOnlySpan<byte> data, Span<byte> result)
}
}

private static Tuple<GenContext, BlockingCollection<RxVm>> CreateGeneration(byte[] key, randomx_flags? flags, int vmCount, string keyString)
private static Tuple<GenContext, BlockingCollection<RxVm>> CreateGeneration(byte[] key,
randomx_flags flags, int vmCount, string keyString)
{
var vms = new BlockingCollection<RxVm>();

Expand All @@ -250,40 +248,50 @@ private static Tuple<GenContext, BlockingCollection<RxVm>> CreateGeneration(byte
void createVm(int index)
{
var start = DateTime.Now;
logger.Info(() => $"Creating VM {index + 1} for seed hash {keyString} ...");
logger.Info(() => $"Creating VM {index + 1} [{flags}] for seed hash {keyString} ...");

var vm = new RxVm();
vm.Init(key, flags);

vms.Add(vm);

logger.Info(() => $"VM {index + 1} created in {DateTime.Now - start} ({vm.Flags})");
logger.Info(() => $"VM {index + 1} created in {DateTime.Now - start}");
};

Parallel.For(0, vmCount, createVm);

return generation;
}

private static Tuple<GenContext, BlockingCollection<RxVm>> GetGeneration(byte[] key, randomx_flags? flags, int vmCount)
private static Tuple<GenContext, BlockingCollection<RxVm>> GetGeneration(byte[] key,
randomx_flags? flagsOverride, randomx_flags? flagsAdd, int vmCount)
{
var keyString = key.ToHexString();

if(!generations.TryGetValue(keyString, out var item))
lock(generations)
{
item = CreateGeneration(key, flags, vmCount, keyString);
if(!generations.TryGetValue(keyString, out var item))
{
var flags = flagsOverride ?? randomx_get_flags();

generations[keyString] = item;
}
if(flagsAdd.HasValue)
flags |= flagsAdd.Value;

return item;
item = CreateGeneration(key, flags, vmCount, keyString);

generations[keyString] = item;
}

return item;
}
}

public static void CalculateHash(byte[] key, ReadOnlySpan<byte> data, Span<byte> result, randomx_flags? flags = null, int vmCount = 1)
public static void CalculateHash(byte[] key, ReadOnlySpan<byte> data, Span<byte> result,
randomx_flags? flagsOverride = null, randomx_flags? flagsAdd = null, int vmCount = 1)
{
Contract.Requires<ArgumentException>(result.Length >= 32, $"{nameof(result)} must be greater or equal 32 bytes");

var (ctx, vms) = GetGeneration(key, flags, vmCount);
var (ctx, vms) = GetGeneration(key, flagsOverride, flagsAdd, vmCount);
RxVm vm = null;

try
Expand Down

0 comments on commit 858a1fe

Please sign in to comment.