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 #1425 from oliverw/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Oliver Weichhold authored Sep 26, 2022
2 parents 670d302 + 1b110d6 commit b0f872f
Show file tree
Hide file tree
Showing 260 changed files with 7,329 additions and 43,078 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,5 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

# End of https://www.toptal.com/developers/gitignore/api/rider
# End of https://www.toptal.com/developers/gitignore/api/rider
.idea
4 changes: 4 additions & 0 deletions build-debian-11.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# install install-dependencies
sudo apt-get update; \
sudo apt-get -y install wget

# add dotnet repo
sudo wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
Expand Down
4 changes: 4 additions & 0 deletions build-ubuntu-20.04.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# install install-dependencies
sudo apt-get update; \
sudo apt-get -y install wget

# add dotnet repo
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
Expand Down
4 changes: 4 additions & 0 deletions build-ubuntu-21.04.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# install install-dependencies
sudo apt-get update; \
sudo apt-get -y install wget

# add dotnet repo
wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
Expand Down
12 changes: 12 additions & 0 deletions build-ubuntu-22.04.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# dotnet 6 or higher is included in Ubuntu 22.04 and up

# install dev-dependencies
sudo apt-get update; \
sudo apt-get -y install dotnet-sdk-6.0 git cmake build-essential libssl-dev pkg-config libboost-all-dev libsodium-dev libzmq5

(cd src/Miningcore && \
BUILDIR=${1:-../../build} && \
echo "Building into $BUILDIR" && \
dotnet publish -c Release --framework net6.0 -o $BUILDIR)
Binary file modified libs/runtimes/win-x64/libcryptonote.dll
Binary file not shown.
48 changes: 48 additions & 0 deletions src/Miningcore.Tests/Banning/IntegratedBanManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Net;
using System.Threading;
using Autofac;
using Miningcore.Banning;
using Miningcore.Configuration;
using Xunit;

namespace Miningcore.Tests.Banning;

public class IntegratedBanManagerTests : TestBase
{
private static readonly IPAddress address = IPAddress.Parse("192.168.1.1");

[Fact]
public void Ban_Valid_Address()
{
var manager = ModuleInitializer.Container.ResolveKeyed<IBanManager>(BanManagerKind.Integrated);

Assert.False(manager.IsBanned(address));
manager.Ban(address, TimeSpan.FromSeconds(1));
Assert.True(manager.IsBanned(address));

// let it expire
Thread.Sleep(TimeSpan.FromSeconds(2));
Assert.False(manager.IsBanned(address));
}

[Fact]
public void Throw_Invalid_Duration()
{
var manager = ModuleInitializer.Container.ResolveKeyed<IBanManager>(BanManagerKind.Integrated);

Assert.ThrowsAny<ArgumentException>(() => manager.Ban(address, TimeSpan.Zero));
}

[Fact]
public void Dont_Ban_Loopback()
{
var manager = ModuleInitializer.Container.ResolveKeyed<IBanManager>(BanManagerKind.Integrated);

manager.Ban(IPAddress.Loopback, TimeSpan.FromSeconds(1));
Assert.False(manager.IsBanned(address));

manager.Ban(IPAddress.IPv6Loopback, TimeSpan.FromSeconds(1));
Assert.False(manager.IsBanned(address));
}
}
34 changes: 34 additions & 0 deletions src/Miningcore.Tests/Benchmarks/BenchmarkRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Running;
using Miningcore.Tests.Benchmarks.Stratum;
using Xunit;
using Xunit.Abstractions;

namespace Miningcore.Tests.Benchmarks;

public class Benchmarks
{
private readonly ITestOutputHelper output;

public Benchmarks(ITestOutputHelper output)
{
this.output = output;
}

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

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

BenchmarkRunner.Run<StratumConnectionBenchmarks>(config);

// write benchmark summary
output.WriteLine(logger.GetLog());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Buffers;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using BenchmarkDotNet.Attributes;
using Microsoft.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Miningcore.JsonRpc;
using Miningcore.Stratum;
using Miningcore.Time;
using NLog;
#pragma warning disable 8974

namespace Miningcore.Tests.Benchmarks.Stratum;

[MemoryDiagnoser]
public class StratumConnectionBenchmarks : TestBase
{
private const string JsonRpcVersion = "2.0";
private const string ConnectionId = "foo";
private const string requestString = "{\"params\": [\"slush.miner1\", \"password\"], \"id\": 42, \"method\": \"mining.authorize\"}\\n";
private const string ProcessRequestAsyncMethod = "ProcessRequestAsync";

private RecyclableMemoryStreamManager rmsm;
private IMasterClock clock;
private ILogger logger;

private StratumConnection connection;
private PrivateObject wrapper;

[GlobalSetup]
public void Setup()
{
ModuleInitializer.Initialize();

rmsm = ModuleInitializer.Container.Resolve<RecyclableMemoryStreamManager>();
clock = ModuleInitializer.Container.Resolve<IMasterClock>();
logger = new NullLogger(LogManager.LogFactory);

connection = new(logger, rmsm, clock, ConnectionId);
wrapper = new(connection);
}

Task OnPlaceholderRequestAsync(StratumConnection con, JsonRpcRequest request, CancellationToken ct)
{
return Task.CompletedTask;
}

[Benchmark]
public async Task ProcessRequest_Handle_Valid_Request()
{
await (Task) wrapper.Invoke(ProcessRequestAsyncMethod,
CancellationToken.None,
OnPlaceholderRequestAsync,
new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(requestString)));
}
}
20 changes: 20 additions & 0 deletions src/Miningcore.Tests/Crypto/CrytonoteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public void Crytonote_DecodeAddress()
Assert.Equal(18ul, result);
}

[Fact]
public void Crytonote_DecodeSubAddress()
{
var address = "84k5FLcuZeQ9vUmTfRJkpxCxdppVF5wWdPpxhU4SdTZmAD1i1YH81rPf8XRAsbpc7Na4GG7A8xscjQbqMETLZCXZ7Cdfb7X";
var result = CryptonoteBindings.DecodeAddress(address);

Assert.Equal(42ul, result);
}

[Fact]
public void Cryptonote_DecodeAddress_Should_Throw_On_Null_Or_Empty_Argument()
{
Expand All @@ -40,4 +49,15 @@ public void Crytonote_DecodeIntegratedAddress()

Assert.Equal(19ul, result);
}

[Fact]
public void Cryptonote_CryptonightHashFast()
{
var value = "8519e039172b0d70e5ca7b3383d6b3167315a422747b73f019cf9528f0fde341fd0f2a63030ba6450525cf6de31837669af6f1df8131faf50aaab8d3a7405589".HexToByteArray();
var hash = new byte[32];
CryptonoteBindings.CryptonightHashFast(value, hash);

var result = hash.ToHexString();
Assert.Equal("daedcec20429ffd440ab70a0a3a549fbc89745581f539fd2ac945388698e2db2", result);
}
}
11 changes: 7 additions & 4 deletions src/Miningcore.Tests/Miningcore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="BenchmarkDotNet.Annotations" Version="0.13.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Microsoft.Reactive.Testing" Version="5.0.0" />
<PackageReference Include="NLog" Version="5.0.1" />
<PackageReference Include="Npgsql" Version="6.0.5" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="NLog" Version="5.0.4" />
<PackageReference Include="Npgsql" Version="6.0.6" />
<PackageReference Include="NSubstitute" Version="4.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
4 changes: 4 additions & 0 deletions src/Miningcore.Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using AutoMapper;
using Miningcore.Configuration;
using Miningcore.Native;
using Miningcore.Tests.Util;
using Miningcore.Time;

namespace Miningcore.Tests;

Expand Down Expand Up @@ -38,6 +40,8 @@ public static void Initialize()

builder.Register((ctx, parms) => amConf.CreateMapper());

builder.RegisterType<MockMasterClock>().AsImplementedInterfaces();

// Autofac Container
container = builder.Build();

Expand Down
129 changes: 129 additions & 0 deletions src/Miningcore.Tests/Stratum/StratumConnectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using System.Buffers;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Microsoft.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Miningcore.JsonRpc;
using Miningcore.Stratum;
using Miningcore.Time;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using NSubstitute;
using Xunit;

#pragma warning disable 8974

namespace Miningcore.Tests.Stratum;

public class StratumConnectionTests : TestBase
{
private const string JsonRpcVersion = "2.0";
private const string ConnectionId = "foo";
private const string requestString = "{\"params\": [\"slush.miner1\", \"password\"], \"id\": 42, \"method\": \"mining.authorize\"}\\n";
private const string ProcessRequestAsyncMethod = "ProcessRequestAsync";

private static readonly RecyclableMemoryStreamManager rmsm = ModuleInitializer.Container.Resolve<RecyclableMemoryStreamManager>();
private static readonly IMasterClock clock = ModuleInitializer.Container.Resolve<IMasterClock>();
private static readonly ILogger logger = new NullLogger(LogManager.LogFactory);

[Fact]
public async Task ProcessRequest_Handle_Valid_Request()
{
var connection = new StratumConnection(logger, rmsm, clock, ConnectionId);
var wrapper = new PrivateObject(connection);

Task handler(StratumConnection con, JsonRpcRequest request, CancellationToken ct)
{
Assert.Equal(request.JsonRpc, JsonRpcVersion);
Assert.Equal((long) request.Id, 42);
Assert.Equal(request.Method, "mining.authorize");
Assert.True(request.Params is JArray);
Assert.Equal(request.ParamsAs<JArray>().Count, 2);
Assert.Equal(request.ParamsAs<JArray>()[0], "slush.miner1");
Assert.Equal(request.ParamsAs<JArray>()[1], "password");

return Task.CompletedTask;
}

await (Task) wrapper.Invoke(ProcessRequestAsyncMethod,
CancellationToken.None,
handler,
new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(requestString)));
}

[Fact]
public async Task ProcessRequest_Throw_On_Unparseable_Request()
{
const string invalidRequestString = "foo bar\\n";

var connection = new StratumConnection(logger, rmsm, clock, ConnectionId);
var wrapper = new PrivateObject(connection);
var callCount = 0;

Task handler(StratumConnection con, JsonRpcRequest request, CancellationToken ct)
{
callCount++;
return Task.CompletedTask;
}

await Assert.ThrowsAnyAsync<JsonException>(()=> (Task) wrapper.Invoke(ProcessRequestAsyncMethod,
CancellationToken.None,
handler,
new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(invalidRequestString))));

Assert.Equal(callCount, 0);
}

[Fact]
public async Task ProcessRequest_Honor_CancellationToken()
{
var connection = new StratumConnection(logger, rmsm, clock, ConnectionId);
var wrapper = new PrivateObject(connection);
var callCount = 0;

async Task handler(StratumConnection con, JsonRpcRequest request, CancellationToken ct)
{
callCount++;

await Task.Delay(TimeSpan.FromSeconds(1), ct);
}

using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(20));

await Assert.ThrowsAnyAsync<TaskCanceledException>(()=> (Task) wrapper.Invoke(ProcessRequestAsyncMethod,
cts.Token,
handler,
new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(requestString))));

Assert.Equal(callCount, 1);
}

// [Fact]
// public async Task DetectSslHandshake_Positive()
// {
// const string MethodName = "DetectSslHandshake";
//
// var connection = new StratumConnection(logger, rmsm, clock, ConnectionId);
// var wrapper = new PrivateObject(connection);
//
// var socket = Substitute.For<Socket>(SocketType.Stream, ProtocolType.Tcp);
// var buf = new byte[1];
//
// socket.ReceiveAsync(buf, SocketFlags.Peek, CancellationToken.None).ReturnsForAnyArgs(ValueTask.FromResult(1)).AndDoes(info =>
// {
// var _buf = info.ArgAt<Memory<byte>>(0);
// _buf.Span[0] = 0x16;
// });
//
// var result = await (Task<bool>) wrapper.Invoke(MethodName,
// socket,
// CancellationToken.None);
//
// Assert.True(result);
// }
}
Loading

0 comments on commit b0f872f

Please sign in to comment.