Skip to content

Commit

Permalink
Sort amounts in the balance check (#3023)
Browse files Browse the repository at this point in the history
There are no functional changes, we are just sorting the utxos and channel diffs for easier analysis on busy nodes.
  • Loading branch information
pm47 authored Mar 4, 2025
1 parent 37a3f9d commit c7a288b
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,38 @@ private class BalanceActor(context: ActorContext[Command],
)
filteredByStatus.foreach {
case (status, filteredUtxos) =>
val amount = filteredUtxos.map(_.amount.toDouble).sum
log.info(s"we have ${filteredUtxos.length} $status utxos ($amount mBTC)")
val amount = filteredUtxos.map(_.amount.toSatoshi).sum.toMilliBtc
log.info(s"we have {} {} utxos ({})", filteredUtxos.length, status, amount.toBtc)
Monitoring.Metrics.UtxoCount.withTag(Monitoring.Tags.UtxoStatus, status).update(filteredUtxos.length)
Monitoring.Metrics.BitcoinBalance.withTag(Monitoring.Tags.UtxoStatus, status).update(amount)
Monitoring.Metrics.BitcoinBalance.withTag(Monitoring.Tags.UtxoStatus, status).update(amount.toDouble)
}

previousBalance_opt match {
case Some(previousBalance) =>
log.info("on-chain diff={}", balance.onChain.total - previousBalance.onChain.total)
val utxosBefore = previousBalance.onChain.confirmed ++ previousBalance.onChain.unconfirmed
val utxosAfter = balance.onChain.confirmed ++ balance.onChain.unconfirmed
val utxosAdded = utxosAfter -- utxosBefore.keys
val utxosRemoved = utxosBefore -- utxosAfter.keys
utxosAdded.foreach { case (outPoint, amount) => log.info("+ utxo={} amount={}", outPoint, amount) }
utxosRemoved.foreach { case (outPoint, amount) => log.info("- utxo={} amount={}", outPoint, amount) }
utxosAdded
.toList.sortBy(-_._2)
.foreach { case (outPoint, amount) => log.info("+ utxo={} amount={}", outPoint, amount) }
utxosRemoved
.toList.sortBy(-_._2)
.foreach { case (outPoint, amount) => log.info("- utxo={} amount={}", outPoint, amount) }

log.info("off-chain diff={}", balance.offChain.total - previousBalance.offChain.total)
val offchainBalancesBefore = previousBalance.channels.view.mapValues(computeOffChainBalance(previousBalance.knownPreimages, _).total)
val offchainBalancesAfter = balance.channels.view.mapValues(computeOffChainBalance(balance.knownPreimages, _).total)
offchainBalancesAfter
.map { case (channelId, balanceAfter) => (channelId, offchainBalancesBefore.getOrElse(channelId, Btc(0)), balanceAfter) }
.filter { case (_, balanceBefore, balanceAfter) => balanceAfter > balanceBefore }
.foreach { case (channelId, balanceBefore, balanceAfter) => log.info("+ channelId={} amount={}", channelId, balanceAfter - balanceBefore) }
.map { case (channelId, balanceAfter) => (channelId, balanceAfter - offchainBalancesBefore.getOrElse(channelId, Btc(0))) }
.filter { case (_, balanceDiff) => balanceDiff > 0.sat }
.toList.sortBy(-_._2)
.foreach { case (channelId, balanceDiff) => log.info("+ channelId={} amount={}", channelId, balanceDiff) }
offchainBalancesBefore
.map { case (channelId, balanceBefore) => (channelId, balanceBefore, offchainBalancesAfter.getOrElse(channelId, Btc(0))) }
.filter { case (_, balanceBefore, balanceAfter) => balanceBefore > balanceAfter }
.foreach { case (channelId, balanceBefore, balanceAfter) => log.info("- channelId={} amount={}", channelId, balanceBefore - balanceAfter) }
.map { case (channelId, balanceBefore) => (channelId, balanceBefore - offchainBalancesAfter.getOrElse(channelId, Btc(0))) }
.filter { case (_, balanceDiff) => balanceDiff > 0.sat }
.toList.sortBy(-_._2)
.foreach { case (channelId, balanceDiff) => log.info("- channelId={} amount={}", channelId, balanceDiff) }
case None => ()
}

Expand Down

0 comments on commit c7a288b

Please sign in to comment.