Skip to content

Commit

Permalink
Backend(LN): catch NOnionException TorOperations
Browse files Browse the repository at this point in the history
Catch the NOnionException after Retrys have finished and the
problem still exists. We catch the exception and return an
error.

Fixes: nblockchain#181
Fixes: nblockchain#182
  • Loading branch information
parhamsaremi committed Nov 9, 2022
1 parent d51d215 commit 773e86b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
37 changes: 27 additions & 10 deletions src/GWallet.Backend/UtxoCoin/Lightning/Network.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ open NBitcoin
open DotNetLightning.Peer
open DotNetLightning.Utils
open ResultUtils.Portability
open NOnion
open NOnion.Network
open NOnion.Directory
open NOnion.Services
Expand All @@ -36,7 +37,7 @@ type PeerDisconnectedError =
not self.Abruptly

type HandshakeError =
| TcpConnect of seq<SocketException>
| TcpConnect of seq<Exception>
| TcpAccept of seq<SocketException>
| DisconnectedOnAct1 of PeerDisconnectedError
| InvalidAct1 of PeerError
Expand All @@ -48,7 +49,7 @@ type HandshakeError =
member self.Message =
match self with
| TcpConnect errs ->
let messages = Seq.map (fun (err: SocketException) -> err.Message) errs
let messages = Seq.map (fun (err: Exception) -> err.Message) errs
SPrintF1 "TCP connection failed: %s" (String.concat "; " messages)
| TcpAccept errs ->
let messages = Seq.map (fun (err: SocketException) -> err.Message) errs
Expand Down Expand Up @@ -331,7 +332,7 @@ type internal TransportStream =
static member private TcpTransportConnect
(localEndPointOpt: Option<IPEndPoint>)
(remoteEndPoint: IPEndPoint)
: Async<Result<TcpClient, seq<SocketException>>> = async {
: Async<Result<TcpClient, seq<Exception>>> = async {
let client = new TcpClient (remoteEndPoint.AddressFamily)
match localEndPointOpt with
| Some localEndPoint ->
Expand All @@ -352,7 +353,13 @@ type internal TransportStream =
| ex ->
client.Close()
let socketExceptions = FindSingleException<SocketException> ex
return Error socketExceptions
let exceptions =
seq {
for socketException in socketExceptions do
yield socketException :> Exception
}

return Error exceptions
}

static member private AcceptAny (listener: IncomingConnectionMethod)
Expand Down Expand Up @@ -419,23 +426,33 @@ type internal TransportStream =

static member private TorTransportConnect
(nonionEndPoint: NOnionEndPoint)
: Async<Result<TorServiceClient, seq<SocketException>>> =
: Async<Result<TorServiceClient, seq<Exception>>> =
async {
let! directory = TorOperations.GetTorDirectory()
try
let! torClient = TorOperations.TorConnect directory nonionEndPoint.Url
Infrastructure.LogDebug <| SPrintF1 "Connected %s" nonionEndPoint.Url
return Ok torClient
let! maybeTorClient = TorOperations.TorConnect directory nonionEndPoint.Url
match maybeTorClient with
| Ok torClient ->
Infrastructure.LogDebug <| SPrintF1 "Connected %s" nonionEndPoint.Url
return Ok torClient
| Error ex ->
return Error (ex :> Exception |> Seq.singleton)
with
| ex ->
let socketExceptions = FindSingleException<SocketException> ex
return Error socketExceptions
let exceptions =
seq {
for socketException in socketExceptions do
yield socketException :> Exception
}

return Error exceptions
}

static member private TransportConnect
(localEndPointOpt: Option<IPEndPoint>)
(node: NodeIdentifier)
: Async<Result<TransportClientType, seq<SocketException>>> =
: Async<Result<TransportClientType, seq<Exception>>> =
async {
match node with
| NodeIdentifier.TcpEndPoint remoteEndPoint ->
Expand Down
17 changes: 13 additions & 4 deletions src/GWallet.Backend/UtxoCoin/TorOperations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open System.Net
open System.Text.RegularExpressions
open System.Diagnostics

open ResultUtils.Portability
open NOnion
open NOnion.Directory
open NOnion.Services
Expand Down Expand Up @@ -110,13 +111,21 @@ module internal TorOperations =
Config.TOR_CONNECTION_RETRY_COUNT
}

let internal TorConnect directory url =
let internal TorConnect directory url:
Async<Result<TorServiceClient, NOnionException>> =
async {
return! FSharpUtil.Retry<TorServiceClient, NOnionException>
(fun _ -> TorServiceClient.Connect directory url)
Config.TOR_CONNECTION_RETRY_COUNT
try
let! connectedServiceClient =
FSharpUtil.Retry<TorServiceClient, NOnionException>
(fun _ -> TorServiceClient.Connect directory url)
Config.TOR_CONNECTION_RETRY_COUNT
return Ok connectedServiceClient
with
| :? NOnionException as ex ->
return Error ex
}


let internal ExtractServerListFromGithub() : List<(string*string)> =
let urlToTorServerList = "https://raw.githubusercontent.com/torproject/tor/main/src/app/config/fallback_dirs.inc"
use webClient = new WebClient()
Expand Down

0 comments on commit 773e86b

Please sign in to comment.