From 1dbb913f1693059b88a9daa6928d24ee15e4e4c8 Mon Sep 17 00:00:00 2001 From: pm47 Date: Fri, 23 Jun 2023 15:07:37 +0200 Subject: [PATCH] retrieve feerates everytime we reconnect to electrum And catch errors, so if current Electrum server has a problem we can retry with a new one. --- .../kotlin/fr/acinq/lightning/io/Peer.kt | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt index ac58377af..b5dd6caa9 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt @@ -196,8 +196,8 @@ class Peer( } launch { watcher.client.connectionState.filter { it == Connection.ESTABLISHED }.collect { - // onchain fees are retrieved punctually, when electrum status moves to Connection.ESTABLISHED - // since the application is not running most of the time, and when it is, it will be only for a few minutes, this is good enough. + // Onchain fees are retrieved once when we establish a connection to an electrum server. + // It is acceptable since the application will typically not be running more than a few minutes at a time. // (for a node that is online most of the time things would be different and we would need to re-evaluate onchain fee estimates on a regular basis) updateEstimateFees() } @@ -258,21 +258,24 @@ class Peer( } private suspend fun updateEstimateFees() { - watcher.client.connectionState.filter { it == Connection.ESTABLISHED }.first() - val sortedFees = listOf( - watcher.client.estimateFees(2), - watcher.client.estimateFees(6), - watcher.client.estimateFees(18), - watcher.client.estimateFees(144), - ) - logger.info { "on-chain fees: $sortedFees" } - // TODO: If some feerates are null, we may implement a retry - onChainFeeratesFlow.value = OnChainFeerates( - fundingFeerate = sortedFees[3].feerate ?: FeeratePerKw(FeeratePerByte(2.sat)), - mutualCloseFeerate = sortedFees[2].feerate ?: FeeratePerKw(FeeratePerByte(10.sat)), - claimMainFeerate = sortedFees[1].feerate ?: FeeratePerKw(FeeratePerByte(20.sat)), - fastFeerate = sortedFees[0].feerate ?: FeeratePerKw(FeeratePerByte(50.sat)) - ) + try { + val sortedFees = listOf( + watcher.client.estimateFees(2), + watcher.client.estimateFees(6), + watcher.client.estimateFees(18), + watcher.client.estimateFees(144), + ) + logger.info { "on-chain fees: $sortedFees" } + // TODO: If some feerates are null, we may implement a retry + onChainFeeratesFlow.value = OnChainFeerates( + fundingFeerate = sortedFees[3].feerate ?: FeeratePerKw(FeeratePerByte(2.sat)), + mutualCloseFeerate = sortedFees[2].feerate ?: FeeratePerKw(FeeratePerByte(10.sat)), + claimMainFeerate = sortedFees[1].feerate ?: FeeratePerKw(FeeratePerByte(20.sat)), + fastFeerate = sortedFees[0].feerate ?: FeeratePerKw(FeeratePerByte(50.sat)) + ) + } catch (ex: Throwable) { + logger.error(ex) { "cannot retrieve feerates from electrum" } + } } fun connect() {