Skip to content

Commit

Permalink
New call to retrieve the channel through GRPC (#307)
Browse files Browse the repository at this point in the history
* feat(nodeguard.proto): add GetChannel RPC method to retrieve channel information by ID
feat(NodeGuardService.cs): implement GetChannel method to retrieve channel information by ID

* fix(nodeguard.proto): fix typo in CHANNEL_STATUS enum value from CLOSE to CLOSED for better consistency and clarity
  • Loading branch information
markettes authored Sep 28, 2023
1 parent e9b5f4e commit 5fb960e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/Proto/nodeguard.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ service NodeGuardService {
Gets the status for the provided withdrawals request ids
*/
rpc GetWithdrawalsRequestStatus(GetWithdrawalsRequestStatusRequest) returns (GetWithdrawalsRequestStatusResponse);

/*
Gets a channel by id
*/
rpc GetChannel(GetChannelRequest) returns (GetChannelResponse);
}

message GetLiquidityRulesRequest {
Expand Down Expand Up @@ -235,7 +240,7 @@ enum CHANNEL_OPERATION_STATUS {
ONCHAIN_CONFIRMATION_PENDING = 5;
ONCHAIN_CONFIRMED = 6;
FAILED = 7;
FINALIZINGPSBT = 8;
FINALIZING_PSBT = 8;
}

enum CHANNEL_OPERATION_TYPE {
Expand Down Expand Up @@ -330,3 +335,25 @@ message WithdrawalRequest {
message GetWithdrawalsRequestStatusResponse {
repeated WithdrawalRequest withdrawal_requests = 1;
}

message GetChannelRequest {
// Channel ID from NGs database
int32 channel_id = 1;
}

enum CHANNEL_STATUS {
OPEN = 0;
CLOSED = 1;
}

message GetChannelResponse {
string funding_tx = 1;
uint32 output_index = 2;
uint64 chan_id = 3;
int64 sats_amount = 4;
optional string btc_close_address = 5;
CHANNEL_STATUS status = 6;
bool created_by_nodeguard = 7;
bool is_automated_liquidity_enabled = 8;
bool is_private = 9;
}
63 changes: 61 additions & 2 deletions src/Rpc/NodeGuardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
using NodeGuard.Jobs;
using NodeGuard.Services;
using Grpc.Core;
using Microsoft.EntityFrameworkCore.Infrastructure;
using NBitcoin;
using NBitcoin.RPC;
using NBXplorer.DerivationStrategy;
using NBXplorer.Models;
using Nodeguard;
using Quartz;
using LiquidityRule = NodeGuard.Data.Models.LiquidityRule;
using Node = Nodeguard.Node;
using Wallet = NodeGuard.Data.Models.Wallet;

namespace NodeGuard.Rpc;
Expand Down Expand Up @@ -44,6 +47,8 @@ Task<GetNewWalletAddressResponse> GetNewWalletAddress(GetNewWalletAddressRequest
Task<GetAvailableUtxosResponse> GetAvailableUtxos(GetAvailableUtxosRequest request, ServerCallContext context);

Task<GetWithdrawalsRequestStatusResponse> GetWithdrawalsRequestStatus(GetWithdrawalsRequestStatusRequest request, ServerCallContext context);

Task<GetChannelResponse> GetChannel(GetChannelRequest request, ServerCallContext context);
}

/// <summary>
Expand Down Expand Up @@ -610,12 +615,34 @@ public override async Task<GetChannelOperationRequestResponse> GetChannelOperati
throw new RpcException(new Status(StatusCode.NotFound, "Channel operation request not found"));
}

var status = channelOperationRequest.Status switch
{
ChannelOperationRequestStatus.Approved => CHANNEL_OPERATION_STATUS.Approved,
ChannelOperationRequestStatus.Cancelled => CHANNEL_OPERATION_STATUS.Cancelled,
ChannelOperationRequestStatus.Rejected => CHANNEL_OPERATION_STATUS.Rejected,
ChannelOperationRequestStatus.Pending => CHANNEL_OPERATION_STATUS.Pending,
ChannelOperationRequestStatus.PSBTSignaturesPending => CHANNEL_OPERATION_STATUS.PsbtSignaturesPending,
ChannelOperationRequestStatus.OnChainConfirmationPending => CHANNEL_OPERATION_STATUS
.OnchainConfirmationPending,
ChannelOperationRequestStatus.OnChainConfirmed => CHANNEL_OPERATION_STATUS.OnchainConfirmed,
ChannelOperationRequestStatus.Failed => CHANNEL_OPERATION_STATUS.Failed,
ChannelOperationRequestStatus.FinalizingPSBT => CHANNEL_OPERATION_STATUS.FinalizingPsbt,
_ => throw new ArgumentOutOfRangeException(nameof(channelOperationRequest.Status), channelOperationRequest.Status, "Unknown status")
};

var type = channelOperationRequest.RequestType switch
{
OperationRequestType.Open => CHANNEL_OPERATION_TYPE.OpenChannel,
OperationRequestType.Close => CHANNEL_OPERATION_TYPE.CloseChannel,
_ => throw new ArgumentOutOfRangeException(nameof(channelOperationRequest.RequestType), channelOperationRequest.RequestType, "Unknown type")
};

var result = new GetChannelOperationRequestResponse
{
SatsAmount = channelOperationRequest.SatsAmount,
Description = channelOperationRequest.Description,
Status = (CHANNEL_OPERATION_STATUS)((int)channelOperationRequest.Status - 1),
Type = (CHANNEL_OPERATION_TYPE)((int)channelOperationRequest.RequestType - 1),
Status = status,
Type = type,
SourceNodeId = channelOperationRequest.SourceNodeId,
Private = channelOperationRequest.IsChannelPrivate,
JobId = channelOperationRequest.JobId
Expand Down Expand Up @@ -859,4 +886,36 @@ public override async Task<GetWithdrawalsRequestStatusResponse> GetWithdrawalsRe
}
};
}

public override async Task<GetChannelResponse> GetChannel(GetChannelRequest request, ServerCallContext context)
{
var channel = await _channelRepository.GetById(request.ChannelId);
if (channel == null)
{
throw new RpcException(new Status(StatusCode.NotFound, "Channel not found"));
}

var status = channel.Status switch
{
Channel.ChannelStatus.Open => CHANNEL_STATUS.Open,
Channel.ChannelStatus.Closed => CHANNEL_STATUS.Closed,
_ => throw new ArgumentOutOfRangeException(nameof(channel.Status), channel.Status, "Unknown status")
};

var result = new GetChannelResponse()
{
FundingTx = channel.FundingTx,
OutputIndex = channel.FundingTxOutputIndex,
ChanId = channel.ChanId,
SatsAmount = channel.SatsAmount,
Status = status,
CreatedByNodeguard = channel.CreatedByNodeGuard,
IsAutomatedLiquidityEnabled = channel.IsAutomatedLiquidityEnabled,
IsPrivate = channel.IsPrivate,
};

result.BtcCloseAddress = channel.BtcCloseAddress != null ? channel.BtcCloseAddress : String.Empty;

return result;
}
}

0 comments on commit 5fb960e

Please sign in to comment.