diff --git a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt index 215c48d26..9ca651368 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt @@ -123,7 +123,7 @@ class Peer( // channels map, indexed by channel id // note that a channel starts with a temporary id then switches to its final id once accepted - private val _channelsFlow = MutableStateFlow>(HashMap()) + private val _channelsFlow = MutableStateFlow>(emptyMap()) val channelsFlow: StateFlow> get() = _channelsFlow private var _channels by _channelsFlow @@ -152,6 +152,10 @@ class Peer( val currentTipFlow = MutableStateFlow?>(null) val onChainFeeratesFlow = MutableStateFlow(null) + // signals when the peer has loaded channels from the database, allowing to differentiate between "channels not yet loaded" and "no channels at all". + private val _initialized = MutableStateFlow(false) + val initialized get() = _initialized.asSharedFlow() + private val _channelLogger = nodeParams.loggerFactory.newLogger(ChannelState::class) private suspend fun ChannelState.process(cmd: ChannelCommand): Pair> { val state = this @@ -209,6 +213,7 @@ class Peer( launch { // we don't restore closed channels val channels = db.channels.listLocalChannels().filterNot { it is Closed }.associateBy { it.channelId } + _initialized.emit(true) channels.values.forEach { logger.info { "restoring channel ${it.channelId} from local storage" } val state = WaitForInit