| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Infrastructure.Repositories; |
| | | 4 | | |
| | | 5 | | using Database.Bitcoin; |
| | | 6 | | using Database.Channel; |
| | | 7 | | using Database.Node; |
| | | 8 | | using Domain.Bitcoin.Interfaces; |
| | | 9 | | using Domain.Bitcoin.ValueObjects; |
| | | 10 | | using Domain.Bitcoin.Wallet.Models; |
| | | 11 | | using Domain.Channels.Interfaces; |
| | | 12 | | using Domain.Channels.Models; |
| | | 13 | | using Domain.Crypto.Hashes; |
| | | 14 | | using Domain.Node.Interfaces; |
| | | 15 | | using Domain.Node.Models; |
| | | 16 | | using Domain.Persistence.Interfaces; |
| | | 17 | | using Domain.Serialization.Interfaces; |
| | | 18 | | using Persistence.Contexts; |
| | | 19 | | |
| | | 20 | | public class UnitOfWork : IUnitOfWork |
| | | 21 | | { |
| | | 22 | | private readonly NLightningDbContext _context; |
| | | 23 | | private readonly ILogger<UnitOfWork> _logger; |
| | | 24 | | private readonly IMessageSerializer _messageSerializer; |
| | | 25 | | private readonly ISha256 _sha256; |
| | | 26 | | private readonly IUtxoMemoryRepository _utxoMemoryRepository; |
| | | 27 | | |
| | | 28 | | // Bitcoin repositories |
| | | 29 | | private BlockchainStateDbRepository? _blockchainStateDbRepository; |
| | | 30 | | private WatchedTransactionDbRepository? _watchedTransactionDbRepository; |
| | | 31 | | private WalletAddressesDbRepository? _walletAddressesDbRepository; |
| | | 32 | | private UtxoDbRepository? _utxoDbRepository; |
| | | 33 | | |
| | | 34 | | // Channel repositories |
| | | 35 | | private ChannelConfigDbRepository? _channelConfigDbRepository; |
| | | 36 | | private ChannelDbRepository? _channelDbRepository; |
| | | 37 | | private ChannelKeySetDbRepository? _channelKeySetDbRepository; |
| | | 38 | | private HtlcDbRepository? _htlcDbRepository; |
| | | 39 | | |
| | | 40 | | // Node repositories |
| | | 41 | | private PeerDbRepository? _peerDbRepository; |
| | | 42 | | |
| | | 43 | | public IBlockchainStateDbRepository BlockchainStateDbRepository => |
| | 0 | 44 | | _blockchainStateDbRepository ??= new BlockchainStateDbRepository(_context); |
| | | 45 | | |
| | | 46 | | public IWatchedTransactionDbRepository WatchedTransactionDbRepository => |
| | 0 | 47 | | _watchedTransactionDbRepository ??= new WatchedTransactionDbRepository(_context); |
| | | 48 | | |
| | | 49 | | public IWalletAddressesDbRepository WalletAddressesDbRepository => |
| | 0 | 50 | | _walletAddressesDbRepository ??= new WalletAddressesDbRepository(_context); |
| | | 51 | | |
| | 0 | 52 | | public IUtxoDbRepository UtxoDbRepository => _utxoDbRepository ??= new UtxoDbRepository(_context); |
| | | 53 | | |
| | | 54 | | public IChannelConfigDbRepository ChannelConfigDbRepository => |
| | 0 | 55 | | _channelConfigDbRepository ??= new ChannelConfigDbRepository(_context); |
| | | 56 | | |
| | | 57 | | public IChannelDbRepository ChannelDbRepository => |
| | 0 | 58 | | _channelDbRepository ??= new ChannelDbRepository(_context, _messageSerializer, _sha256); |
| | | 59 | | |
| | | 60 | | public IChannelKeySetDbRepository ChannelKeySetDbRepository => |
| | 0 | 61 | | _channelKeySetDbRepository ??= new ChannelKeySetDbRepository(_context); |
| | | 62 | | |
| | | 63 | | public IHtlcDbRepository HtlcDbRepository => |
| | 0 | 64 | | _htlcDbRepository ??= new HtlcDbRepository(_context, _messageSerializer); |
| | | 65 | | |
| | | 66 | | public IPeerDbRepository PeerDbRepository => |
| | 0 | 67 | | _peerDbRepository ??= new PeerDbRepository(_context); |
| | | 68 | | |
| | 0 | 69 | | public UnitOfWork(NLightningDbContext context, ILogger<UnitOfWork> logger, IMessageSerializer messageSerializer, |
| | 0 | 70 | | ISha256 sha256, IUtxoMemoryRepository utxoMemoryRepository) |
| | 0 | 71 | | { |
| | 0 | 72 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 0 | 73 | | _logger = logger; |
| | 0 | 74 | | _messageSerializer = messageSerializer; |
| | 0 | 75 | | _sha256 = sha256; |
| | 0 | 76 | | _utxoMemoryRepository = utxoMemoryRepository; |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public async Task<ICollection<PeerModel>> GetPeersForStartupAsync() |
| | 0 | 80 | | { |
| | 0 | 81 | | var peers = await PeerDbRepository.GetAllAsync(); |
| | 0 | 82 | | var peerList = peers.ToList(); |
| | 0 | 83 | | foreach (var peer in peerList) |
| | 0 | 84 | | { |
| | 0 | 85 | | var channels = await ChannelDbRepository.GetByPeerIdAsync(peer.NodeId); |
| | 0 | 86 | | var channelList = channels.ToList(); |
| | 0 | 87 | | if (channelList.Count > 0) |
| | 0 | 88 | | peer.Channels = channelList as List<ChannelModel>; |
| | 0 | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return peerList; |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | public void AddUtxo(UtxoModel utxoModel) |
| | 0 | 95 | | { |
| | | 96 | | try |
| | 0 | 97 | | { |
| | 0 | 98 | | _utxoMemoryRepository.Add(utxoModel); |
| | 0 | 99 | | } |
| | 0 | 100 | | catch (Exception e) |
| | 0 | 101 | | { |
| | 0 | 102 | | _logger.LogError(e, "Failed to add Utxo to memory repository"); |
| | 0 | 103 | | throw; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | try |
| | 0 | 107 | | { |
| | 0 | 108 | | UtxoDbRepository.Add(utxoModel); |
| | 0 | 109 | | } |
| | 0 | 110 | | catch (Exception e) |
| | 0 | 111 | | { |
| | 0 | 112 | | _logger.LogError(e, "Failed to add Utxo to the database"); |
| | | 113 | | |
| | | 114 | | // Rollback memory repository operation |
| | 0 | 115 | | _utxoMemoryRepository.Spend(utxoModel); |
| | 0 | 116 | | } |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | public void TrySpendUtxo(TxId transactionId, uint index) |
| | 0 | 120 | | { |
| | | 121 | | // Check if utxo exists in memory |
| | 0 | 122 | | if (!_utxoMemoryRepository.TryGetUtxo(transactionId, index, out var utxoModel)) |
| | 0 | 123 | | return; |
| | | 124 | | |
| | | 125 | | try |
| | 0 | 126 | | { |
| | 0 | 127 | | _utxoMemoryRepository.Spend(utxoModel); |
| | 0 | 128 | | } |
| | 0 | 129 | | catch (Exception e) |
| | 0 | 130 | | { |
| | 0 | 131 | | _logger.LogError(e, "Failed to spend Utxo from memory repository"); |
| | 0 | 132 | | throw; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | try |
| | 0 | 136 | | { |
| | 0 | 137 | | UtxoDbRepository.Spend(utxoModel); |
| | 0 | 138 | | } |
| | 0 | 139 | | catch (Exception e) |
| | 0 | 140 | | { |
| | 0 | 141 | | _logger.LogError(e, "Failed to spend Utxo from the database"); |
| | | 142 | | |
| | | 143 | | // Rollback memory repository operation |
| | 0 | 144 | | _utxoMemoryRepository.Add(utxoModel); |
| | 0 | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | public void SaveChanges() |
| | 0 | 149 | | { |
| | 0 | 150 | | _context.SaveChanges(); |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | public Task SaveChangesAsync() |
| | 0 | 154 | | { |
| | 0 | 155 | | return _context.SaveChangesAsync(); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | #region Dispose Pattern |
| | | 159 | | |
| | | 160 | | private bool _disposed; |
| | | 161 | | |
| | | 162 | | protected virtual void Dispose(bool disposing) |
| | 0 | 163 | | { |
| | 0 | 164 | | if (!_disposed) |
| | 0 | 165 | | { |
| | 0 | 166 | | if (disposing) |
| | 0 | 167 | | _context.Dispose(); |
| | 0 | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | _disposed = true; |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | public void Dispose() |
| | 0 | 174 | | { |
| | 0 | 175 | | Dispose(true); |
| | 0 | 176 | | GC.SuppressFinalize(this); |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | #endregion |
| | | 180 | | } |