< 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: 30_15166811759
Line coverage
92%
Covered lines: 26
Uncovered lines: 2
Coverable lines: 28
Total lines: 111
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)
 30        {
 1231            featureSet.FeatureFlags.Length = 13;
 32        }
 33
 34        // Convert BitArray to byte array
 19235        var bytes = new byte[(featureSet.FeatureFlags.Length + 7) / 8];
 19236        featureSet.FeatureFlags.CopyTo(bytes, 0);
 37
 38        // Set bytes as big endian
 19239        if (BitConverter.IsLittleEndian)
 40        {
 19241            Array.Reverse(bytes);
 42        }
 43
 44        // Trim leading zero bytes
 19245        var leadingZeroBytes = 0;
 474046        foreach (var t in bytes)
 47        {
 227248            if (t == 0)
 49            {
 208450                leadingZeroBytes++;
 51            }
 52            else
 53            {
 54                break;
 55            }
 56        }
 57
 19258        var trimmedBytes = bytes[leadingZeroBytes..];
 59
 60        // Write the length of the byte array or 1 if all bytes are zero
 19261        if (includeLength)
 62        {
 13263            await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)trimmedBytes.Length));
 64        }
 65
 66        // Otherwise, return the array starting from the first non-zero byte
 19267        await stream.WriteAsync(trimmedBytes);
 19268    }
 69
 70    /// <summary>
 71    /// Deserializes the features from a binary reader.
 72    /// </summary>
 73    /// <param name="stream">The stream to read from.</param>
 74    /// <param name="includeLength">If the length of the byte array is included.</param>
 75    /// <remarks>
 76    /// If the length of the byte array is included, the first 2 bytes are read as the length of the byte array.
 77    /// </remarks>
 78    /// <returns>The deserialized features.</returns>
 79    /// <exception cref="SerializationException">Error deserializing Features</exception>
 80    public async Task<FeatureSet> DeserializeAsync(Stream stream, bool includeLength = true)
 81    {
 82        try
 83        {
 8884            var length = 8;
 85
 8886            var bytes = new byte[2];
 8887            if (includeLength)
 88            {
 89                // Read the length of the byte array
 8490                await stream.ReadExactlyAsync(bytes);
 8491                length = EndianBitConverter.ToUInt16BigEndian(bytes);
 92            }
 93
 94            // Read the byte array
 8895            bytes = new byte[length];
 8896            await stream.ReadExactlyAsync(bytes);
 97
 8898            if (BitConverter.IsLittleEndian)
 99            {
 88100                Array.Reverse(bytes);
 101            }
 102
 103            // Convert the byte array to BitArray
 88104            return new FeatureSet { FeatureFlags = new BitArray(bytes) };
 105        }
 0106        catch (Exception e)
 107        {
 0108            throw new SerializationException("Error deserializing Features", e);
 109        }
 88110    }
 111}