| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Daemon.Services; |
| | | 5 | | |
| | | 6 | | using Contracts.Control; |
| | | 7 | | using Domain.Node.Options; |
| | | 8 | | using Domain.Persistence.Interfaces; |
| | | 9 | | using Domain.Protocol.Interfaces; |
| | | 10 | | using Infrastructure.Transport.Interfaces; |
| | | 11 | | using Interfaces; |
| | | 12 | | |
| | | 13 | | public sealed class NodeInfoQueryService : INodeInfoQueryService |
| | | 14 | | { |
| | | 15 | | private readonly NodeOptions _nodeOptions; |
| | | 16 | | private readonly ISecureKeyManager _secureKeyManager; |
| | | 17 | | private readonly IServiceProvider _services; |
| | | 18 | | private readonly ITcpService _tcpService; |
| | | 19 | | |
| | 0 | 20 | | public NodeInfoQueryService(IOptions<NodeOptions> nodeOptions, ISecureKeyManager secureKeyManager, |
| | 0 | 21 | | IServiceProvider services, ITcpService tcpService) |
| | | 22 | | { |
| | 0 | 23 | | _nodeOptions = nodeOptions.Value; |
| | 0 | 24 | | _secureKeyManager = secureKeyManager; |
| | 0 | 25 | | _services = services; |
| | 0 | 26 | | _tcpService = tcpService; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task<NodeInfoResponse> QueryAsync(CancellationToken ct) |
| | | 30 | | { |
| | | 31 | | // resolve per-call scope to access repositories |
| | 0 | 32 | | using var scope = _services.CreateScope(); |
| | 0 | 33 | | var uow = scope.ServiceProvider.GetService<IUnitOfWork>(); |
| | | 34 | | |
| | 0 | 35 | | var bestHashHex = string.Empty; |
| | 0 | 36 | | long bestHeight = 0; |
| | 0 | 37 | | DateTimeOffset? bestTime = null; |
| | | 38 | | |
| | 0 | 39 | | if (uow is not null) |
| | | 40 | | { |
| | | 41 | | try |
| | | 42 | | { |
| | 0 | 43 | | var state = await uow.BlockchainStateDbRepository.GetStateAsync(); |
| | 0 | 44 | | if (state is not null) |
| | | 45 | | { |
| | 0 | 46 | | bestHeight = state.LastProcessedHeight; |
| | 0 | 47 | | bestHashHex = state.LastProcessedBlockHash.ToString(); |
| | 0 | 48 | | bestTime = state.LastProcessedAt; |
| | | 49 | | } |
| | 0 | 50 | | } |
| | 0 | 51 | | catch |
| | | 52 | | { |
| | | 53 | | // ignore, return defaults |
| | 0 | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | var pubKeyString = _secureKeyManager.GetNodePubKey().ToString(); |
| | 0 | 58 | | var listeningToString = string.Join(',', _tcpService.ListeningTo.Select(e => e.ToString()).ToList()); |
| | | 59 | | |
| | 0 | 60 | | return new NodeInfoResponse |
| | 0 | 61 | | { |
| | 0 | 62 | | PubKey = pubKeyString, |
| | 0 | 63 | | ListeningTo = listeningToString, |
| | 0 | 64 | | Network = _nodeOptions.BitcoinNetwork, |
| | 0 | 65 | | BestBlockHash = bestHashHex, |
| | 0 | 66 | | BestBlockHeight = bestHeight, |
| | 0 | 67 | | BestBlockTime = bestTime, |
| | 0 | 68 | | Implementation = "NLightning", |
| | 0 | 69 | | Version = typeof(NodeInfoQueryService).Assembly.GetName().Version?.ToString() |
| | 0 | 70 | | }; |
| | 0 | 71 | | } |
| | | 72 | | } |