resolve channel id by downloading transactions

This commit is contained in:
Carsten Otto
2021-11-15 17:47:28 +01:00
parent dd6341a18d
commit 19e664ff35
48 changed files with 1083 additions and 56 deletions

View File

@@ -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);
}
}

View File

@@ -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("");
}