Skip to content

Commit e501ec1

Browse files
committed
refactor: standardize operation names in logs
1 parent fa58393 commit e501ec1

File tree

1 file changed

+56
-61
lines changed

1 file changed

+56
-61
lines changed

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class LightningRepo @Inject constructor(
127127
if (!_lightningState.value.nodeLifecycleState.canRun()) {
128128
return@withContext Result.failure(
129129
Exception(
130-
"Cannot execute $operationName: Node is ${_lightningState.value.nodeLifecycleState} and not starting"
130+
"Cannot execute '$operationName': Node is ${_lightningState.value.nodeLifecycleState} and not starting"
131131
)
132132
)
133133
}
@@ -138,9 +138,9 @@ class LightningRepo @Inject constructor(
138138
}
139139

140140
// Otherwise, wait for it to transition to running state
141-
Logger.verbose("Waiting for node runs to execute $operationName", context = TAG)
141+
Logger.verbose("Waiting for node runs to execute '$operationName'", context = TAG)
142142
_lightningState.first { it.nodeLifecycleState.isRunning() }
143-
Logger.debug("Operation executed: $operationName", context = TAG)
143+
Logger.debug("Operation executed: '$operationName'", context = TAG)
144144
true
145145
} ?: false
146146

@@ -159,7 +159,7 @@ class LightningRepo @Inject constructor(
159159
// Cancellation is expected during pull-to-refresh, rethrow per Kotlin best practices
160160
throw e
161161
} catch (e: Throwable) {
162-
Logger.error("$operationName error", e, context = TAG)
162+
Logger.error("Error executing '$operationName'", e, context = TAG)
163163
Result.failure(e)
164164
}
165165
}
@@ -312,7 +312,7 @@ class LightningRepo @Inject constructor(
312312
}
313313
}
314314

315-
suspend fun sync(): Result<Unit> = executeWhenNodeRunning("Sync") {
315+
suspend fun sync(): Result<Unit> = executeWhenNodeRunning("sync") {
316316
// If sync is in progress, mark pending and skip
317317
if (!syncMutex.tryLock()) {
318318
syncPending.set(true)
@@ -531,7 +531,7 @@ class LightningRepo @Inject constructor(
531531
return@withContext Result.success(Unit)
532532
}
533533

534-
suspend fun connectToTrustedPeers(): Result<Unit> = executeWhenNodeRunning("Connect to trusted peers") {
534+
suspend fun connectToTrustedPeers(): Result<Unit> = executeWhenNodeRunning("connectToTrustedPeers") {
535535
lightningService.connectToTrustedPeers()
536536
Result.success(Unit)
537537
}
@@ -544,13 +544,13 @@ class LightningRepo @Inject constructor(
544544
Result.success(Unit)
545545
}
546546

547-
suspend fun disconnectPeer(peer: PeerDetails): Result<Unit> = executeWhenNodeRunning("Disconnect peer") {
547+
suspend fun disconnectPeer(peer: PeerDetails): Result<Unit> = executeWhenNodeRunning("disconnectPeer") {
548548
lightningService.disconnectPeer(peer)
549549
syncState()
550550
Result.success(Unit)
551551
}
552552

553-
suspend fun newAddress(): Result<String> = executeWhenNodeRunning("New address") {
553+
suspend fun newAddress(): Result<String> = executeWhenNodeRunning("newAddress") {
554554
val address = lightningService.newAddress()
555555
Result.success(address)
556556
}
@@ -559,7 +559,7 @@ class LightningRepo @Inject constructor(
559559
amountSats: ULong? = null,
560560
description: String,
561561
expirySeconds: UInt = 86_400u,
562-
): Result<String> = executeWhenNodeRunning("Create invoice") {
562+
): Result<String> = executeWhenNodeRunning("createInvoice") {
563563
updateGeoBlockState()
564564
val invoice = lightningService.receive(amountSats, description, expirySeconds)
565565
Result.success(invoice)
@@ -631,12 +631,14 @@ class LightningRepo @Inject constructor(
631631
Logger.error("Error requesting lnurl auth, k1: $k1, callback: $callback, domain: $domain", it)
632632
}
633633

634-
suspend fun payInvoice(bolt11: String, sats: ULong? = null): Result<PaymentId> =
635-
executeWhenNodeRunning("Pay invoice") {
636-
val paymentId = lightningService.send(bolt11 = bolt11, sats = sats)
637-
syncState()
638-
Result.success(paymentId)
639-
}
634+
suspend fun payInvoice(
635+
bolt11: String,
636+
sats: ULong? = null,
637+
): Result<PaymentId> = executeWhenNodeRunning("payInvoice") {
638+
val paymentId = lightningService.send(bolt11 = bolt11, sats = sats)
639+
syncState()
640+
Result.success(paymentId)
641+
}
640642

641643
@Suppress("LongParameterList")
642644
suspend fun sendOnChain(
@@ -649,46 +651,45 @@ class LightningRepo @Inject constructor(
649651
channelId: String? = null,
650652
isMaxAmount: Boolean = false,
651653
tags: List<String> = emptyList(),
652-
): Result<Txid> =
653-
executeWhenNodeRunning("sendOnChain") {
654-
require(address.isNotEmpty()) { "Send address cannot be empty" }
654+
): Result<Txid> = executeWhenNodeRunning("sendOnChain") {
655+
require(address.isNotEmpty()) { "Send address cannot be empty" }
655656

656-
val transactionSpeed = speed ?: settingsStore.data.first().defaultTransactionSpeed
657-
val satsPerVByte = getFeeRateForSpeed(transactionSpeed, feeRates).getOrThrow().toUInt()
657+
val transactionSpeed = speed ?: settingsStore.data.first().defaultTransactionSpeed
658+
val satsPerVByte = getFeeRateForSpeed(transactionSpeed, feeRates).getOrThrow().toUInt()
658659

659-
// if utxos are manually specified, use them, otherwise run auto coin select if enabled
660-
val finalUtxosToSpend = utxosToSpend ?: determineUtxosToSpend(
661-
sats = sats,
662-
satsPerVByte = satsPerVByte,
663-
)
660+
// if utxos are manually specified, use them, otherwise run auto coin select if enabled
661+
val finalUtxosToSpend = utxosToSpend ?: determineUtxosToSpend(
662+
sats = sats,
663+
satsPerVByte = satsPerVByte,
664+
)
664665

665-
Logger.debug("UTXOs selected to spend: $finalUtxosToSpend", context = TAG)
666+
Logger.debug("UTXOs selected to spend: $finalUtxosToSpend", context = TAG)
666667

667-
val txId = lightningService.send(
668-
address = address,
669-
sats = sats,
670-
satsPerVByte = satsPerVByte,
671-
utxosToSpend = finalUtxosToSpend,
672-
isMaxAmount = isMaxAmount
673-
)
668+
val txId = lightningService.send(
669+
address = address,
670+
sats = sats,
671+
satsPerVByte = satsPerVByte,
672+
utxosToSpend = finalUtxosToSpend,
673+
isMaxAmount = isMaxAmount
674+
)
674675

675-
val preActivityMetadata = PreActivityMetadata(
676-
paymentId = txId,
677-
createdAt = nowTimestamp().toEpochMilli().toULong(),
678-
tags = tags,
679-
paymentHash = null,
680-
txId = txId,
681-
address = address,
682-
isReceive = false,
683-
feeRate = satsPerVByte.toULong(),
684-
isTransfer = isTransfer,
685-
channelId = channelId ?: "",
686-
)
687-
preActivityMetadataRepo.addPreActivityMetadata(preActivityMetadata)
676+
val preActivityMetadata = PreActivityMetadata(
677+
paymentId = txId,
678+
createdAt = nowTimestamp().toEpochMilli().toULong(),
679+
tags = tags,
680+
paymentHash = null,
681+
txId = txId,
682+
address = address,
683+
isReceive = false,
684+
feeRate = satsPerVByte.toULong(),
685+
isTransfer = isTransfer,
686+
channelId = channelId ?: "",
687+
)
688+
preActivityMetadataRepo.addPreActivityMetadata(preActivityMetadata)
688689

689-
syncState()
690-
Result.success(txId)
691-
}
690+
syncState()
691+
Result.success(txId)
692+
}
692693

693694
suspend fun determineUtxosToSpend(
694695
sats: ULong,
@@ -731,7 +732,7 @@ class LightningRepo @Inject constructor(
731732
Result.success(payments)
732733
}
733734

734-
suspend fun getAddressBalance(address: String): Result<ULong> = executeWhenNodeRunning("Get address balance") {
735+
suspend fun getAddressBalance(address: String): Result<ULong> = executeWhenNodeRunning("getAddressBalance") {
735736
runCatching {
736737
lightningService.getAddressBalance(address)
737738
}
@@ -787,7 +788,7 @@ class LightningRepo @Inject constructor(
787788

788789
suspend fun calculateCpfpFeeRate(
789790
parentTxId: Txid,
790-
): Result<ULong> = executeWhenNodeRunning("Calculate CPFP fee rate") {
791+
): Result<ULong> = executeWhenNodeRunning("calculateCpfpFeeRate") {
791792
Result.success(lightningService.calculateCpfpFeeRate(parentTxid = parentTxId).toSatPerVbCeil())
792793
}
793794

@@ -796,7 +797,7 @@ class LightningRepo @Inject constructor(
796797
channelAmountSats: ULong,
797798
pushToCounterpartySats: ULong? = null,
798799
channelConfig: ChannelConfig? = null,
799-
): Result<OpenChannelResult> = executeWhenNodeRunning("Open channel") {
800+
): Result<OpenChannelResult> = executeWhenNodeRunning("openChannel") {
800801
val result = lightningService.openChannel(peer, channelAmountSats, pushToCounterpartySats, channelConfig)
801802
syncState()
802803
result
@@ -931,25 +932,19 @@ class LightningRepo @Inject constructor(
931932
try {
932933
if (originalTxId.isBlank()) {
933934
return@executeWhenNodeRunning Result.failure(
934-
IllegalArgumentException(
935-
"originalTxId is null or empty: $originalTxId"
936-
)
935+
IllegalArgumentException("originalTxId is null or empty: $originalTxId")
937936
)
938937
}
939938

940939
if (destinationAddress.isBlank()) {
941940
return@executeWhenNodeRunning Result.failure(
942-
IllegalArgumentException(
943-
"destinationAddress is null or empty: $destinationAddress"
944-
)
941+
IllegalArgumentException("destinationAddress is null or empty: $destinationAddress")
945942
)
946943
}
947944

948945
if (satsPerVByte <= 0u) {
949946
return@executeWhenNodeRunning Result.failure(
950-
IllegalArgumentException(
951-
"satsPerVByte invalid: $satsPerVByte"
952-
)
947+
IllegalArgumentException("satsPerVByte invalid: $satsPerVByte")
953948
)
954949
}
955950

0 commit comments

Comments
 (0)