Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Peer.bootChannels map #485

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,9 @@ class Peer(

private val logger = MDCLogger(nodeParams.loggerFactory.newLogger(this::class), staticMdc = mapOf("remoteNodeId" to remoteNodeId))

// The channels map, as initially loaded from the database at "boot" (on Peer.init).
// As the channelsFlow is unavailable until the electrum connection is up-and-running,
// this may provide useful information for the UI.
private val _bootChannelsFlow = MutableStateFlow<Map<ByteVector32, ChannelState>?>(null)
val bootChannelsFlow: StateFlow<Map<ByteVector32, ChannelState>?> get() = _bootChannelsFlow

// 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<Map<ByteVector32, ChannelState>>(HashMap())
private val _channelsFlow = MutableStateFlow<Map<ByteVector32, ChannelState>>(emptyMap())
val channelsFlow: StateFlow<Map<ByteVector32, ChannelState>> get() = _channelsFlow

private var _channels by _channelsFlow
Expand Down Expand Up @@ -158,6 +152,10 @@ class Peer(
val currentTipFlow = MutableStateFlow<Pair<Int, BlockHeader>?>(null)
val onChainFeeratesFlow = MutableStateFlow<OnChainFeerates?>(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<Boolean>(false)
val initialized get() = _initialized.asSharedFlow()

private val _channelLogger = nodeParams.loggerFactory.newLogger(ChannelState::class)
private suspend fun ChannelState.process(cmd: ChannelCommand): Pair<ChannelState, List<ChannelAction>> {
val state = this
Expand Down Expand Up @@ -215,16 +213,16 @@ class Peer(
launch {
// we don't restore closed channels
val bootChannels = db.channels.listLocalChannels().filterNot { it is Closed }
_bootChannelsFlow.value = bootChannels.associateBy { it.channelId }
val channelIds = bootChannels.map {
_channels = bootChannels.associateBy { it.channelId }
_initialized.emit(true)
bootChannels.forEach {
logger.info { "restoring channel ${it.channelId} from local storage" }
val state = WaitForInit
val (state1, actions) = state.process(ChannelCommand.Init.Restore(it))
processActions(it.channelId, actions)
_channels = _channels + (it.channelId to state1)
it.channelId
}
logger.info { "restored ${channelIds.size} channels" }
logger.info { "restored ${bootChannels.size} channels" }
launch {
watchSwapInWallet(bootChannels)
}
Expand All @@ -233,6 +231,7 @@ class Peer(
// But maybe we were offline for too long and it is why our peer couldn't settle these htlcs in time.
// We give them a bit of time after we reconnect to send us their latest htlc updates.
delay(timeMillis = nodeParams.checkHtlcTimeoutAfterStartupDelaySeconds.toLong() * 1000)
val channelIds = channels.keys
logger.info { "checking for timed out htlcs for channels: ${channelIds.joinToString(", ")}" }
channelIds.forEach { input.send(WrappedChannelCommand(it, ChannelCommand.Commitment.CheckHtlcTimeout)) }
}
Expand Down