| | 1 | | using NLightning.Domain.Channels.Enums; |
| | 2 | | using NLightning.Domain.Channels.Interfaces; |
| | 3 | | using NLightning.Domain.Channels.ValueObjects; |
| | 4 | | using NLightning.Domain.Crypto.ValueObjects; |
| | 5 | | using NLightning.Domain.Money; |
| | 6 | | using NLightning.Domain.Protocol.Messages; |
| | 7 | | using NLightning.Domain.Serialization.Interfaces; |
| | 8 | | using NLightning.Infrastructure.Persistence.Contexts; |
| | 9 | | using NLightning.Infrastructure.Persistence.Entities.Channel; |
| | 10 | |
|
| | 11 | | namespace NLightning.Infrastructure.Repositories.Database.Channel; |
| | 12 | |
|
| | 13 | | public class HtlcDbRepository : BaseDbRepository<HtlcEntity>, IHtlcDbRepository |
| | 14 | | { |
| | 15 | | private readonly IMessageSerializer _messageSerializer; |
| | 16 | |
|
| 0 | 17 | | public HtlcDbRepository(NLightningDbContext context, IMessageSerializer messageSerializer) : base(context) |
| 0 | 18 | | { |
| 0 | 19 | | _messageSerializer = messageSerializer; |
| 0 | 20 | | } |
| | 21 | |
|
| | 22 | | public async Task AddAsync(ChannelId channelId, Htlc htlc) |
| 0 | 23 | | { |
| 0 | 24 | | var htlcEntity = await MapDomainToEntityAsync(channelId, htlc, _messageSerializer); |
| 0 | 25 | | Insert(htlcEntity); |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | public async Task UpdateAsync(ChannelId channelId, Htlc htlc) |
| 0 | 29 | | { |
| 0 | 30 | | var htlcEntity = await MapDomainToEntityAsync(channelId, htlc, _messageSerializer); |
| 0 | 31 | | Update(htlcEntity); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public Task DeleteAsync(ChannelId channelId, ulong htlcId, HtlcDirection direction) |
| 0 | 35 | | { |
| 0 | 36 | | return DeleteByIdAsync((channelId, htlcId, (byte)direction)); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | public void DeleteAllForChannelId(ChannelId channelId) |
| 0 | 40 | | { |
| 0 | 41 | | DeleteWhere(h => h.ChannelId.Equals(channelId)); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public async Task<Htlc?> GetByIdAsync(ChannelId channelId, ulong htlcId, HtlcDirection direction) |
| 0 | 45 | | { |
| 0 | 46 | | var htlcEntity = await base.GetByIdAsync((channelId, htlcId, (byte)direction)); |
| | 47 | |
|
| 0 | 48 | | if (htlcEntity == null) |
| 0 | 49 | | return null; |
| | 50 | |
|
| 0 | 51 | | return await MapEntityToDomainAsync(htlcEntity, _messageSerializer); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public async Task<IEnumerable<Htlc>> GetAllForChannelAsync(ChannelId channelId) |
| 0 | 55 | | { |
| 0 | 56 | | var htlcEntities = Get(h => h.ChannelId.Equals(channelId)).ToList(); |
| | 57 | |
|
| 0 | 58 | | return await Task.WhenAll(htlcEntities.Select(h => MapEntityToDomainAsync(h, _messageSerializer))); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public async Task<IEnumerable<Htlc>> GetByChannelIdAndStateAsync(ChannelId channelId, HtlcState state) |
| 0 | 62 | | { |
| 0 | 63 | | var htlcEntities = Get(h => h.ChannelId.Equals(channelId) && h.State.Equals(state)).ToList(); |
| | 64 | |
|
| 0 | 65 | | return await Task.WhenAll(htlcEntities.Select(h => MapEntityToDomainAsync(h, _messageSerializer))); |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public async Task<IEnumerable<Htlc>> GetByChannelIdAndDirectionAsync(ChannelId channelId, HtlcDirection direction) |
| 0 | 69 | | { |
| 0 | 70 | | var htlcEntities = Get(h => h.ChannelId.Equals(channelId) && h.Direction.Equals(direction)).ToList(); |
| | 71 | |
|
| 0 | 72 | | return await Task.WhenAll(htlcEntities.Select(h => MapEntityToDomainAsync(h, _messageSerializer))); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | internal static async Task<HtlcEntity> MapDomainToEntityAsync(ChannelId channelId, Htlc htlc, |
| | 76 | | IMessageSerializer messageSerializer) |
| 0 | 77 | | { |
| 0 | 78 | | using var stream = new MemoryStream(); |
| 0 | 79 | | await messageSerializer.SerializeAsync(htlc.AddMessage, stream); |
| | 80 | |
|
| 0 | 81 | | return new HtlcEntity |
| 0 | 82 | | { |
| 0 | 83 | | ChannelId = channelId, |
| 0 | 84 | | HtlcId = htlc.Id, |
| 0 | 85 | | AmountMsat = htlc.Amount.MilliSatoshi, |
| 0 | 86 | | PaymentHash = htlc.PaymentHash, |
| 0 | 87 | | CltvExpiry = htlc.CltvExpiry, |
| 0 | 88 | | State = (byte)htlc.State, |
| 0 | 89 | | Direction = (byte)htlc.Direction, |
| 0 | 90 | | ObscuredCommitmentNumber = htlc.ObscuredCommitmentNumber, |
| 0 | 91 | | AddMessageBytes = stream.ToArray(), |
| 0 | 92 | | PaymentPreimage = htlc.PaymentPreimage |
| 0 | 93 | | }; |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | internal static async Task<Htlc> MapEntityToDomainAsync(HtlcEntity htlcEntity, IMessageSerializer messageSerializer) |
| 0 | 97 | | { |
| 0 | 98 | | Hash? paymentPreimage = null; |
| 0 | 99 | | if (htlcEntity.PaymentPreimage is not null) |
| 0 | 100 | | paymentPreimage = htlcEntity.PaymentPreimage; |
| | 101 | |
|
| 0 | 102 | | CompactSignature? signature = null; |
| 0 | 103 | | if (htlcEntity.Signature is not null) |
| 0 | 104 | | signature = htlcEntity.Signature; |
| | 105 | |
|
| 0 | 106 | | using var stream = new MemoryStream(htlcEntity.AddMessageBytes); |
| 0 | 107 | | var addMessage = await messageSerializer.DeserializeMessageAsync<UpdateAddHtlcMessage>(stream) ?? |
| 0 | 108 | | throw new InvalidOperationException("Failed to deserialize HTLC add message"); |
| 0 | 109 | | return new Htlc(LightningMoney.MilliSatoshis(htlcEntity.AmountMsat), addMessage, |
| 0 | 110 | | (HtlcDirection)htlcEntity.Direction, htlcEntity.CltvExpiry, htlcEntity.HtlcId, |
| 0 | 111 | | htlcEntity.ObscuredCommitmentNumber, htlcEntity.PaymentHash, |
| 0 | 112 | | (HtlcState)htlcEntity.State, paymentPreimage, signature); |
| 0 | 113 | | } |
| | 114 | | } |