| | | 1 | | using MessagePack; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Daemon.Ipc.Handlers; |
| | | 5 | | |
| | | 6 | | using Daemon.Interfaces; |
| | | 7 | | using Domain.Client.Constants; |
| | | 8 | | using Domain.Client.Enums; |
| | | 9 | | using Domain.Crypto.ValueObjects; |
| | | 10 | | using Interfaces; |
| | | 11 | | using Services.Ipc.Factories; |
| | | 12 | | using Transport.Ipc; |
| | | 13 | | using Transport.Ipc.Responses; |
| | | 14 | | |
| | | 15 | | internal sealed class NodeInfoIpcHandler : IIpcCommandHandler |
| | | 16 | | { |
| | | 17 | | private readonly INodeInfoQueryService _query; |
| | | 18 | | private readonly ILogger<NodeInfoIpcHandler> _logger; |
| | | 19 | | |
| | 0 | 20 | | public ClientCommand Command => ClientCommand.NodeInfo; |
| | | 21 | | |
| | 0 | 22 | | public NodeInfoIpcHandler(INodeInfoQueryService query, ILogger<NodeInfoIpcHandler> logger) |
| | | 23 | | { |
| | 0 | 24 | | _query = query; |
| | 0 | 25 | | _logger = logger; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<IpcEnvelope> HandleAsync(IpcEnvelope envelope, CancellationToken ct) |
| | | 29 | | { |
| | | 30 | | try |
| | | 31 | | { |
| | 0 | 32 | | var resp = await _query.QueryAsync(ct); |
| | 0 | 33 | | var ipcResp = new NodeInfoIpcResponse |
| | 0 | 34 | | { |
| | 0 | 35 | | PubKey = new CompactPubKey(Convert.FromHexString(resp.PubKey)), |
| | 0 | 36 | | ListeningTo = resp.ListeningTo.Split(',').ToList(), |
| | 0 | 37 | | Network = resp.Network, |
| | 0 | 38 | | BestBlockHash = new Hash(Convert.FromHexString(resp.BestBlockHash)), |
| | 0 | 39 | | BestBlockHeight = resp.BestBlockHeight, |
| | 0 | 40 | | BestBlockTime = resp.BestBlockTime, |
| | 0 | 41 | | Implementation = resp.Implementation, |
| | 0 | 42 | | Version = resp.Version |
| | 0 | 43 | | }; |
| | 0 | 44 | | var payload = MessagePackSerializer.Serialize(ipcResp, cancellationToken: ct); |
| | 0 | 45 | | return new IpcEnvelope |
| | 0 | 46 | | { |
| | 0 | 47 | | Version = envelope.Version, |
| | 0 | 48 | | Command = envelope.Command, |
| | 0 | 49 | | CorrelationId = envelope.CorrelationId, |
| | 0 | 50 | | Kind = IpcEnvelopeKind.Response, |
| | 0 | 51 | | Payload = payload |
| | 0 | 52 | | }; |
| | | 53 | | } |
| | 0 | 54 | | catch (Exception e) |
| | | 55 | | { |
| | 0 | 56 | | _logger.LogError(e, "Error handling node info"); |
| | 0 | 57 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ServerError, e.Message); |
| | | 58 | | } |
| | 0 | 59 | | } |
| | | 60 | | } |