From 3b375929c15b544fa27e8ca52f4d9cecc1d0e362 Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Wed, 19 May 2021 06:07:28 +0200 Subject: [PATCH] Support wider server sync info in greenfield server info (#2511) fixes #2498 --- .../Contracts/ISyncSummaryProvider.cs | 9 ++++ BTCPayServer.Client/Models/ServerInfoData.cs | 11 ++-- .../GreenField/ServerInfoController.cs | 52 ++++++------------- .../Services/EthereumSyncSummaryProvider.cs | 24 ++++++++- .../Monero/Services/MoneroRPCProvider.cs | 4 +- .../Services/MoneroSyncSummaryProvider.cs | 22 ++++++++ .../Services/NBXSyncSummaryProvider.cs | 28 ++++++++++ 7 files changed, 106 insertions(+), 44 deletions(-) diff --git a/BTCPayServer.Abstractions/Contracts/ISyncSummaryProvider.cs b/BTCPayServer.Abstractions/Contracts/ISyncSummaryProvider.cs index 5f1171f32..8af42fdf8 100644 --- a/BTCPayServer.Abstractions/Contracts/ISyncSummaryProvider.cs +++ b/BTCPayServer.Abstractions/Contracts/ISyncSummaryProvider.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + namespace BTCPayServer.Abstractions.Contracts { public interface ISyncSummaryProvider @@ -5,5 +7,12 @@ namespace BTCPayServer.Abstractions.Contracts bool AllAvailable(); string Partial { get; } + IEnumerable GetStatuses(); + } + + public interface ISyncStatus + { + public string CryptoCode { get; set; } + public bool Available { get; } } } diff --git a/BTCPayServer.Client/Models/ServerInfoData.cs b/BTCPayServer.Client/Models/ServerInfoData.cs index d028ed618..1e33f2d99 100644 --- a/BTCPayServer.Client/Models/ServerInfoData.cs +++ b/BTCPayServer.Client/Models/ServerInfoData.cs @@ -27,12 +27,17 @@ namespace BTCPayServer.Client.Models /// /// detailed sync information per chain /// - public IEnumerable SyncStatus { get; set; } + public IEnumerable SyncStatus { get; set; } } - - public class ServerInfoSyncStatusData + + public class SyncStatus { public string CryptoCode { get; set; } + public virtual bool Available { get; set; } + } + + public class ServerInfoSyncStatusData: SyncStatus + { public int ChainHeight { get; set; } public int? SyncHeight { get; set; } public ServerInfoNodeData NodeInformation { get; set; } diff --git a/BTCPayServer/Controllers/GreenField/ServerInfoController.cs b/BTCPayServer/Controllers/GreenField/ServerInfoController.cs index 363f1a961..faacc4d8c 100644 --- a/BTCPayServer/Controllers/GreenField/ServerInfoController.cs +++ b/BTCPayServer/Controllers/GreenField/ServerInfoController.cs @@ -1,18 +1,14 @@ +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Abstractions.Contracts; using BTCPayServer.Client.Models; -using BTCPayServer.Data; -using BTCPayServer.HostedServices; -using BTCPayServer.Security; using BTCPayServer.Services; using BTCPayServer.Services.Invoices; -using BTCPayServer.Services.Stores; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors; -using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; -using NBXplorer.Models; namespace BTCPayServer.Controllers.GreenField { @@ -21,59 +17,41 @@ namespace BTCPayServer.Controllers.GreenField public class GreenFieldServerInfoController : Controller { private readonly BTCPayServerEnvironment _env; - private readonly NBXplorerDashboard _dashBoard; - private readonly StoreRepository _storeRepository; - private readonly UserManager _userManager; - private readonly BTCPayNetworkProvider _networkProvider; private readonly PaymentMethodHandlerDictionary _paymentMethodHandlerDictionary; + private readonly IEnumerable _summaryProviders; public GreenFieldServerInfoController( BTCPayServerEnvironment env, - NBXplorerDashboard dashBoard, - StoreRepository storeRepository, - UserManager userManager, - BTCPayNetworkProvider networkProvider, - PaymentMethodHandlerDictionary paymentMethodHandlerDictionary) + PaymentMethodHandlerDictionary paymentMethodHandlerDictionary, + IEnumerablesummaryProviders) { _env = env; - _dashBoard = dashBoard; - _storeRepository = storeRepository; - _userManager = userManager; - _networkProvider = networkProvider; _paymentMethodHandlerDictionary = paymentMethodHandlerDictionary; + _summaryProviders = summaryProviders; } [Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)] [HttpGet("~/api/v1/server/info")] public async Task ServerInfo() { - var stores = await _storeRepository.GetStoresByUserId(_userManager.GetUserId(User)); var supportedPaymentMethods = _paymentMethodHandlerDictionary .SelectMany(handler => handler.GetSupportedPaymentMethods().Select(id => id.ToString())) .Distinct(); - var syncStatus = _dashBoard.GetAll() - .Where(summary => summary.Network.ShowSyncSummary) - .Select(summary => new ServerInfoSyncStatusData - { - CryptoCode = summary.Network.CryptoCode, - NodeInformation = summary.Status.BitcoinStatus is BitcoinStatus s ? new ServerInfoNodeData() - { - Headers = s.Headers, - Blocks = s.Blocks, - VerificationProgress = s.VerificationProgress - } : null, - ChainHeight = summary.Status.ChainHeight, - SyncHeight = summary.Status.SyncHeight - }); - ServerInfoData model = new ServerInfoData + + ServerInfoData model = new ServerInfoData2 { - FullySynched = _dashBoard.IsFullySynched(), - SyncStatus = syncStatus, + FullySynched = _summaryProviders.All(provider => provider.AllAvailable()), + SyncStatus = _summaryProviders.SelectMany(provider => provider.GetStatuses()), Onion = _env.OnionUrl, Version = _env.Version, SupportedPaymentMethods = supportedPaymentMethods }; return Ok(model); } + + public class ServerInfoData2 : ServerInfoData + { + public new IEnumerable SyncStatus { get; set; } + } } } diff --git a/BTCPayServer/Services/Altcoins/Ethereum/Services/EthereumSyncSummaryProvider.cs b/BTCPayServer/Services/Altcoins/Ethereum/Services/EthereumSyncSummaryProvider.cs index 791269c74..f706610af 100644 --- a/BTCPayServer/Services/Altcoins/Ethereum/Services/EthereumSyncSummaryProvider.cs +++ b/BTCPayServer/Services/Altcoins/Ethereum/Services/EthereumSyncSummaryProvider.cs @@ -1,4 +1,6 @@ #if ALTCOINS +using System.Collections.Generic; +using System.Linq; using BTCPayServer.Abstractions.Contracts; namespace BTCPayServer.Services.Altcoins.Ethereum.Services @@ -6,10 +8,12 @@ namespace BTCPayServer.Services.Altcoins.Ethereum.Services public class EthereumSyncSummaryProvider : ISyncSummaryProvider { private readonly EthereumService _ethereumService; + private readonly BTCPayNetworkProvider _btcPayNetworkProvider; - public EthereumSyncSummaryProvider(EthereumService ethereumService) + public EthereumSyncSummaryProvider(EthereumService ethereumService, BTCPayNetworkProvider btcPayNetworkProvider) { _ethereumService = ethereumService; + _btcPayNetworkProvider = btcPayNetworkProvider; } public bool AllAvailable() @@ -18,6 +22,24 @@ namespace BTCPayServer.Services.Altcoins.Ethereum.Services } public string Partial { get; } = "Ethereum/ETHSyncSummary"; + public IEnumerable GetStatuses() + { + return _btcPayNetworkProvider + .GetAll() + .OfType() + .Where(network => !(network is ERC20BTCPayNetwork)) + .Select(network => network.CryptoCode).Select(network => new SyncStatus() + { + CryptoCode = network, + Available = _ethereumService.IsAvailable(network, out _) + }); + } + + public class SyncStatus : ISyncStatus + { + public string CryptoCode { get; set; } + public bool Available { get; set; } + } } } #endif diff --git a/BTCPayServer/Services/Altcoins/Monero/Services/MoneroRPCProvider.cs b/BTCPayServer/Services/Altcoins/Monero/Services/MoneroRPCProvider.cs index 03cd629eb..50d7130af 100644 --- a/BTCPayServer/Services/Altcoins/Monero/Services/MoneroRPCProvider.cs +++ b/BTCPayServer/Services/Altcoins/Monero/Services/MoneroRPCProvider.cs @@ -15,7 +15,6 @@ namespace BTCPayServer.Services.Altcoins.Monero.Services { private readonly MoneroLikeConfiguration _moneroLikeConfiguration; private readonly EventAggregator _eventAggregator; - private readonly BTCPayServerEnvironment _btcPayServerEnvironment; public ImmutableDictionary DaemonRpcClients; public ImmutableDictionary WalletRpcClients; @@ -24,11 +23,10 @@ namespace BTCPayServer.Services.Altcoins.Monero.Services public ConcurrentDictionary Summaries => _summaries; - public MoneroRPCProvider(MoneroLikeConfiguration moneroLikeConfiguration, EventAggregator eventAggregator, IHttpClientFactory httpClientFactory, BTCPayServerEnvironment btcPayServerEnvironment) + public MoneroRPCProvider(MoneroLikeConfiguration moneroLikeConfiguration, EventAggregator eventAggregator, IHttpClientFactory httpClientFactory) { _moneroLikeConfiguration = moneroLikeConfiguration; _eventAggregator = eventAggregator; - _btcPayServerEnvironment = btcPayServerEnvironment; DaemonRpcClients = _moneroLikeConfiguration.MoneroLikeConfigurationItems.ToImmutableDictionary(pair => pair.Key, pair => new JsonRpcClient(pair.Value.DaemonRpcUri, "", "", httpClientFactory.CreateClient())); diff --git a/BTCPayServer/Services/Altcoins/Monero/Services/MoneroSyncSummaryProvider.cs b/BTCPayServer/Services/Altcoins/Monero/Services/MoneroSyncSummaryProvider.cs index 910ff0ea0..0af5ba3dc 100644 --- a/BTCPayServer/Services/Altcoins/Monero/Services/MoneroSyncSummaryProvider.cs +++ b/BTCPayServer/Services/Altcoins/Monero/Services/MoneroSyncSummaryProvider.cs @@ -1,6 +1,8 @@ #if ALTCOINS +using System.Collections.Generic; using System.Linq; using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Client.Models; namespace BTCPayServer.Services.Altcoins.Monero.Services { @@ -19,6 +21,26 @@ namespace BTCPayServer.Services.Altcoins.Monero.Services } public string Partial { get; } = "Monero/MoneroSyncSummary"; + public IEnumerable GetStatuses() + { + return _moneroRpcProvider.Summaries.Select(pair => new MoneroSyncStatus() + { + Summary = pair.Value, CryptoCode = pair.Key + }); + } + } + + public class MoneroSyncStatus: SyncStatus, ISyncStatus + { + public override bool Available + { + get + { + return Summary?.WalletAvailable ?? false; + } + } + + public MoneroRPCProvider.MoneroLikeSummary Summary { get; set; } } } #endif diff --git a/BTCPayServer/Services/NBXSyncSummaryProvider.cs b/BTCPayServer/Services/NBXSyncSummaryProvider.cs index 56499b585..3925d2ebb 100644 --- a/BTCPayServer/Services/NBXSyncSummaryProvider.cs +++ b/BTCPayServer/Services/NBXSyncSummaryProvider.cs @@ -1,5 +1,9 @@ +using System.Collections.Generic; +using System.Linq; using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Client.Models; using BTCPayServer.HostedServices; +using NBXplorer.Models; namespace BTCPayServer.Services { @@ -18,5 +22,29 @@ namespace BTCPayServer.Services } public string Partial { get; } = "Bitcoin/NBXSyncSummary"; + public IEnumerable GetStatuses() + { + return _nbXplorerDashboard.GetAll() + .Where(summary => summary.Network.ShowSyncSummary) + .Select(summary => new ServerInfoSyncStatusData2 + { + CryptoCode = summary.Network.CryptoCode, + NodeInformation = summary.Status.BitcoinStatus is BitcoinStatus s ? new ServerInfoNodeData() + { + Headers = s.Headers, + Blocks = s.Blocks, + VerificationProgress = s.VerificationProgress + } : null, + ChainHeight = summary.Status.ChainHeight, + SyncHeight = summary.Status.SyncHeight + }); + } + + public class ServerInfoSyncStatusData2: ServerInfoSyncStatusData, ISyncStatus + { + + } } + + }