directly resolve channel id

This commit is contained in:
Carsten Otto
2021-11-16 18:33:58 +01:00
parent 4d5ba9d2d8
commit b16ae5bba4
26 changed files with 213 additions and 589 deletions

View File

@@ -1,6 +1,7 @@
package de.cotto.lndmanagej.service;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.ChannelIdResolver;
import de.cotto.lndmanagej.model.ChannelPoint;
import de.cotto.lndmanagej.transactions.model.Transaction;
import de.cotto.lndmanagej.transactions.service.TransactionService;
@@ -9,14 +10,15 @@ import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
public class ChannelIdResolver {
public class ChannelIdResolverImpl implements ChannelIdResolver {
private final TransactionService transactionService;
public ChannelIdResolver(TransactionService transactionService) {
public ChannelIdResolverImpl(TransactionService transactionService) {
this.transactionService = transactionService;
}
public Optional<ChannelId> resolve(ChannelPoint channelPoint) {
@Override
public Optional<ChannelId> resolveFromChannelPoint(ChannelPoint channelPoint) {
return transactionService.getTransaction(channelPoint.getTransactionHash())
.map(transaction -> getChannelId(transaction, channelPoint));
}

View File

@@ -7,10 +7,8 @@ import de.cotto.lndmanagej.model.ClosedChannel;
import de.cotto.lndmanagej.model.LocalChannel;
import de.cotto.lndmanagej.model.LocalOpenChannel;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.model.UnresolvedClosedChannel;
import org.springframework.stereotype.Component;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -22,11 +20,9 @@ public class ChannelService {
private final LoadingCache<Object, Set<LocalOpenChannel>> channelsCache;
private final LoadingCache<Object, Set<ClosedChannel>> closedChannelsCache;
private final GrpcChannels grpcChannels;
private final ChannelIdResolver channelIdResolver;
public ChannelService(GrpcChannels grpcChannels, ChannelIdResolver channelIdResolver) {
public ChannelService(GrpcChannels grpcChannels) {
this.grpcChannels = grpcChannels;
this.channelIdResolver = channelIdResolver;
channelsCache = new CacheBuilder()
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
.build(this.grpcChannels::getChannels);
@@ -35,21 +31,6 @@ public class ChannelService {
.build(this::getClosedChannelsWithoutCache);
}
private Set<ClosedChannel> getClosedChannelsWithoutCache() {
return grpcChannels.getUnresolvedClosedChannels().stream()
.map(this::toClosedChannel)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
}
private Optional<ClosedChannel> toClosedChannel(UnresolvedClosedChannel unresolvedClosedChannel) {
if (unresolvedClosedChannel.getId().isUnresolved()) {
return channelIdResolver.resolve(unresolvedClosedChannel.getChannelPoint())
.map(channelId -> ClosedChannel.create(unresolvedClosedChannel, channelId));
}
return Optional.of(ClosedChannel.create(unresolvedClosedChannel));
}
public Set<LocalOpenChannel> getOpenChannels() {
return channelsCache.getUnchecked("");
}
@@ -68,7 +49,11 @@ public class ChannelService {
Stream<LocalOpenChannel> openChannels = getOpenChannelsWith(pubkey).stream();
Stream<ClosedChannel> closedChannels = getClosedChannels().stream()
.filter(c -> c.getRemotePubkey().equals(pubkey));
return Stream.concat(openChannels, closedChannels)
return Stream.of(openChannels, closedChannels).flatMap(s -> s)
.collect(Collectors.toSet());
}
private Set<ClosedChannel> getClosedChannelsWithoutCache() {
return grpcChannels.getClosedChannels();
}
}