This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 673
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1425 from oliverw/dev
Dev
- Loading branch information
Showing
260 changed files
with
7,329 additions
and
43,078 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Miningcore.Tests/Benchmarks/Stratum/StratumConnectionBenchmarks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
} |
Oops, something went wrong.