Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no clear error when channel opening failing when no room in utxos for change #367

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Helpers/CustomExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public PeerNotOnlineException(string? message = null): base(message) {}
public class RemoteCanceledFundingException : Exception
{
public RemoteCanceledFundingException(string? message = null): base(message) {}
}

public class NotEnoughRoomInUtxosForFeesException : Exception
{
public NotEnoughRoomInUtxosForFeesException(): base("Not enough room in the UTXOs to cover the fees") {}
}
17 changes: 16 additions & 1 deletion src/Helpers/ValidationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static void ValidatePubKey(ValidatorEventArgs obj, List<Node> nodes, stri
}
}

public static void validateDestNode(ValidatorEventArgs obj, Node? destNode)
public static void ValidateDestNode(ValidatorEventArgs obj, Node? destNode)
{
obj.Status = ValidationStatus.Success;
if (destNode == null)
Expand All @@ -138,6 +138,21 @@ public static void validateDestNode(ValidatorEventArgs obj, Node? destNode)
}
}

public static void ValidateAmount(ValidatorEventArgs obj, decimal amount, decimal selectedUtxosAmount, bool isChangeless)
{
obj.Status = ValidationStatus.Success;
if (!isChangeless && amount > selectedUtxosAmount)
{
obj.Status = ValidationStatus.Error;
obj.ErrorText = "The amount is greater than the selected UTXOs amount";
}
else if (!isChangeless && amount == selectedUtxosAmount)
{
obj.Status = ValidationStatus.Error;
obj.ErrorText = "The amount to send is equal to the selected UTXOs amount, there is no room in the UTXOs for change";
}
}

/// <summary>
/// Validated a xpub expect header and lenght size
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/ChannelOpenJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private async Task<bool> LogToChannelRequest(int openRequestId, Exception e, IJo
try
{
var request = await _channelOperationRequestRepository.GetById(openRequestId);
if (e is PeerNotOnlineException or RemoteCanceledFundingException)
if (e is PeerNotOnlineException or RemoteCanceledFundingException or NotEnoughRoomInUtxosForFeesException)
{
request.StatusLogs.Add(ChannelStatusLog.Error(e.Message));
}
Expand Down
37 changes: 25 additions & 12 deletions src/Pages/ChannelRequests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<DataGridColumn TItem="ChannelOperationRequest" Field="DestNode.Name" Caption="Remote Node" Sortable="false" Displayable="@IsPendingRequestsColumnVisible(PendingChannelsColumnName.RemoteNode)" PopupFieldColumnSize="ColumnSize.Is12" Editable="true"
Validator="ValidationRule.IsAlphanumeric">
<EditTemplate>
<Validation Validator="@((ValidatorEventArgs obj) => ValidationHelper.validateDestNode(obj, _selectedDestNode))" @ref="_destNodeValidation">
<Validation Validator="@((ValidatorEventArgs obj) => ValidationHelper.ValidateDestNode(obj, _selectedDestNode))" @ref="_destNodeValidation">
<Addons>
<Addon AddonType="AddonType.Body">
<TextEdit @bind-Text="@_destNodeName" Placeholder="Type the destination Node public key">
Expand Down Expand Up @@ -141,16 +141,22 @@
</DisplayTemplate>
<EditTemplate>
<Validation Validator="@ValidationHelper.ValidateChannelCapacity" @ref="_capacityValidation">
<NumericPicker TValue="decimal" @bind-Value="@_amount" Min="@_minimumChannelCapacity" Max="@_maxChannelCapacity" Decimals="8" Step="0.00001m" CurrencySymbol=" ₿" Disabled="_isChangeless"/>
<FieldHelp>
@{
decimal amountToShow = _amount < _maxChannelCapacity
? _amount
: _maxChannelCapacity;
decimal convertedAmount = Math.Round(PriceConversionService.SatToUsdConversion(new Money(amountToShow, MoneyUnit.BTC).Satoshi, _btcPrice), 2);
}
@($"Minimum {_minimumChannelCapacity:f8}. Current amount: {convertedAmount} USD")
<Validation Validator="@((ValidatorEventArgs obj) => ValidationHelper.ValidateAmount(obj, _amount, SelectedUTXOsValue(), _isChangeless))" @ref="_changelessValidation">
<NumericPicker TValue="decimal" @bind-Value="@_amount" Min="@_minimumChannelCapacity" Max="@_maxChannelCapacity" Decimals="8" Step="0.00001m" CurrencySymbol=" ₿" Disabled="_isChangeless">
<Feedback>
<ValidationError/>
</Feedback>
</NumericPicker>
<FieldHelp>
@{
decimal amountToShow = _amount < _maxChannelCapacity
? _amount
: _maxChannelCapacity;
decimal convertedAmount = Math.Round(PriceConversionService.SatToUsdConversion(new Money(amountToShow, MoneyUnit.BTC).Satoshi, _btcPrice), 2);
}
@($"Minimum {_minimumChannelCapacity:f8}. Current amount: {convertedAmount} USD")
</FieldHelp>
</Validation>
</Validation>
<div class="mb-3">
<Button Color="Color.Primary" Disabled="@(!_selectedWalletId.HasValue)" Clicked="@OpenCoinSelectionModal">Select Coins</Button> or use
Expand Down Expand Up @@ -398,7 +404,7 @@
TemplatePsbtString="@_templatePSBTString"
SignedPSBT="@_psbt"/>

<CancelOrRejectPopup

Check warning on line 407 in src/Pages/ChannelRequests.razor

View workflow job for this annotation

GitHub Actions / build-and-test

Component 'CancelOrRejectPopup' expects a value for the parameter 'Reason', but a value may not have been provided.
@ref=@_rejectCancelModalRef
Title='@(_selectedStatusActionString + " operation: " + _selectedRequest?.Id)'
Validator="@RejectReasonValidator"
Expand All @@ -423,7 +429,8 @@

<UTXOSelectorModal
@ref="_utxoSelectorModalRef"
OnClose="@OnCloseCoinSelectionModal"/>
OnClose="@OnCloseCoinSelectionModal"
IsWalletWithdrawalValidation="false"/>

@inject IChannelOperationRequestRepository ChannelOperationRequestRepository
@inject IChannelOperationRequestPSBTRepository ChannelOperationRequestPsbtRepository
Expand Down Expand Up @@ -477,6 +484,7 @@
private Validation? _sourceNodeValidation;
private Validation? _destNodeValidation;
private Validation? _capacityValidation;
private Validation? _changelessValidation;

private decimal _btcPrice;

Expand Down Expand Up @@ -653,7 +661,7 @@
{
if (LoggedUser == null) return;

List<Validation?> validators = new() { _destNodeValidation, _sourceNodeValidation, _walletValidation };
List<Validation?> validators = new() { _destNodeValidation, _sourceNodeValidation, _walletValidation, _changelessValidation };
if (_selectedUTXOs.Count == 0)
{
validators.Add(_capacityValidation);
Expand Down Expand Up @@ -999,6 +1007,11 @@
{
_amount = SelectedUTXOsValue();
}
// Most probably, if the user selected all UTXOs is because they want to do a changeless operation
if (_selectedUTXOs.Count == _utxoSelectorModalRef.UTXOList.Count)
{
_isChangeless = true;
}
StateHasChanged();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Pages/Withdrawals.razor
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@
TemplatePsbtString="@_templatePsbtString"
ApproveRequestDelegate="async () => await ApproveRequestDelegate()"/>

<CancelOrRejectPopup

Check warning on line 411 in src/Pages/Withdrawals.razor

View workflow job for this annotation

GitHub Actions / build-and-test

Component 'CancelOrRejectPopup' expects a value for the parameter 'Reason', but a value may not have been provided.
@ref="@_rejectCancelModalRef"
Title='@("Wallet withdrawal:" + _selectedRequest?.Id)'
Validator="@RejectReasonValidator"
Expand All @@ -426,7 +426,7 @@
<UTXOSelectorModal
@ref="_utxoSelectorModalRef"
OnClose="@OnCloseCoinSelectionModal"
IsWalletWithdrawalValidation="false"/>
IsWalletWithdrawalValidation="true"/>

@inject IToastService ToastService
@inject IWalletWithdrawalRequestRepository WalletWithdrawalRequestRepository
Expand Down
4 changes: 4 additions & 0 deletions src/Services/LightningService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ public async Task OpenChannel(ChannelOperationRequest channelOperationRequest)
o.Value != channelOperationRequest.SatsAmount) ??
channelfundingTx.Outputs.First();
changeOutput.Value = totalIn - totalOut - totalChangefulFees;
if (changeOutput.Value < 0)
{
throw new NotEnoughRoomInUtxosForFeesException();
}

//We merge changeFixedPSBT with the other PSBT with the change fixed
fundedPSBT = channelfundingTx.CreatePSBT(network).UpdateFrom(fundedPSBT);
Expand Down
6 changes: 0 additions & 6 deletions src/Shared/UTXOSelectorModal.razor
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@
</div>
</div>
<Row Class="mx-1">
<Feedback>@String.Join(",", GetUTXOsValues())</Feedback>

Check warning on line 103 in src/Shared/UTXOSelectorModal.razor

View workflow job for this annotation

GitHub Actions / build-and-test

Found markup element with unexpected name 'Feedback'. If this is intended to be a component, add a @using directive for its namespace.
</Row>
<Row Class="mx-1">
<Feedback class="text-danger">@(_validation.Messages != null ? String.Join(",", _validation.Messages) : "")</Feedback>

Check warning on line 106 in src/Shared/UTXOSelectorModal.razor

View workflow job for this annotation

GitHub Actions / build-and-test

Found markup element with unexpected name 'Feedback'. If this is intended to be a component, add a @using directive for its namespace.
</Row>
</ModalBody>
<ModalFooter>
Expand Down Expand Up @@ -226,17 +226,11 @@
{
minimumValue = new Money(Constants.MINIMUM_CHANNEL_CAPACITY_SATS).ToUnit(MoneyUnit.BTC);
}
var maxChannelRegtestValue = new Money(Constants.MAXIMUM_CHANNEL_CAPACITY_SATS_REGTEST).ToUnit(MoneyUnit.BTC);
if (selectedUTXOsValue < minimumValue)
{
e.ErrorText = $"The combined amount of the UTXOs selected must be greater than {minimumValue:f8} BTC";
e.Status = ValidationStatus.Error;
}
else if (selectedUTXOsValue > maxChannelRegtestValue && network == Network.RegTest && IsWalletWithdrawalValidation)
{
e.ErrorText = $"The combined amount of the UTXOs selected must be lower than {maxChannelRegtestValue:f8} BTC";
e.Status = ValidationStatus.Error;
}
RodriFS marked this conversation as resolved.
Show resolved Hide resolved
else
{
e.Status = ValidationStatus.Success;
Expand Down
Loading