Skip to content

Commit 1e4fa71

Browse files
committed
Backend(LN): catch NOnionException TorOperations
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
1 parent d51d215 commit 1e4fa71

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/GWallet.Backend/UtxoCoin/Lightning/Network.fs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ open NBitcoin
1010
open DotNetLightning.Peer
1111
open DotNetLightning.Utils
1212
open ResultUtils.Portability
13+
open NOnion
1314
open NOnion.Network
1415
open NOnion.Directory
1516
open NOnion.Services
@@ -36,7 +37,7 @@ type PeerDisconnectedError =
3637
not self.Abruptly
3738

3839
type HandshakeError =
39-
| TcpConnect of seq<SocketException>
40+
| TcpConnect of seq<Exception>
4041
| TcpAccept of seq<SocketException>
4142
| DisconnectedOnAct1 of PeerDisconnectedError
4243
| InvalidAct1 of PeerError
@@ -48,7 +49,7 @@ type HandshakeError =
4849
member self.Message =
4950
match self with
5051
| TcpConnect errs ->
51-
let messages = Seq.map (fun (err: SocketException) -> err.Message) errs
52+
let messages = Seq.map (fun (err: Exception) -> err.Message) errs
5253
SPrintF1 "TCP connection failed: %s" (String.concat "; " messages)
5354
| TcpAccept errs ->
5455
let messages = Seq.map (fun (err: SocketException) -> err.Message) errs
@@ -331,7 +332,7 @@ type internal TransportStream =
331332
static member private TcpTransportConnect
332333
(localEndPointOpt: Option<IPEndPoint>)
333334
(remoteEndPoint: IPEndPoint)
334-
: Async<Result<TcpClient, seq<SocketException>>> = async {
335+
: Async<Result<TcpClient, seq<Exception>>> = async {
335336
let client = new TcpClient (remoteEndPoint.AddressFamily)
336337
match localEndPointOpt with
337338
| Some localEndPoint ->
@@ -352,7 +353,13 @@ type internal TransportStream =
352353
| ex ->
353354
client.Close()
354355
let socketExceptions = FindSingleException<SocketException> ex
355-
return Error socketExceptions
356+
let exceptions =
357+
seq {
358+
for socketException in socketExceptions do
359+
yield socketException :> Exception
360+
}
361+
362+
return Error exceptions
356363
}
357364

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

420427
static member private TorTransportConnect
421428
(nonionEndPoint: NOnionEndPoint)
422-
: Async<Result<TorServiceClient, seq<SocketException>>> =
429+
: Async<Result<TorServiceClient, seq<Exception>>> =
423430
async {
424431
let! directory = TorOperations.GetTorDirectory()
425432
try
426-
let! torClient = TorOperations.TorConnect directory nonionEndPoint.Url
427-
Infrastructure.LogDebug <| SPrintF1 "Connected %s" nonionEndPoint.Url
428-
return Ok torClient
433+
let! maybeTorClient = TorOperations.TorConnect directory nonionEndPoint.Url
434+
match maybeTorClient with
435+
| Ok torClient ->
436+
Infrastructure.LogDebug <| SPrintF1 "Connected %s" nonionEndPoint.Url
437+
return Ok torClient
438+
| Error ex ->
439+
return Error (ex :> Exception |> Seq.singleton)
429440
with
430441
| ex ->
431442
let socketExceptions = FindSingleException<SocketException> ex
432-
return Error socketExceptions
443+
let exceptions =
444+
seq {
445+
for socketException in socketExceptions do
446+
yield socketException :> Exception
447+
}
448+
449+
return Error exceptions
433450
}
434451

435452
static member private TransportConnect
436453
(localEndPointOpt: Option<IPEndPoint>)
437454
(node: NodeIdentifier)
438-
: Async<Result<TransportClientType, seq<SocketException>>> =
455+
: Async<Result<TransportClientType, seq<Exception>>> =
439456
async {
440457
match node with
441458
| NodeIdentifier.TcpEndPoint remoteEndPoint ->

src/GWallet.Backend/UtxoCoin/TorOperations.fs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open System.Net
66
open System.Text.RegularExpressions
77
open System.Diagnostics
88

9+
open ResultUtils.Portability
910
open NOnion
1011
open NOnion.Directory
1112
open NOnion.Services
@@ -110,13 +111,21 @@ module internal TorOperations =
110111
Config.TOR_CONNECTION_RETRY_COUNT
111112
}
112113

113-
let internal TorConnect directory url =
114+
let internal TorConnect directory url:
115+
Async<Result<TorServiceClient, NOnionException>> =
114116
async {
115-
return! FSharpUtil.Retry<TorServiceClient, NOnionException>
116-
(fun _ -> TorServiceClient.Connect directory url)
117-
Config.TOR_CONNECTION_RETRY_COUNT
117+
try
118+
let! connectedServiceClient =
119+
FSharpUtil.Retry<TorServiceClient, NOnionException>
120+
(fun _ -> TorServiceClient.Connect directory url)
121+
Config.TOR_CONNECTION_RETRY_COUNT
122+
return Ok connectedServiceClient
123+
with
124+
| :? NOnionException as ex ->
125+
return Error ex
118126
}
119127

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

0 commit comments

Comments
 (0)