< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Repositories.UnitOfWork
Assembly: NLightning.Infrastructure.Repositories
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Infrastructure.Repositories/UnitOfWork.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 84
Coverable lines: 84
Total lines: 180
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 30
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/UnitOfWork.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2
 3namespace NLightning.Infrastructure.Repositories;
 4
 5using Database.Bitcoin;
 6using Database.Channel;
 7using Database.Node;
 8using Domain.Bitcoin.Interfaces;
 9using Domain.Bitcoin.ValueObjects;
 10using Domain.Bitcoin.Wallet.Models;
 11using Domain.Channels.Interfaces;
 12using Domain.Channels.Models;
 13using Domain.Crypto.Hashes;
 14using Domain.Node.Interfaces;
 15using Domain.Node.Models;
 16using Domain.Persistence.Interfaces;
 17using Domain.Serialization.Interfaces;
 18using Persistence.Contexts;
 19
 20public 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 =>
 044        _blockchainStateDbRepository ??= new BlockchainStateDbRepository(_context);
 45
 46    public IWatchedTransactionDbRepository WatchedTransactionDbRepository =>
 047        _watchedTransactionDbRepository ??= new WatchedTransactionDbRepository(_context);
 48
 49    public IWalletAddressesDbRepository WalletAddressesDbRepository =>
 050        _walletAddressesDbRepository ??= new WalletAddressesDbRepository(_context);
 51
 052    public IUtxoDbRepository UtxoDbRepository => _utxoDbRepository ??= new UtxoDbRepository(_context);
 53
 54    public IChannelConfigDbRepository ChannelConfigDbRepository =>
 055        _channelConfigDbRepository ??= new ChannelConfigDbRepository(_context);
 56
 57    public IChannelDbRepository ChannelDbRepository =>
 058        _channelDbRepository ??= new ChannelDbRepository(_context, _messageSerializer, _sha256);
 59
 60    public IChannelKeySetDbRepository ChannelKeySetDbRepository =>
 061        _channelKeySetDbRepository ??= new ChannelKeySetDbRepository(_context);
 62
 63    public IHtlcDbRepository HtlcDbRepository =>
 064        _htlcDbRepository ??= new HtlcDbRepository(_context, _messageSerializer);
 65
 66    public IPeerDbRepository PeerDbRepository =>
 067        _peerDbRepository ??= new PeerDbRepository(_context);
 68
 069    public UnitOfWork(NLightningDbContext context, ILogger<UnitOfWork> logger, IMessageSerializer messageSerializer,
 070                      ISha256 sha256, IUtxoMemoryRepository utxoMemoryRepository)
 071    {
 072        _context = context ?? throw new ArgumentNullException(nameof(context));
 073        _logger = logger;
 074        _messageSerializer = messageSerializer;
 075        _sha256 = sha256;
 076        _utxoMemoryRepository = utxoMemoryRepository;
 077    }
 78
 79    public async Task<ICollection<PeerModel>> GetPeersForStartupAsync()
 080    {
 081        var peers = await PeerDbRepository.GetAllAsync();
 082        var peerList = peers.ToList();
 083        foreach (var peer in peerList)
 084        {
 085            var channels = await ChannelDbRepository.GetByPeerIdAsync(peer.NodeId);
 086            var channelList = channels.ToList();
 087            if (channelList.Count > 0)
 088                peer.Channels = channelList as List<ChannelModel>;
 089        }
 90
 091        return peerList;
 092    }
 93
 94    public void AddUtxo(UtxoModel utxoModel)
 095    {
 96        try
 097        {
 098            _utxoMemoryRepository.Add(utxoModel);
 099        }
 0100        catch (Exception e)
 0101        {
 0102            _logger.LogError(e, "Failed to add Utxo to memory repository");
 0103            throw;
 104        }
 105
 106        try
 0107        {
 0108            UtxoDbRepository.Add(utxoModel);
 0109        }
 0110        catch (Exception e)
 0111        {
 0112            _logger.LogError(e, "Failed to add Utxo to the database");
 113
 114            // Rollback memory repository operation
 0115            _utxoMemoryRepository.Spend(utxoModel);
 0116        }
 0117    }
 118
 119    public void TrySpendUtxo(TxId transactionId, uint index)
 0120    {
 121        // Check if utxo exists in memory
 0122        if (!_utxoMemoryRepository.TryGetUtxo(transactionId, index, out var utxoModel))
 0123            return;
 124
 125        try
 0126        {
 0127            _utxoMemoryRepository.Spend(utxoModel);
 0128        }
 0129        catch (Exception e)
 0130        {
 0131            _logger.LogError(e, "Failed to spend Utxo from memory repository");
 0132            throw;
 133        }
 134
 135        try
 0136        {
 0137            UtxoDbRepository.Spend(utxoModel);
 0138        }
 0139        catch (Exception e)
 0140        {
 0141            _logger.LogError(e, "Failed to spend Utxo from the database");
 142
 143            // Rollback memory repository operation
 0144            _utxoMemoryRepository.Add(utxoModel);
 0145        }
 0146    }
 147
 148    public void SaveChanges()
 0149    {
 0150        _context.SaveChanges();
 0151    }
 152
 153    public Task SaveChangesAsync()
 0154    {
 0155        return _context.SaveChangesAsync();
 0156    }
 157
 158    #region Dispose Pattern
 159
 160    private bool _disposed;
 161
 162    protected virtual void Dispose(bool disposing)
 0163    {
 0164        if (!_disposed)
 0165        {
 0166            if (disposing)
 0167                _context.Dispose();
 0168        }
 169
 0170        _disposed = true;
 0171    }
 172
 173    public void Dispose()
 0174    {
 0175        Dispose(true);
 0176        GC.SuppressFinalize(this);
 0177    }
 178
 179    #endregion
 180}