Skip to content

Commit

Permalink
P2P: fix verification rules for NetworkAddressWithTime
Browse files Browse the repository at this point in the history
Without this commit every `Addr` message containing UnknownCapability
will be considered as invalid. The desired behaviour is to include node
with UnknownCapability into the list of peers.

Follow the nspcc-dev/neo-go#3778, should be a
part of #3639.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Dec 28, 2024
1 parent d2c34e4 commit 8a67aba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Neo/Network/P2P/Payloads/NetworkAddressWithTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ void ISerializable.Deserialize(ref MemoryReader reader)
Capabilities = new NodeCapability[reader.ReadVarInt(VersionPayload.MaxCapabilities)];
for (int x = 0, max = Capabilities.Length; x < max; x++)
Capabilities[x] = NodeCapability.DeserializeFrom(ref reader);
if (Capabilities.Select(p => p.Type).Distinct().Count() != Capabilities.Length)
// Verify that no duplicating capabilities are included. Unknown capabilities are not
// taken into account but still preserved to be able to share through the network.
var capabilities = Capabilities.Where(c => c is not UnknownCapability);
if (capabilities.Select(p => p.Type).Distinct().Count() != capabilities.Count())
throw new FormatException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using FluentAssertions;
using FluentAssertions.Equivalency;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Extensions;
using Neo.IO;
Expand Down Expand Up @@ -40,7 +41,7 @@ public void SizeAndEndPoint_Get()
[TestMethod]
public void DeserializeAndSerialize()
{
var test = NetworkAddressWithTime.Create(IPAddress.Any, 1, new NodeCapability[] { new ServerCapability(NodeCapabilityType.TcpServer, 22) });
var test = NetworkAddressWithTime.Create(IPAddress.Any, 1, new NodeCapability[] { new ServerCapability(NodeCapabilityType.TcpServer, 22), new UnknownCapability(NodeCapabilityType.Extension0), new UnknownCapability(NodeCapabilityType.Extension0) });
var clone = test.ToArray().AsSerializable<NetworkAddressWithTime>();

CollectionAssert.AreEqual(test.Capabilities.ToByteArray(), clone.Capabilities.ToByteArray());
Expand Down

0 comments on commit 8a67aba

Please sign in to comment.