< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Bitcoin.Comparers.TransactionOutputComparer
Assembly: NLightning.Infrastructure.Bitcoin
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Comparers/TransactionOutputComparer.cs
Tag: 36_15743069263
Line coverage
84%
Covered lines: 27
Uncovered lines: 5
Coverable lines: 32
Total lines: 77
Line coverage: 84.3%
Branch coverage
66%
Covered branches: 24
Total branches: 36
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
get_Instance()100%11100%
Compare(...)62.5%39.283280.77%
CompareScriptPubKey(...)100%44100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Comparers/TransactionOutputComparer.cs

#LineLine coverage
 1namespace NLightning.Infrastructure.Bitcoin.Comparers;
 2
 3using Outputs;
 4
 5public class TransactionOutputComparer : IComparer<BaseOutput>
 6{
 1087    public static TransactionOutputComparer Instance { get; } = new();
 8
 9    public int Compare(BaseOutput? x, BaseOutput? y)
 10    {
 11        switch (x, y)
 12        {
 13            // Deal with nulls
 14            case (null, null):
 415                return 0;
 16            case (null, not null):
 417                return -1;
 18            case (not null, null):
 419                return 1;
 20        }
 21
 22        // Compare by value (satoshis)
 51623        var valueComparison = x.Amount.Satoshi.CompareTo(y.Amount.Satoshi);
 51624        if (valueComparison != 0)
 25        {
 45626            return valueComparison;
 27        }
 28
 29        // Compare by scriptPubKey lexicographically
 6030        var scriptComparison = CompareScriptPubKey(x.ScriptPubKey.ToBytes(), y.ScriptPubKey.ToBytes());
 6031        if (scriptComparison != 0)
 32        {
 4433            return scriptComparison;
 34        }
 35
 36        // For HTLC outputs, compare by CLTV expiry
 1637        if (x is OfferedHtlcOutput or ReceivedHtlcOutput &&
 1638            y is OfferedHtlcOutput or ReceivedHtlcOutput)
 39        {
 1640            ulong xExpiry = x switch
 1641            {
 1642                OfferedHtlcOutput offered => offered.CltvExpiry,
 043                ReceivedHtlcOutput received => received.CltvExpiry,
 044                _ => 0
 1645            };
 46
 1647            ulong yExpiry = y switch
 1648            {
 1649                OfferedHtlcOutput offered => offered.CltvExpiry,
 050                ReceivedHtlcOutput received => received.CltvExpiry,
 051                _ => 0
 1652            };
 53
 1654            if (xExpiry != yExpiry)
 55            {
 1656                return xExpiry.CompareTo(yExpiry);
 57            }
 58        }
 59
 060        return 0;
 61    }
 62
 63    private static int CompareScriptPubKey(ReadOnlySpan<byte> script1, ReadOnlySpan<byte> script2)
 64    {
 6065        var length = Math.Min(script1.Length, script2.Length);
 147266        for (var i = 0; i < length; i++)
 67        {
 70468            if (script1[i] != script2[i])
 69            {
 2870                return script1[i].CompareTo(script2[i]);
 71            }
 72        }
 73
 74        // Compare by length if scripts are identical up to the length of the shorter one
 3275        return script1.Length.CompareTo(script2.Length);
 76    }
 77}