mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-31 19:54:29 +01:00
Merge branch 'main' into fix-5746
This commit is contained in:
@@ -6,6 +6,7 @@ import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.FailureCode;
|
||||
import de.cotto.lndmanagej.model.HexString;
|
||||
import de.cotto.lndmanagej.model.LiquidityChangeListener;
|
||||
import de.cotto.lndmanagej.model.PaymentAttemptHop;
|
||||
import de.cotto.lndmanagej.model.PaymentListener;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
@@ -22,15 +23,18 @@ public class LiquidityInformationUpdater implements PaymentListener {
|
||||
private final GrpcGetInfo grpcGetInfo;
|
||||
private final GrpcChannelPolicy grpcChannelPolicy;
|
||||
private final LiquidityBoundsService liquidityBoundsService;
|
||||
private final List<LiquidityChangeListener> liquidityChangeListeners;
|
||||
|
||||
public LiquidityInformationUpdater(
|
||||
GrpcGetInfo grpcGetInfo,
|
||||
GrpcChannelPolicy grpcChannelPolicy,
|
||||
LiquidityBoundsService liquidityBoundsService
|
||||
LiquidityBoundsService liquidityBoundsService,
|
||||
List<LiquidityChangeListener> liquidityChangeListeners
|
||||
) {
|
||||
this.grpcGetInfo = grpcGetInfo;
|
||||
this.grpcChannelPolicy = grpcChannelPolicy;
|
||||
this.liquidityBoundsService = liquidityBoundsService;
|
||||
this.liquidityChangeListeners = liquidityChangeListeners;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,12 +77,16 @@ public class LiquidityInformationUpdater implements PaymentListener {
|
||||
}
|
||||
|
||||
private void updateInFlight(List<PaymentAttemptHop> paymentAttemptHops, boolean negate) {
|
||||
Pubkey startNode = grpcGetInfo.getPubkey();
|
||||
Pubkey ownNode = grpcGetInfo.getPubkey();
|
||||
Pubkey startNode = ownNode;
|
||||
for (PaymentAttemptHop hop : paymentAttemptHops) {
|
||||
Pubkey endNode = getOtherNode(hop, startNode).orElse(null);
|
||||
if (endNode == null) {
|
||||
return;
|
||||
}
|
||||
if (ownNode.equals(startNode)) {
|
||||
liquidityChangeListeners.forEach(l -> l.amountChanged(endNode));
|
||||
}
|
||||
Coins amount = hop.amount();
|
||||
if (negate) {
|
||||
liquidityBoundsService.markAsInFlight(startNode, endNode, amount.negate());
|
||||
|
||||
@@ -5,6 +5,7 @@ import de.cotto.lndmanagej.grpc.GrpcGetInfo;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.FailureCode;
|
||||
import de.cotto.lndmanagej.model.HexString;
|
||||
import de.cotto.lndmanagej.model.LiquidityChangeListener;
|
||||
import de.cotto.lndmanagej.model.PaymentAttemptHop;
|
||||
import de.cotto.lndmanagej.model.PaymentListener;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -13,7 +14,6 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
@@ -63,7 +63,6 @@ class LiquidityInformationUpdaterTest {
|
||||
new PaymentAttemptHop(Optional.empty(), Coins.ofSatoshis(80), Optional.empty())
|
||||
);
|
||||
|
||||
@InjectMocks
|
||||
private LiquidityInformationUpdater liquidityInformationUpdater;
|
||||
|
||||
@Mock
|
||||
@@ -75,8 +74,17 @@ class LiquidityInformationUpdaterTest {
|
||||
@Mock
|
||||
private LiquidityBoundsService liquidityBoundsService;
|
||||
|
||||
@Mock
|
||||
private LiquidityChangeListener liquidityChangeListener;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
liquidityInformationUpdater = new LiquidityInformationUpdater(
|
||||
grpcGetInfo,
|
||||
grpcChannelPolicy,
|
||||
liquidityBoundsService,
|
||||
List.of(liquidityChangeListener)
|
||||
);
|
||||
lenient().when(grpcGetInfo.getPubkey()).thenReturn(PUBKEY);
|
||||
lenient().when(grpcChannelPolicy.getOtherPubkey(CHANNEL_ID, PUBKEY)).thenReturn(Optional.of(PUBKEY_2));
|
||||
lenient().when(grpcChannelPolicy.getOtherPubkey(CHANNEL_ID_2, PUBKEY_2)).thenReturn(Optional.of(PUBKEY_3));
|
||||
@@ -465,6 +473,18 @@ class LiquidityInformationUpdaterTest {
|
||||
verifyNoMoreInteractions(liquidityBoundsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void notifies_liquidity_change_listener_on_in_flight_change_addition() {
|
||||
liquidityInformationUpdater.forNewPaymentAttempt(hopsWithChannelIdsAndPubkeys);
|
||||
verify(liquidityChangeListener).amountChanged(PUBKEY_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void notifies_liquidity_change_listener_on_in_flight_change_removal() {
|
||||
liquidityInformationUpdater.removeInFlight(hopsWithChannelIdsAndPubkeys);
|
||||
verify(liquidityChangeListener).amountChanged(PUBKEY_2);
|
||||
}
|
||||
|
||||
private void assertAllAvailableForFailureFromFinalNode(FailureCode failureCode) {
|
||||
liquidityInformationUpdater.failure(hopsWithChannelIdsAndPubkeys, failureCode, 3);
|
||||
verify(liquidityBoundsService).markAsAvailable(PUBKEY, PUBKEY_2, Coins.ofSatoshis(100));
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
import de.cotto.lndmanagej.caching.CacheBuilder;
|
||||
import de.cotto.lndmanagej.configuration.ConfigurationService;
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.LiquidityChangeListener;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
import io.grpc.Status;
|
||||
import io.grpc.StatusRuntimeException;
|
||||
@@ -53,7 +54,7 @@ import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@SuppressWarnings("PMD.ExcessiveImports")
|
||||
public class GrpcService extends GrpcBase {
|
||||
public class GrpcService extends GrpcBase implements LiquidityChangeListener {
|
||||
private static final Duration CHANNELS_CACHE_REFRESH = Duration.ofMillis(100);
|
||||
private static final Duration CHANNELS_CACHE_EXPIRY = Duration.ofMillis(200);
|
||||
|
||||
@@ -242,4 +243,9 @@ public class GrpcService extends GrpcBase {
|
||||
public Optional<AddInvoiceResponse> addInvoice(Invoice invoice) {
|
||||
return get(() -> lightningStub.addInvoice(invoice));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void amountChanged(Pubkey peer) {
|
||||
channelsCache.invalidateAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package de.cotto.lndmanagej.model;
|
||||
|
||||
public interface LiquidityChangeListener {
|
||||
void amountChanged(Pubkey peer);
|
||||
}
|
||||
Reference in New Issue
Block a user