Skip to content

Commit

Permalink
Make SwapInManager a peer field
Browse files Browse the repository at this point in the history
And initialize it once we have loaded channels from the DB.
  • Loading branch information
t-bast committed Jun 21, 2023
1 parent a0b96b8 commit fe264cd
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class Peer(
private val _channelsFlow = MutableStateFlow<Map<ByteVector32, ChannelState>>(HashMap())
val channelsFlow: StateFlow<Map<ByteVector32, ChannelState>> get() = _channelsFlow

private var swapInManager: SwapInManager? = null

private var _channels by _channelsFlow
val channels: Map<ByteVector32, ChannelState> get() = _channelsFlow.value

Expand Down Expand Up @@ -227,6 +229,7 @@ class Peer(
_channels = _channels + (it.channelId to state1)
it.channelId
}
swapInManager = SwapInManager(bootChannels, logger)
logger.info { "restored ${channelIds.size} channels" }
launch { watchSwapInWallet() }
launch {
Expand All @@ -237,8 +240,7 @@ class Peer(
logger.info { "checking for timed out htlcs for channels: ${channelIds.joinToString(", ")}" }
channelIds.forEach { input.send(WrappedChannelCommand(it, ChannelCommand.Commitment.CheckHtlcTimeout)) }
}
val swapInManager = SwapInManager(bootChannels, logger)
run(swapInManager)
run()
}
launch {
var previousState = connectionState.value
Expand Down Expand Up @@ -683,14 +685,14 @@ class Peer(
Secp256k1DHFunctions, Chacha20Poly1305CipherFunctions, SHA256HashFunctions
)

private suspend fun run(swapInManager: SwapInManager) {
private suspend fun run() {
logger.info { "peer is active" }
for (event in input) {
processEvent(swapInManager, event)
processEvent(event)
}
}

private suspend fun processEvent(swapInManager: SwapInManager, cmd: PeerCommand) {
private suspend fun processEvent(cmd: PeerCommand) {
when (cmd) {
is BytesReceived -> {
val msg = try {
Expand Down Expand Up @@ -1054,7 +1056,7 @@ class Peer(
}
}
is WalletCommand -> {
swapInManager.process(cmd)?.let { input.send(it) }
swapInManager?.process(cmd)?.let { input.send(it) }
}
is Disconnected -> {
logger.warning { "disconnecting channels" }
Expand Down

0 comments on commit fe264cd

Please sign in to comment.