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

Ethash: Abstract and light cache #1608

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/callisto_pool.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
}
},
"chainTypeOverride": "Callisto",
"dagDir": "<Your directory for the dag file>",
"daemons": [
{
"host": "127.0.0.1",
Expand Down
1 change: 0 additions & 1 deletion examples/ethereum_pool.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
}
},
"chainTypeOverride": "Ethereum",
"dagDir": "<Your directory for the dag file>",
"daemons": [
{
"host": "127.0.0.1",
Expand Down
1 change: 0 additions & 1 deletion examples/ethereumclassic_pool.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
}
},
"chainTypeOverride": "Classic",
"dagDir": "<Your directory for the dag file>",
"daemons": [
{
"host": "127.0.0.1",
Expand Down
1 change: 0 additions & 1 deletion examples/ubiq_pool.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
}
},
"chainTypeOverride": "Ubiq",
"dagDir": "<Your directory for the dag file>",
"daemons": [
{
"host": "127.0.0.1",
Expand Down
18 changes: 17 additions & 1 deletion src/Miningcore.Tests/Benchmarks/BenchmarkRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Running;
using Miningcore.Tests.Benchmarks.Crypto;
using Miningcore.Tests.Benchmarks.Stratum;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -18,7 +19,7 @@ public Benchmarks(ITestOutputHelper output)
}

[Fact(Skip = "** Uncomment me to run benchmarks **")]
public void Run_Benchmarks()
public void Run_Stratum_Benchmarks()
{
var logger = new AccumulationLogger();

Expand All @@ -31,4 +32,19 @@ public void Run_Benchmarks()
// write benchmark summary
output.WriteLine(logger.GetLog());
}

[Fact(Skip = "** Uncomment me to run benchmarks **")]
public void Run_Crypto_Benchmarks()
{
var logger = new AccumulationLogger();

var config = ManualConfig.Create(DefaultConfig.Instance)
.AddLogger(logger)
.WithOptions(ConfigOptions.DisableOptimizationsValidator);

BenchmarkRunner.Run<EthashBenchmarks>(config);

// write benchmark summary
output.WriteLine(logger.GetLog());
}
}
39 changes: 39 additions & 0 deletions src/Miningcore.Tests/Benchmarks/Crypto/EthashBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Miningcore.Extensions;
using System.Globalization;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Miningcore.Crypto.Hashing.Ethash.Ethash;
using NLog;

namespace Miningcore.Tests.Benchmarks.Crypto;


[MemoryDiagnoser]
public class EthashBenchmarks : TestBase
{
private readonly byte[] testHash = "5fc898f16035bf5ac9c6d9077ae1e3d5fc1ecc3c9fd5bee8bb00e810fdacbaa0".HexToByteArray();
private readonly ulong testNonce = ulong.Parse("50377003e5d830ca", NumberStyles.HexNumber, CultureInfo.InvariantCulture);
private const int testHeight = 60000;

private ILogger logger;

private readonly EthashLight ethash = new EthashLight();

[GlobalSetup]
public void Setup()
{
ModuleInitializer.Initialize();
logger = new NullLogger(LogManager.LogFactory);

ethash.Setup(3, 0);
}


[Benchmark]
public async Task Ethash_Compute()
{
var cache = await ethash.GetCacheAsync(logger, testHeight);
cache.Compute(logger, testHash, testNonce, out var mixDigest, out var result);
}
}

81 changes: 81 additions & 0 deletions src/Miningcore.Tests/Crypto/EthashTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Globalization;
using System.Threading.Tasks;
using Miningcore.Crypto.Hashing.Ethash.Ethash;
using Miningcore.Extensions;
using NLog;
using Xunit;

namespace Miningcore.Tests.Crypto;


public record TestBlock
{
public double Difficulty { get; set; }
public byte[] Hash { get; set; }
public ulong Nonce { get; set; }
public byte[] MixDigest { get; set; }
public ulong Height { get; set; }
}

public class EthashTests : TestBase
{
private static readonly ILogger logger = new NullLogger(LogManager.LogFactory);

[Fact]
public async Task Ethash_Hash()
{
var ethash = new EthashLight();
ethash.Setup(3, 0);

Assert.Equal("Ethash", ethash.AlgoName);

var testBlocks = new TestBlock[]
{
new TestBlock {
Height = 22,
Hash = "372eca2454ead349c3df0ab5d00b0b706b23e49d469387db91811cee0358fc6d".HexToByteArray(),
Difficulty = 132416,
Nonce = ulong.Parse("495732e0ed7a801c", NumberStyles.HexNumber, CultureInfo.InvariantCulture),
MixDigest = "2f74cdeb198af0b9abe65d22d372e22fb2d474371774a9583c1cc427a07939f5".HexToByteArray(),
},
new TestBlock {
Height = 30001,
Hash = "7e44356ee3441623bc72a683fd3708fdf75e971bbe294f33e539eedad4b92b34".HexToByteArray(),
Difficulty = 1532671,
Nonce = ulong.Parse("318df1c8adef7e5e", NumberStyles.HexNumber, CultureInfo.InvariantCulture),
MixDigest = "144b180aad09ae3c81fb07be92c8e6351b5646dda80e6844ae1b697e55ddde84".HexToByteArray(),
},
new TestBlock {
Height = 60000,
Hash = "5fc898f16035bf5ac9c6d9077ae1e3d5fc1ecc3c9fd5bee8bb00e810fdacbaa0".HexToByteArray(),
Difficulty = 2467358,
Nonce = ulong.Parse("50377003e5d830ca", NumberStyles.HexNumber, CultureInfo.InvariantCulture),
MixDigest = "ab546a5b73c452ae86dadd36f0ed83a6745226717d3798832d1b20b489e82063".HexToByteArray(),
},
};

var invalidBlock = new TestBlock
{
Height = 61439999, // 61440000 causes the c lib to crash
Hash = "foo".HexToByteArray(),
Difficulty = 0,
Nonce = ulong.Parse("cafebabec00000fe", NumberStyles.HexNumber, CultureInfo.InvariantCulture),
MixDigest = "bar".HexToByteArray(),
};

// test valid blocks
foreach(var testBlock in testBlocks)
{
var cache = await ethash.GetCacheAsync(logger, testBlock.Height);
var ok = cache.Compute(logger, testBlock.Hash, testBlock.Nonce, out var mixDigest, out var result);
Assert.True(ok);
Assert.Equal(testBlock.MixDigest, mixDigest);
}

// test invalid block
var invalidCache = await ethash.GetCacheAsync(logger, invalidBlock.Height);
var invalidOk = invalidCache.Compute(logger, invalidBlock.Hash, invalidBlock.Nonce, out var invalidMixDigest, out var invalidResult);
Assert.True(invalidOk);
Assert.NotEqual(invalidBlock.MixDigest, invalidMixDigest);
}
}
10 changes: 10 additions & 0 deletions src/Miningcore.Tests/Miningcore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,14 @@

<Copy SourceFiles="@(Libs)" DestinationFolder="$(OutDir)" />
</Target>
<ItemGroup>
<None Include="$(OutDir)\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>%(RecursiveDir)\%(Filename)%(Extension)</Link>
<Visible>False</Visible>
</None>
</ItemGroup>

</Project>


23 changes: 15 additions & 8 deletions src/Miningcore/AutofacModule.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
using System.Reflection;
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IO;
using Miningcore.Api;
using Miningcore.Banning;
using Miningcore.Blockchain.Bitcoin;
using Miningcore.Blockchain.Conceal;
using Miningcore.Blockchain.Cryptonote;
using Miningcore.Blockchain.Equihash;
using Miningcore.Blockchain.Ergo;
using Miningcore.Blockchain.Ethereum;
using Miningcore.Configuration;
using Miningcore.Crypto;
using Miningcore.Crypto.Hashing.Equihash;
using Miningcore.Crypto.Hashing.Ethash;
using Miningcore.Messaging;
using Miningcore.Mining;
using Miningcore.Nicehash;
using Miningcore.Notifications;
using Miningcore.Payments;
using Miningcore.Payments.PaymentSchemes;
using Miningcore.Pushover;
using Miningcore.Time;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Module = Autofac.Module;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IO;
using Miningcore.Blockchain.Ergo;
using Miningcore.Nicehash;
using Miningcore.Pushover;

namespace Miningcore;

Expand Down Expand Up @@ -78,9 +79,15 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterAssemblyTypes(ThisAssembly)
.Where(t => t.GetCustomAttributes<IdentifierAttribute>().Any() &&
t.GetInterfaces().Any(i => i.IsAssignableFrom(typeof(IHashAlgorithm))))
.Named<IHashAlgorithm>(t=> t.GetCustomAttributes<IdentifierAttribute>().First().Name)
.Named<IHashAlgorithm>(t => t.GetCustomAttributes<IdentifierAttribute>().First().Name)
.PropertiesAutowired();

builder.RegisterAssemblyTypes(ThisAssembly)
.Where(t => t.GetCustomAttributes<IdentifierAttribute>().Any() &&
t.GetInterfaces().Any(i => i.IsAssignableFrom(typeof(IEthashLight))))
.Named<IEthashLight>(t => t.GetCustomAttributes<IdentifierAttribute>().First().Name)
.PropertiesAutowired();

builder.RegisterAssemblyTypes(ThisAssembly)
.Where(t => t.IsAssignableTo<EquihashSolver>())
.PropertiesAutowired()
Expand Down Expand Up @@ -148,12 +155,12 @@ protected override void Load(ContainerBuilder builder)
// Bitcoin and family

builder.RegisterType<BitcoinJobManager>();

//////////////////////
// Conceal

builder.RegisterType<ConcealJobManager>();

//////////////////////
// Cryptonote

Expand Down
4 changes: 2 additions & 2 deletions src/Miningcore/Blockchain/Conceal/ConcealPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ protected override async Task SetupJobManager(CancellationToken ct)
disposables.Add(manager.Blocks
.Select(_ => Observable.FromAsync(() =>
Guard(OnNewJobAsync,
ex=> logger.Debug(() => $"{nameof(OnNewJobAsync)}: {ex.Message}"))))
ex => logger.Debug(() => $"{nameof(OnNewJobAsync)}: {ex.Message}"))))
.Concat()
.Subscribe(_ => { }, ex =>
{
Expand All @@ -338,7 +338,7 @@ private string GetMinerAlgo()
{
case CryptonightHashType.CryptonightCCX:
return $"cn-ccx";

case CryptonightHashType.CryptonightGPU:
return $"cn-gpu";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ namespace Miningcore.Blockchain.Ethereum.Configuration;

public class EthereumPoolConfigExtra
{
/// <summary>
/// Base directory for generated DAGs
/// </summary>
public string DagDir { get; set; }

/// <summary>
/// Useful to specify the real chain type when running geth
/// </summary>
Expand Down
4 changes: 1 addition & 3 deletions src/Miningcore/Blockchain/Ethereum/EthereumConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace Miningcore.Blockchain.Ethereum;
public class EthereumConstants
{
public const ulong EpochLength = 30000;
public const ulong CacheSizeForTesting = 1024;
public const ulong DagSizeForTesting = 1024 * 32;
public static BigInteger BigMaxValue = BigInteger.Pow(2, 256);
public static double Pow2x32 = Math.Pow(2, 32);
public static BigInteger BigPow2x32 = new(Pow2x32);
Expand Down Expand Up @@ -113,7 +111,7 @@ public enum GethChainType
MainPow = 10001,
EtherOne = 4949,
Pink = 10100,

Unknown = -1,
}

Expand Down
Loading