| | 1 | | using System.Collections.Immutable; |
| | 2 | | using Microsoft.EntityFrameworkCore; |
| | 3 | | using NLightning.Domain.Protocol.Models; |
| | 4 | |
|
| | 5 | | namespace NLightning.Infrastructure.Repositories.Database.Channel; |
| | 6 | |
|
| | 7 | | using Domain.Bitcoin.Transactions.Outputs; |
| | 8 | | using Domain.Channels.Enums; |
| | 9 | | using Domain.Channels.Interfaces; |
| | 10 | | using Domain.Channels.Models; |
| | 11 | | using Domain.Channels.ValueObjects; |
| | 12 | | using Domain.Crypto.Constants; |
| | 13 | | using Domain.Crypto.Hashes; |
| | 14 | | using Domain.Crypto.ValueObjects; |
| | 15 | | using Domain.Money; |
| | 16 | | using Domain.Serialization.Interfaces; |
| | 17 | | using Persistence.Contexts; |
| | 18 | | using Persistence.Entities.Channel; |
| | 19 | |
|
| | 20 | | public class ChannelDbRepository : BaseDbRepository<ChannelEntity>, IChannelDbRepository |
| | 21 | | { |
| | 22 | | private readonly IMessageSerializer _messageSerializer; |
| | 23 | | private readonly ISha256 _sha256; |
| | 24 | |
|
| | 25 | | public ChannelDbRepository(NLightningDbContext context, IMessageSerializer messageSerializer, ISha256 sha256) |
| 0 | 26 | | : base(context) |
| 0 | 27 | | { |
| 0 | 28 | | _messageSerializer = messageSerializer ?? throw new ArgumentNullException(nameof(messageSerializer)); |
| 0 | 29 | | _sha256 = sha256 ?? throw new ArgumentNullException(nameof(sha256)); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public async Task AddAsync(ChannelModel channelModel) |
| 0 | 33 | | { |
| 0 | 34 | | var channelEntity = await MapDomainToEntity(channelModel, _messageSerializer); |
| 0 | 35 | | Insert(channelEntity); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public async Task UpdateAsync(ChannelModel channelModel) |
| 0 | 39 | | { |
| 0 | 40 | | var channelEntity = await MapDomainToEntity(channelModel, _messageSerializer); |
| 0 | 41 | | Update(channelEntity); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public async Task<ChannelModel?> GetByIdAsync(ChannelId channelId) |
| 0 | 45 | | { |
| 0 | 46 | | var channelEntity = await DbSet |
| 0 | 47 | | .AsNoTracking() |
| 0 | 48 | | .Include(c => c.Config) |
| 0 | 49 | | .Include(c => c.KeySets) |
| 0 | 50 | | .Include(c => c.Htlcs) |
| 0 | 51 | | .FirstOrDefaultAsync(c => c.ChannelId == channelId); |
| | 52 | |
|
| 0 | 53 | | if (channelEntity is null) |
| 0 | 54 | | return null; |
| | 55 | |
|
| 0 | 56 | | return await MapEntityToDomain(channelEntity, _messageSerializer, _sha256); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public async Task<IEnumerable<ChannelModel>> GetAllAsync() |
| 0 | 60 | | { |
| 0 | 61 | | var channelEntities = await DbSet |
| 0 | 62 | | .AsNoTracking() |
| 0 | 63 | | .Include(c => c.Config) |
| 0 | 64 | | .Include(c => c.KeySets) |
| 0 | 65 | | .Include(c => c.Htlcs) |
| 0 | 66 | | .ToListAsync(); |
| | 67 | |
|
| 0 | 68 | | return await Task.WhenAll( |
| 0 | 69 | | channelEntities.Select(async entity => |
| 0 | 70 | | await MapEntityToDomain(entity, _messageSerializer, _sha256))); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public async Task<IEnumerable<ChannelModel>> GetReadyChannelsAsync() |
| 0 | 74 | | { |
| 0 | 75 | | byte[] readyStateList = |
| 0 | 76 | | [ |
| 0 | 77 | | (byte)ChannelState.V1FundingCreated, |
| 0 | 78 | | (byte)ChannelState.V1FundingSigned, |
| 0 | 79 | | (byte)ChannelState.ReadyForThem, |
| 0 | 80 | | (byte)ChannelState.ReadyForUs, |
| 0 | 81 | | (byte)ChannelState.Open, |
| 0 | 82 | | (byte)ChannelState.Closing |
| 0 | 83 | | ]; |
| | 84 | |
|
| 0 | 85 | | var channelEntities = await DbSet |
| 0 | 86 | | .AsNoTracking() |
| 0 | 87 | | .Include(c => c.Config) |
| 0 | 88 | | .Include(c => c.KeySets) |
| 0 | 89 | | .Include(c => c.Htlcs) |
| 0 | 90 | | .Where(c => readyStateList.Contains(c.State)) |
| 0 | 91 | | .ToListAsync(); |
| | 92 | |
|
| 0 | 93 | | return await Task.WhenAll( |
| 0 | 94 | | channelEntities.Select(async entity => |
| 0 | 95 | | await MapEntityToDomain(entity, _messageSerializer, _sha256))); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public async Task<IEnumerable<ChannelModel?>> GetByPeerIdAsync(CompactPubKey peerNodeId) |
| 0 | 99 | | { |
| 0 | 100 | | var channelEntities = await DbSet |
| 0 | 101 | | .AsNoTracking() |
| 0 | 102 | | .Include(c => c.Config) |
| 0 | 103 | | .Include(c => c.KeySets) |
| 0 | 104 | | .Include(c => c.Htlcs) |
| 0 | 105 | | .Where(c => c.RemoteNodeId.Equals(peerNodeId)) |
| 0 | 106 | | .ToListAsync(); |
| | 107 | |
|
| 0 | 108 | | return await Task.WhenAll( |
| 0 | 109 | | channelEntities.Select(async entity => |
| 0 | 110 | | await MapEntityToDomain(entity, _messageSerializer, _sha256))); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | internal static async Task<ChannelEntity> MapDomainToEntity(ChannelModel channelModel, |
| | 114 | | IMessageSerializer messageSerializer) |
| 0 | 115 | | { |
| 0 | 116 | | var config = ChannelConfigDbRepository.MapDomainToEntity(channelModel.ChannelId, channelModel.ChannelConfig); |
| 0 | 117 | | ImmutableArray<ChannelKeySetEntity> keySets = |
| 0 | 118 | | [ |
| 0 | 119 | | ChannelKeySetDbRepository.MapDomainToEntity(channelModel.ChannelId, true, channelModel.LocalKeySet), |
| 0 | 120 | | ChannelKeySetDbRepository.MapDomainToEntity(channelModel.ChannelId, false, channelModel.RemoteKeySet) |
| 0 | 121 | | ]; |
| | 122 | |
|
| 0 | 123 | | var htlcs = new List<Htlc>(); |
| 0 | 124 | | htlcs.AddRange(GetHtlcsOrNull(channelModel.LocalOfferedHtlcs)); |
| 0 | 125 | | htlcs.AddRange(GetHtlcsOrNull(channelModel.LocalFulfilledHtlcs)); |
| 0 | 126 | | htlcs.AddRange(GetHtlcsOrNull(channelModel.LocalOldHtlcs)); |
| 0 | 127 | | htlcs.AddRange(GetHtlcsOrNull(channelModel.RemoteOfferedHtlcs)); |
| 0 | 128 | | htlcs.AddRange(GetHtlcsOrNull(channelModel.RemoteFulfilledHtlcs)); |
| 0 | 129 | | htlcs.AddRange(GetHtlcsOrNull(channelModel.RemoteOldHtlcs)); |
| | 130 | |
|
| 0 | 131 | | List<HtlcEntity>? htlcEntities = null; |
| 0 | 132 | | if (htlcs.Count > 0) |
| 0 | 133 | | { |
| 0 | 134 | | htlcEntities = []; |
| | 135 | |
|
| 0 | 136 | | foreach (var htlc in htlcs) |
| 0 | 137 | | htlcEntities.Add( |
| 0 | 138 | | await HtlcDbRepository.MapDomainToEntityAsync(channelModel.ChannelId, htlc, messageSerializer)); |
| 0 | 139 | | } |
| | 140 | |
|
| 0 | 141 | | return new ChannelEntity |
| 0 | 142 | | { |
| 0 | 143 | | ChannelId = channelModel.ChannelId, |
| 0 | 144 | |
|
| 0 | 145 | | FundingCreatedAtBlockHeight = channelModel.FundingCreatedAtBlockHeight, |
| 0 | 146 | | FundingTxId = channelModel.FundingOutput.TransactionId ?? new byte[CryptoConstants.Sha256HashLen], |
| 0 | 147 | | FundingOutputIndex = channelModel.FundingOutput.Index ?? 0, |
| 0 | 148 | | FundingAmountSatoshis = channelModel.FundingOutput.Amount.Satoshi, |
| 0 | 149 | |
|
| 0 | 150 | | IsInitiator = channelModel.IsInitiator, |
| 0 | 151 | | RemoteNodeId = channelModel.RemoteNodeId, |
| 0 | 152 | | State = (byte)channelModel.State, |
| 0 | 153 | | Version = (byte)channelModel.Version, |
| 0 | 154 | |
|
| 0 | 155 | | LocalBalanceSatoshis = channelModel.LocalBalance.Satoshi, |
| 0 | 156 | | RemoteBalanceSatoshis = channelModel.RemoteBalance.Satoshi, |
| 0 | 157 | |
|
| 0 | 158 | | LocalNextHtlcId = channelModel.LocalNextHtlcId, |
| 0 | 159 | | RemoteNextHtlcId = channelModel.RemoteNextHtlcId, |
| 0 | 160 | | LocalRevocationNumber = channelModel.LocalRevocationNumber, |
| 0 | 161 | | RemoteRevocationNumber = channelModel.RemoteRevocationNumber, |
| 0 | 162 | | LastSentSignature = channelModel.LastSentSignature?.Value ?? null, |
| 0 | 163 | | LastReceivedSignature = channelModel.LastReceivedSignature?.Value ?? null, |
| 0 | 164 | |
|
| 0 | 165 | | Config = config, |
| 0 | 166 | | KeySets = keySets, |
| 0 | 167 | | Htlcs = htlcEntities |
| 0 | 168 | | }; |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | internal static async Task<ChannelModel> MapEntityToDomain(ChannelEntity channelEntity, |
| | 172 | | IMessageSerializer messageSerializer, ISha256 sha256) |
| 0 | 173 | | { |
| 0 | 174 | | if (channelEntity.Config is null) |
| 0 | 175 | | throw new InvalidOperationException( |
| 0 | 176 | | "Channel config cannot be null when mapping channel entity to domain model."); |
| | 177 | |
|
| 0 | 178 | | if (channelEntity.KeySets is not { Count: 2 }) |
| 0 | 179 | | throw new InvalidOperationException( |
| 0 | 180 | | "Channel key sets must contain exactly two entries when mapping channel entity to domain model."); |
| | 181 | |
|
| 0 | 182 | | var localKeySetEntity = channelEntity.KeySets.FirstOrDefault(k => k.IsLocal) ?? |
| 0 | 183 | | throw new InvalidOperationException( |
| 0 | 184 | | "Local key set cannot be null when mapping channel entity to domain model."); |
| 0 | 185 | | var remoteKeySetEntity = channelEntity.KeySets.FirstOrDefault(k => !k.IsLocal) ?? |
| 0 | 186 | | throw new InvalidOperationException( |
| 0 | 187 | | "Remote key set cannot be null when mapping channel entity to domain model."); |
| 0 | 188 | | var config = ChannelConfigDbRepository.MapEntityToDomain(channelEntity.Config); |
| 0 | 189 | | var localKeySet = ChannelKeySetDbRepository.MapEntityToDomain(localKeySetEntity); |
| 0 | 190 | | var remoteKeySet = ChannelKeySetDbRepository.MapEntityToDomain(remoteKeySetEntity); |
| | 191 | |
|
| 0 | 192 | | var localOfferedHtlcs = new List<Htlc>(); |
| 0 | 193 | | var localFulfilledHtlcs = new List<Htlc>(); |
| 0 | 194 | | var localOldHtlcs = new List<Htlc>(); |
| 0 | 195 | | var remoteOfferedHtlcs = new List<Htlc>(); |
| 0 | 196 | | var remoteFulfilledHtlcs = new List<Htlc>(); |
| 0 | 197 | | var remoteOldHtlcs = new List<Htlc>(); |
| 0 | 198 | | if (channelEntity.Htlcs is { Count: > 0 }) |
| 0 | 199 | | { |
| 0 | 200 | | foreach (var htlc in channelEntity.Htlcs.Where(h => h.State.Equals(HtlcState.Offered))) |
| 0 | 201 | | { |
| 0 | 202 | | var domainHtlc = await HtlcDbRepository.MapEntityToDomainAsync(htlc, messageSerializer); |
| 0 | 203 | | if (htlc.Direction.Equals(HtlcDirection.Outgoing)) |
| 0 | 204 | | localOfferedHtlcs.Add(domainHtlc); |
| | 205 | | else |
| 0 | 206 | | remoteOfferedHtlcs.Add(domainHtlc); |
| 0 | 207 | | } |
| | 208 | |
|
| 0 | 209 | | foreach (var htlc in channelEntity.Htlcs.Where(h => h.State.Equals(HtlcState.Fulfilled))) |
| 0 | 210 | | { |
| 0 | 211 | | var domainHtlc = await HtlcDbRepository.MapEntityToDomainAsync(htlc, messageSerializer); |
| 0 | 212 | | if (htlc.Direction.Equals(HtlcDirection.Outgoing)) |
| 0 | 213 | | localFulfilledHtlcs.Add(domainHtlc); |
| | 214 | | else |
| 0 | 215 | | remoteFulfilledHtlcs.Add(domainHtlc); |
| 0 | 216 | | } |
| | 217 | |
|
| 0 | 218 | | byte[] oldStates = [(byte)HtlcState.Expired, (byte)HtlcState.Failed]; |
| 0 | 219 | | foreach (var htlc in channelEntity.Htlcs.Where(h => oldStates.Contains(h.State))) |
| 0 | 220 | | { |
| 0 | 221 | | var domainHtlc = await HtlcDbRepository.MapEntityToDomainAsync(htlc, messageSerializer); |
| 0 | 222 | | if (htlc.Direction.Equals(HtlcDirection.Outgoing)) |
| 0 | 223 | | localOldHtlcs.Add(domainHtlc); |
| | 224 | | else |
| 0 | 225 | | remoteOldHtlcs.Add(domainHtlc); |
| 0 | 226 | | } |
| 0 | 227 | | } |
| | 228 | |
|
| 0 | 229 | | var fundingOutput = new FundingOutputInfo(LightningMoney.Satoshis(channelEntity.FundingAmountSatoshis), |
| 0 | 230 | | localKeySet.FundingCompactPubKey, localKeySet.FundingCompactPubKey) |
| 0 | 231 | | { |
| 0 | 232 | | Index = channelEntity.FundingOutputIndex, |
| 0 | 233 | | TransactionId = channelEntity.FundingTxId |
| 0 | 234 | | }; |
| | 235 | |
|
| 0 | 236 | | var commitmentNumber = |
| 0 | 237 | | new CommitmentNumber(localKeySet.PaymentCompactBasepoint, remoteKeySet.PaymentCompactBasepoint, sha256, |
| 0 | 238 | | channelEntity.LocalRevocationNumber + 1); |
| | 239 | |
|
| 0 | 240 | | var remoteNodeId = channelEntity.RemoteNodeId; |
| | 241 | |
|
| 0 | 242 | | CompactSignature? lastSentSig = null; |
| 0 | 243 | | if (channelEntity.LastSentSignature != null) |
| 0 | 244 | | lastSentSig = new CompactSignature(channelEntity.LastSentSignature); |
| | 245 | |
|
| 0 | 246 | | CompactSignature? lastReceivedSig = null; |
| 0 | 247 | | if (channelEntity.LastReceivedSignature != null) |
| 0 | 248 | | lastReceivedSig = new CompactSignature(channelEntity.LastReceivedSignature); |
| | 249 | |
|
| 0 | 250 | | return new ChannelModel(config, channelEntity.ChannelId, commitmentNumber, fundingOutput, |
| 0 | 251 | | channelEntity.IsInitiator, lastSentSig, lastReceivedSig, |
| 0 | 252 | | LightningMoney.Satoshis(channelEntity.LocalBalanceSatoshis), localKeySet, |
| 0 | 253 | | channelEntity.LocalNextHtlcId, channelEntity.LocalRevocationNumber, |
| 0 | 254 | | LightningMoney.Satoshis(channelEntity.RemoteBalanceSatoshis), remoteKeySet, |
| 0 | 255 | | channelEntity.RemoteNextHtlcId, remoteNodeId, channelEntity.RemoteRevocationNumber, |
| 0 | 256 | | (ChannelState)channelEntity.State, (ChannelVersion)channelEntity.Version, |
| 0 | 257 | | localOfferedHtlcs, localFulfilledHtlcs, localOldHtlcs, null, remoteOfferedHtlcs, |
| 0 | 258 | | remoteFulfilledHtlcs, remoteOldHtlcs) |
| 0 | 259 | | { |
| 0 | 260 | | FundingCreatedAtBlockHeight = channelEntity.FundingCreatedAtBlockHeight |
| 0 | 261 | | }; |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | private static ICollection<Htlc> GetHtlcsOrNull(ICollection<Htlc>? htlcs) |
| 0 | 265 | | { |
| 0 | 266 | | return htlcs is { Count: > 0 } ? htlcs : []; |
| 0 | 267 | | } |
| | 268 | | } |