< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Serialization.Node.FeatureSetSerializer
Assembly: NLightning.Infrastructure.Serialization
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/Node/FeatureSetSerializer.cs
Tag: 36_15743069263
Line coverage
92%
Covered lines: 26
Uncovered lines: 2
Coverable lines: 28
Total lines: 103
Line coverage: 92.8%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SerializeAsync()100%1010100%
DeserializeAsync()100%4.06484.62%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/Node/FeatureSetSerializer.cs

#LineLine coverage
 1using System.Collections;
 2using System.Runtime.Serialization;
 3
 4namespace NLightning.Infrastructure.Serialization.Node;
 5
 6using Converters;
 7using Domain.Node;
 8using Interfaces;
 9
 10public class FeatureSetSerializer : IFeatureSetSerializer
 11{
 12    /// <summary>
 13    /// Serializes the features to a binary writer.
 14    /// </summary>
 15    /// <param name="featureSet">The features to serialize.</param>
 16    /// <param name="stream">The stream to write to.</param>
 17    /// <param name="asGlobal">If the features should be serialized as a global feature set.</param>
 18    /// <param name="includeLength">If the length of the byte array should be included.</param>
 19    /// <remarks>
 20    /// If the features are serialized as a global feature set, only the first 13 bits are serialized.
 21    /// </remarks>
 22    /// <remarks>
 23    /// If the length of the byte array is included, the first 2 bytes are written as the length of the byte array.
 24    /// </remarks>
 25    public async Task SerializeAsync(FeatureSet featureSet, Stream stream, bool asGlobal = false,
 26                                     bool includeLength = true)
 27    {
 28        // If it's a global feature, cut out any bit greater than 13
 19229        if (asGlobal)
 1230            featureSet.FeatureFlags.Length = 13;
 31
 32        // Convert BitArray to byte array
 19233        var bytes = new byte[(featureSet.FeatureFlags.Length + 7) / 8];
 19234        featureSet.FeatureFlags.CopyTo(bytes, 0);
 35
 36        // Set bytes as big endian
 19237        if (BitConverter.IsLittleEndian)
 19238            Array.Reverse(bytes);
 39
 40        // Trim leading zero bytes
 19241        var leadingZeroBytes = 0;
 474042        foreach (var t in bytes)
 43        {
 227244            if (t == 0)
 45            {
 208446                leadingZeroBytes++;
 47            }
 48            else
 49            {
 50                break;
 51            }
 52        }
 53
 19254        var trimmedBytes = bytes[leadingZeroBytes..];
 55
 56        // Write the length of the byte array or 1 if all bytes are zero
 19257        if (includeLength)
 13258            await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)trimmedBytes.Length));
 59
 60        // Otherwise, return the array starting from the first non-zero byte
 19261        await stream.WriteAsync(trimmedBytes);
 19262    }
 63
 64    /// <summary>
 65    /// Deserializes the features from a binary reader.
 66    /// </summary>
 67    /// <param name="stream">The stream to read from.</param>
 68    /// <param name="includeLength">If the length of the byte array is included.</param>
 69    /// <remarks>
 70    /// If the length of the byte array is included, the first 2 bytes are read as the length of the byte array.
 71    /// </remarks>
 72    /// <returns>The deserialized features.</returns>
 73    /// <exception cref="SerializationException">Error deserializing Features</exception>
 74    public async Task<FeatureSet> DeserializeAsync(Stream stream, bool includeLength = true)
 75    {
 76        try
 77        {
 8878            var length = 8;
 79
 8880            var bytes = new byte[2];
 8881            if (includeLength)
 82            {
 83                // Read the length of the byte array
 8484                await stream.ReadExactlyAsync(bytes);
 8485                length = EndianBitConverter.ToUInt16BigEndian(bytes);
 86            }
 87
 88            // Read the byte array
 8889            bytes = new byte[length];
 8890            await stream.ReadExactlyAsync(bytes);
 91
 8892            if (BitConverter.IsLittleEndian)
 8893                Array.Reverse(bytes);
 94
 95            // Convert the byte array to BitArray
 8896            return new FeatureSet { FeatureFlags = new BitArray(bytes) };
 97        }
 098        catch (Exception e)
 99        {
 0100            throw new SerializationException("Error deserializing Features", e);
 101        }
 88102    }
 103}