Skip to content

Commit

Permalink
feat: exclude frozen utxos
Browse files Browse the repository at this point in the history
  • Loading branch information
markettes committed Jun 28, 2024
1 parent 1ff1f20 commit b61e449
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public class Constants
/// Max ratio of the tx total input sum that could be used as fee
/// </summary>
public static decimal MAX_TX_FEE_RATIO =0.5m;


public static string IsFrozenTag = "frozen";

private static string? GetEnvironmentalVariableOrThrowIfNotTesting(string envVariableName, string? errorMessage = null)
{
// If it is a command from ef or a test, ignore the empty env variables
Expand Down
15 changes: 13 additions & 2 deletions src/Rpc/NodeGuardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class NodeGuardService : Nodeguard.NodeGuardService.NodeGuardServiceBase,
private readonly IScheduler _scheduler;
private readonly ILightningService _lightningService;
private readonly IFMUTXORepository _fmutxoRepository;
private readonly IUTXOTagRepository _utxoTagRepository;

public NodeGuardService(ILogger<NodeGuardService> logger,
ILiquidityRuleRepository liquidityRuleRepository,
Expand All @@ -86,7 +87,8 @@ public NodeGuardService(ILogger<NodeGuardService> logger,
IChannelRepository channelRepository,
ICoinSelectionService coinSelectionService,
ILightningService lightningService,
IFMUTXORepository fmutxoRepository
IFMUTXORepository fmutxoRepository,
IUTXOTagRepository utxoTagRepository
)
{
_logger = logger;
Expand All @@ -103,6 +105,7 @@ IFMUTXORepository fmutxoRepository
_coinSelectionService = coinSelectionService;
_lightningService = lightningService;
_fmutxoRepository = fmutxoRepository;
_utxoTagRepository = utxoTagRepository;
_scheduler = Task.Run(() => _schedulerFactory.GetScheduler()).Result;
}

Expand Down Expand Up @@ -892,13 +895,21 @@ public override async Task<GetAvailableUtxosResponse> GetAvailableUtxos(GetAvail
}

var lockedUtxos = await _fmutxoRepository.GetLockedUTXOs();
var frozenUtxos = await _utxoTagRepository.GetByKeyValue(Constants.IsFrozenTag, "true");

var ignoreOutpoints = new List<string>();
var listLocked = lockedUtxos.Select(utxo => $"{utxo.TxId}-{utxo.OutputIndex}").ToList();
var listFrozen = frozenUtxos.Select(utxo => utxo.Outpoint).ToList();
ignoreOutpoints.AddRange(listLocked);
ignoreOutpoints.AddRange(listFrozen);

var utxos = await _nbXplorerService.GetUTXOsByLimitAsync(
derivationStrategy,
coinSelectionStrategy,
request.Limit,
request.Amount,
request.ClosestTo,
lockedUtxos.Select(utxo => $"{utxo.TxId}-{utxo.OutputIndex}").ToList()
ignoreOutpoints
);

var confirmedUtxos = utxos.Confirmed.UTXOs.Select(utxo => new Utxo()
Expand Down
23 changes: 16 additions & 7 deletions src/Services/CoinSelectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ public class CoinSelectionService: ICoinSelectionService
private readonly INBXplorerService _nbXplorerService;
private readonly IChannelOperationRequestRepository _channelOperationRequestRepository;
private readonly IWalletWithdrawalRequestRepository _walletWithdrawalRequestRepository;
private readonly IUTXOTagRepository _utxoTagRepository;

public CoinSelectionService(
ILogger<BitcoinService> logger,
IMapper mapper,
IFMUTXORepository fmutxoRepository,
INBXplorerService nbXplorerService,
IChannelOperationRequestRepository channelOperationRequestRepository,
IWalletWithdrawalRequestRepository walletWithdrawalRequestRepository
IWalletWithdrawalRequestRepository walletWithdrawalRequestRepository,
IUTXOTagRepository utxoTagRepository
)
{
_logger = logger;
Expand All @@ -98,6 +100,7 @@ IWalletWithdrawalRequestRepository walletWithdrawalRequestRepository
_nbXplorerService = nbXplorerService;
_channelOperationRequestRepository = channelOperationRequestRepository;
_walletWithdrawalRequestRepository = walletWithdrawalRequestRepository;
_utxoTagRepository = utxoTagRepository;
}

private IBitcoinRequestRepository GetRepository(BitcoinRequestType requestType)
Expand Down Expand Up @@ -140,19 +143,25 @@ public async Task<List<UTXO>> GetLockedUTXOsForRequest(IBitcoinRequest bitcoinRe
return utxos.Confirmed.UTXOs.Where(utxo => lockedUTXOsList.Contains(utxo.Outpoint.ToString())).ToList();
}

private async Task<List<UTXO>> FilterUnlockedUTXOs(UTXOChanges? utxoChanges)
private async Task<List<UTXO>> FilterLockedFrozenUTXOs(UTXOChanges? utxoChanges)
{
var lockedUTXOs = await _fmutxoRepository.GetLockedUTXOs();
var frozenUTXOs = await _utxoTagRepository.GetByKeyValue(Constants.IsFrozenTag, "true");
var listLocked = lockedUTXOs.Select(utxo => $"{utxo.TxId}-{utxo.OutputIndex}").ToList();
var listFrozen = frozenUTXOs.Select(utxo => utxo.Outpoint).ToList();
var frozenAndLockedOutpoints = new List<string>();
frozenAndLockedOutpoints.AddRange(listLocked);
frozenAndLockedOutpoints.AddRange(listFrozen);

utxoChanges.RemoveDuplicateUTXOs();

var availableUTXOs = new List<UTXO>();
foreach (var utxo in utxoChanges.Confirmed.UTXOs)
{
var fmUtxo = _mapper.Map<UTXO, FMUTXO>(utxo);

if (lockedUTXOs.Contains(fmUtxo))
if (frozenAndLockedOutpoints.Contains(utxo.Outpoint.ToString()))
{
_logger.LogInformation("Removing UTXO: {Utxo} from UTXO set as it is locked", fmUtxo.ToString());
_logger.LogInformation("Removing UTXO: {Utxo} from UTXO set as it is locked", utxo.Outpoint.ToString());
}
else
{
Expand All @@ -166,7 +175,7 @@ private async Task<List<UTXO>> FilterUnlockedUTXOs(UTXOChanges? utxoChanges)
public async Task<List<UTXO>> GetAvailableUTXOsAsync(DerivationStrategyBase derivationStrategy)
{
var utxoChanges = await _nbXplorerService.GetUTXOsAsync(derivationStrategy);
return await FilterUnlockedUTXOs(utxoChanges);
return await FilterLockedFrozenUTXOs(utxoChanges);
}

public async Task<List<UTXO>> GetAvailableUTXOsAsync(DerivationStrategyBase derivationStrategy, CoinSelectionStrategy strategy, int limit, long amount, long closestTo)
Expand All @@ -180,7 +189,7 @@ public async Task<List<UTXO>> GetAvailableUTXOsAsync(DerivationStrategyBase deri
{
utxoChanges = await _nbXplorerService.GetUTXOsAsync(derivationStrategy);
}
return await FilterUnlockedUTXOs(utxoChanges);
return await FilterLockedFrozenUTXOs(utxoChanges);
}

/// <summary>
Expand Down

0 comments on commit b61e449

Please sign in to comment.