@@ -393,9 +393,8 @@ class LightningRepo @Inject constructor(
393393 }
394394 }
395395
396- @Suppress(" TooGenericExceptionCaught" )
397396 private suspend fun registerClosedChannel (channelId : String , reason : ClosureReason ? ) = withContext(bgDispatcher) {
398- try {
397+ runCatching {
399398 val channel = channelCache[channelId] ? : run {
400399 Logger .error(" Could not find channel details for closed channel: channelId=$channelId " , context = TAG )
401400 return @withContext
@@ -437,115 +436,105 @@ class LightningRepo @Inject constructor(
437436 channelCache.remove(channelId)
438437
439438 Logger .info(" Registered closed channel: ${channel.userChannelId} " , context = TAG )
440- } catch (e : Throwable ) {
441- Logger .error(" Failed to register closed channel: $e " , e , context = TAG )
439+ }.onFailure {
440+ Logger .error(" Failed to register closed channel: $it " , it , context = TAG )
442441 }
443442 }
444443
445444 @Suppress(" TooGenericExceptionCaught" )
446445 suspend fun wipeStorage (walletIndex : Int ): Result <Unit > = withContext(bgDispatcher) {
447446 Logger .debug(" wipeStorage called, stopping node first" , context = TAG )
448- stop().onSuccess {
449- return @withContext try {
450- Logger .debug(" node stopped, calling wipeStorage" , context = TAG )
451- lightningService.wipeStorage(walletIndex)
452- _lightningState .update {
453- LightningState (
454- nodeStatus = it.nodeStatus,
455- nodeLifecycleState = it.nodeLifecycleState,
456- )
457- }
458- setRecoveryMode(false )
459- Result .success(Unit )
460- } catch (e: Throwable ) {
461- Logger .error(" Wipe storage error" , e, context = TAG )
462- Result .failure(e)
447+ stop().mapCatching {
448+ Logger .debug(" node stopped, calling wipeStorage" , context = TAG )
449+ lightningService.wipeStorage(walletIndex)
450+ _lightningState .update {
451+ LightningState (
452+ nodeStatus = it.nodeStatus,
453+ nodeLifecycleState = it.nodeLifecycleState,
454+ )
463455 }
464- }.onFailure { e ->
465- return @withContext Result .failure(e)
456+ setRecoveryMode(false )
457+ }.onFailure {
458+ Logger .error(" wipeStorage error" , it, context = TAG )
466459 }
467460 }
468461
469462 suspend fun restartWithElectrumServer (newServerUrl : String ): Result <Unit > = withContext(bgDispatcher) {
470- Logger .info(" Changing ldk-node electrum server to: '$newServerUrl '" )
463+ Logger .info(" Changing ldk-node electrum server to: '$newServerUrl '" , context = TAG )
471464
472465 waitForNodeToStop().onFailure { return @withContext Result .failure(it) }
473466 stop().onFailure {
474- Logger .error(" Failed to stop node during electrum server change" , it)
467+ Logger .error(" Failed to stop node during electrum server change" , it, context = TAG )
475468 return @withContext Result .failure(it)
476469 }
477470
478- Logger .debug(" Starting node with new electrum server: '$newServerUrl '" )
471+ Logger .debug(" Starting node with new electrum server: '$newServerUrl '" , context = TAG )
479472
480473 start(
481474 shouldRetry = false ,
482475 customServerUrl = newServerUrl,
483- ).onFailure { startError ->
484- Logger .warn(" Failed ldk-node config change, attempting recovery…" )
476+ ).onFailure {
477+ Logger .warn(" Failed ldk-node config change, attempting recovery…" , context = TAG )
485478 restartWithPreviousConfig()
486- return @withContext Result .failure(startError)
487479 }.onSuccess {
488480 settingsStore.update { it.copy(electrumServer = newServerUrl) }
489481
490- Logger .info(" Successfully changed electrum server" )
491- return @withContext Result .success(Unit )
482+ Logger .info(" Successfully changed electrum server" , context = TAG )
492483 }
493484 }
494485
495486 suspend fun restartWithRgsServer (newRgsUrl : String ): Result <Unit > = withContext(bgDispatcher) {
496- Logger .info(" Changing ldk-node RGS server to: '$newRgsUrl '" )
487+ Logger .info(" Changing ldk-node RGS server to: '$newRgsUrl '" , context = TAG )
497488
498489 waitForNodeToStop().onFailure { return @withContext Result .failure(it) }
499490 stop().onFailure {
500- Logger .error(" Failed to stop node during RGS server change" , it)
491+ Logger .error(" Failed to stop node during RGS server change" , it, context = TAG )
501492 return @withContext Result .failure(it)
502493 }
503494
504- Logger .debug(" Starting node with new RGS server: '$newRgsUrl '" )
495+ Logger .debug(" Starting node with new RGS server: '$newRgsUrl '" , context = TAG )
505496
506497 start(
507498 shouldRetry = false ,
508499 customRgsServerUrl = newRgsUrl,
509- ).onFailure { startError ->
510- Logger .warn(" Failed ldk-node config change, attempting recovery…" )
500+ ).onFailure {
501+ Logger .warn(" Failed ldk-node config change, attempting recovery…" , context = TAG )
511502 restartWithPreviousConfig()
512- return @withContext Result .failure(startError)
513503 }.onSuccess {
514504 settingsStore.update { it.copy(rgsServerUrl = newRgsUrl) }
515505
516- Logger .info(" Successfully changed RGS server" )
517- return @withContext Result .success(Unit )
506+ Logger .info(" Successfully changed RGS server" , context = TAG )
518507 }
519508 }
520509
521510 private suspend fun restartWithPreviousConfig (): Result <Unit > = withContext(bgDispatcher) {
522- Logger .debug(" Stopping node for recovery attempt" )
511+ Logger .debug(" Stopping node for recovery attempt" , context = TAG )
523512
524513 stop().onFailure { e ->
525- Logger .error(" Failed to stop node during recovery" , e)
514+ Logger .error(" Failed to stop node during recovery" , e, context = TAG )
526515 return @withContext Result .failure(e)
527516 }
528517
529- Logger .debug(" Starting node with previous config for recovery" )
518+ Logger .debug(" Starting node with previous config for recovery" , context = TAG )
530519
531520 start(
532521 shouldRetry = false ,
533522 ).onSuccess {
534- Logger .debug(" Successfully started node with previous config" )
535- }.onFailure { e ->
536- Logger .error(" Failed starting node with previous config" , e )
523+ Logger .debug(" Successfully started node with previous config" , context = TAG )
524+ }.onFailure {
525+ Logger .error(" Failed starting node with previous config" , it, context = TAG )
537526 }
538527 }
539528
540529 private suspend fun waitForNodeToStop (): Result <Unit > = withContext(bgDispatcher) {
541530 if (_lightningState .value.nodeLifecycleState == NodeLifecycleState .Stopping ) {
542- Logger .debug(" Waiting for node to stop…" )
531+ Logger .debug(" Waiting for node to stop…" , context = TAG )
543532 val stopped = withTimeoutOrNull(30 .seconds) {
544533 _lightningState .first { it.nodeLifecycleState == NodeLifecycleState .Stopped }
545534 }
546535 if (stopped == null ) {
547536 val error = NodeStopTimeoutException ()
548- Logger .warn(error.message)
537+ Logger .warn(error.message, context = TAG )
549538 return @withContext Result .failure(error)
550539 }
551540 }
@@ -558,17 +547,15 @@ class LightningRepo @Inject constructor(
558547 }
559548
560549 suspend fun connectPeer (peer : PeerDetails ): Result <Unit > = executeWhenNodeRunning(" connectPeer" ) {
561- lightningService.connectPeer(peer).onFailure { e ->
562- return @executeWhenNodeRunning Result .failure(e )
550+ lightningService.connectPeer(peer).map {
551+ syncState( )
563552 }
564- syncState()
565- Result .success(Unit )
566553 }
567554
568555 suspend fun disconnectPeer (peer : PeerDetails ): Result <Unit > = executeWhenNodeRunning(" disconnectPeer" ) {
569- lightningService.disconnectPeer(peer)
570- syncState()
571- Result .success( Unit )
556+ lightningService.disconnectPeer(peer).map {
557+ syncState()
558+ }
572559 }
573560
574561 suspend fun newAddress (): Result <String > = executeWhenNodeRunning(" newAddress" ) {
@@ -586,6 +573,7 @@ class LightningRepo @Inject constructor(
586573 Result .success(invoice)
587574 }
588575
576+ @Suppress(" ForbiddenComment" )
589577 suspend fun fetchLnurlInvoice (
590578 callbackUrl : String ,
591579 amountSats : ULong ,
@@ -597,7 +585,11 @@ class LightningRepo @Inject constructor(
597585 val decoded = (decode(bolt11) as Scanner .Lightning ).invoice
598586 return @runCatching decoded
599587 }.onFailure {
600- Logger .error(" Error fetching lnurl invoice, url: $callbackUrl , amount: $amountSats , comment: $comment " , it)
588+ Logger .error(
589+ " fetchLnurlInvoice error, url: $callbackUrl , amount: $amountSats , comment: $comment " ,
590+ it,
591+ context = TAG ,
592+ )
601593 }
602594 }
603595
@@ -606,8 +598,8 @@ class LightningRepo @Inject constructor(
606598 callback : String ,
607599 paymentRequest : String ,
608600 ): Result <LnurlWithdrawResponse > = executeWhenNodeRunning(" requestLnurlWithdraw" ) {
609- val callbackUrl = createWithdrawCallbackUrl(k1 = k1 , callback = callback, paymentRequest = paymentRequest)
610- Logger .debug(" handleLnurlWithdraw callbackUrl generated: '$callbackUrl '" )
601+ val callbackUrl = createWithdrawCallbackUrl(k1, callback, paymentRequest)
602+ Logger .debug(" handleLnurlWithdraw callbackUrl generated: '$callbackUrl '" , context = TAG )
611603 lnurlService.requestLnurlWithdraw(callbackUrl)
612604 }
613605
@@ -645,11 +637,11 @@ class LightningRepo @Inject constructor(
645637 bip39Passphrase = passphrase,
646638 )
647639
648- Logger .debug(" LNURL auth result: '$result '" )
640+ Logger .debug(" LNURL auth result: '$result '" , context = TAG )
649641
650642 return @runCatching result
651643 }.onFailure {
652- Logger .error(" Error requesting lnurl auth, k1: $k1 , callback: $callback , domain: $domain " , it)
644+ Logger .error(" Error requesting lnurl auth, k1: $k1 , callback: $callback , domain: $domain " , it, context = TAG )
653645 }
654646
655647 suspend fun payInvoice (
@@ -720,7 +712,6 @@ class LightningRepo @Inject constructor(
720712 val settings = settingsStore.data.first()
721713 if (settings.coinSelectAuto) {
722714 val coinSelectionPreference = settings.coinSelectPreference
723-
724715 val allSpendableUtxos = lightningService.listSpendableOutputs().getOrThrow()
725716
726717 if (coinSelectionPreference == CoinSelectionPreference .Consolidate ) {
@@ -891,9 +882,8 @@ class LightningRepo @Inject constructor(
891882 channels : List <ChannelDetails >,
892883 ): Pair <List <ChannelDetails >, List<ChannelDetails>> = lightningService.separateTrustedChannels(channels)
893884
894- @Suppress(" TooGenericExceptionCaught" )
895885 suspend fun registerForNotifications (token : String? = null) = executeWhenNodeRunning(" registerForNotifications" ) {
896- return @executeWhenNodeRunning try {
886+ runCatching {
897887 val token = token ? : firebaseMessaging.token.await()
898888 val cachedToken = keychain.loadString(Keychain .Key .PUSH_NOTIFICATION_TOKEN .name)
899889
@@ -905,76 +895,47 @@ class LightningRepo @Inject constructor(
905895 }
906896
907897 lspNotificationsService.registerDevice(token)
908- Result .success(Unit )
909- } catch (e: Throwable ) {
910- Logger .error(" Register for notifications error" , e)
911- Result .failure(e)
898+ }.onFailure {
899+ Logger .error(" Register for notifications error" , it)
912900 }
913901 }
914902
915903 fun registerForNotificationsAsync (token : String ) = scope.launch { registerForNotifications(token) }
916904
917- @Suppress(" TooGenericExceptionCaught" )
918905 suspend fun bumpFeeByRbf (
919906 originalTxId : Txid ,
920907 satsPerVByte : ULong ,
921908 ): Result <Txid > = executeWhenNodeRunning(" bumpFeeByRbf" ) {
922- try {
923- if (originalTxId.isBlank()) {
924- return @executeWhenNodeRunning Result .failure(
925- IllegalArgumentException (" originalTxId is null or empty: $originalTxId " )
926- )
927- }
928-
929- if (satsPerVByte <= 0u ) {
930- return @executeWhenNodeRunning Result .failure(
931- IllegalArgumentException (" satsPerVByte invalid: $satsPerVByte " )
932- )
933- }
909+ runCatching {
910+ require(! originalTxId.isBlank()) { " originalTxId is null or empty: $originalTxId " }
911+ require(satsPerVByte > 0u ) { " satsPerVByte invalid: $satsPerVByte " }
934912
935913 val replacementTxId = lightningService.bumpFeeByRbf(
936914 txid = originalTxId,
937915 satsPerVByte = satsPerVByte,
938916 )
939917 Logger .debug(
940918 " bumpFeeByRbf success, replacementTxId: $replacementTxId " +
941- " originalTxId: $originalTxId , satsPerVByte: $satsPerVByte "
919+ " originalTxId: $originalTxId , satsPerVByte: $satsPerVByte " ,
920+ context = TAG ,
942921 )
943- Result .success( replacementTxId)
944- } catch (e : Throwable ) {
922+ return @runCatching replacementTxId
923+ }.onFailure {
945924 Logger .error(
946- " bumpFeeByRbf error originalTxId: $originalTxId , satsPerVByte: $satsPerVByte " ,
947- e,
948- context = TAG
925+ " bumpFeeByRbf error originalTxId: $originalTxId , satsPerVByte: $satsPerVByte " , it, context = TAG ,
949926 )
950- Result .failure(e)
951927 }
952928 }
953929
954- @Suppress(" TooGenericExceptionCaught" )
955930 suspend fun accelerateByCpfp (
956931 originalTxId : Txid ,
957932 satsPerVByte : ULong ,
958933 destinationAddress : Address ,
959934 ): Result <Txid > = executeWhenNodeRunning(" accelerateByCpfp" ) {
960- try {
961- if (originalTxId.isBlank()) {
962- return @executeWhenNodeRunning Result .failure(
963- IllegalArgumentException (" originalTxId is null or empty: $originalTxId " )
964- )
965- }
966-
967- if (destinationAddress.isBlank()) {
968- return @executeWhenNodeRunning Result .failure(
969- IllegalArgumentException (" destinationAddress is null or empty: $destinationAddress " )
970- )
971- }
972-
973- if (satsPerVByte <= 0u ) {
974- return @executeWhenNodeRunning Result .failure(
975- IllegalArgumentException (" satsPerVByte invalid: $satsPerVByte " )
976- )
977- }
935+ runCatching {
936+ require(! originalTxId.isBlank()) { " originalTxId is null or empty: $originalTxId " }
937+ require(! destinationAddress.isBlank()) { " destinationAddress is null or empty: $destinationAddress " }
938+ require(satsPerVByte > 0u ) { " satsPerVByte invalid: $satsPerVByte " }
978939
979940 val newDestinationTxId = lightningService.accelerateByCpfp(
980941 txid = originalTxId,
@@ -986,39 +947,37 @@ class LightningRepo @Inject constructor(
986947 " originalTxId: $originalTxId , satsPerVByte: $satsPerVByte " +
987948 " destinationAddress: $destinationAddress "
988949 )
989- Result .success( newDestinationTxId)
990- } catch (e : Throwable ) {
950+ return @runCatching newDestinationTxId
951+ }.onFailure {
991952 Logger .error(
992953 " accelerateByCpfp error originalTxId: $originalTxId , " +
993954 " satsPerVByte: $satsPerVByte destinationAddress: $destinationAddress " ,
994- e ,
995- context = TAG
955+ it ,
956+ context = TAG ,
996957 )
997- Result .failure(e)
998958 }
999959 }
1000960
1001- suspend fun estimateRoutingFees (bolt11 : String ): Result <ULong > =
1002- executeWhenNodeRunning(" estimateRoutingFees" ) {
1003- Logger .info(" Estimating routing fees for bolt11: $bolt11 " )
1004- lightningService.estimateRoutingFees(bolt11)
1005- .onSuccess {
1006- Logger .info(" Routing fees estimated: $it " )
1007- }
1008- .onFailure {
1009- Logger .error(" Routing fees estimation failed" , it)
1010- }
1011- }
961+ suspend fun estimateRoutingFees (bolt11 : String ): Result <ULong > = executeWhenNodeRunning(" estimateRoutingFees" ) {
962+ Logger .info(" Estimating routing fees for bolt11: $bolt11 " )
963+ lightningService.estimateRoutingFees(bolt11)
964+ .onSuccess {
965+ Logger .info(" Routing fees estimated: $it " , context = TAG )
966+ }
967+ .onFailure {
968+ Logger .error(" Routing fees estimation failed" , it, context = TAG )
969+ }
970+ }
1012971
1013972 suspend fun estimateRoutingFeesForAmount (bolt11 : String , amountSats : ULong ): Result <ULong > =
1014973 executeWhenNodeRunning(" estimateRoutingFeesForAmount" ) {
1015974 Logger .info(" Estimating routing fees for amount: $amountSats " )
1016975 lightningService.estimateRoutingFeesForAmount(bolt11, amountSats)
1017976 .onSuccess {
1018- Logger .info(" Routing fees estimated: $it " )
977+ Logger .info(" Routing fees estimated: $it " , context = TAG )
1019978 }
1020979 .onFailure {
1021- Logger .error(" Routing fees estimation failed" , it)
980+ Logger .error(" Routing fees estimation failed" , it, context = TAG )
1022981 }
1023982 }
1024983
0 commit comments