@@ -51,6 +51,7 @@ import to.bitkit.utils.LdkError
5151import to.bitkit.utils.LdkLogWriter
5252import to.bitkit.utils.Logger
5353import to.bitkit.utils.ServiceError
54+ import java.util.concurrent.ConcurrentHashMap
5455import javax.inject.Inject
5556import javax.inject.Singleton
5657import kotlin.io.path.Path
@@ -73,7 +74,7 @@ class LightningService @Inject constructor(
7374
7475 private lateinit var trustedPeers: List <PeerDetails >
7576
76- private val channelCache = mutableMapOf <String , ChannelDetails >()
77+ private val channelCache = ConcurrentHashMap <String , ChannelDetails >()
7778
7879 suspend fun setup (
7980 walletIndex : Int ,
@@ -250,9 +251,7 @@ class LightningService @Inject constructor(
250251 private suspend fun registerClosedChannel (channelId : String , reason : String? ) {
251252 try {
252253 val channel = ServiceQueue .LDK .background {
253- channelCache[channelId]?.also {
254- channelCache.remove(channelId)
255- }
254+ channelCache[channelId]
256255 } ? : run {
257256 Logger .error(
258257 " Could not find channel details for closed channel: channelId=$channelId " ,
@@ -261,15 +260,18 @@ class LightningService @Inject constructor(
261260 return @registerClosedChannel
262261 }
263262
264- val channelName = channel.inboundScidAlias?.toString()
265- ? : channel.channelId.take(10 ) + " …"
266-
267263 val fundingTxo = channel.fundingTxo
268264 if (fundingTxo == null ) {
269- Logger .error(" Channel has no funding transaction" , context = TAG )
270- return
265+ Logger .error(
266+ " Channel has no funding transaction, cannot persist closed channel: channelId=$channelId " ,
267+ context = TAG
268+ )
269+ return @registerClosedChannel
271270 }
272271
272+ val channelName = channel.inboundScidAlias?.toString()
273+ ? : channel.channelId.take(CHANNEL_ID_PREVIEW_LENGTH ) + " …"
274+
273275 val closedAt = (System .currentTimeMillis() / 1000L ).toULong()
274276
275277 val closedChannel = ClosedChannelDetails (
@@ -286,12 +288,17 @@ class LightningService @Inject constructor(
286288 forwardingFeeProportionalMillionths = channel.config.forwardingFeeProportionalMillionths,
287289 forwardingFeeBaseMsat = channel.config.forwardingFeeBaseMsat,
288290 channelName = channelName,
289- channelClosureReason = reason ? : " "
291+ channelClosureReason = reason.orEmpty()
290292 )
291293
292294 ServiceQueue .CORE .background {
293295 upsertClosedChannel(closedChannel)
294296 }
297+
298+ ServiceQueue .LDK .background {
299+ channelCache.remove(channelId)
300+ }
301+
295302 Logger .info(" Registered closed channel: ${channel.userChannelId} " , context = TAG )
296303 } catch (e: Exception ) {
297304 Logger .error(" Failed to register closed channel: $e " , e, context = TAG )
@@ -860,6 +867,7 @@ class LightningService @Inject constructor(
860867
861868 companion object {
862869 private const val TAG = " LightningService"
870+ private const val CHANNEL_ID_PREVIEW_LENGTH = 10
863871 }
864872}
865873
0 commit comments