< Summary - Combined Code Coverage

Information
Class: NLightning.Domain.Channels.ValueObjects.ShortChannelId
Assembly: NLightning.Domain
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Domain/Channels/ValueObjects/ShortChannelId.cs
Tag: 36_15743069263
Line coverage
91%
Covered lines: 44
Uncovered lines: 4
Coverable lines: 48
Total lines: 121
Line coverage: 91.6%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
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(...)100%11100%
Equals(...)0%620%
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_Inequality(...)100%210%
op_Equality(...)100%11100%

File(s)

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

#LineLine coverage
 1namespace NLightning.Domain.Channels.ValueObjects;
 2
 3using Domain.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 : IEquatable<ShortChannelId>, IValueObject
 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        [
 11629            (byte)(BlockHeight >> 16),
 11630            (byte)(BlockHeight >> 8),
 11631            (byte)BlockHeight,
 11632            (byte)(TransactionIndex >> 16),
 11633            (byte)(TransactionIndex >> 8),
 11634            (byte)TransactionIndex,
 11635            (byte)(OutputIndex >> 8),
 11636            (byte)OutputIndex
 11637        ];
 11638    }
 39
 40    public ShortChannelId(byte[] value)
 41    {
 3242        if (value.Length != Length)
 43        {
 444            throw new ArgumentException($"ShortChannelId must be {Length} bytes", nameof(value));
 45        }
 46
 2847        _value = value;
 48
 2849        BlockHeight = (uint)((value[0] << 16) | (value[1] << 8) | value[2]);
 2850        TransactionIndex = (uint)((value[3] << 16) | (value[4] << 8) | value[5]);
 2851        OutputIndex = (ushort)((value[6] << 8) | value[7]);
 2852    }
 53
 454    public ShortChannelId(ulong channelId) : this(
 455        (uint)((channelId >> 40) & 0xFFFFFF), // BLOCK_HEIGHT
 456        (uint)((channelId >> 16) & 0xFFFF), // TRANSACTION_INDEX
 457        (ushort)(channelId & 0xFF) // OUTPUT_INDEX
 458    )
 59    {
 460    }
 61
 62    public static ShortChannelId Parse(string shortChannelId)
 63    {
 3264        var parts = shortChannelId.Split('x');
 3265        if (parts.Length != 3)
 66        {
 467            throw new FormatException("Invalid short_channel_id format");
 68        }
 69
 2870        return new ShortChannelId(
 2871            uint.Parse(parts[0]),
 2872            uint.Parse(parts[1]),
 2873            ushort.Parse(parts[2])
 2874        );
 75    }
 76
 77    #region Overrides
 78
 79    public override string ToString()
 80    {
 481        return $"{BlockHeight}x{TransactionIndex}x{OutputIndex}";
 82    }
 83
 84    public bool Equals(ShortChannelId other)
 85    {
 3286        return _value.SequenceEqual(other._value);
 87    }
 88
 89    public override bool Equals(object? obj)
 90    {
 091        return obj is ShortChannelId other && Equals(other);
 92    }
 93
 94    public override int GetHashCode()
 95    {
 896        return HashCode.Combine(BlockHeight, TransactionIndex, OutputIndex);
 97    }
 98
 99    #endregion
 100
 101    #region Implicit Operators
 102
 48103    public static implicit operator byte[](ShortChannelId s) => s._value;
 8104    public static implicit operator ShortChannelId(byte[] value) => new(value);
 4105    public static implicit operator ReadOnlyMemory<byte>(ShortChannelId s) => s._value;
 28106    public static implicit operator ReadOnlySpan<byte>(ShortChannelId s) => s._value;
 0107    public static implicit operator ShortChannelId(Span<byte> value) => new(value.ToArray());
 0108    public static implicit operator ShortChannelId(ulong value) => new(value);
 109
 110    public static bool operator !=(ShortChannelId left, ShortChannelId right)
 111    {
 0112        return !left.Equals(right);
 113    }
 114
 115    public static bool operator ==(ShortChannelId left, ShortChannelId right)
 116    {
 8117        return left.Equals(right);
 118    }
 119
 120    #endregion
 121}