diff --git a/src/MiningCore.Tests/Crypto/EthashTests.cs b/src/MiningCore.Tests/Crypto/EthashTests.cs index a47fac5dc..056a3f6dd 100644 --- a/src/MiningCore.Tests/Crypto/EthashTests.cs +++ b/src/MiningCore.Tests/Crypto/EthashTests.cs @@ -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() { @@ -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)); } } @@ -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)); } } @@ -111,7 +114,7 @@ public async Task EthHash_VerifyAsync_Should_Throw_On_Null_Argument() { using (var ethash = new EthashLight(3)) { - await Assert.ThrowsAsync(async () => await ethash.VerifyBlockAsync(null)); + await Assert.ThrowsAsync(async () => await ethash.VerifyBlockAsync(null, logger)); } } } diff --git a/src/MiningCore/Blockchain/Ethereum/EthereumJob.cs b/src/MiningCore/Blockchain/Ethereum/EthereumJob.cs index 5d263a308..66307f0bd 100644 --- a/src/MiningCore/Blockchain/Ethereum/EthereumJob.cs +++ b/src/MiningCore/Blockchain/Ethereum/EthereumJob.cs @@ -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")) @@ -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) { @@ -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(); diff --git a/src/MiningCore/Blockchain/Ethereum/EthereumJobManager.cs b/src/MiningCore/Blockchain/Ethereum/EthereumJobManager.cs index ba2b52991..a2313ed68 100644 --- a/src/MiningCore/Blockchain/Ethereum/EthereumJobManager.cs +++ b/src/MiningCore/Blockchain/Ethereum/EthereumJobManager.cs @@ -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) { @@ -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(); diff --git a/src/MiningCore/Crypto/Hashing/Ethash/Cache.cs b/src/MiningCore/Crypto/Hashing/Ethash/Cache.cs index 1d6f1d1f6..9f521322b 100644 --- a/src/MiningCore/Crypto/Hashing/Ethash/Cache.cs +++ b/src/MiningCore/Crypto/Hashing/Ethash/Cache.cs @@ -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(); @@ -33,7 +32,7 @@ public void Dispose() } } - public async Task GenerateAsync() + public async Task GenerateAsync(ILogger logger) { await Task.Run(() => { @@ -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)); diff --git a/src/MiningCore/Crypto/Hashing/Ethash/Dag.cs b/src/MiningCore/Crypto/Hashing/Ethash/Dag.cs index afaabd4c0..d8b9573a4 100644 --- a/src/MiningCore/Crypto/Hashing/Ethash/Dag.cs +++ b/src/MiningCore/Crypto/Hashing/Ethash/Dag.cs @@ -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(); @@ -57,7 +56,7 @@ public void Dispose() } } - public async Task GenerateAsync(string dagDir) + public async Task GenerateAsync(string dagDir, ILogger logger) { Contract.Requires(!string.IsNullOrEmpty(dagDir), $"{nameof(dagDir)} must not be empty"); @@ -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)); diff --git a/src/MiningCore/Crypto/Hashing/Ethash/EthashFull.cs b/src/MiningCore/Crypto/Hashing/Ethash/EthashFull.cs index 1867fbc56..70611d27d 100644 --- a/src/MiningCore/Crypto/Hashing/Ethash/EthashFull.cs +++ b/src/MiningCore/Crypto/Hashing/Ethash/EthashFull.cs @@ -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 caches = new Dictionary(); @@ -33,7 +32,7 @@ public void Dispose() value.Dispose(); } - public async Task GetDagAsync(ulong block) + public async Task GetDagAsync(ulong block, ILogger logger) { var epoch = block / EthereumConstants.EpochLength; Dag result; @@ -52,7 +51,7 @@ public async Task 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); } @@ -68,7 +67,7 @@ public async Task 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); } @@ -77,11 +76,11 @@ public async Task 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 } } @@ -89,7 +88,7 @@ public async Task GetDagAsync(ulong block) result.LastUsed = DateTime.Now; } - await result.GenerateAsync(dagDir); + await result.GenerateAsync(dagDir, logger); return result; } } diff --git a/src/MiningCore/Crypto/Hashing/Ethash/EthashLight.cs b/src/MiningCore/Crypto/Hashing/Ethash/EthashLight.cs index 70939e864..1fe7b1cef 100644 --- a/src/MiningCore/Crypto/Hashing/Ethash/EthashLight.cs +++ b/src/MiningCore/Crypto/Hashing/Ethash/EthashLight.cs @@ -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 caches = new Dictionary(); @@ -29,7 +28,7 @@ public void Dispose() value.Dispose(); } - public async Task VerifyBlockAsync(Block block) + public async Task VerifyBlockAsync(Block block, ILogger logger) { Contract.RequiresNonNull(block, nameof(block)); @@ -46,10 +45,10 @@ public async Task 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" @@ -63,7 +62,7 @@ public async Task VerifyBlockAsync(Block block) return result; } - private async Task GetCacheAsync(ulong block) + private async Task GetCacheAsync(ulong block, ILogger logger) { var epoch = block / EthereumConstants.EpochLength; Cache result; @@ -111,7 +110,7 @@ private async Task GetCacheAsync(ulong block) future = new Cache(epoch + 1); #pragma warning disable 4014 - future.GenerateAsync(); + future.GenerateAsync(logger); #pragma warning restore 4014 } } @@ -119,7 +118,7 @@ private async Task GetCacheAsync(ulong block) result.LastUsed = DateTime.Now; } - await result.GenerateAsync(); + await result.GenerateAsync(logger); return result; } }