diff --git a/src/Nethermind/Nethermind.Network/P2P/Subprotocols/Snap/Messages/StorageRangesMessageSerializer.cs b/src/Nethermind/Nethermind.Network/P2P/Subprotocols/Snap/Messages/StorageRangesMessageSerializer.cs index ea7f1ddb7e8..3305091410d 100644 --- a/src/Nethermind/Nethermind.Network/P2P/Subprotocols/Snap/Messages/StorageRangesMessageSerializer.cs +++ b/src/Nethermind/Nethermind.Network/P2P/Subprotocols/Snap/Messages/StorageRangesMessageSerializer.cs @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only +using System; using DotNetty.Buffers; using Nethermind.Core.Crypto; using Nethermind.Serialization.Rlp; @@ -8,8 +9,18 @@ namespace Nethermind.Network.P2P.Subprotocols.Snap.Messages { - public class StorageRangesMessageSerializer : IZeroMessageSerializer + public sealed class StorageRangesMessageSerializer : IZeroMessageSerializer { + private readonly Func _decodeSlot; + private readonly Func _decodeSlotArray; + + public StorageRangesMessageSerializer() + { + // Capture closures once + _decodeSlot = DecodeSlot; + _decodeSlotArray = s => s.DecodeArray(_decodeSlot); + } + public void Serialize(IByteBuffer byteBuffer, StorageRangeMessage message) { (int contentLength, int allSlotsLength, int[] accountSlotsLengths, int proofsLength) = CalculateLengths(message); @@ -71,7 +82,7 @@ public StorageRangeMessage Deserialize(IByteBuffer byteBuffer) stream.ReadSequenceLength(); message.RequestId = stream.DecodeLong(); - message.Slots = stream.DecodeArray(s => s.DecodeArray(DecodeSlot)); + message.Slots = stream.DecodeArray(_decodeSlotArray); message.Proofs = stream.DecodeArray(s => s.DecodeByteArray()); return message; diff --git a/src/Nethermind/Nethermind.State/Snap/PathWithStorageSlot.cs b/src/Nethermind/Nethermind.State/Snap/PathWithStorageSlot.cs index 7864d661014..6bbbacef0cc 100644 --- a/src/Nethermind/Nethermind.State/Snap/PathWithStorageSlot.cs +++ b/src/Nethermind/Nethermind.State/Snap/PathWithStorageSlot.cs @@ -1,19 +1,31 @@ // SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only +using System; +using System.Numerics; using Nethermind.Core.Crypto; namespace Nethermind.State.Snap { - public class PathWithStorageSlot + public readonly struct PathWithStorageSlot(ValueHash256 keyHash, byte[] slotRlpValue) + : IEquatable, IEqualityOperators { - public PathWithStorageSlot(ValueHash256 keyHash, byte[] slotRlpValue) + public ValueHash256 Path { get; } = keyHash; + public byte[] SlotRlpValue { get; } = slotRlpValue; + + public bool Equals(in PathWithStorageSlot other) { - Path = keyHash; - SlotRlpValue = slotRlpValue; + return Path == other.Path && SlotRlpValue.AsSpan().SequenceEqual(other.SlotRlpValue); } - public ValueHash256 Path { get; set; } - public byte[] SlotRlpValue { get; set; } + public bool Equals(PathWithStorageSlot other) => Equals(in other); + + public static bool operator ==(PathWithStorageSlot left, PathWithStorageSlot right) => left.Equals(in right); + + public static bool operator !=(PathWithStorageSlot left, PathWithStorageSlot right) => !left.Equals(in right); + + public override bool Equals(object obj) => obj is PathWithStorageSlot pws && Equals(in pws); + + public override int GetHashCode() => throw new NotImplementedException(); } }