Skip to content

Commit

Permalink
Upgrade multiple C# Nugget packages and apply necessary code refactor…
Browse files Browse the repository at this point in the history
…ing in order to be compatible

Fix Cache corruption issue for PROGPOW and ETHEREUM
Fix a little oversight in ALEPHIUM when the pool does not send back most rejected shares to the mining software
  • Loading branch information
ceedii committed Jan 24, 2024
1 parent 8f73bf2 commit cd3f102
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/Miningcore.Tests/Miningcore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageReference Include="BenchmarkDotNet.Annotations" Version="0.13.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.Reactive.Testing" Version="5.0.0" />
<PackageReference Include="NLog" Version="5.1.1" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="Npgsql" Version="7.0.1" />
<PackageReference Include="NSubstitute" Version="4.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand Down
9 changes: 6 additions & 3 deletions src/Miningcore/AutofacModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ protected override void Load(ContainerBuilder builder)
.SingleInstance();

builder.RegisterInstance(new RecyclableMemoryStreamManager
{
ThrowExceptionOnToArray = true
});
(
new RecyclableMemoryStreamManager.Options
{
ThrowExceptionOnToArray = true
}
));

builder.RegisterType<StandardClock>()
.AsImplementedInterfaces()
Expand Down
5 changes: 5 additions & 0 deletions src/Miningcore/Blockchain/Alephium/AlephiumPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ protected override async Task OnRequestAsync(StratumConnection connection,
break;
}
}

catch(AlephiumStratumException ex)
{
await connection.RespondAsync(new JsonRpcResponse(new JsonRpcError((int) ex.Code, ex.Message, null), request.Id, false));
}

catch(StratumException ex)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Miningcore/Blockchain/Beam/BeamBlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public void ReadWrite(BitcoinStream stream)
stream.ReadWrite(ref nVersion);
stream.ReadWrite(ref hashPrevBlock);
stream.ReadWrite(ref hashMerkleRoot);
stream.ReadWrite(ref hashReserved);
stream.ReadWrite(hashReserved);
stream.ReadWrite(ref nTime);
stream.ReadWrite(ref nBits);
stream.ReadWrite(ref nonceBytes);
stream.ReadWrite(nonceBytes);
}

#endregion
Expand Down
22 changes: 11 additions & 11 deletions src/Miningcore/Blockchain/Bitcoin/BitcoinJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ protected virtual void BuildCoinbase()

// serialize (simulated) input transaction
bs.ReadWriteAsVarInt(ref txInputCount);
bs.ReadWrite(ref sha256Empty);
bs.ReadWrite(sha256Empty);
bs.ReadWrite(ref txInPrevOutIndex);

// signature script initial part
bs.ReadWriteAsVarInt(ref sigScriptLength);
bs.ReadWrite(ref sigScriptInitialBytes);
bs.ReadWrite(sigScriptInitialBytes);

// done
coinbaseInitial = stream.ToArray();
Expand All @@ -125,14 +125,14 @@ protected virtual void BuildCoinbase()
var bs = new BitcoinStream(stream, true);

// signature script final part
bs.ReadWrite(ref scriptSigFinalBytes);
bs.ReadWrite(scriptSigFinalBytes);

// tx in sequence
bs.ReadWrite(ref txInSequence);

// serialize output transaction
var txOutBytes = SerializeOutputTransaction(txOut);
bs.ReadWrite(ref txOutBytes);
bs.ReadWrite(txOutBytes);

// misc
bs.ReadWrite(ref txLockTime);
Expand Down Expand Up @@ -189,7 +189,7 @@ protected virtual byte[] SerializeOutputTransaction(Transaction tx)

bs.ReadWrite(ref amount);
bs.ReadWriteAsVarInt(ref rawLength);
bs.ReadWrite(ref raw);
bs.ReadWrite(raw);
}

// serialize outputs
Expand All @@ -202,7 +202,7 @@ protected virtual byte[] SerializeOutputTransaction(Transaction tx)

bs.ReadWrite(ref amount);
bs.ReadWriteAsVarInt(ref rawLength);
bs.ReadWrite(ref raw);
bs.ReadWrite(raw);
}

return stream.ToArray();
Expand Down Expand Up @@ -400,11 +400,11 @@ protected virtual byte[] SerializeBlock(byte[] header, byte[] coinbase)
{
var bs = new BitcoinStream(stream, true);

bs.ReadWrite(ref header);
bs.ReadWrite(header);
bs.ReadWriteAsVarInt(ref transactionCount);

bs.ReadWrite(ref coinbase);
bs.ReadWrite(ref rawTransactionBuffer);
bs.ReadWrite(coinbase);
bs.ReadWrite(rawTransactionBuffer);

// POS coins require a zero byte appended to block which the daemon replaces with the signature
if(isPoS)
Expand All @@ -418,8 +418,8 @@ protected virtual byte[] SerializeBlock(byte[] header, byte[] coinbase)
var mweb = BlockTemplate.Extra.SafeExtensionDataAs<MwebBlockTemplateExtra>();
var mwebRaw = mweb.Mweb.HexToByteArray();

bs.ReadWrite(ref separator);
bs.ReadWrite(ref mwebRaw);
bs.ReadWrite(separator);
bs.ReadWrite(mwebRaw);
}

return stream.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ protected override void BuildCoinbase()

// serialize (simulated) input transaction
bs.ReadWriteAsVarInt(ref txInputCount);
bs.ReadWrite(ref sha256Empty);
bs.ReadWrite(sha256Empty);
bs.ReadWrite(ref coinbaseIndex);
bs.ReadWrite(ref script);
bs.ReadWrite(ref coinbaseSequence);

// serialize output transaction
var txOutBytes = SerializeOutputTransaction(txOut);
bs.ReadWrite(ref txOutBytes);
bs.ReadWrite(txOutBytes);

// misc
bs.ReadWrite(ref txLockTime);
Expand Down Expand Up @@ -95,7 +95,7 @@ private byte[] SerializeOutputTransaction(Transaction tx)

bs.ReadWrite(ref amount);
bs.ReadWriteAsVarInt(ref rawLength);
bs.ReadWrite(ref raw);
bs.ReadWrite(raw);
}

// serialize outputs
Expand All @@ -108,7 +108,7 @@ private byte[] SerializeOutputTransaction(Transaction tx)

bs.ReadWrite(ref amount);
bs.ReadWriteAsVarInt(ref rawLength);
bs.ReadWrite(ref raw);
bs.ReadWrite(raw);
}

return stream.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ protected override void BuildCoinbase()

// serialize (simulated) input transaction
bs.ReadWriteAsVarInt(ref txInputCount);
bs.ReadWrite(ref sha256Empty);
bs.ReadWrite(sha256Empty);
bs.ReadWrite(ref coinbaseIndex);
// bs.ReadWrite(ref serializedBlockHeightBytes);
bs.ReadWrite(ref script);
bs.ReadWrite(ref coinbaseSequence);

// serialize output transaction
var txOutBytes = SerializeOutputTransaction(txOut);
bs.ReadWrite(ref txOutBytes);
bs.ReadWrite(txOutBytes);

// misc
bs.ReadWrite(ref txLockTime);
Expand Down Expand Up @@ -269,7 +269,7 @@ private byte[] SerializeOutputTransaction(Transaction tx)

bs.ReadWrite(ref amount);
bs.ReadWriteAsVarInt(ref rawLength);
bs.ReadWrite(ref raw);
bs.ReadWrite(raw);
}

// serialize witness (segwit)
Expand All @@ -281,7 +281,7 @@ private byte[] SerializeOutputTransaction(Transaction tx)

bs.ReadWrite(ref amount);
bs.ReadWriteAsVarInt(ref rawLength);
bs.ReadWrite(ref raw);
bs.ReadWrite(raw);
}

return stream.ToArray();
Expand Down Expand Up @@ -311,8 +311,8 @@ private byte[] SerializeVeruscoinBlock(Span<byte> header, Span<byte> coinbase, S
{
var bs = new BitcoinStream(stream, true);

bs.ReadWrite(ref header);
bs.ReadWrite(ref solution);
bs.ReadWrite(header);
bs.ReadWrite(solution);

/* var txCount = transactionCount.ToString();
if (Math.Abs(txCount.Length % 2) == 1)
Expand Down Expand Up @@ -341,8 +341,8 @@ private byte[] SerializeVeruscoinBlock(Span<byte> header, Span<byte> coinbase, S
} */

bs.ReadWriteAsVarInt(ref transactionCount);
bs.ReadWrite(ref coinbase);
bs.ReadWrite(ref rawTransactionBuffer);
bs.ReadWrite(coinbase);
bs.ReadWrite(rawTransactionBuffer);

return stream.ToArray();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Miningcore/Blockchain/Equihash/EquihashBlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public void ReadWrite(BitcoinStream stream)
stream.ReadWrite(ref nVersion);
stream.ReadWrite(ref hashPrevBlock);
stream.ReadWrite(ref hashMerkleRoot);
stream.ReadWrite(ref hashReserved);
stream.ReadWrite(hashReserved);
stream.ReadWrite(ref nTime);
stream.ReadWrite(ref nBits);
stream.ReadWrite(ref nonceBytes);
stream.ReadWrite(nonceBytes);
}

#endregion
Expand Down
8 changes: 4 additions & 4 deletions src/Miningcore/Blockchain/Equihash/EquihashJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ private byte[] SerializeBlock(Span<byte> header, Span<byte> coinbase, Span<byte>
{
var bs = new BitcoinStream(stream, true);

bs.ReadWrite(ref header);
bs.ReadWrite(ref solution);
bs.ReadWrite(header);
bs.ReadWrite(solution);
bs.ReadWriteAsVarInt(ref transactionCount);
bs.ReadWrite(ref coinbase);
bs.ReadWrite(ref rawTransactionBuffer);
bs.ReadWrite(coinbase);
bs.ReadWrite(rawTransactionBuffer);

return stream.ToArray();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Miningcore/Blockchain/Progpow/ProgpowJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ protected virtual byte[] SerializeBlock(byte[] header, byte[] coinbase, ulong no
{
var bs = new BitcoinStream(stream, true);

bs.ReadWrite(ref header);
bs.ReadWrite(header);
bs.ReadWrite(ref nonce);
bs.ReadWrite(ref mixHash);
bs.ReadWrite(mixHash);
bs.ReadWriteAsVarInt(ref transactionCount);

bs.ReadWrite(ref coinbase);
bs.ReadWrite(ref rawTransactionBuffer);
bs.ReadWrite(coinbase);
bs.ReadWrite(rawTransactionBuffer);

return stream.ToArray();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Miningcore/Configuration/ClusterConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public partial class EthereumCoinTemplate
public EthereumCoinTemplate()
{
ethashLightValue = new Lazy<IEthashLight>(() =>
EthashFactory.GetEthash(ComponentContext, Ethasher));
EthashFactory.GetEthash(Symbol, ComponentContext, Ethasher));
}

private readonly Lazy<IEthashLight> ethashLightValue;
Expand Down Expand Up @@ -278,7 +278,7 @@ public partial class ProgpowCoinTemplate
public ProgpowCoinTemplate() : base()
{
progpowLightValue = new Lazy<IProgpowLight>(() =>
ProgpowFactory.GetProgpow(ComponentContext, Progpower));
ProgpowFactory.GetProgpow(Symbol, ComponentContext, Progpower));
}

private readonly Lazy<IProgpowLight> progpowLightValue;
Expand Down
8 changes: 4 additions & 4 deletions src/Miningcore/Crypto/Hashing/Ethash/EthashFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ public static class EthashFactory
{
private static readonly ConcurrentDictionary<string, IEthashLight> cacheFull = new();

public static IEthashLight GetEthash(IComponentContext ctx, string name)
public static IEthashLight GetEthash(string symbol, IComponentContext ctx, string name)
{
if(name == "")
if(string.IsNullOrEmpty(symbol) || string.IsNullOrEmpty(name))
return null;

// check cache
if(cacheFull.TryGetValue(name, out var result))
if(cacheFull.TryGetValue(symbol, out var result))
return result;

result = ctx.ResolveNamed<IEthashLight>(name);

cacheFull.TryAdd(name, result);
cacheFull.TryAdd(symbol, result);

return result;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Miningcore/Crypto/Hashing/Progpow/ProgpowFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ public static class ProgpowFactory
{
private static readonly ConcurrentDictionary<string, IProgpowLight> cacheFull = new();

public static IProgpowLight GetProgpow(IComponentContext ctx, string name)
public static IProgpowLight GetProgpow(string symbol, IComponentContext ctx, string name)
{
if(name == "")
if(string.IsNullOrEmpty(symbol) || string.IsNullOrEmpty(name))
return null;

// check cache
if(cacheFull.TryGetValue(name, out var result))
if(cacheFull.TryGetValue(symbol, out var result))
return result;

result = ctx.ResolveNamed<IProgpowLight>(name);

cacheFull.TryAdd(name, result);
cacheFull.TryAdd(symbol, result);

return result;
}
Expand Down
38 changes: 19 additions & 19 deletions src/Miningcore/Miningcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,40 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.15" />
<PackageReference Include="Npgsql" Version="7.0.1" />
<PackageReference Include="NSwag.AspNetCore" Version="13.20.0" />
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="13.20.0" />
<PackageReference Include="NSwag.MSBuild" Version="13.20.0">
<PackageReference Include="NSwag.AspNetCore" Version="14.0.2" />
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="14.0.2" />
<PackageReference Include="NSwag.MSBuild" Version="14.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog.Extensions.Hosting" Version="5.2.1" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="Dapper" Version="2.1.15" />
<PackageReference Include="FluentValidation" Version="11.4.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.2.2" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.4.0" />
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
<PackageReference Include="FluentValidation" Version="11.5.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.5.1" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="MailKit" Version="3.5.0" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
<PackageReference Include="NBitcoin" Version="7.0.24" />
<PackageReference Include="NBitcoin.Altcoins" Version="3.0.18" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="NBitcoin" Version="7.0.32" />
<PackageReference Include="NBitcoin.Altcoins" Version="3.0.21" />
<PackageReference Include="NBitcoin.Zcash" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="NLog" Version="5.1.1" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.1.0" />
<PackageReference Include="prometheus-net" Version="8.1.0" />
<PackageReference Include="prometheus-net" Version="8.2.0" />
<PackageReference Include="protobuf-net" Version="3.1.33" />
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.57.0" />
<PackageReference Include="Google.Protobuf" Version="3.24.2" />
<PackageReference Include="Grpc.Net.Client" Version="2.57.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public PPLNSPaymentScheme(
private readonly IBlockRepository blockRepo;
private readonly IConnectionFactory cf;
private readonly IShareRepository shareRepo;
private static readonly ILogger logger = LogManager.GetLogger("PPLNS Payment", typeof(PPLNSPaymentScheme));
private static readonly ILogger logger = LogManager.GetLogger("PPLNS Payment");

private const int RetryCount = 4;
private IAsyncPolicy shareReadFaultPolicy;
Expand Down
Loading

0 comments on commit cd3f102

Please sign in to comment.