mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-31 03:34:36 +01:00
resolve channel id by downloading transactions
This commit is contained in:
@@ -3,11 +3,13 @@ package de.cotto.lndmanagej.service;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import de.cotto.lndmanagej.caching.CacheBuilder;
|
||||
import de.cotto.lndmanagej.grpc.GrpcChannels;
|
||||
import de.cotto.lndmanagej.model.ClosedChannel;
|
||||
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;
|
||||
|
||||
@@ -16,22 +18,41 @@ public class ChannelService {
|
||||
private static final int CACHE_EXPIRY_MINUTES = 1;
|
||||
|
||||
private final LoadingCache<Object, Set<LocalOpenChannel>> channelsCache;
|
||||
private final LoadingCache<Object, Set<UnresolvedClosedChannel>> closedChannelsCache;
|
||||
private final LoadingCache<Object, Set<ClosedChannel>> closedChannelsCache;
|
||||
private final GrpcChannels grpcChannels;
|
||||
private final ChannelIdResolver channelIdResolver;
|
||||
|
||||
public ChannelService(GrpcChannels grpcChannels) {
|
||||
public ChannelService(GrpcChannels grpcChannels, ChannelIdResolver channelIdResolver) {
|
||||
this.grpcChannels = grpcChannels;
|
||||
this.channelIdResolver = channelIdResolver;
|
||||
channelsCache = new CacheBuilder()
|
||||
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
|
||||
.build(grpcChannels::getChannels);
|
||||
.build(this.grpcChannels::getChannels);
|
||||
closedChannelsCache = new CacheBuilder()
|
||||
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
|
||||
.build(grpcChannels::getUnresolvedClosedChannels);
|
||||
.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("");
|
||||
}
|
||||
|
||||
public Set<UnresolvedClosedChannel> getClosedChannels() {
|
||||
public Set<ClosedChannel> getClosedChannels() {
|
||||
return closedChannelsCache.getUnchecked("");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user