public void SetPeerContext(IPeerContext peerContext) { _networkPeerContext = peerContext as NetworkPeerContext ?? throw new ArgumentException("Expected NetworkPeerContext", nameof(peerContext));; _deserializationContext.SetInitiator(_networkPeerContext.Direction == PeerConnectionDirection.Outbound); LightningEndpoint?lightningEndpoint = null; if (peerContext.Direction == PeerConnectionDirection.Outbound) { OutgoingConnectionEndPoint endpoint = peerContext.Features.Get <OutgoingConnectionEndPoint>(); if (endpoint == null || !endpoint.Items.TryGetValue(nameof(LightningEndpoint), out object?res)) { _logger.LogError("Remote connection was not found "); throw new ApplicationException("Initiator connection must have a public key of the remote node"); } if (res == null) { _logger.LogError("Remote connection type is invalid"); throw new ApplicationException("Remote connection type is invalid"); } lightningEndpoint = (LightningEndpoint)res; } _handshakeProtocol = new HandshakeWithNoiseProtocol(_nodeContext, lightningEndpoint?.NodePubKey, _handshakeProcessor); _networkPeerContext.SetHandshakeProtocol(_handshakeProtocol); }
public async ValueTask AttemptConnectionAsync(OutgoingConnectionEndPoint remoteEndPoint, CancellationToken cancellation) { using IDisposable logScope = _logger.BeginScope("Outbound connection to {RemoteEndPoint}", remoteEndPoint); _eventBus.Publish(new PeerConnectionAttempt(remoteEndPoint.EndPoint.AsIPEndPoint())); bool connectionEstablished = false; try { await using ConnectionContext connection = await _client.ConnectAsync(remoteEndPoint.EndPoint).ConfigureAwait(false); // we store the RemoteEndPoint class as a feature of the connection so we can then copy it into the PeerContext in the ClientConnectionHandler.OnConnectedAsync connection.Features.Set(remoteEndPoint); connectionEstablished = true; await _clientConnectionHandler.OnConnectedAsync(connection).ConfigureAwait(false); } catch (OperationCanceledException) { _logger.LogDebug("Connection to {RemoteEndPoint} canceled", remoteEndPoint.EndPoint); _eventBus.Publish(new PeerConnectionAttemptFailed(remoteEndPoint.EndPoint.AsIPEndPoint(), "Operation canceled.")); } catch (Exception ex) { if (!connectionEstablished) { _eventBus.Publish(new PeerConnectionAttemptFailed(remoteEndPoint.EndPoint.AsIPEndPoint(), ex.Message)); } else { _logger.LogError(ex, "AttemptConnectionAsync failed"); //throw; } } }
public override IPeerContext CreateOutgoingPeerContext( string peerId, EndPoint localEndPoint, OutgoingConnectionEndPoint outgoingConnectionEndPoint, INetworkMessageWriter messageWriter) { var peerContext = (NetworkPeerContext)base.CreateOutgoingPeerContext(peerId, localEndPoint, outgoingConnectionEndPoint, messageWriter); // At this point we can enrich the context from the DI return(peerContext); }
/// <summary> /// Tries the add end point. /// /// </summary> /// <param name="endPoint">The end point.</param> /// <returns><see langword="true"/> if the endpoint has been added, <see langword="false"/> if the endpoint was already listed.</returns> public bool TryAddLightningEndPoint(LightningEndpoint endPoint) { endPoint.EndPoint = endPoint.EndPoint.AsIPEndPoint().EnsureIPv6(); if (connectionsToAttempt.Exists(ip => ip.Equals(endPoint))) { logger.LogDebug("EndPoint {RemoteEndPoint} already in the list of connections attempt.", endPoint); return(false); } else { var outgoingConnectionEndPoint = new OutgoingConnectionEndPoint(endPoint.EndPoint.AsIPEndPoint()); outgoingConnectionEndPoint.Items[nameof(LightningEndpoint)] = endPoint; connectionsToAttempt.Add(outgoingConnectionEndPoint); logger.LogDebug("EndPoint {RemoteEndPoint} added to the list of connections attempt.", endPoint); return(true); } }
public ExampleRequiredConnection(ILogger <ExampleRequiredConnection> logger, IEventBus eventBus, IOptions <ExampleSettings> options, IConnectivityPeerStats serverPeerStats, IForgeClientConnectivity forgeConnectivity, IPeriodicWork connectionLoop) : base(logger, eventBus, serverPeerStats, forgeConnectivity, connectionLoop) { _settings = options.Value !; foreach (ExampleClientPeerBinding peerBinding in _settings.Connections) { if (!peerBinding.TryGetExampleEndPoint(out ExampleEndPoint? endPoint)) { logger.LogWarning("Required connection skipped because of wrong format, check settings file. {Endpoint}", peerBinding.EndPoint); continue; } var remoteEndPoint = new OutgoingConnectionEndPoint(endPoint); remoteEndPoint.Items[nameof(endPoint.MyExtraInformation)] = endPoint.MyExtraInformation; _connectionsToAttempt.Add(remoteEndPoint); } }
public ValueTask AttemptConnectionAsync(OutgoingConnectionEndPoint remoteEndPoint, CancellationToken cancellation) => throw new NotImplementedException(ERROR);
public override IPeerContext CreateOutgoingPeerContext(string peerId, EndPoint localEndPoint, OutgoingConnectionEndPoint outgoingConnectionEndPoint, INetworkMessageWriter messageWriter) { //we know the returned type is correct because we specified it in our inheritance PeerContextFactory<ExamplePeerContext> var peerContext = (ExamplePeerContext)base.CreateOutgoingPeerContext(peerId, localEndPoint, outgoingConnectionEndPoint, messageWriter); /// outgoing PeerContext has a feature of type <see cref="OutgoingConnectionEndPoint"/> that we use to store additional information /// for peers we want to connect to (e.g. we could store a public key of the peer to start an encrypted communication. /// Since this information may be important to us, we decide to have an explicit property in our <see cref="ExamplePeerContext"/> so we can /// access that information easily in our code. /// Note that we set that information in our <see cref="Client.ExampleRequiredConnection"/> connector. string myExtraInformation = (string)peerContext.Features.Get <OutgoingConnectionEndPoint>().Items[nameof(ExampleEndPoint.MyExtraInformation)]; peerContext.MyExtraInformation = myExtraInformation; return(peerContext); }
public virtual IPeerContext CreateOutgoingPeerContext(string peerId, EndPoint localEndPoint, OutgoingConnectionEndPoint outgoingConnectionEndPoint, INetworkMessageWriter messageWriter) { IPeerContext peerContext = Create(PeerConnectionDirection.Outbound, peerId, localEndPoint, outgoingConnectionEndPoint.EndPoint, messageWriter); peerContext.Features.Set(outgoingConnectionEndPoint); return(peerContext); }