< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Serialization.ValueObjects.WitnessTypeSerializer
Assembly: NLightning.Infrastructure.Serialization
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/ValueObjects/WitnessTypeSerializer.cs
Tag: 36_15743069263
Line coverage
82%
Covered lines: 14
Uncovered lines: 3
Coverable lines: 17
Total lines: 69
Line coverage: 82.3%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SerializeAsync()50%2.03280%
DeserializeAsync()100%11100%
NLightning-Domain-Serialization-Interfaces-IValueObjectTypeSerializer-DeserializeAsync()100%210%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Serialization/ValueObjects/WitnessTypeSerializer.cs

#LineLine coverage
 1using System.Buffers;
 2using System.Runtime.Serialization;
 3using NLightning.Domain.Bitcoin.ValueObjects;
 4using NLightning.Domain.Interfaces;
 5using NLightning.Domain.Serialization.Interfaces;
 6
 7namespace NLightning.Infrastructure.Serialization.ValueObjects;
 8
 9using Converters;
 10
 11public class WitnessTypeSerializer : IValueObjectTypeSerializer<Witness>
 12{
 13    /// <summary>
 14    /// Serializes a Witness value into a stream.
 15    /// </summary>
 16    /// <param name="valueObject">The Witness value to serialize.</param>
 17    /// <param name="stream">The stream where the serialized value will be written.</param>
 18    /// <returns>A task that represents the asynchronous serialization operation.</returns>
 19    /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception>
 20    /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception>
 21    public async Task SerializeAsync(IValueObject valueObject, Stream stream)
 22    {
 1623        if (valueObject is not Witness witness)
 024            throw new ArgumentException("Value object must be of type Witness.", nameof(valueObject));
 25
 1626        await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(witness.Length));
 1627        await stream.WriteAsync(witness);
 1628    }
 29
 30    /// <summary>
 31    /// Deserializes a Witness value from a stream.
 32    /// </summary>
 33    /// <param name="stream">The stream from which the Witness value will be deserialized.</param>
 34    /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized Witness 
 35    /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ
 36    /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception>
 37    public async Task<Witness> DeserializeAsync(Stream stream)
 38    {
 2039        var buffer = ArrayPool<byte>.Shared.Rent(sizeof(ushort));
 40
 41        try
 42        {
 2043            await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ushort)]);
 1644            var len = EndianBitConverter.ToUInt16BigEndian(buffer[..sizeof(ushort)]);
 45
 46            // if (length > CryptoConstants.MAX_SIGNATURE_SIZE)
 47            //     throw new SerializationException(
 48            //         $"Witness length {length} exceeds maximum size of {CryptoConstants.MAX_SIGNATURE_SIZE} bytes.");
 49
 1650            var witnessBytes = new byte[len];
 1651            await stream.ReadExactlyAsync(witnessBytes);
 52
 1653            return new Witness(witnessBytes);
 54        }
 455        catch (Exception e)
 56        {
 457            throw new SerializationException("Error deserializing Witness", e);
 58        }
 59        finally
 60        {
 2061            ArrayPool<byte>.Shared.Return(buffer);
 62        }
 1663    }
 64
 65    async Task<IValueObject> IValueObjectTypeSerializer.DeserializeAsync(Stream stream)
 66    {
 067        return await DeserializeAsync(stream);
 068    }
 69}