< 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: 57_24045730253
Line coverage
88%
Covered lines: 22
Uncovered lines: 3
Coverable lines: 25
Total lines: 98
Line coverage: 88%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SerializeAsync()80%10.061091.67%
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        // Convert BitArray to a byte array
 12029        var bytes = featureSet.GetBytes(asGlobal) ?? throw new SerializationException("Feature set is empty");
 30
 31        // Set bytes as big endian
 12032        if (BitConverter.IsLittleEndian)
 12033            Array.Reverse(bytes);
 34
 35        // Trim leading zero bytes
 12036        var leadingZeroBytes = 0;
 35637        foreach (var t in bytes)
 38        {
 11639            if (t == 0)
 40            {
 041                leadingZeroBytes++;
 42            }
 43            else
 44            {
 45                break;
 46            }
 47        }
 48
 12049        var trimmedBytes = bytes[leadingZeroBytes..];
 50
 51        // Write the length of the byte array or 1 if all bytes are zero
 12052        if (includeLength)
 9253            await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)trimmedBytes.Length));
 54
 55        // Otherwise, return the array starting from the first non-zero byte
 12056        await stream.WriteAsync(trimmedBytes);
 12057    }
 58
 59    /// <summary>
 60    /// Deserializes the features from a binary reader.
 61    /// </summary>
 62    /// <param name="stream">The stream to read from.</param>
 63    /// <param name="includeLength">If the length of the byte array is included.</param>
 64    /// <remarks>
 65    /// If the length of the byte array is included, the first 2 bytes are read as the length of the byte array.
 66    /// </remarks>
 67    /// <returns>The deserialized features.</returns>
 68    /// <exception cref="SerializationException">Error deserializing Features</exception>
 69    public async Task<FeatureSet> DeserializeAsync(Stream stream, bool includeLength = true)
 70    {
 71        try
 72        {
 8873            var length = 8;
 74
 8875            var bytes = new byte[2];
 8876            if (includeLength)
 77            {
 78                // Read the length of the byte array
 8479                await stream.ReadExactlyAsync(bytes);
 8480                length = EndianBitConverter.ToUInt16BigEndian(bytes);
 81            }
 82
 83            // Read the byte array
 8884            bytes = new byte[length];
 8885            await stream.ReadExactlyAsync(bytes);
 86
 8887            if (BitConverter.IsLittleEndian)
 8888                Array.Reverse(bytes);
 89
 90            // Convert the byte array to BitArray
 8891            return new FeatureSet { FeatureFlags = new BitArray(bytes) };
 92        }
 093        catch (Exception e)
 94        {
 095            throw new SerializationException("Error deserializing Features", e);
 96        }
 8897    }
 98}