< Summary - Combined Code Coverage

Information
Class: NLightning.Domain.ValueObjects.ShortChannelId
Assembly: NLightning.Domain
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Domain/ValueObjects/ShortChannelId.cs
Tag: 30_15166811759
Line coverage
88%
Covered lines: 45
Uncovered lines: 6
Coverable lines: 51
Total lines: 122
Line coverage: 88.2%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%22100%
.ctor(...)100%11100%
Parse(...)100%22100%
ToString()100%11100%
Equals(...)0%620%
Equals(...)100%44100%
GetHashCode()100%11100%
op_Implicit(...)100%11100%
op_Implicit(...)100%11100%
op_Implicit(...)100%11100%
op_Implicit(...)100%11100%
op_Implicit(...)100%210%
op_Implicit(...)100%210%
op_Equality(...)100%11100%
op_Inequality(...)100%210%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Domain/ValueObjects/ShortChannelId.cs

#LineLine coverage
 1namespace NLightning.Domain.ValueObjects;
 2
 3using Interfaces;
 4
 5/// <summary>
 6/// Represents a short channel id.
 7/// </summary>
 8/// <remarks>
 9/// The short channel id is a unique description of the funding transaction.
 10/// </remarks>
 11public readonly struct ShortChannelId : IValueObject, IEquatable<ShortChannelId>
 12{
 13    private readonly byte[] _value;
 14
 15    public const int LENGTH = 8;
 16
 17    public readonly uint BlockHeight;
 18    public readonly uint TransactionIndex;
 19    public readonly ushort OutputIndex;
 20
 21    public ShortChannelId(uint blockHeight, uint transactionIndex, ushort outputIndex)
 22    {
 11623        BlockHeight = blockHeight;
 11624        TransactionIndex = transactionIndex;
 11625        OutputIndex = outputIndex;
 26
 11627        _value = [
 11628            (byte)(BlockHeight >> 16),
 11629            (byte)(BlockHeight >> 8),
 11630            (byte)BlockHeight,
 11631            (byte)(TransactionIndex >> 16),
 11632            (byte)(TransactionIndex >> 8),
 11633            (byte)TransactionIndex,
 11634            (byte)(OutputIndex >> 8),
 11635            (byte)OutputIndex
 11636        ];
 11637    }
 38
 39    public ShortChannelId(byte[] value)
 40    {
 3241        if (value.Length != LENGTH)
 42        {
 443            throw new ArgumentException($"ShortChannelId must be {LENGTH} bytes", nameof(value));
 44        }
 45
 2846        _value = value;
 47
 2848        BlockHeight = (uint)((value[0] << 16) | (value[1] << 8) | value[2]);
 2849        TransactionIndex = (uint)((value[3] << 16) | (value[4] << 8) | value[5]);
 2850        OutputIndex = (ushort)((value[6] << 8) | value[7]);
 2851    }
 52
 453    public ShortChannelId(ulong channelId) : this(
 454        (uint)((channelId >> 40) & 0xFFFFFF), // BLOCK_HEIGHT
 455        (uint)((channelId >> 16) & 0xFFFF),   // TRANSACTION_INDEX
 456        (ushort)(channelId & 0xFF)            // OUTPUT_INDEX
 457    )
 458    { }
 59
 60    public static ShortChannelId Parse(string shortChannelId)
 61    {
 3262        var parts = shortChannelId.Split('x');
 3263        if (parts.Length != 3)
 64        {
 465            throw new FormatException("Invalid short_channel_id format");
 66        }
 67
 2868        return new ShortChannelId(
 2869            uint.Parse(parts[0]),
 2870            uint.Parse(parts[1]),
 2871            ushort.Parse(parts[2])
 2872        );
 73    }
 74
 75    #region Overrides
 76    public override string ToString()
 77    {
 478        return $"{BlockHeight}x{TransactionIndex}x{OutputIndex}";
 79    }
 80
 81    public override bool Equals(object? obj)
 82    {
 083        if (obj is ShortChannelId other)
 84        {
 085            return Equals(other);
 86        }
 87
 088        return false;
 89    }
 90
 91    public bool Equals(ShortChannelId other)
 92    {
 3293        return BlockHeight == other.BlockHeight &&
 3294               TransactionIndex == other.TransactionIndex &&
 3295               OutputIndex == other.OutputIndex;
 96    }
 97
 98    public override int GetHashCode()
 99    {
 8100        return HashCode.Combine(BlockHeight, TransactionIndex, OutputIndex);
 101    }
 102    #endregion
 103
 104    #region Implicit Operators
 48105    public static implicit operator byte[](ShortChannelId s) => s._value;
 8106    public static implicit operator ShortChannelId(byte[] value) => new(value);
 4107    public static implicit operator ReadOnlyMemory<byte>(ShortChannelId s) => s._value;
 28108    public static implicit operator ReadOnlySpan<byte>(ShortChannelId s) => s._value;
 0109    public static implicit operator ShortChannelId(Span<byte> value) => new(value.ToArray());
 0110    public static implicit operator ShortChannelId(ulong value) => new(value);
 111
 112    public static bool operator ==(ShortChannelId left, ShortChannelId right)
 113    {
 8114        return left.Equals(right);
 115    }
 116
 117    public static bool operator !=(ShortChannelId left, ShortChannelId right)
 118    {
 0119        return !(left == right);
 120    }
 121    #endregion
 122}