Skip to content

Commit f2fe18e

Browse files
authored
Merge pull request #718 from synonymdev/feat/optimise-ldk-node-use
chore: integrate latest ldk-node and bitkit-core
2 parents a945e57 + 93103cc commit f2fe18e

9 files changed

Lines changed: 108 additions & 130 deletions

File tree

app/src/main/java/to/bitkit/ext/ChannelDetails.kt

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,69 @@ import org.lightningdevkit.ldknode.MaxDustHtlcExposure
1515
* - our_reserve: Our reserve that we get back on close
1616
*/
1717
val ChannelDetails.amountOnClose: ULong
18+
@Suppress("ForbiddenComment")
1819
get() {
20+
// TODO: use channelDetails.claimableOnCloseSats
1921
val outboundCapacitySat = this.outboundCapacityMsat / 1000u
2022
val ourReserve = this.unspendablePunishmentReserve ?: 0u
2123

2224
return outboundCapacitySat + ourReserve
2325
}
2426

2527
/** Returns only `open` channels, filtering out pending ones. */
26-
fun List<ChannelDetails>.filterOpen(): List<ChannelDetails> {
27-
return this.filter { it.isChannelReady }
28-
}
28+
fun List<ChannelDetails>.filterOpen(): List<ChannelDetails> = this.filter { it.isChannelReady }
2929

3030
/** Returns only `pending` channels. */
31-
fun List<ChannelDetails>.filterPending(): List<ChannelDetails> {
32-
return this.filterNot { it.isChannelReady }
33-
}
31+
fun List<ChannelDetails>.filterPending(): List<ChannelDetails> = this.filterNot { it.isChannelReady }
3432

3533
/** Returns a limit in sats as close as possible to the HTLC limit we can currently send. */
36-
fun List<ChannelDetails>?.totalNextOutboundHtlcLimitSats(): ULong {
37-
return this?.filter { it.isUsable }
38-
?.sumOf { it.nextOutboundHtlcLimitMsat / 1000u }
39-
?: 0u
40-
}
34+
fun List<ChannelDetails>?.totalNextOutboundHtlcLimitSats(): ULong = this?.filter { it.isUsable }
35+
?.sumOf { it.nextOutboundHtlcLimitMsat / 1000u }
36+
?: 0u
4137

4238
/** Calculates the total remote balance (inbound capacity) from open channels. */
43-
fun List<ChannelDetails>.calculateRemoteBalance(): ULong {
44-
return this
45-
.filterOpen()
46-
.sumOf { it.inboundCapacityMsat / 1000u }
47-
}
39+
fun List<ChannelDetails>.calculateRemoteBalance(): ULong = this
40+
.filterOpen()
41+
.sumOf { it.inboundCapacityMsat / 1000u }
4842

49-
fun createChannelDetails(): ChannelDetails {
50-
return ChannelDetails(
51-
channelId = "channelId",
52-
counterpartyNodeId = "counterpartyNodeId",
53-
fundingTxo = null,
54-
shortChannelId = null,
55-
outboundScidAlias = null,
56-
inboundScidAlias = null,
57-
channelValueSats = 0u,
58-
unspendablePunishmentReserve = null,
59-
userChannelId = "0",
60-
feerateSatPer1000Weight = 0u,
61-
outboundCapacityMsat = 0u,
62-
inboundCapacityMsat = 0u,
63-
confirmationsRequired = null,
64-
confirmations = null,
65-
isOutbound = false,
66-
isChannelReady = false,
67-
isUsable = false,
68-
isAnnounced = false,
69-
cltvExpiryDelta = null,
70-
counterpartyUnspendablePunishmentReserve = 0u,
71-
counterpartyOutboundHtlcMinimumMsat = null,
72-
counterpartyOutboundHtlcMaximumMsat = null,
73-
counterpartyForwardingInfoFeeBaseMsat = null,
74-
counterpartyForwardingInfoFeeProportionalMillionths = null,
75-
counterpartyForwardingInfoCltvExpiryDelta = null,
76-
nextOutboundHtlcLimitMsat = 0u,
77-
nextOutboundHtlcMinimumMsat = 0u,
78-
forceCloseSpendDelay = null,
79-
inboundHtlcMinimumMsat = 0u,
80-
inboundHtlcMaximumMsat = null,
81-
config = ChannelConfig(
82-
forwardingFeeProportionalMillionths = 0u,
83-
forwardingFeeBaseMsat = 0u,
84-
cltvExpiryDelta = 0u,
85-
maxDustHtlcExposure = MaxDustHtlcExposure.FixedLimit(limitMsat = 0u),
86-
forceCloseAvoidanceMaxFeeSatoshis = 0u,
87-
acceptUnderpayingHtlcs = false,
88-
),
89-
)
90-
}
43+
fun createChannelDetails(): ChannelDetails = ChannelDetails(
44+
channelId = "channelId",
45+
counterpartyNodeId = "counterpartyNodeId",
46+
fundingTxo = null,
47+
shortChannelId = null,
48+
outboundScidAlias = null,
49+
inboundScidAlias = null,
50+
channelValueSats = 0u,
51+
unspendablePunishmentReserve = null,
52+
userChannelId = "0",
53+
feerateSatPer1000Weight = 0u,
54+
outboundCapacityMsat = 0u,
55+
inboundCapacityMsat = 0u,
56+
confirmationsRequired = null,
57+
confirmations = null,
58+
isOutbound = false,
59+
isChannelReady = false,
60+
isUsable = false,
61+
isAnnounced = false,
62+
cltvExpiryDelta = null,
63+
counterpartyUnspendablePunishmentReserve = 0u,
64+
counterpartyOutboundHtlcMinimumMsat = null,
65+
counterpartyOutboundHtlcMaximumMsat = null,
66+
counterpartyForwardingInfoFeeBaseMsat = null,
67+
counterpartyForwardingInfoFeeProportionalMillionths = null,
68+
counterpartyForwardingInfoCltvExpiryDelta = null,
69+
nextOutboundHtlcLimitMsat = 0u,
70+
nextOutboundHtlcMinimumMsat = 0u,
71+
forceCloseSpendDelay = null,
72+
inboundHtlcMinimumMsat = 0u,
73+
inboundHtlcMaximumMsat = null,
74+
claimableOnCloseSats = 0u,
75+
config = ChannelConfig(
76+
forwardingFeeProportionalMillionths = 0u,
77+
forwardingFeeBaseMsat = 0u,
78+
cltvExpiryDelta = 0u,
79+
maxDustHtlcExposure = MaxDustHtlcExposure.FixedLimit(limitMsat = 0u),
80+
forceCloseAvoidanceMaxFeeSatoshis = 0u,
81+
acceptUnderpayingHtlcs = false,
82+
),
83+
)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,6 @@ class LightningRepo @Inject constructor(
659659
lnurlService.requestLnurlWithdraw(callbackUrl)
660660
}
661661

662-
suspend fun fetchLnurlChannelInfo(url: String) = lnurlService.fetchLnurlChannelInfo(url)
663-
664662
suspend fun requestLnurlChannel(
665663
k1: String,
666664
callback: String,

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,8 @@ class CoreService @Inject constructor(
186186
com.synonym.bitkitcore.isAddressUsed(address = address)
187187
}
188188

189-
@Suppress("ForbiddenComment")
190189
suspend fun decode(input: String): Scanner = ServiceQueue.CORE.background {
191-
// TODO: Remove lowercase workaround when https://github.com/synonymdev/bitkit-core/issues/66 is fixed
192-
com.synonym.bitkitcore.decode(input.lowercase())
190+
com.synonym.bitkitcore.decode(input)
193191
}
194192

195193
companion object {

app/src/main/java/to/bitkit/services/LnurlService.kt

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,6 @@ class LnurlService @Inject constructor(
6363
return@runCatching response.body<LnurlPayResponse>()
6464
}
6565

66-
suspend fun fetchLnurlChannelInfo(url: String): Result<LnurlChannelInfoResponse> = runCatching {
67-
Logger.debug("Fetching LNURL channel info from: $url", context = TAG)
68-
69-
val response: HttpResponse = client.get(url)
70-
Logger.debug("Http call: $response", context = TAG)
71-
72-
if (!response.status.isSuccess()) {
73-
throw HttpError("fetchLnurlChannelInfo error: '${response.status.description}'", response.status.value)
74-
}
75-
76-
return@runCatching response.body<LnurlChannelInfoResponse>()
77-
}.onFailure {
78-
Logger.warn("Failed to fetch channel info", it, context = TAG)
79-
}
80-
8166
suspend fun requestLnurlChannel(url: String): Result<LnurlChannelResponse> = runCatching {
8267
Logger.debug("Requesting LNURL channel request via: '$url'", context = TAG)
8368

@@ -127,11 +112,3 @@ data class LnurlChannelResponse(
127112
val status: String? = null,
128113
val reason: String? = null,
129114
)
130-
131-
@Serializable
132-
data class LnurlChannelInfoResponse(
133-
val uri: String,
134-
val tag: String,
135-
val callback: String,
136-
val k1: String,
137-
)

app/src/main/java/to/bitkit/ui/screens/transfer/external/LnurlChannelViewModel.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,11 @@ class LnurlChannelViewModel @Inject constructor(
3636

3737
private fun fetchChannelInfo() {
3838
viewModelScope.launch {
39-
lightningRepo.fetchLnurlChannelInfo(params.uri)
40-
.onSuccess { channelInfo ->
41-
val peer = runCatching { PeerDetails.of(channelInfo.uri) }.getOrElse {
42-
errorToast(it)
43-
return@onSuccess
44-
}
45-
_uiState.update { it.copy(peer = peer) }
46-
}
47-
.onFailure { error ->
48-
val message = context.getString(R.string.other__lnurl_channel_error_raw)
49-
.replace("{raw}", error.message.orEmpty())
50-
errorToast(Exception(message))
51-
}
39+
val peer = runCatching { PeerDetails.of(params.uri) }.getOrElse {
40+
errorToast(it)
41+
return@launch
42+
}
43+
_uiState.update { it.copy(peer = peer) }
5244
}
5345
}
5446

app/src/main/java/to/bitkit/ui/screens/wallets/receive/ReceiveQrScreen.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ private fun PreviewAutoMode() {
694694
forceCloseSpendDelay = null,
695695
inboundHtlcMinimumMsat = 0uL,
696696
inboundHtlcMaximumMsat = null,
697+
claimableOnCloseSats = 0uL,
697698
config = org.lightningdevkit.ldknode.ChannelConfig(
698699
forwardingFeeProportionalMillionths = 0u,
699700
forwardingFeeBaseMsat = 0u,
@@ -764,6 +765,7 @@ private fun PreviewSpendingMode() {
764765
forceCloseSpendDelay = null,
765766
inboundHtlcMinimumMsat = 0uL,
766767
inboundHtlcMaximumMsat = null,
768+
claimableOnCloseSats = 0uL,
767769
config = org.lightningdevkit.ldknode.ChannelConfig(
768770
forwardingFeeProportionalMillionths = 0u,
769771
forwardingFeeBaseMsat = 0u,

app/src/main/java/to/bitkit/utils/Logger.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,18 @@ class LogSaverImpl(
239239
}
240240
}
241241

242-
private fun log(message: String, level: LogLevel = LogLevel.INFO) {
242+
private fun log(
243+
message: String,
244+
level: LogLevel = LogLevel.INFO,
245+
androidLog: (String, String) -> Unit = { tag, msg -> Log.i(tag, msg) },
246+
) {
243247
val formatted = formatLog(level, message, TAG, getCallerPath(), getCallerLine())
244-
Log.i(APP, formatted)
248+
androidLog(APP, formatted)
245249
save(formatted)
246250
}
247251

248252
private fun cleanupOldLogFiles(maxTotalSizeMB: Int = 20) {
249-
log("Deleting old log files…", LogLevel.VERBOSE)
253+
log("Deleting old log files…", LogLevel.VERBOSE, Log::v)
250254
val logDir = runCatching { Env.logDir }.getOrNull() ?: return
251255

252256
val logFiles = logDir
@@ -263,15 +267,15 @@ class LogSaverImpl(
263267
if (totalSize <= maxSizeBytes) return
264268

265269
runCatching {
266-
Log.d(APP, "Deleting old log file: '${file.name}'")
270+
log("Deleting old log file: '${file.name}'", LogLevel.DEBUG, Log::d)
267271
if (file.delete()) {
268272
totalSize -= file.length()
269273
}
270274
}.onFailure {
271-
Log.w(APP, "Failed to delete old log file: '${file.name}'", it)
275+
log("Failed to delete old log file: '${file.name}'", LogLevel.WARN, Log::w)
272276
}
273277
}
274-
Log.v(APP, "Deleted all old log files.")
278+
log("Deleted all old log files.", LogLevel.VERBOSE, Log::v)
275279
}
276280

277281
companion object {

0 commit comments

Comments
 (0)