< 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: 30_15166811759
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{
 1687    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)
 60423        var valueComparison = x.Amount.CompareTo(y.Amount);
 60424        if (valueComparison != 0)
 25        {
 50026            return valueComparison;
 27        }
 28
 29        // Compare by scriptPubKey lexicographically
 10430        var scriptComparison = CompareScriptPubKey(x.ScriptPubKey.ToBytes(), y.ScriptPubKey.ToBytes());
 10431        if (scriptComparison != 0)
 32        {
 8433            return scriptComparison;
 34        }
 35
 36        // For HTLC outputs, compare by CLTV expiry
 2037        if (x is OfferedHtlcOutput or ReceivedHtlcOutput &&
 2038            y is OfferedHtlcOutput or ReceivedHtlcOutput)
 39        {
 2040            ulong xExpiry = x switch
 2041            {
 2042                OfferedHtlcOutput offered => offered.CltvExpiry,
 043                ReceivedHtlcOutput received => received.CltvExpiry,
 044                _ => 0
 2045            };
 46
 2047            ulong yExpiry = y switch
 2048            {
 2049                OfferedHtlcOutput offered => offered.CltvExpiry,
 050                ReceivedHtlcOutput received => received.CltvExpiry,
 051                _ => 0
 2052            };
 53
 2054            if (xExpiry != yExpiry)
 55            {
 2056                return xExpiry.CompareTo(yExpiry);
 57            }
 58        }
 59
 060        return 0;
 61    }
 62
 63    private static int CompareScriptPubKey(ReadOnlySpan<byte> script1, ReadOnlySpan<byte> script2)
 64    {
 10465        var length = Math.Min(script1.Length, script2.Length);
 199266        for (var i = 0; i < length; i++)
 67        {
 96068            if (script1[i] != script2[i])
 69            {
 6870                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
 3675        return script1.Length.CompareTo(script2.Length);
 76    }
 77}