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

Commit

Permalink
Ethhash logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Weichhold committed Mar 22, 2018
1 parent dc38d9e commit c24b393
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
19 changes: 11 additions & 8 deletions src/MiningCore.Tests/Crypto/EthashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
using MiningCore.Crypto.Hashing.Algorithms;
using MiningCore.Crypto.Hashing.Ethash;
using MiningCore.Extensions;
using NLog;
using Xunit;

namespace MiningCore.Tests.Crypto
{
public class EthashTests : TestBase
{
private ILogger logger = LogManager.GetCurrentClassLogger();

[Fact]
public async Task Ethhash_Verify_Valid_Blocks()
{
Expand Down Expand Up @@ -47,9 +50,9 @@ public async Task Ethhash_Verify_Valid_Blocks()

using (var ethash = new EthashLight(3))
{
Assert.True(await ethash.VerifyBlockAsync(validBlocks[0]));
Assert.True(await ethash.VerifyBlockAsync(validBlocks[1]));
Assert.True(await ethash.VerifyBlockAsync(validBlocks[2]));
Assert.True(await ethash.VerifyBlockAsync(validBlocks[0], logger));
Assert.True(await ethash.VerifyBlockAsync(validBlocks[1], logger));
Assert.True(await ethash.VerifyBlockAsync(validBlocks[2], logger));
}
}

Expand Down Expand Up @@ -99,10 +102,10 @@ public async Task Ethhash_Verify_Invalid_Blocks()

using (var ethash = new EthashLight(3))
{
Assert.False(await ethash.VerifyBlockAsync(invalidBlocks[0]));
Assert.False(await ethash.VerifyBlockAsync(invalidBlocks[1]));
Assert.False(await ethash.VerifyBlockAsync(invalidBlocks[2]));
Assert.False(await ethash.VerifyBlockAsync(invalidBlocks[3]));
Assert.False(await ethash.VerifyBlockAsync(invalidBlocks[0], logger));
Assert.False(await ethash.VerifyBlockAsync(invalidBlocks[1], logger));
Assert.False(await ethash.VerifyBlockAsync(invalidBlocks[2], logger));
Assert.False(await ethash.VerifyBlockAsync(invalidBlocks[3], logger));
}
}

Expand All @@ -111,7 +114,7 @@ public async Task EthHash_VerifyAsync_Should_Throw_On_Null_Argument()
{
using (var ethash = new EthashLight(3))
{
await Assert.ThrowsAsync<ArgumentNullException>(async () => await ethash.VerifyBlockAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await ethash.VerifyBlockAsync(null, logger));
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/MiningCore/Blockchain/Ethereum/EthereumJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
using MiningCore.Extensions;
using MiningCore.Stratum;
using NBitcoin;
using NLog;

namespace MiningCore.Blockchain.Ethereum
{
public class EthereumJob
{
public EthereumJob(string id, EthereumBlockTemplate blockTemplate)
public EthereumJob(string id, EthereumBlockTemplate blockTemplate, ILogger logger)
{
Id = id;
BlockTemplate = blockTemplate;
this.logger = logger;

var target = blockTemplate.Target;
if (target.StartsWith("0x"))
Expand All @@ -29,6 +31,7 @@ public EthereumJob(string id, EthereumBlockTemplate blockTemplate)
public string Id { get; }
public EthereumBlockTemplate BlockTemplate { get; }
private readonly uint256 blockTarget;
private readonly ILogger logger;

private void RegisterNonce(StratumClient worker, string nonce)
{
Expand Down Expand Up @@ -63,10 +66,10 @@ private void RegisterNonce(StratumClient worker, string nonce)
var fullNonce = ulong.Parse(fullNonceHex, NumberStyles.HexNumber);

// get dag for block
var dag = await ethash.GetDagAsync(BlockTemplate.Height);
var dag = await ethash.GetDagAsync(BlockTemplate.Height, logger);

// compute
if (!dag.Compute(BlockTemplate.Header.HexToByteArray(), fullNonce, out var mixDigest, out var resultBytes))
if (!dag.Compute(logger, BlockTemplate.Header.HexToByteArray(), fullNonce, out var mixDigest, out var resultBytes))
throw new StratumException(StratumError.MinusOne, "bad hash");

resultBytes.ReverseArray();
Expand Down
20 changes: 9 additions & 11 deletions src/MiningCore/Blockchain/Ethereum/EthereumJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected bool UpdateJob(EthereumBlockTemplate blockTemplate)
var jobId = NextJobId("x8");

// update template
job = new EthereumJob(jobId, blockTemplate);
job = new EthereumJob(jobId, blockTemplate, logger);

lock (jobLock)
{
Expand Down Expand Up @@ -556,20 +556,18 @@ protected override async Task PostStartInitAsync()
{
var blockTemplate = await GetBlockTemplateAsync();

if (blockTemplate == null)
if (blockTemplate != null)
{
logger.Info(() => $"[{LogCat}] Waiting for first valid block template");
logger.Info(() => $"[{LogCat}] Getting current DAG ...");

await Task.Delay(TimeSpan.FromSeconds(5));
continue;
}

logger.Info(() => $"[{LogCat}] Getting current DAG ...");
await ethash.GetDagAsync(blockTemplate.Height, logger);

await ethash.GetDagAsync(blockTemplate.Height);
logger.Info(() => $"[{LogCat}] Got current DAG");
break;
}

logger.Info(() => $"[{LogCat}] Got current DAG");
break;
logger.Info(() => $"[{LogCat}] Waiting for first valid block template");
await Task.Delay(TimeSpan.FromSeconds(5));
}

SetupJobUpdates();
Expand Down
5 changes: 2 additions & 3 deletions src/MiningCore/Crypto/Hashing/Ethash/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public Cache(ulong epoch)
LastUsed = DateTime.Now;
}

private static readonly ILogger logger = LogManager.GetCurrentClassLogger();
private IntPtr handle = IntPtr.Zero;
private bool isGenerated = false;
private readonly object genLock = new object();
Expand All @@ -33,7 +32,7 @@ public void Dispose()
}
}

public async Task GenerateAsync()
public async Task GenerateAsync(ILogger logger)
{
await Task.Run(() =>
{
Expand All @@ -54,7 +53,7 @@ await Task.Run(() =>
});
}

public unsafe bool Compute(byte[] hash, ulong nonce, out byte[] mixDigest, out byte[] result)
public unsafe bool Compute(ILogger logger, byte[] hash, ulong nonce, out byte[] mixDigest, out byte[] result)
{
Contract.RequiresNonNull(hash, nameof(hash));

Expand Down
5 changes: 2 additions & 3 deletions src/MiningCore/Crypto/Hashing/Ethash/Dag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public Dag(ulong epoch)

public ulong Epoch { get; set; }

private static readonly ILogger logger = LogManager.GetCurrentClassLogger();
private IntPtr handle = IntPtr.Zero;
private bool isGenerated = false;
private readonly object genLock = new object();
Expand Down Expand Up @@ -57,7 +56,7 @@ public void Dispose()
}
}

public async Task GenerateAsync(string dagDir)
public async Task GenerateAsync(string dagDir, ILogger logger)
{
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(dagDir), $"{nameof(dagDir)} must not be empty");

Expand Down Expand Up @@ -101,7 +100,7 @@ await Task.Run(() =>
});
}

public unsafe bool Compute(byte[] hash, ulong nonce, out byte[] mixDigest, out byte[] result)
public unsafe bool Compute(ILogger logger, byte[] hash, ulong nonce, out byte[] mixDigest, out byte[] result)
{
Contract.RequiresNonNull(hash, nameof(hash));

Expand Down
13 changes: 6 additions & 7 deletions src/MiningCore/Crypto/Hashing/Ethash/EthashFull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public EthashFull(int numCaches, string dagDir, bool noFutureDag = false)
this.noFutureDag = noFutureDag;
}

private static readonly ILogger logger = LogManager.GetCurrentClassLogger();
private int numCaches; // Maximum number of caches to keep before eviction (only init, don't modify)
private readonly object cacheLock = new object();
private readonly Dictionary<ulong, Dag> caches = new Dictionary<ulong, Dag>();
Expand All @@ -33,7 +32,7 @@ public void Dispose()
value.Dispose();
}

public async Task<Dag> GetDagAsync(ulong block)
public async Task<Dag> GetDagAsync(ulong block, ILogger logger)
{
var epoch = block / EthereumConstants.EpochLength;
Dag result;
Expand All @@ -52,7 +51,7 @@ public async Task<Dag> GetDagAsync(ulong block)
var key = caches.First(pair => pair.Value == toEvict).Key;
var epochToEvict = toEvict.Epoch;

logger.Debug(() => $"Evicting DAG for epoch {epochToEvict} in favour of epoch {epoch}");
logger.Info(() => $"Evicting DAG for epoch {epochToEvict} in favour of epoch {epoch}");
toEvict.Dispose();
caches.Remove(key);
}
Expand All @@ -68,7 +67,7 @@ public async Task<Dag> GetDagAsync(ulong block)

else
{
logger.Debug(() => $"No pre-generated DAG available, creating new for epoch {epoch}");
logger.Info(() => $"No pre-generated DAG available, creating new for epoch {epoch}");
result = new Dag(epoch);
}

Expand All @@ -77,19 +76,19 @@ public async Task<Dag> GetDagAsync(ulong block)
// If we just used up the future cache, or need a refresh, regenerate
if ((future == null || future.Epoch <= epoch) && !noFutureDag)
{
logger.Debug(() => $"Pre-generating DAG for epoch {epoch + 1}");
logger.Info(() => $"Pre-generating DAG for epoch {epoch + 1}");
future = new Dag(epoch + 1);

#pragma warning disable 4014
future.GenerateAsync(dagDir);
future.GenerateAsync(dagDir, logger);
#pragma warning restore 4014
}
}

result.LastUsed = DateTime.Now;
}

await result.GenerateAsync(dagDir);
await result.GenerateAsync(dagDir, logger);
return result;
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/MiningCore/Crypto/Hashing/Ethash/EthashLight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public EthashLight(int numCaches)
this.numCaches = numCaches;
}

private static readonly ILogger logger = LogManager.GetCurrentClassLogger();
private int numCaches; // Maximum number of caches to keep before eviction (only init, don't modify)
private readonly object cacheLock = new object();
private readonly Dictionary<ulong, Cache> caches = new Dictionary<ulong, Cache>();
Expand All @@ -29,7 +28,7 @@ public void Dispose()
value.Dispose();
}

public async Task<bool> VerifyBlockAsync(Block block)
public async Task<bool> VerifyBlockAsync(Block block, ILogger logger)
{
Contract.RequiresNonNull(block, nameof(block));

Expand All @@ -46,10 +45,10 @@ public async Task<bool> VerifyBlockAsync(Block block)
}

// look up cache
var cache = await GetCacheAsync(block.Height);
var cache = await GetCacheAsync(block.Height, logger);

// Recompute the hash using the cache
if (!cache.Compute(block.HashNoNonce, block.Nonce, out var mixDigest, out var resultBytes))
if (!cache.Compute(logger, block.HashNoNonce, block.Nonce, out var mixDigest, out var resultBytes))
return false;

// avoid mixdigest malleability as it's not included in a block's "hashNononce"
Expand All @@ -63,7 +62,7 @@ public async Task<bool> VerifyBlockAsync(Block block)
return result;
}

private async Task<Cache> GetCacheAsync(ulong block)
private async Task<Cache> GetCacheAsync(ulong block, ILogger logger)
{
var epoch = block / EthereumConstants.EpochLength;
Cache result;
Expand Down Expand Up @@ -111,15 +110,15 @@ private async Task<Cache> GetCacheAsync(ulong block)
future = new Cache(epoch + 1);

#pragma warning disable 4014
future.GenerateAsync();
future.GenerateAsync(logger);
#pragma warning restore 4014
}
}

result.LastUsed = DateTime.Now;
}

await result.GenerateAsync();
await result.GenerateAsync(logger);
return result;
}
}
Expand Down

0 comments on commit c24b393

Please sign in to comment.