Skip to content

Commit

Permalink
files changed
Browse files Browse the repository at this point in the history
  • Loading branch information
rose2221 committed Aug 16, 2024
1 parent d8fd029 commit 4c09a5e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/libp2p/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Nethermind.Multiformats.Address" Version="1.1.2" />
<PackageVersion Include="NLog" Version="5.3.3" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.12" />
<PackageVersion Include="Noise.NET" Version="1.0.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.16" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NLog" />
<PackageReference Include="NLog.Extensions.Logging" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="NUnit.Analyzers">
Expand Down
49 changes: 49 additions & 0 deletions src/libp2p/Libp2p.Protocols.Noise.Tests/NoiseProtocolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Nethermind.Libp2p.Protocols.Noise.Tests;

[TestFixture]
[Parallelizable(scope: ParallelScope.All)]
public class NoiseProtocolTests
{
[Test]
Expand All @@ -30,6 +31,54 @@ public async Task Test_ConnectionEstablished_AfterHandshake()
// Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo("proto1"));
// channelFactory.Received().SubDialAndBind(downChannelFromProtocolPov, peerContext, proto1);
// await downChannel.CloseAsync();
}
[Test]
public async Task Test_ConnectionEstablished_With_PreSelectedMuxer()
{

IChannel downChannel = new TestChannel();
IChannel downChannelFromProtocolPov = ((TestChannel)downChannel).Reverse();
IChannelFactory channelFactory = Substitute.For<IChannelFactory>();
IPeerContext peerContext = Substitute.For<IPeerContext>();
IPeerContext listenerContext = Substitute.For<IPeerContext>();

IProtocol? proto1 = Substitute.For<IProtocol>();

proto1.Id.Returns("proto1");
channelFactory.SubProtocols.Returns(new[] { proto1 });
IChannel upChannel = new TestChannel();
channelFactory.SubDialAndBind(Arg.Any<IChannel>(), Arg.Any<IPeerContext>(), Arg.Any<IProtocol>())
.Returns(Task.FromResult(upChannel));
channelFactory.SubListenAndBind(Arg.Any<IChannel>(), Arg.Any<IPeerContext>(), Arg.Any<IProtocol>())
.Returns(Task.CompletedTask);
var multiplexerSettings = new MultiplexerSettings();
multiplexerSettings.Add(proto1);
NoiseProtocol proto = new(multiplexerSettings);



peerContext.LocalPeer.Identity.Returns(new Identity());
listenerContext.LocalPeer.Identity.Returns(new Identity());
string peerId = peerContext.LocalPeer.Identity.PeerId.ToString(); // Get the PeerId as a string
Multiaddress localAddr = $"/ip4/0.0.0.0/tcp/0/p2p/{peerId}";
peerContext.RemotePeer.Address.Returns(localAddr);
string listenerPeerId = listenerContext.LocalPeer.Identity.PeerId.ToString();
Multiaddress listenerAddr = $"/ip4/0.0.0.0/tcp/0/p2p/{listenerPeerId}";
listenerContext.RemotePeer.Address.Returns(listenerAddr);

Task ListenTask = proto.ListenAsync(downChannel, channelFactory, listenerContext);
Task DialTask = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);

await DialTask;
await ListenTask;
Assert.That(peerContext.SpecificProtocolRequest.SubProtocol, Is.EqualTo(proto1));



await downChannel.CloseAsync();

await upChannel.CloseAsync();

}

[Test]
Expand Down
5 changes: 5 additions & 0 deletions src/libp2p/Libp2p.Protocols.Noise.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

global using Nethermind.Libp2p.Core;
global using Nethermind.Libp2p.Core.TestsBase;
global using NSubstitute;
global using NUnit.Framework;
global using Multiformats.Address;
global using System.Threading.Tasks;
17 changes: 13 additions & 4 deletions src/libp2p/Libp2p.Protocols.Noise/NoiseProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class NoiseProtocol(MultiplexerSettings? multiplexerSettings = null, ILog
{
StreamMuxers =
{
multiplexerSettings is null || multiplexerSettings.Multiplexers.Any() ? ["na"] : [.. multiplexerSettings.Multiplexers.Select(proto => proto.Id)]
}
multiplexerSettings is null ? ["na"] : multiplexerSettings.Multiplexers.Any() ? [.. multiplexerSettings.Multiplexers.Select(proto => proto.Id)] : []
}
};

public string Id => "/noise";
Expand Down Expand Up @@ -60,6 +60,15 @@ public async Task DialAsync(IChannel downChannel, IChannelFactory? upChannelFact
NoiseHandshakePayload? msg1Decoded = NoiseHandshakePayload.Parser.ParseFrom(buffer.AsSpan(0, msg1.BytesRead));
PublicKey? msg1KeyDecoded = PublicKey.Parser.ParseFrom(msg1Decoded.IdentityKey);
//var key = new byte[] { 0x1 }.Concat(clientStatic.PublicKey).ToArray();
if (_extensions.StreamMuxers.Any())
{
var selectedProtocol = upChannelFactory?.SubProtocols.FirstOrDefault(proto => proto.Id == _extensions.StreamMuxers[0]);
context.SpecificProtocolRequest = new ChannelRequest
{
SubProtocol = selectedProtocol,
CompletionSource = context.SpecificProtocolRequest?.CompletionSource
};
}

PeerId remotePeerId = new(msg1KeyDecoded);
if (!context.RemotePeer.Address.Has<P2P>())
Expand Down Expand Up @@ -95,7 +104,7 @@ public async Task DialAsync(IChannel downChannel, IChannelFactory? upChannelFact

IChannel upChannel = upChannelFactory.SubDial(context);

await ExchangeData(transport, downChannel, upChannel);
_ = ExchangeData(transport, downChannel, upChannel);

_ = upChannel.CloseAsync();
_logger?.LogDebug("Closed");
Expand Down Expand Up @@ -155,7 +164,7 @@ public async Task ListenAsync(IChannel downChannel, IChannelFactory? upChannelFa

IChannel upChannel = upChannelFactory.SubListen(context);

await ExchangeData(transport, downChannel, upChannel);
_ = ExchangeData(transport, downChannel, upChannel);

_ = upChannel.CloseAsync();
_logger?.LogDebug("Closed");
Expand Down

0 comments on commit 4c09a5e

Please sign in to comment.