Skip to content

Commit

Permalink
Update cs-multihash
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 committed Jun 17, 2024
1 parent f54d3f4 commit fffd0cf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task Test_ConnectionEstablished_AfterHandshake()
.Returns(Task.CompletedTask);

MultistreamProtocol proto = new();
_ = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
Task dialTask = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
_ = Task.Run(async () =>
{
await downChannel.WriteLineAsync(proto.Id);
Expand All @@ -35,6 +35,9 @@ public async Task Test_ConnectionEstablished_AfterHandshake()

Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo(proto.Id));
Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo("proto1"));

await dialTask;

_ = channelFactory.Received().SubDialAndBind(downChannelFromProtocolPov, peerContext, proto1);
await downChannel.CloseAsync();
}
Expand All @@ -58,7 +61,7 @@ public async Task Test_ConnectionEstablished_AfterHandshake_With_SpecificRequest
.Returns(Task.CompletedTask);

MultistreamProtocol proto = new();
_ = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
Task dialTask = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
_ = Task.Run(async () =>
{
await downChannel.WriteLineAsync(proto.Id);
Expand All @@ -67,7 +70,9 @@ public async Task Test_ConnectionEstablished_AfterHandshake_With_SpecificRequest

Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo(proto.Id));
Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo("proto1"));
await Task.Delay(30);

await dialTask;

_ = channelFactory.Received().SubDialAndBind(downChannelFromProtocolPov, peerContext, proto1);
await downChannel.CloseAsync();
}
Expand All @@ -92,11 +97,13 @@ public async Task Test_ConnectionClosed_ForUnknownProtocol()
await downChannel.WriteLineAsync("proto2");
});

_ = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
Task dialTask = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);

Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo(proto.Id));
Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo("proto1"));

await dialTask;

_ = channelFactory.DidNotReceive().SubDialAndBind(downChannelFromProtocolPov, peerContext, proto1);
}

Expand All @@ -119,7 +126,7 @@ public async Task Test_ConnectionEstablished_ForAnyOfProtocols()
.Returns(Task.CompletedTask);

MultistreamProtocol proto = new();
_ = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
Task dialTask = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
_ = Task.Run(async () =>
{
await downChannel.WriteLineAsync(proto.Id);
Expand All @@ -131,7 +138,8 @@ public async Task Test_ConnectionEstablished_ForAnyOfProtocols()
Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo(proto1.Id));
Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo(proto2.Id));

await Task.Delay(30);
await dialTask;

_ = channelFactory.Received().SubDialAndBind(downChannelFromProtocolPov, peerContext, proto2);
await upChannel.CloseAsync();
}
Expand All @@ -152,7 +160,7 @@ public async Task Test_ConnectionClosed_ForBadProtocol()
channelFactory.SubProtocols.Returns(new[] { proto1, proto2 });

MultistreamProtocol proto = new();
_ = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
Task dialTask = proto.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);
_ = Task.Run(async () =>
{
await downChannel.WriteLineAsync(proto.Id);
Expand All @@ -162,6 +170,9 @@ public async Task Test_ConnectionClosed_ForBadProtocol()

Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo(proto.Id));
Assert.That(await downChannel.ReadLineAsync(), Is.EqualTo(proto1.Id));

await dialTask;

_ = channelFactory.DidNotReceiveWithAnyArgs().SubDialAndBind(null!, null!, (IProtocol)null!);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public MultistreamProtocol(ILoggerFactory? loggerFactory = null)
{
_logger = loggerFactory?.CreateLogger<MultistreamProtocol>();
}
public async Task DialAsync(IChannel channel, IChannelFactory channelFactory,
public async Task DialAsync(IChannel channel, IChannelFactory? channelFactory,
IPeerContext context)
{
if (!await SendHello(channel))
Expand Down Expand Up @@ -59,7 +59,7 @@ public async Task DialAsync(IChannel channel, IChannelFactory channelFactory,
}
else
{
foreach (IProtocol selector in channelFactory.SubProtocols)
foreach (IProtocol selector in channelFactory!.SubProtocols)
{
bool? dialResult = await DialProtocol(selector);
if (dialResult == true)
Expand All @@ -83,7 +83,7 @@ public async Task DialAsync(IChannel channel, IChannelFactory channelFactory,
await channelFactory.SubDialAndBind(channel, context, selected);
}

public async Task ListenAsync(IChannel channel, IChannelFactory channelFactory,
public async Task ListenAsync(IChannel channel, IChannelFactory? channelFactory,
IPeerContext context)
{
if (!await SendHello(channel))
Expand All @@ -96,7 +96,7 @@ public async Task ListenAsync(IChannel channel, IChannelFactory channelFactory,
for (; ; )
{
string proto = await channel.ReadLineAsync();
selected = channelFactory.SubProtocols.FirstOrDefault(x => x.Id == proto);
selected = channelFactory!.SubProtocols.FirstOrDefault(x => x.Id == proto);
if (selected is not null)
{
await channel.WriteLineAsync(selected.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ public async Task Test_Peer_is_in_fpeers()
await Task.Delay(100);
_ = peer.Received().DialAsync(discoveredPeer, Arg.Any<CancellationToken>());

router.OutboundConnection(discoveredPeer, PubsubRouter.FloodsubProtocolVersion, Task.CompletedTask, sentRpcs.Add);
router.InboundConnection(discoveredPeer, PubsubRouter.FloodsubProtocolVersion, Task.CompletedTask, Task.CompletedTask, () => Task.CompletedTask);
TaskCompletionSource tcs = new();

router.OutboundConnection(discoveredPeer, PubsubRouter.FloodsubProtocolVersion, tcs.Task, sentRpcs.Add);
router.InboundConnection(discoveredPeer, PubsubRouter.FloodsubProtocolVersion, tcs.Task, tcs.Task, () => Task.CompletedTask);
await router.OnRpc(peerId, new Rpc().WithTopics(new[] { commonTopic }, Enumerable.Empty<string>()));

Assert.Multiple(() =>
{
Assert.That(state.FloodsubPeers[commonTopic], Has.Member(peerId));
Assert.That(sentRpcs.Any(rpc => rpc.Subscriptions.Any(s => s.Subscribe && s.Topicid == commonTopic)), Is.True);
});

tcs.SetResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public async Task Test_New_messages_are_sent_to_mesh_only()
Assert.That(state.FloodsubPeers.Keys, Has.Member(commonTopic));
Assert.That(state.GossipsubPeers.Keys, Has.Member(commonTopic));

TaskCompletionSource tcs = new();

foreach (int index in Enumerable.Range(1, peerCount))
{
Multiaddress discoveredPeer = TestPeers.Multiaddr(index);
PeerId peerId = TestPeers.PeerId(index);

discovery.OnAddPeer!(new[] { discoveredPeer });
router.OutboundConnection(discoveredPeer, PubsubRouter.GossipsubProtocolVersionV10, Task.CompletedTask, sentRpcs.Add);
router.InboundConnection(discoveredPeer, PubsubRouter.GossipsubProtocolVersionV10, Task.CompletedTask, Task.CompletedTask, () => Task.CompletedTask);
router.OutboundConnection(discoveredPeer, PubsubRouter.GossipsubProtocolVersionV10, tcs.Task, sentRpcs.Add);
router.InboundConnection(discoveredPeer, PubsubRouter.GossipsubProtocolVersionV10, tcs.Task, tcs.Task, () => Task.CompletedTask);
await router.OnRpc(peerId, new Rpc().WithTopics(new[] { commonTopic }, Enumerable.Empty<string>()));
}

Expand All @@ -47,5 +49,7 @@ public async Task Test_New_messages_are_sent_to_mesh_only()
Assert.That(state.GossipsubPeers[commonTopic], Has.Count.EqualTo(peerCount));
Assert.That(state.Mesh[commonTopic], Has.Count.EqualTo(Settings.Default.Degree));
});

tcs.SetResult();
}
}
1 change: 1 addition & 0 deletions src/libp2p/Libp2p.Protocols.Quic/QuicProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

Expand Down

0 comments on commit fffd0cf

Please sign in to comment.