< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Repositories.Database.Channel.HtlcDbRepository
Assembly: NLightning.Infrastructure.Repositories
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Repositories/Database/Channel/HtlcDbRepository.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 68
Coverable lines: 68
Total lines: 114
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Repositories/Database/Channel/HtlcDbRepository.cs

#LineLine coverage
 1using NLightning.Domain.Channels.Enums;
 2using NLightning.Domain.Channels.Interfaces;
 3using NLightning.Domain.Channels.ValueObjects;
 4using NLightning.Domain.Crypto.ValueObjects;
 5using NLightning.Domain.Money;
 6using NLightning.Domain.Protocol.Messages;
 7using NLightning.Domain.Serialization.Interfaces;
 8using NLightning.Infrastructure.Persistence.Contexts;
 9using NLightning.Infrastructure.Persistence.Entities.Channel;
 10
 11namespace NLightning.Infrastructure.Repositories.Database.Channel;
 12
 13public class HtlcDbRepository : BaseDbRepository<HtlcEntity>, IHtlcDbRepository
 14{
 15    private readonly IMessageSerializer _messageSerializer;
 16
 017    public HtlcDbRepository(NLightningDbContext context, IMessageSerializer messageSerializer) : base(context)
 018    {
 019        _messageSerializer = messageSerializer;
 020    }
 21
 22    public async Task AddAsync(ChannelId channelId, Htlc htlc)
 023    {
 024        var htlcEntity = await MapDomainToEntityAsync(channelId, htlc, _messageSerializer);
 025        Insert(htlcEntity);
 026    }
 27
 28    public async Task UpdateAsync(ChannelId channelId, Htlc htlc)
 029    {
 030        var htlcEntity = await MapDomainToEntityAsync(channelId, htlc, _messageSerializer);
 031        Update(htlcEntity);
 032    }
 33
 34    public Task DeleteAsync(ChannelId channelId, ulong htlcId, HtlcDirection direction)
 035    {
 036        return DeleteByIdAsync((channelId, htlcId, (byte)direction));
 037    }
 38
 39    public void DeleteAllForChannelId(ChannelId channelId)
 040    {
 041        DeleteWhere(h => h.ChannelId.Equals(channelId));
 042    }
 43
 44    public async Task<Htlc?> GetByIdAsync(ChannelId channelId, ulong htlcId, HtlcDirection direction)
 045    {
 046        var htlcEntity = await base.GetByIdAsync((channelId, htlcId, (byte)direction));
 47
 048        if (htlcEntity == null)
 049            return null;
 50
 051        return await MapEntityToDomainAsync(htlcEntity, _messageSerializer);
 052    }
 53
 54    public async Task<IEnumerable<Htlc>> GetAllForChannelAsync(ChannelId channelId)
 055    {
 056        var htlcEntities = Get(h => h.ChannelId.Equals(channelId)).ToList();
 57
 058        return await Task.WhenAll(htlcEntities.Select(h => MapEntityToDomainAsync(h, _messageSerializer)));
 059    }
 60
 61    public async Task<IEnumerable<Htlc>> GetByChannelIdAndStateAsync(ChannelId channelId, HtlcState state)
 062    {
 063        var htlcEntities = Get(h => h.ChannelId.Equals(channelId) && h.State.Equals(state)).ToList();
 64
 065        return await Task.WhenAll(htlcEntities.Select(h => MapEntityToDomainAsync(h, _messageSerializer)));
 066    }
 67
 68    public async Task<IEnumerable<Htlc>> GetByChannelIdAndDirectionAsync(ChannelId channelId, HtlcDirection direction)
 069    {
 070        var htlcEntities = Get(h => h.ChannelId.Equals(channelId) && h.Direction.Equals(direction)).ToList();
 71
 072        return await Task.WhenAll(htlcEntities.Select(h => MapEntityToDomainAsync(h, _messageSerializer)));
 073    }
 74
 75    internal static async Task<HtlcEntity> MapDomainToEntityAsync(ChannelId channelId, Htlc htlc,
 76                                                                  IMessageSerializer messageSerializer)
 077    {
 078        using var stream = new MemoryStream();
 079        await messageSerializer.SerializeAsync(htlc.AddMessage, stream);
 80
 081        return new HtlcEntity
 082        {
 083            ChannelId = channelId,
 084            HtlcId = htlc.Id,
 085            AmountMsat = htlc.Amount.MilliSatoshi,
 086            PaymentHash = htlc.PaymentHash,
 087            CltvExpiry = htlc.CltvExpiry,
 088            State = (byte)htlc.State,
 089            Direction = (byte)htlc.Direction,
 090            ObscuredCommitmentNumber = htlc.ObscuredCommitmentNumber,
 091            AddMessageBytes = stream.ToArray(),
 092            PaymentPreimage = htlc.PaymentPreimage
 093        };
 094    }
 95
 96    internal static async Task<Htlc> MapEntityToDomainAsync(HtlcEntity htlcEntity, IMessageSerializer messageSerializer)
 097    {
 098        Hash? paymentPreimage = null;
 099        if (htlcEntity.PaymentPreimage is not null)
 0100            paymentPreimage = htlcEntity.PaymentPreimage;
 101
 0102        CompactSignature? signature = null;
 0103        if (htlcEntity.Signature is not null)
 0104            signature = htlcEntity.Signature;
 105
 0106        using var stream = new MemoryStream(htlcEntity.AddMessageBytes);
 0107        var addMessage = await messageSerializer.DeserializeMessageAsync<UpdateAddHtlcMessage>(stream) ??
 0108                         throw new InvalidOperationException("Failed to deserialize HTLC add message");
 0109        return new Htlc(LightningMoney.MilliSatoshis(htlcEntity.AmountMsat), addMessage,
 0110                        (HtlcDirection)htlcEntity.Direction, htlcEntity.CltvExpiry, htlcEntity.HtlcId,
 0111                        htlcEntity.ObscuredCommitmentNumber, htlcEntity.PaymentHash,
 0112                        (HtlcState)htlcEntity.State, paymentPreimage, signature);
 0113    }
 114}