Skip to content

Commit

Permalink
Add multihash project; update deps; clean up (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 authored Sep 27, 2023
1 parent 4d2b9e5 commit 10c3a15
Show file tree
Hide file tree
Showing 30 changed files with 64 additions and 52 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ jobs:
env:
WORKING_DIR: src/libp2p
steps:

- name: Check out repository
uses: actions/checkout@v3

with:
submodules: true

- name: Set up .NET
uses: actions/setup-dotnet@v3
with:
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/pubish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ jobs:
BUILD_CONFIG: Release
WORKING_DIR: src/libp2p
steps:

- name: Check out repository
uses: actions/checkout@v3

with:
submodules: true

- name: Set up .NET
uses: actions/setup-dotnet@v3
with:
Expand All @@ -40,7 +42,7 @@ jobs:
-p:ContinuousIntegrationBuild=true \
-p:Commit=${{ github.sha }} \
-p:VersionSuffix=preview.${{ github.run_number }}
- name: Test
working-directory: ${{ env.WORKING_DIR }}
run: |
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ jobs:
BUILD_CONFIG: release
WORKING_DIR: src/libp2p
steps:

- name: Check out repository
uses: actions/checkout@v3

with:
submodules: true

- name: Set up .NET
uses: actions/setup-dotnet@v3
with:
Expand All @@ -27,7 +29,7 @@ jobs:
- name: Build
working-directory: ${{ env.WORKING_DIR }}
run: dotnet build -c ${{ env.BUILD_CONFIG }} --no-restore -p:ContinuousIntegrationBuild=true -p:Commit=${{ github.sha }}

- name: Test
working-directory: ${{ env.WORKING_DIR }}
run: |
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "src/multicodec"]
path = src/multicodec
url = https://github.com/multiformats/multicodec.git
[submodule "src/cs-multihash"]
path = src/cs-multihash
url = https://github.com/flcl42/cs-multihash.git
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ As a stack implementer, you may be interested in [more advanced tutorials](./doc

**Contributions are welcome**, kindly check the [issues](https://github.com/NethermindEth/dotnet-libp2p/issues) tab, everything there if not assigned to a person can be taken into work. More details in [CONTRIBUTING.md](./CONTRIBUTING.md).

## Building the solution

The solution depends on external repositories.

```sh
git clone https://github.com/NethermindEth/dotnet-libp2p.git --recursive
cd ./src/libp2p/
dotnet build
dotnet test
```

## Roadmap

From the beginning, the target is to provide a performant well-tested implementation that works on multiple platforms, with high throughput and low memory profile.
Expand Down
1 change: 1 addition & 0 deletions src/cs-multihash
Submodule cs-multihash added at 6dad45
1 change: 0 additions & 1 deletion src/libp2p/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Multiformats.Hash" Version="1.5.0" />
<PackageVersion Include="Noise.NET" Version="1.0.0" />
<PackageVersion Include="NSubstitute" Version="5.0.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.16" />
Expand Down
1 change: 0 additions & 1 deletion src/libp2p/Libp2p.Core.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Buffers;
using System.Diagnostics;


Channel chan = new();
IChannel revChan = ((Channel)chan).Reverse;

Expand Down
1 change: 0 additions & 1 deletion src/libp2p/Libp2p.Core/IChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public interface IChannelFactory

IChannel SubListenAndBind(IChannel parentChannel, IPeerContext context, IChannelRequest? request = null);


IChannel SubDial(IPeerContext context, IProtocol protocol)
{
return SubDial(context, new ChannelRequest { SubProtocol = protocol });
Expand Down
12 changes: 6 additions & 6 deletions src/libp2p/Libp2p.Core/Identity.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

extern alias BouncyCastleCryptography;
using BouncyCastleCryptography::Org.BouncyCastle.Asn1.X9;
using BouncyCastleCryptography::Org.BouncyCastle.Math;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Math;
using Google.Protobuf;
using Nethermind.Libp2p.Core.Dto;
using BouncyCastleCryptography::Org.BouncyCastle.Math.EC.Rfc8032;
using BouncyCastleCryptography::Org.BouncyCastle.Security;
using Org.BouncyCastle.Math.EC.Rfc8032;
using Org.BouncyCastle.Security;
using System.Security.Cryptography;
using System.Buffers;
using Org.BouncyCastle.Crypto.Parameters;

namespace Nethermind.Libp2p.Core;

Expand Down Expand Up @@ -73,7 +73,7 @@ private static PublicKey GetPublicKey(PrivateKey privateKey)
case KeyType.Secp256K1:
{
X9ECParameters curve = ECNamedCurveTable.GetByName("secp256k1");
BouncyCastleCryptography::Org.BouncyCastle.Math.EC.ECPoint pointQ
Org.BouncyCastle.Math.EC.ECPoint pointQ
= curve.G.Multiply(new BigInteger(1, privateKey.Data.Span));
publicKeyData = ByteString.CopyFrom(pointQ.GetEncoded(true));
}
Expand Down
1 change: 0 additions & 1 deletion src/libp2p/Libp2p.Core/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT


namespace System.Runtime.CompilerServices;

internal static class IsExternalInit
Expand Down
4 changes: 2 additions & 2 deletions src/libp2p/Libp2p.Core/Libp2p.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\cs-multihash\src\Multiformats.Hash\Multiformats.Hash.csproj" />
<ProjectReference Include="..\Libp2p.Generators.Enums\Libp2p.Generators.Enums.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Libp2p.Generators.Protobuf\Libp2p.Generators.Protobuf.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Aliases="BouncyCastleCryptography" />
<PackageReference Include="BouncyCastle.Cryptography" />
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
Expand All @@ -33,7 +34,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Multiformats.Hash" />
<PackageReference Include="SimpleBase" />
</ItemGroup>

Expand Down
4 changes: 1 addition & 3 deletions src/libp2p/Libp2p.Core/PeerFactoryBuilderBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern alias BouncyCastleCryptography;

// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -113,7 +113,6 @@ public ProtocolStack Over(ProtocolStack stack)
return stack;
}


public ProtocolStack Or(ProtocolStack stack)
{
if (Parent is null)
Expand All @@ -130,7 +129,6 @@ public override string ToString()
}
}


protected abstract ProtocolStack BuildStack();

public IPeerFactory Build()
Expand Down
1 change: 0 additions & 1 deletion src/libp2p/Libp2p.Generators.Enums/EnumsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void Execute(GeneratorExecutionContext context)
$"namespace Nethermind.Libp2p.Core.Enums;\npublic enum {e}\n{{\n{string.Join("", vs)}}}\n");
}


string? Cap(string? s)
{
return string.IsNullOrEmpty(s)
Expand Down
1 change: 0 additions & 1 deletion src/libp2p/Libp2p.Generators.Enums/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT


namespace System.Runtime.CompilerServices;

internal static class IsExternalInit
Expand Down
6 changes: 3 additions & 3 deletions src/libp2p/Libp2p.Protocols.IpTcp/IpTcpProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task ListenAsync(IChannel channel, IChannelFactory? channelFactory,
_logger?.LogInformation("ListenAsync({contextId})", context.Id);

Socket srv = new(SocketType.Stream, ProtocolType.Tcp);
Core.Multiaddr addr = context.LocalPeer.Address;
Multiaddr addr = context.LocalPeer.Address;
Core.Enums.Multiaddr ipProtocol = addr.Has(Core.Enums.Multiaddr.Ip4) ? Core.Enums.Multiaddr.Ip4 : Core.Enums.Multiaddr.Ip6;
IPAddress ipAddress = IPAddress.Parse(addr.At(ipProtocol)!);
int tcpPort = int.Parse(addr.At(Core.Enums.Multiaddr.Tcp)!);
Expand Down Expand Up @@ -73,7 +73,7 @@ await Task.Run(async () =>
.Replace(
Core.Enums.Multiaddr.Tcp,
remoteIpEndpoint.Port.ToString());
clientContext.RemotePeer.Address = new Core.Multiaddr()
clientContext.RemotePeer.Address = new Multiaddr()
.Append(remoteIpEndpoint.AddressFamily == AddressFamily.InterNetwork
? Core.Enums.Multiaddr.Ip4
: Core.Enums.Multiaddr.Ip6, remoteIpEndpoint.Address.ToString())
Expand Down Expand Up @@ -130,7 +130,7 @@ public async Task DialAsync(IChannel channel, IChannelFactory channelFactory, IP

TaskCompletionSource<bool?> waitForStop = new(TaskCreationOptions.RunContinuationsAsynchronously);
Socket client = new(SocketType.Stream, ProtocolType.Tcp);
Core.Multiaddr addr = context.RemotePeer.Address;
Multiaddr addr = context.RemotePeer.Address;
Core.Enums.Multiaddr ipProtocol = addr.Has(Core.Enums.Multiaddr.Ip4) ? Core.Enums.Multiaddr.Ip4 : Core.Enums.Multiaddr.Ip6;
IPAddress ipAddress = IPAddress.Parse(addr.At(ipProtocol)!);
int tcpPort = int.Parse(addr.At(Core.Enums.Multiaddr.Tcp)!);
Expand Down
2 changes: 1 addition & 1 deletion src/libp2p/Libp2p.Protocols.MDns/MDnsDiscoveryProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task DiscoverAsync(Core.Multiaddr localPeerAddr, CancellationToken
.Select(x => x.Strings.Where(x => x.StartsWith("dnsaddr")))
.SelectMany(x => x).Select(x => new Core.Multiaddr(x.Replace("dnsaddr=", ""))).ToArray();
_logger?.LogTrace("Inst disc {0}, nmsg: {1}", e.ServiceInstanceName, e.Message);
if (Enumerable.Any(records) && !peers.Contains(Enumerable.First(records)) && localPeerAddr.At(Core.Enums.Multiaddr.P2p) != Enumerable.First<Core.Multiaddr>(records).At(Core.Enums.Multiaddr.P2p))
if (Enumerable.Any(records) && !peers.Contains(Enumerable.First(records)) && localPeerAddr.At(Core.Enums.Multiaddr.P2p) != Enumerable.First(records).At(Core.Enums.Multiaddr.P2p))
{
List<string> peerAddresses = new();
foreach (Core.Multiaddr peer in records)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\cs-multihash\src\Multiformats.Hash\Multiformats.Hash.csproj" />
<ProjectReference Include="..\Libp2p.Core\Libp2p.Core.csproj" />
<ProjectReference Include="..\Libp2p.Generators.Protobuf\Libp2p.Generators.Protobuf.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Aliases="BouncyCastleCryptography" />
<PackageReference Include="BouncyCastle.Cryptography" />
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="Microsoft.SourceLink.GitHub">
<PrivateAssets>all</PrivateAssets>
Expand Down
6 changes: 2 additions & 4 deletions src/libp2p/Libp2p.Protocols.Noise/NoiseProtocol.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

extern alias BouncyCastleCryptography;
using System.Buffers;
using System.Buffers.Binary;
using Google.Protobuf;
using Nethermind.Libp2p.Core;
using Noise;
using System.Text;
using BouncyCastleCryptography::Org.BouncyCastle.Math.EC.Rfc8032;
using Org.BouncyCastle.Math.EC.Rfc8032;
using Microsoft.Extensions.Logging;
using Nethermind.Libp2p.Protocols.Noise.Dto;
using PublicKey = Nethermind.Libp2p.Core.Dto.PublicKey;
using BouncyCastleCryptography::Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Utilities.Encoders;

namespace Nethermind.Libp2p.Protocols;

Expand Down Expand Up @@ -50,7 +49,6 @@ public async Task DialAsync(IChannel downChannel, IChannelFactory upChannelFacto
await downChannel.WriteAsync(new ReadOnlySequence<byte>(lenBytes));
await downChannel.WriteAsync(new ReadOnlySequence<byte>(buffer, 0, msg0.BytesWritten));


lenBytes = (await downChannel.ReadAsync(2)).ToArray();
int len = (int)BinaryPrimitives.ReadInt16BigEndian(lenBytes.AsSpan());
ReadOnlySequence<byte> received = await downChannel.ReadAsync(len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace Nethermind.Libp2p.Protocols.Multistream.Tests;


[TestFixture]
public class GossipsubProtocolTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\cs-multihash\src\Multiformats.Hash\Multiformats.Hash.csproj" />
<ProjectReference Include="..\Libp2p.Core\Libp2p.Core.csproj" />
<ProjectReference Include="..\Libp2p.Generators.Protobuf\Libp2p.Generators.Protobuf.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="BouncyCastle.Cryptography" Aliases="BouncyCastleCryptography" />
<PackageReference Include="BouncyCastle.Cryptography" />
<PackageReference Include="Microsoft.SourceLink.GitHub">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
1 change: 0 additions & 1 deletion src/libp2p/Libp2p.Protocols.Pubsub/PubsubProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public async Task DialAsync(IChannel channel, IChannelFactory? channelFactory,
string peerId = context.RemotePeer.Address.At(MultiaddrEnum.P2p)!;
_logger?.LogDebug($"Dialed({context.Id}) {context.RemotePeer.Address}");


CancellationToken token = router.OutboundConnection(peerId, Id, (rpc) =>
{
_ = channel.WritePrefixedProtobufAsync(rpc);
Expand Down
3 changes: 1 addition & 2 deletions src/libp2p/Libp2p.Protocols.Pubsub/RpcExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

extern alias BouncyCastleCryptography;
using Google.Protobuf;
using BouncyCastleCryptography::Org.BouncyCastle.Math.EC.Rfc8032;
using Org.BouncyCastle.Math.EC.Rfc8032;
using System.Buffers.Binary;
using System.Text;
using Multiformats.Hash;
Expand Down
1 change: 0 additions & 1 deletion src/libp2p/Libp2p.Protocols.Pubsub/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT


global using Multiaddr = Nethermind.Libp2p.Core.Multiaddr;
global using MultiaddrEnum = Nethermind.Libp2p.Core.Enums.Multiaddr;
using System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\cs-multihash\src\Multiformats.Hash\Multiformats.Hash.csproj" />
<ProjectReference Include="..\Libp2p.Core\Libp2p.Core.csproj" />
</ItemGroup>

Expand Down
6 changes: 6 additions & 0 deletions src/libp2p/Libp2p.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libp2p.Protocols.Pubsub.Tes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libp2p.Protocols.Ping", "Libp2p.Protocols.Ping\Libp2p.Protocols.Ping.csproj", "{FC0E9BCE-2848-45DC-AE20-FB7E862A199E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Multiformats.Hash", "..\cs-multihash\src\Multiformats.Hash\Multiformats.Hash.csproj", "{064158B8-C0A0-4CE5-8D6A-77FE657788FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -151,6 +153,10 @@ Global
{FC0E9BCE-2848-45DC-AE20-FB7E862A199E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC0E9BCE-2848-45DC-AE20-FB7E862A199E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC0E9BCE-2848-45DC-AE20-FB7E862A199E}.Release|Any CPU.Build.0 = Release|Any CPU
{064158B8-C0A0-4CE5-8D6A-77FE657788FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{064158B8-C0A0-4CE5-8D6A-77FE657788FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{064158B8-C0A0-4CE5-8D6A-77FE657788FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{064158B8-C0A0-4CE5-8D6A-77FE657788FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion src/samples/chat/Chat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" Aliases="BouncyCastleCryptography" />
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" />
<PackageReference Include="Google.Protobuf" Version="3.24.1" />
<PackageReference Include="Google.Protobuf.Tools" Version="3.24.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/samples/perf-benchmarks/PerfBenchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" Aliases="BouncyCastleCryptography" />
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" />
<PackageReference Include="Google.Protobuf" Version="3.24.1" />
<PackageReference Include="Google.Protobuf.Tools" Version="3.24.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
Expand Down
Loading

0 comments on commit 10c3a15

Please sign in to comment.