From 8a67abad94c785722a5d264fae37491a6a1a6f0a Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Sat, 28 Dec 2024 11:02:15 +0300 Subject: [PATCH] P2P: fix verification rules for NetworkAddressWithTime 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 https://github.com/nspcc-dev/neo-go/pull/3778, should be a part of https://github.com/neo-project/neo/pull/3639. Signed-off-by: Anna Shaleva --- src/Neo/Network/P2P/Payloads/NetworkAddressWithTime.cs | 5 ++++- .../Network/P2P/Payloads/UT_NetworkAddressWithTime.cs | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Neo/Network/P2P/Payloads/NetworkAddressWithTime.cs b/src/Neo/Network/P2P/Payloads/NetworkAddressWithTime.cs index 0b7e0acc37..6b51f2e033 100644 --- a/src/Neo/Network/P2P/Payloads/NetworkAddressWithTime.cs +++ b/src/Neo/Network/P2P/Payloads/NetworkAddressWithTime.cs @@ -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(); } diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NetworkAddressWithTime.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NetworkAddressWithTime.cs index 3e2c6c336c..e3196adf7a 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NetworkAddressWithTime.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NetworkAddressWithTime.cs @@ -10,6 +10,7 @@ // modifications are permitted. using FluentAssertions; +using FluentAssertions.Equivalency; using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Extensions; using Neo.IO; @@ -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(); CollectionAssert.AreEqual(test.Capabilities.ToByteArray(), clone.Capabilities.ToByteArray());