add aggregated balance information to node details

This commit is contained in:
Carsten Otto
2021-11-22 10:23:28 +01:00
parent 0d93411f13
commit 4a245e15f5
12 changed files with 218 additions and 21 deletions

View File

@@ -1,16 +1,19 @@
package de.cotto.lndmanagej.controller;
import com.codahale.metrics.MetricRegistry;
import de.cotto.lndmanagej.controller.dto.BalanceInformationDto;
import de.cotto.lndmanagej.controller.dto.ChannelsForNodeDto;
import de.cotto.lndmanagej.controller.dto.NodeDetailsDto;
import de.cotto.lndmanagej.controller.dto.ObjectMapperConfiguration;
import de.cotto.lndmanagej.controller.dto.OnChainCostsDto;
import de.cotto.lndmanagej.metrics.Metrics;
import de.cotto.lndmanagej.model.BalanceInformation;
import de.cotto.lndmanagej.model.Channel;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.Node;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.service.BalanceService;
import de.cotto.lndmanagej.service.ChannelService;
import de.cotto.lndmanagej.service.NodeService;
import de.cotto.lndmanagej.service.OnChainCostService;
@@ -32,17 +35,20 @@ public class NodeController {
private final Metrics metrics;
private final ChannelService channelService;
private final OnChainCostService onChainCostService;
private final BalanceService balanceService;
public NodeController(
NodeService nodeService,
ChannelService channelService,
Metrics metrics,
OnChainCostService onChainCostService
OnChainCostService onChainCostService,
BalanceService balanceService
) {
this.nodeService = nodeService;
this.metrics = metrics;
this.channelService = channelService;
this.onChainCostService = onChainCostService;
this.balanceService = balanceService;
}
@GetMapping("/alias")
@@ -57,6 +63,7 @@ public class NodeController {
Node node = nodeService.getNode(pubkey);
Coins openCosts = onChainCostService.getOpenCostsWith(pubkey);
Coins closeCosts = onChainCostService.getCloseCostsWith(pubkey);
BalanceInformation balanceInformation = balanceService.getBalanceInformation(pubkey);
return new NodeDetailsDto(
pubkey,
node.alias(),
@@ -65,6 +72,7 @@ public class NodeController {
toSortedList(channelService.getWaitingCloseChannelsFor(pubkey)),
toSortedList(channelService.getForceClosingChannelsFor(pubkey)),
new OnChainCostsDto(openCosts, closeCosts),
BalanceInformationDto.createFrom(balanceInformation),
node.online()
);
}

View File

@@ -0,0 +1,28 @@
package de.cotto.lndmanagej.controller.dto;
import de.cotto.lndmanagej.model.BalanceInformation;
import de.cotto.lndmanagej.model.Coins;
public record BalanceInformationDto(
String localBalance,
String localReserve,
String localAvailable,
String remoteBalance,
String remoteReserve,
String remoteAvailable
) {
public static BalanceInformationDto createFrom(BalanceInformation balanceInformation) {
return new BalanceInformationDto(
toString(balanceInformation.localBalance()),
toString(balanceInformation.localReserve()),
toString(balanceInformation.localAvailable()),
toString(balanceInformation.remoteBalance()),
toString(balanceInformation.remoteReserve()),
toString(balanceInformation.remoteAvailable())
);
}
private static String toString(Coins coins) {
return String.valueOf(coins.satoshis());
}
}

View File

@@ -15,6 +15,7 @@ public record NodeDetailsDto(
List<ChannelId> waitingCloseChannels,
List<ChannelId> pendingForceClosingChannels,
OnChainCostsDto onChainCosts,
BalanceInformationDto balance,
boolean online
) {
}

View File

@@ -2,6 +2,7 @@ package de.cotto.lndmanagej.service;
import de.cotto.lndmanagej.grpc.GrpcChannels;
import de.cotto.lndmanagej.model.BalanceInformation;
import de.cotto.lndmanagej.model.Channel;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.LocalOpenChannel;
@@ -29,7 +30,7 @@ public class BalanceService {
public Coins getAvailableLocalBalance(ChannelId channelId) {
return getBalanceInformation(channelId)
.map(BalanceInformation::availableLocalBalance)
.map(BalanceInformation::localAvailable)
.orElse(Coins.NONE);
}
@@ -42,10 +43,18 @@ public class BalanceService {
public Coins getAvailableRemoteBalance(ChannelId channelId) {
return getBalanceInformation(channelId)
.map(BalanceInformation::availableRemoteBalance)
.map(BalanceInformation::remoteAvailable)
.orElse(Coins.NONE);
}
public BalanceInformation getBalanceInformation(Pubkey pubkey) {
return channelService.getOpenChannelsWith(pubkey).stream()
.map(Channel::getId)
.map(this::getBalanceInformation)
.flatMap(Optional::stream)
.reduce(BalanceInformation.EMPTY, BalanceInformation::add);
}
private Optional<BalanceInformation> getBalanceInformation(ChannelId channelId) {
return grpcChannels.getChannel(channelId)
.map(LocalOpenChannel::getBalanceInformation);