| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Persistence.EntityConfiguration.Channel; |
| | 5 | |
|
| | 6 | | using Domain.Bitcoin.Transactions.Constants; |
| | 7 | | using Domain.Channels.Constants; |
| | 8 | | using Entities.Channel; |
| | 9 | | using Enums; |
| | 10 | | using ValueConverters; |
| | 11 | |
|
| | 12 | | public static class HtlcEntityConfiguration |
| | 13 | | { |
| | 14 | | public static void ConfigureHtlcEntity(this ModelBuilder modelBuilder, DatabaseType databaseType) |
| | 15 | | { |
| 0 | 16 | | modelBuilder.Entity<HtlcEntity>(entity => |
| 0 | 17 | | { |
| 0 | 18 | | // Configure the composite key using ChannelId, HtlcId, and Direction |
| 0 | 19 | | entity.HasKey(h => new { h.ChannelId, h.HtlcId, h.Direction }); |
| 0 | 20 | |
|
| 0 | 21 | | // Set required props |
| 0 | 22 | | entity.Property(e => e.HtlcId).IsRequired(); |
| 0 | 23 | | entity.Property(e => e.Direction).IsRequired(); |
| 0 | 24 | | entity.Property(e => e.AmountMsat).IsRequired(); |
| 0 | 25 | | entity.Property(e => e.CltvExpiry).IsRequired(); |
| 0 | 26 | | entity.Property(e => e.State).IsRequired(); |
| 0 | 27 | | entity.Property(e => e.ObscuredCommitmentNumber).IsRequired(); |
| 0 | 28 | |
|
| 0 | 29 | | // Required byte[] properties |
| 0 | 30 | | entity.Property(h => h.ChannelId) |
| 0 | 31 | | .HasConversion<ChannelIdConverter>() |
| 0 | 32 | | .IsRequired(); |
| 0 | 33 | | entity.Property(h => h.PaymentHash).IsRequired(); |
| 0 | 34 | | entity.Property(h => h.AddMessageBytes).IsRequired(); |
| 0 | 35 | |
|
| 0 | 36 | | // Nullable byte[] properties |
| 0 | 37 | | entity.Property(h => h.PaymentPreimage).IsRequired(false); |
| 0 | 38 | | entity.Property(h => h.Signature).IsRequired(false); |
| 0 | 39 | |
|
| 0 | 40 | | if (databaseType == DatabaseType.MicrosoftSql) |
| 0 | 41 | | { |
| 0 | 42 | | OptimizeConfigurationForSqlServer(entity); |
| 0 | 43 | | } |
| 0 | 44 | | }); |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | private static void OptimizeConfigurationForSqlServer(EntityTypeBuilder<HtlcEntity> entity) |
| | 48 | | { |
| 0 | 49 | | entity.Property(h => h.ChannelId).HasColumnType($"varbinary({ChannelConstants.ChannelIdLength})"); |
| 0 | 50 | | entity.Property(h => h.PaymentHash).HasColumnType($"varbinary({TransactionConstants.TxIdLength})"); |
| 0 | 51 | | entity.Property(h => h.PaymentPreimage).HasColumnType($"varbinary({TransactionConstants.TxIdLength})"); |
| 0 | 52 | | entity.Property(h => h.AddMessageBytes).HasColumnType("varbinary(max)"); |
| 0 | 53 | | } |
| | 54 | | } |