Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 9 additions & 7 deletions src/Nethermind/Nethermind.Network.Discovery/DiscoveryApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ namespace Nethermind.Network.Discovery;
public class DiscoveryApp : IDiscoveryApp
{
private readonly IDiscoveryConfig _discoveryConfig;
private readonly ITimestamper _timestamper;
protected readonly ITimestamper _timestamper;
private readonly INodesLocator _nodesLocator;
private readonly IDiscoveryManager _discoveryManager;
protected readonly IDiscoveryManager _discoveryManager;
private readonly INodeTable _nodeTable;
private readonly ILogManager _logManager;
protected readonly ILogManager _logManager;
private readonly ILogger _logger;
private readonly IMessageSerializationService _messageSerializationService;
protected readonly IMessageSerializationService _messageSerializationService;
private readonly ICryptoRandom _cryptoRandom;
private readonly INetworkStorage _discoveryStorage;
private readonly DiscoveryPersistenceManager _persistenceManager;
private readonly IProcessExitSource _processExitSource;
private readonly INetworkConfig _networkConfig;
private readonly CancellationTokenSource _stopCts;
private readonly NodeFilter? _inboundMessageFilter;
protected readonly NodeFilter? _inboundMessageFilter;

private NettyDiscoveryHandler? _discoveryHandler;
private Task? _runningTask;
Expand Down Expand Up @@ -178,10 +178,12 @@ private void ResetUnreachableStatus(object? sender, NetworkAvailabilityEventArgs
}
}

protected virtual NettyDiscoveryHandler CreateDiscoveryHandler(IChannel channel) =>
new(_discoveryManager, channel, _messageSerializationService, _timestamper, _logManager, _inboundMessageFilter);

public void InitializeChannel(IChannel channel)
{
_discoveryHandler = new NettyDiscoveryHandler(_discoveryManager, channel, _messageSerializationService,
_timestamper, _logManager, _inboundMessageFilter);
_discoveryHandler = CreateDiscoveryHandler(channel);
_discoveryManager.MsgSender = _discoveryHandler;
_discoveryHandler.OnChannelActivated += OnChannelActivated;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,11 @@ private bool TryParseMessage(DatagramPacket packet, out DiscoveryMsg? msg, out b
return false;
}

byte typeRaw = msgBytes[97];
if (!FastEnum.IsDefined((MsgType)typeRaw))
if (FromMsgTypeByte(msgBytes[97]) is not { } type)
{
if (_logger.IsDebug) _logger.Debug($"Unsupported message type: {typeRaw}, sender: {address}, message {msgBytes.AsSpan(0, size).ToHexString()}");
if (_logger.IsDebug) _logger.Debug($"Unsupported message type: {msgBytes[97]}, sender: {address}, message {msgBytes.AsSpan(0, size).ToHexString()}");
return false;
}

MsgType type = (MsgType)typeRaw;
if (_logger.IsTrace) _logger.Trace($"Received message: {type}");

if (address is IPEndPoint remoteEndpoint && !TryAcceptInbound(remoteEndpoint))
Expand Down Expand Up @@ -194,6 +191,9 @@ protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket
}
}

protected virtual MsgType? FromMsgTypeByte(byte b) =>
FastEnum.IsDefined((MsgType)b) ? (MsgType)b : null;

private DiscoveryMsg Deserialize(MsgType type, ArraySegment<byte> msg) => type switch
{
MsgType.Ping => _msgSerializationService.Deserialize<PingMsg>(msg),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ namespace Nethermind.Network.Discovery.Serializers;

public class PingMsgSerializer(IEcdsa ecdsa, [KeyFilter(IProtectedPrivateKey.NodeKey)] IPrivateKeyGenerator nodeKey, INodeIdResolver nodeIdResolver) : DiscoveryMsgSerializerBase(ecdsa, nodeKey, nodeIdResolver), IZeroInnerMessageSerializer<PingMsg>
{
protected byte MsgTypeByte = (byte)MsgType.Ping;
Comment thread
batrr marked this conversation as resolved.
Outdated

public void Serialize(IByteBuffer byteBuffer, PingMsg msg)
{
(int totalLength, int contentLength, int sourceAddressLength, int destinationAddressLength) = GetLength(msg);

byteBuffer.MarkIndex();
PrepareBufferForSerialization(byteBuffer, totalLength, (byte)msg.MsgType);
PrepareBufferForSerialization(byteBuffer, totalLength, MsgTypeByte);
NettyRlpStream stream = new(byteBuffer);
stream.StartSequence(contentLength);
stream.Encode(msg.Version);
Expand Down
5 changes: 3 additions & 2 deletions src/Nethermind/Nethermind.Runner/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,8 @@
"Nethermind.Api": "[1.38.0-unstable, )",
"Nethermind.Consensus": "[1.38.0-unstable, )",
"Nethermind.Core": "[1.38.0-unstable, )",
"Nethermind.Init": "[1.38.0-unstable, )"
"Nethermind.Init": "[1.38.0-unstable, )",
"Nethermind.Network.Discovery": "[1.38.0-unstable, )"
}
},
"AspNetCore.HealthChecks.UI": {
Expand Down Expand Up @@ -2191,4 +2192,4 @@
}
}
}
}
}
39 changes: 39 additions & 0 deletions src/Nethermind/Nethermind.Xdc/Discovery/XdcDiscoveryApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Autofac.Features.AttributeFilters;
using DotNetty.Transport.Channels;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Crypto;
using Nethermind.Db;
using Nethermind.Logging;
using Nethermind.Network;
using Nethermind.Network.Config;
using Nethermind.Network.Discovery;
using Nethermind.Network.Discovery.RoutingTable;

namespace Nethermind.Xdc.Discovery;

public class XdcDiscoveryApp(
Comment thread
batrr marked this conversation as resolved.
[KeyFilter(IProtectedPrivateKey.NodeKey)] IProtectedPrivateKey nodeKey,
INodesLocator nodesLocator,
IDiscoveryManager? discoveryManager,
INodeTable? nodeTable,
IMessageSerializationService? msgSerializationService,
ICryptoRandom? cryptoRandom,
[KeyFilter(DbNames.DiscoveryNodes)] INetworkStorage? discoveryStorage,
DiscoveryPersistenceManager discoveryPersistenceManager,
IProcessExitSource processExitSource,
INetworkConfig? networkConfig,
IDiscoveryConfig? discoveryConfig,
ITimestamper? timestamper,
ILogManager? logManager,
NodeFilter? inboundMessageFilter = null)
: DiscoveryApp(nodeKey, nodesLocator, discoveryManager, nodeTable, msgSerializationService, cryptoRandom,
discoveryStorage, discoveryPersistenceManager, processExitSource, networkConfig, discoveryConfig,
timestamper, logManager, inboundMessageFilter)
{
protected override NettyDiscoveryHandler CreateDiscoveryHandler(IChannel channel) =>
new XdcNettyDiscoveryHandler(_discoveryManager, channel, _messageSerializationService, _timestamper, _logManager, _inboundMessageFilter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using DotNetty.Transport.Channels;
using Nethermind.Core;
using Nethermind.Logging;
using Nethermind.Network;
using Nethermind.Network.Discovery;
using Nethermind.Network.Discovery.Messages;

namespace Nethermind.Xdc.Discovery;

public class XdcNettyDiscoveryHandler(
IDiscoveryMsgListener? discoveryManager,
IChannel? channel,
IMessageSerializationService? msgSerializationService,
ITimestamper? timestamper,
ILogManager? logManager,
NodeFilter? inboundMessageFilter = null)
: NettyDiscoveryHandler(discoveryManager, channel, msgSerializationService, timestamper, logManager, inboundMessageFilter)
{
protected override MsgType? FromMsgTypeByte(byte b) => b switch
Comment thread
batrr marked this conversation as resolved.
{
2 => MsgType.Pong,
3 => MsgType.FindNode,
4 => MsgType.Neighbors,
5 => MsgType.Ping,
_ => null
};
}
15 changes: 15 additions & 0 deletions src/Nethermind/Nethermind.Xdc/Discovery/XdcPingMsgSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Autofac.Features.AttributeFilters;
using Nethermind.Crypto;
using Nethermind.Network.Discovery.Messages;
using Nethermind.Network.Discovery.Serializers;

namespace Nethermind.Xdc.Discovery;

public class XdcPingMsgSerializer : PingMsgSerializer
{
public XdcPingMsgSerializer(IEcdsa ecdsa, [KeyFilter(IProtectedPrivateKey.NodeKey)] IPrivateKeyGenerator nodeKey, INodeIdResolver nodeIdResolver)
: base(ecdsa, nodeKey, nodeIdResolver) => MsgTypeByte = 5;
}
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Xdc/Nethermind.Xdc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ProjectReference Include="..\Nethermind.Consensus\Nethermind.Consensus.csproj" />
<ProjectReference Include="..\Nethermind.Core\Nethermind.Core.csproj" />
<ProjectReference Include="..\Nethermind.Init\Nethermind.Init.csproj" />
<ProjectReference Include="..\Nethermind.Network.Discovery\Nethermind.Network.Discovery.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.Xdc/XdcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using Nethermind.Init.Modules;
using Nethermind.Logging;
using Nethermind.Network;
using Nethermind.Network.Discovery;
using Nethermind.Network.Discovery.Messages;
using Nethermind.Serialization.Rlp;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Synchronization;
Expand All @@ -32,6 +34,7 @@
using Nethermind.Xdc.P2P;
using Nethermind.Xdc.Spec;
using Nethermind.Xdc.TxPool;
using Nethermind.Xdc.Discovery;

namespace Nethermind.Xdc;

Expand Down Expand Up @@ -121,6 +124,7 @@ protected override void Load(ContainerBuilder builder)
.AddMessageSerializer<VoteMsg, VoteMsgSerializer>()
.AddMessageSerializer<SyncInfoMsg, SyncInfoMsgSerializer>()
.AddMessageSerializer<TimeoutMsg, TimeoutMsgSerializer>()
.AddMessageSerializer<PingMsg, XdcPingMsgSerializer>()

.AddLast<ITxGossipPolicy, XdcTxGossipPolicy>()
.AddSingleton<IBlockProducerTxSourceFactory, XdcTxPoolTxSourceFactory>()
Expand All @@ -130,6 +134,8 @@ protected override void Load(ContainerBuilder builder)
.AddSingleton<IGasLimitCalculator, XdcGasLimitCalculator>()
.AddSingleton<IDifficultyCalculator, XdcDifficultyCalculator>()
.AddScoped<IProducedBlockSuggester, XdcBlockSuggester>();

builder.RegisterType<XdcDiscoveryApp>().As<DiscoveryApp>().WithAttributeFiltering().SingleInstance().ExternallyOwned();
Comment thread
batrr marked this conversation as resolved.
}

private ISnapshotManager CreateSnapshotManager([KeyFilter(XdcRocksDbConfigFactory.XdcSnapshotDbName)] IDb db, IBlockTree blockTree, IMasternodeVotingContract votingContract, ISpecProvider specProvider) => new SnapshotManager(db, blockTree, votingContract, specProvider);
Expand Down
Loading