mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-24 08:24:20 +01:00
resolve channel id by downloading transactions
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package de.cotto.lndmanagej.service;
|
||||
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.ChannelPoint;
|
||||
import de.cotto.lndmanagej.transactions.model.Transaction;
|
||||
import de.cotto.lndmanagej.transactions.service.TransactionService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class ChannelIdResolver {
|
||||
private final TransactionService transactionService;
|
||||
|
||||
public ChannelIdResolver(TransactionService transactionService) {
|
||||
this.transactionService = transactionService;
|
||||
}
|
||||
|
||||
public Optional<ChannelId> resolve(ChannelPoint channelPoint) {
|
||||
return transactionService.getTransaction(channelPoint.getTransactionHash())
|
||||
.map(transaction -> getChannelId(transaction, channelPoint));
|
||||
}
|
||||
|
||||
private ChannelId getChannelId(Transaction transaction, ChannelPoint channelPoint) {
|
||||
int block = transaction.blockHeight();
|
||||
int transactionIndex = transaction.positionInBlock();
|
||||
int output = channelPoint.getOutput();
|
||||
return ChannelId.fromCompactForm(block + ":" + transactionIndex + ":" + output);
|
||||
}
|
||||
}
|
||||
@@ -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