Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .NET version of tcp/noise/yamux #298

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 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
7 changes: 5 additions & 2 deletions transport-interop/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ RUST_SUBDIRS := $(wildcard impl/rust/*/.)
NIM_SUBDIRS := $(wildcard impl/nim/*/.)
ZIG_SUBDIRS := $(wildcard impl/zig/*/.)
JAVA_SUBDIRS := $(wildcard impl/java/*/.)
DOTNET_SUBDIRS := $(wildcard impl/dotnet/*/.)

all: $(GO_SUBDIRS) $(JS_SUBDIRS) $(RUST_SUBDIRS) $(NIM_SUBDIRS) $(ZIG_SUBDIRS) $(JAVA_SUBDIRS)
all: $(GO_SUBDIRS) $(JS_SUBDIRS) $(RUST_SUBDIRS) $(NIM_SUBDIRS) $(ZIG_SUBDIRS) $(JAVA_SUBDIRS) $(DOTNET_SUBDIRS)
$(JS_SUBDIRS):
$(MAKE) -C $@
$(GO_SUBDIRS):
Expand All @@ -18,5 +19,7 @@ $(ZIG_SUBDIRS):
$(MAKE) -C $@
$(JAVA_SUBDIRS):
$(MAKE) -C $@
$(DOTNET_SUBDIRS):
$(MAKE) -C $@

.PHONY: $(GO_SUBDIRS) $(JS_SUBDIRS) $(RUST_SUBDIRS) $(NIM_SUBDIRS) $(ZIG_SUBDIRS) $(JAVA_SUBDIRS) all
.PHONY: $(GO_SUBDIRS) $(JS_SUBDIRS) $(RUST_SUBDIRS) $(NIM_SUBDIRS) $(ZIG_SUBDIRS) $(JAVA_SUBDIRS) $(DOTNET_SUBDIRS) all
3 changes: 3 additions & 0 deletions transport-interop/impl/dotnet/v1.0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin/
obj/
image.json
22 changes: 22 additions & 0 deletions transport-interop/impl/dotnet/v1.0/Dockerfile
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /app

COPY . ./
RUN dotnet restore
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/runtime:7.0-jammy
WORKDIR /app

RUN apt update -y && \
apt install curl -y && \
curl -sSL -O https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb && \
dpkg -i packages-microsoft-prod.deb && \
apt update -y && \
apt install libmsquic -y && \
ln -s /usr/lib/x86_64-linux-gnu/libmsquic.so.2 /bin

COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "TestPlansApp.dll"]


13 changes: 13 additions & 0 deletions transport-interop/impl/dotnet/v1.0/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
image_name := dotnet-v1.0

all: image.json

image.json: Dockerfile Program.cs packages.lock.json TestPlansApp.csproj TestPlansApp.sln
IMAGE_NAME=${image_name} ../../../dockerBuildWrapper.sh .
docker image inspect ${image_name} -f "{{.Id}}" | \
xargs -I {} echo "{\"imageID\": \"{}\"}" > $@

.PHONY: clean all

clean:
rm -f bin/ obj/
145 changes: 145 additions & 0 deletions transport-interop/impl/dotnet/v1.0/Program.cs
Copy link
Contributor

@thomaseizinger thomaseizinger Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all other implementations, we keep the actual test within the respective libp2p repository so it can be atomically updated with the library code.

Unless there is a strong reason to not do that for .net, I'd suggest to do the same here.

Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

using Microsoft.Extensions.DependencyInjection;
using Nethermind.Libp2p.Core;
using Nethermind.Libp2p.Protocols;
using StackExchange.Redis;
using System.Diagnostics;
using System.Net.NetworkInformation;
using Microsoft.Extensions.Logging;

try
{
string transport = Environment.GetEnvironmentVariable("transport")!;
string muxer = Environment.GetEnvironmentVariable("muxer")!;
string security = Environment.GetEnvironmentVariable("security")!;

bool isDialer = bool.Parse(Environment.GetEnvironmentVariable("is_dialer")!);
string ip = Environment.GetEnvironmentVariable("ip") ?? "0.0.0.0";

string redisAddr = Environment.GetEnvironmentVariable("redis_addr") ?? "redis:6379";

int testTimeoutSeconds = int.Parse(Environment.GetEnvironmentVariable("test_timeout_seconds") ?? "180");

TestPlansPeerFactoryBuilder builder = new TestPlansPeerFactoryBuilder(transport, muxer, security);
IPeerFactory peerFactory = builder.Build();

Log($"Connecting to redis at {redisAddr}...");
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(redisAddr);
IDatabase db = redis.GetDatabase();

if (isDialer)
{
ILocalPeer localPeer = peerFactory.Create(localAddr: builder.MakeAddress());
string? listenerAddr = null;
while ((listenerAddr = db.ListRightPop("listenerAddr")) is null)
{
await Task.Delay(20);
}

Log($"Dialing {listenerAddr}...");
Stopwatch handshakeStartInstant = Stopwatch.StartNew();
IRemotePeer remotePeer = await localPeer.DialAsync(listenerAddr);

Stopwatch pingIstant = Stopwatch.StartNew();
await remotePeer.DialAsync<PingProtocol>();
long pingRTT = pingIstant.ElapsedMilliseconds;

long handshakePlusOneRTT = handshakeStartInstant.ElapsedMilliseconds;

PrintResult($"{{\"handshakePlusOneRTTMillis\": {handshakePlusOneRTT}, \"pingRTTMilllis\": {pingRTT}}}");
Log("Done");
return 0;
}
else
{
if (ip == "0.0.0.0")
{
IEnumerable<UnicastIPAddressInformation> addresses = NetworkInterface.GetAllNetworkInterfaces()!
.FirstOrDefault(i => i.Name == "eth0")!
.GetIPProperties()
.UnicastAddresses
.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);

Log("Available addresses detected, picking the first: " + string.Join(",", addresses.Select(a => a.Address)));
ip = addresses.First().Address.ToString()!;
}
Log("Starting to listen...");
ILocalPeer localPeer = peerFactory.Create(localAddr: builder.MakeAddress(ip));
IListener listener = await localPeer.ListenAsync(localPeer.Address);
listener.OnConnection += (peer) => { Log($"Connected {peer.Address}"); return Task.CompletedTask; };
Log($"Listening on {listener.Address}");
db.ListRightPush(new RedisKey("listenerAddr"), new RedisValue(localPeer.Address.ToString()));
await Task.Delay(testTimeoutSeconds * 1000);
await listener.DisconnectAsync();
return -1;
}
}
catch (Exception ex)
{
Log(ex.Message);
return -1;
}

static void Log(string info) => Console.Error.WriteLine(info);
static void PrintResult(string info) => Console.WriteLine(info);

class TestPlansPeerFactoryBuilder : PeerFactoryBuilderBase<TestPlansPeerFactoryBuilder, PeerFactory>
{
private readonly string transport;
private readonly string? muxer;
private readonly string? security;
private static IPeerFactoryBuilder? defaultPeerFactoryBuilder;

public TestPlansPeerFactoryBuilder(string transport, string? muxer, string? security)
: base(new ServiceCollection()
.AddScoped(_ => defaultPeerFactoryBuilder!)
.BuildServiceProvider())
{
defaultPeerFactoryBuilder = this;
this.transport = transport;
this.muxer = muxer;
this.security = security;
}

private static readonly string[] stacklessProtocols = new[] { "quic", "quic-v1", "webtransport" };

protected override ProtocolStack BuildStack()
{
ProtocolStack stack = transport switch
{
"tcp" => Over<IpTcpProtocol>(),
"quic-v1" => Over<QuicProtocol>(),
_ => throw new NotImplementedException(),
};

stack = stack.Over<MultistreamProtocol>();

if (!stacklessProtocols.Contains(transport))
{
stack = security switch
{
"noise" => stack.Over<NoiseProtocol>(),
_ => throw new NotImplementedException(),
};
stack = stack.Over<MultistreamProtocol>();
stack = muxer switch
{
"yamux" => stack.Over<YamuxProtocol>(),
_ => throw new NotImplementedException(),
};
stack = stack.Over<MultistreamProtocol>();
}

return stack.AddAppLayerProtocol<IdentifyProtocol>()
.AddAppLayerProtocol<PingProtocol>();
}

public string MakeAddress(string ip = "0.0.0.0", string port = "0") => transport switch
{
"tcp" => $"/ip4/{ip}/tcp/{port}",
"quic-v1" => $"/ip4/{ip}/udp/{port}/quic-v1",
_ => throw new NotImplementedException(),
};
}
29 changes: 29 additions & 0 deletions transport-interop/impl/dotnet/v1.0/TestPlansApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<RestoreLockedMode>true</RestoreLockedMode>
<EnablePreviewFeatures>True</EnablePreviewFeatures>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NRedisStack" Version="0.8.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />

<PackageReference Include="Nethermind.Libp2p.Core" Version="1.0.0-preview.3" />
<PackageReference Include="Nethermind.Libp2p.Protocols.Quic" Version="1.0.0-preview.3" />
<PackageReference Include="Nethermind.Libp2p.Protocols.IpTcp" Version="1.0.0-preview.3" />
<PackageReference Include="Nethermind.Libp2p.Protocols.Noise" Version="1.0.0-preview.3" />
<PackageReference Include="Nethermind.Libp2p.Protocols.Ping" Version="1.0.0-preview.3" />
<PackageReference Include="Nethermind.Libp2p.Protocols.Yamux" Version="1.0.0-preview.3" />
<PackageReference Include="Nethermind.Libp2p.Protocols.Identify" Version="1.0.0-preview.3" />
<PackageReference Include="Nethermind.Libp2p.Protocols.Multistream" Version="1.0.0-preview.3" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions transport-interop/impl/dotnet/v1.0/TestPlansApp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34018.315
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestPlansApp", "TestPlansApp.csproj", "{4B9D7919-740C-4EF0-8890-AB43E6102952}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4B9D7919-740C-4EF0-8890-AB43E6102952}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B9D7919-740C-4EF0-8890-AB43E6102952}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B9D7919-740C-4EF0-8890-AB43E6102952}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B9D7919-740C-4EF0-8890-AB43E6102952}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7BF72338-A0C5-4E70-A0F1-54B1EB8BB378}
EndGlobalSection
EndGlobal
Loading
Loading