From 5fb960ef564b1cd41dcfa21730f708a215fea5fd Mon Sep 17 00:00:00 2001 From: Marcos <33052423+markettes@users.noreply.github.com> Date: Thu, 28 Sep 2023 09:32:24 +0200 Subject: [PATCH] New call to retrieve the channel through GRPC (#307) * 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 --- src/Proto/nodeguard.proto | 29 ++++++++++++++++- src/Rpc/NodeGuardService.cs | 63 +++++++++++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/Proto/nodeguard.proto b/src/Proto/nodeguard.proto index db0d3c52..83a14bf8 100644 --- a/src/Proto/nodeguard.proto +++ b/src/Proto/nodeguard.proto @@ -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 { @@ -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 { @@ -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; +} \ No newline at end of file diff --git a/src/Rpc/NodeGuardService.cs b/src/Rpc/NodeGuardService.cs index 92a0c0cb..001d5da0 100644 --- a/src/Rpc/NodeGuardService.cs +++ b/src/Rpc/NodeGuardService.cs @@ -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; @@ -44,6 +47,8 @@ Task GetNewWalletAddress(GetNewWalletAddressRequest Task GetAvailableUtxos(GetAvailableUtxosRequest request, ServerCallContext context); Task GetWithdrawalsRequestStatus(GetWithdrawalsRequestStatusRequest request, ServerCallContext context); + + Task GetChannel(GetChannelRequest request, ServerCallContext context); } /// @@ -610,12 +615,34 @@ public override async Task 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 @@ -859,4 +886,36 @@ public override async Task GetWithdrawalsRe } }; } + + public override async Task 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; + } } \ No newline at end of file