From 97aff1523665707d914b14c0ea8e716b703a5cb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20A=2EP?= <53834183+Jossec101@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:45:58 +0200 Subject: [PATCH] Add remote node pubkey field to LiquidityRules and protos (#369) * Added remote node pubkey field to LiquidityRules and protos * Update launch.json, tasks.json, and docker-compose.yml files * Fix null reference exception in LiquidityRule.cs and LiquidityRuleRepository.cs --- .vscode/launch.json | 55 +++++++++++-------- .vscode/tasks.json | 6 +- docker/docker-compose.yml | 2 +- src/Automapper/MapperProfile.cs | 7 ++- src/Data/Models/LiquidityRule.cs | 32 ++++++----- .../Repositories/LiquidityRuleRepository.cs | 17 +++--- src/Proto/nodeguard.proto | 3 + 7 files changed, 72 insertions(+), 50 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 195f2aff..58374344 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,12 +5,12 @@ // Use IntelliSense to find out which attributes exist for C# debugging // Use hover for the description of the existing attributes // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md - "name": ".NET Core Launch (web)", + "name": "Debug NG", "type": "coreclr", "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/src/bin/Debug/net7.0/NodeGuard.dll", + "program": "${workspaceFolder}/src/bin/Debug/net8.0/NodeGuard.dll", "args": [], "cwd": "${workspaceFolder}/src", "stopAtEntry": false, @@ -21,34 +21,45 @@ }, "env": { "ASPNETCORE_ENVIRONMENT": "Development", - "POSTGRES_CONNECTIONSTRING": "Host=127.0.0.1;Port=35433;Database=fundsmanager;Username=rw_dev;Password=rw_dev", + "POSTGRES_CONNECTIONSTRING": "Host=127.0.0.1;Port=5432;Database=nodeguard;Username=rw_dev;Password=rw_dev", "BITCOIN_NETWORK": "REGTEST", - "NBXPLORER_URI": "http://nbxplorer:32838", + "MAXIMUM_WITHDRAWAL_BTC_AMOUNT": "21000000", + "NBXPLORER_ENABLE_CUSTOM_BACKEND": "true", + "NBXPLORER_URI": "http://127.0.0.1:32838", "NBXPLORER_BTCRPCUSER": "polaruser", "NBXPLORER_BTCRPCPASSWORD": "polarpass", - "NBXPLORER_BTCRPCURL": "http://host.docker.internal:18443/", - "NBXPLORER_BTCNODEENDPOINT": "host.docker.internal:19444", - "ASPNETCORE_URLS": "http://localhost:5001", - "DEFAULT_DERIVATION_PATH": "m/48'/1'/1'" + "NBXPLORER_BTCRPCURL": "http://127.0.0.1:18443/", + "NBXPLORER_BTCNODEENDPOINT": "127.0.0.1:19444", + "PUSH_NOTIFICATIONS_ONESIGNAL_API_BASE_PATH": "https://onesignal.com/api/v1", + "PUSH_NOTIFICATIONS_ONESIGNAL_APP_ID": "changeme", + "PUSH_NOTIFICATIONS_ONESIGNAL_API_TOKEN": "changeme", + "PUSH_NOTIFICATIONS_ONESIGNAL_ENABLED": "false", + "DEFAULT_DERIVATION_PATH": "m/48'/1'", + "IS_DEV_ENVIRONMENT": "true", + "FUNDSMANAGER_ENDPOINT": "http://localhost:38080", + "Logging__LogLevel__Microsoft": "Warning", + "SWEEPNODEWALLETSJOB_CRON": "0 */1 * * * ?", + "ANCHOR_CLOSINGS_MINIMUM_SATS": "100000", + "ALICE_HOST": "localhost:10001", + "BOB_HOST": "localhost:10002", + "CAROL_HOST": "localhost:10003", + "ENABLE_HW_SUPPORT": "true", + "MINIMUM_WITHDRAWAL_BTC_AMOUNT": "0.001", + "MINIMUM_CHANNEL_CAPACITY_SATS": "20000", + "MEMPOOL_ENDPOINT": "https://mempool.space", + "AMBOSS_ENDPOINT": "https://amboss.space", + "TRANSACTION_CONFIRMATION_MINIMUM_BLOCKS": "6", + "MONITOR_WITHDRAWALS_CRON": "0 */1 * * * ?", + "MONITOR_CHANNELS_CRON": "0 0 */1 * * ?", + "COINGECKO_ENDPOINT": "https://pro-api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin", + "COINGECKO_KEY": "TBD", + "HTTP1_LISTEN_PORT": "38080", + "API_TOKEN_SALT": "H/fCx1+maAFMcdi6idIYEg==" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } }, - { - "name": "Launch against running docker-compose env (DEV)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/src/bin/Debug/net7.0/NodeGuard.dll", - "launchSettingsFilePath": "${workspaceFolder}/src/Properties/launchSettings.json", - "launchSettingsProfile": "NodeGuard local debug", - "cwd": "${workspaceFolder}/src", - "sourceFileMap": { - "/Views": "${workspaceFolder}/Views" - } - }, { "name": ".NET Core Attach", "type": "coreclr", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 072b8d8e..cd0485ad 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,7 +7,7 @@ "type": "process", "args": [ "build", - "${workspaceFolder}/src/FundsManager.csproj", + "${workspaceFolder}/src/NodeGuard.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], @@ -19,7 +19,7 @@ "type": "process", "args": [ "publish", - "${workspaceFolder}/src/FundsManager.csproj", + "${workspaceFolder}/src/NodeGuard.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], @@ -33,7 +33,7 @@ "watch", "run", "--project", - "${workspaceFolder}/src/FundsManager.csproj" + "${workspaceFolder}/src/NodeGuard.csproj" ], "problemMatcher": "$msCompile" } diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 75d9045c..02b774ee 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -13,7 +13,7 @@ services: volumes: - nodeguard_postgres_data:/var/lib/postgresql/data ports: - - 5432:5432 + - 25432:5432 nbxplorer: restart: unless-stopped diff --git a/src/Automapper/MapperProfile.cs b/src/Automapper/MapperProfile.cs index 488083bd..a0b79e1d 100644 --- a/src/Automapper/MapperProfile.cs +++ b/src/Automapper/MapperProfile.cs @@ -65,7 +65,7 @@ public MapperProfile() CreateMap() .ForMember(x => x.TxId, opt => opt.MapFrom(x => x.Outpoint.Hash.ToString())) .ForMember(x => x.OutputIndex, opt => opt.MapFrom(x => x.Outpoint.N)) - .ForMember(x => x.SatsAmount, opt => opt.MapFrom(x => ((Money) x.Value).Satoshi)); + .ForMember(x => x.SatsAmount, opt => opt.MapFrom(x => ((Money)x.Value).Satoshi)); CreateMap() .ForMember(x => x.MinimumLocalBalance, opt => opt.MapFrom(x => x.MinimumLocalBalance ?? 0)) @@ -76,7 +76,8 @@ public MapperProfile() .ForMember(x => x.SwapWalletId, opt => opt.MapFrom(x => x.SwapWalletId)) .ForMember(x => x.ReverseSwapWalletId, opt => opt.MapFrom(x => x.ReverseSwapWalletId ?? 0)) .ForMember(x => x.RebalanceTarget, opt => opt.MapFrom(x => x.RebalanceTarget ?? 0)) - .ForMember(x => x.NodePubkey, opt => opt.MapFrom(x => x.Node.PubKey)); + .ForMember(x => x.NodePubkey, opt => opt.MapFrom(x => x.Node.PubKey)) + .ForMember(x => x.RemoteNodePubkey, opt => opt.MapFrom(x => x.RemoteNodePubkey ?? string.Empty)); CreateMap() .ForMember(x => x.SwapWallet, opt => opt.Ignore()) @@ -93,4 +94,4 @@ public MapperProfile() } } -} \ No newline at end of file +} diff --git a/src/Data/Models/LiquidityRule.cs b/src/Data/Models/LiquidityRule.cs index 2ca74b1e..667e2072 100644 --- a/src/Data/Models/LiquidityRule.cs +++ b/src/Data/Models/LiquidityRule.cs @@ -5,44 +5,48 @@ namespace NodeGuard.Data.Models; /// /// A rule for setting liquidity automation on NodeGuard /// -public class LiquidityRule: Entity +public class LiquidityRule : Entity { - + public decimal? MinimumLocalBalance { get; set; } - + public decimal? MinimumRemoteBalance { get; set; } - + /// /// Target between 0 and 1 that we would like for the channel to be balanced after a rebalancing operation is complete /// public decimal? RebalanceTarget { get; set; } - + /// /// Let's you know if the rule has a wallet or an address as a target for the rebalancing operation /// public bool IsReverseSwapWalletRule { get; set; } - + /// /// In case that is a rule that sends the funds to an address instead of a wallet this is the address /// public string? ReverseSwapAddress { get; set; } - + #region Relationships - + public int ChannelId { get; set; } public Channel Channel { get; set; } - + public int SwapWalletId { get; set; } public Wallet SwapWallet { get; set; } - + public int? ReverseSwapWalletId { get; set; } public Wallet? ReverseSwapWallet { get; set; } - + public int NodeId { get; set; } public Node Node { get; set; } - - //TODO Discuss about a liquidity rule at node level - #endregion + /// + /// The pubkey of the node that is the remote counterparty of the channel + /// + [NotMapped] + public string? RemoteNodePubkey => Channel?.SourceNode?.PubKey != Node.PubKey ? Channel?.SourceNode?.PubKey : Channel?.DestinationNode?.PubKey; + #endregion + } diff --git a/src/Data/Repositories/LiquidityRuleRepository.cs b/src/Data/Repositories/LiquidityRuleRepository.cs index e6da0374..8c0b34ad 100644 --- a/src/Data/Repositories/LiquidityRuleRepository.cs +++ b/src/Data/Repositories/LiquidityRuleRepository.cs @@ -103,18 +103,21 @@ public async Task> GetByNodePubKey(string nodePubKey) { if (string.IsNullOrWhiteSpace(nodePubKey)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(nodePubKey)); - + using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync(); var result = applicationDbContext.LiquidityRules - .Include(x=> x.Node) - .Include(x=> x.SwapWallet) - .ThenInclude(x=> x.InternalWallet) + .Include(x => x.Node) + .Include(x => x.SwapWallet) + .ThenInclude(x => x.InternalWallet) .Include(x => x.ReverseSwapWallet) .ThenInclude(x => x.InternalWallet) - .Include(x=> x.Channel) - .Where(x=> x.Node.PubKey == nodePubKey && x.Channel.IsAutomatedLiquidityEnabled && x.Channel.Status != Channel.ChannelStatus.Closed).ToList(); - + .Include(x => x.Channel) + .ThenInclude(ch => ch.SourceNode) + .Include(x => x.Channel) + .ThenInclude(ch => ch.DestinationNode) + .Where(x => x.Node.PubKey == nodePubKey && x.Channel.IsAutomatedLiquidityEnabled && x.Channel.Status != Channel.ChannelStatus.Closed).ToList(); + return result; } } \ No newline at end of file diff --git a/src/Proto/nodeguard.proto b/src/Proto/nodeguard.proto index ec21d7a4..0c8b8137 100644 --- a/src/Proto/nodeguard.proto +++ b/src/Proto/nodeguard.proto @@ -80,6 +80,7 @@ message GetLiquidityRulesResponse { message LiquidityRule { uint64 channel_id = 1; + // Public key of the node that is the managed node of the channel string node_pubkey = 2; int32 swap_wallet_id = 3; optional int32 reverse_swap_wallet_id = 4; @@ -88,6 +89,8 @@ message LiquidityRule { float rebalance_target = 7; optional string reverse_swap_address = 8; bool is_reverse_swap_wallet_rule = 9; + // Public key of the node that is the counterparty of the channel + string remote_node_pubkey = 10; } message GetNewWalletAddressRequest {