Merge branch 'main' into fix-5746

This commit is contained in:
Carsten Otto
2022-05-17 16:57:37 +02:00
12 changed files with 120 additions and 16 deletions

View File

@@ -55,6 +55,7 @@ Status: `/api/status/` followed by...
* `all-channels`: the channel IDs of all channels (open, closed, waiting close, ...)
* "pending open channels" are not included, as these do not have an ID, yet!
* `all-channels/pubkeys`: the pubkeys of all peers with at least one channel as defined above
* `known-channels`: the number of known channels in the whole network
Channel specific: `/api/channel/{ID}/` (where `{ID}` is the channel ID) followed by...
* (nothing): basic channel information (open height, remote pubkey, capacity, status, ...)

View File

@@ -0,0 +1,23 @@
package de.cotto.lndmanagej.service;
import de.cotto.lndmanagej.grpc.GrpcGraph;
import org.springframework.stereotype.Service;
import java.util.Set;
@Service
public class GraphService {
private final GrpcGraph grpcGraph;
public GraphService(GrpcGraph grpcGraph) {
this.grpcGraph = grpcGraph;
}
public int getNumberOfChannels() {
return grpcGraph.getChannelEdges().map(Set::size).orElse(0);
}
public void resetCache() {
grpcGraph.resetCache();
}
}

View File

@@ -0,0 +1,42 @@
package de.cotto.lndmanagej.service;
import de.cotto.lndmanagej.grpc.GrpcGraph;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;
import java.util.Set;
import static de.cotto.lndmanagej.model.DirectedChannelEdgeFixtures.CHANNEL_EDGE_WITH_POLICY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class GraphServiceTest {
@InjectMocks
private GraphService graphService;
@Mock
private GrpcGraph grpcGraph;
@Test
void getNumberOfEdges_default() {
assertThat(graphService.getNumberOfChannels()).isEqualTo(0);
}
@Test
void getNumberOfEdges() {
when(grpcGraph.getChannelEdges()).thenReturn(Optional.of(Set.of(CHANNEL_EDGE_WITH_POLICY)));
assertThat(graphService.getNumberOfChannels()).isEqualTo(1);
}
@Test
void resetCache() {
graphService.resetCache();
verify(grpcGraph).resetCache();
}
}

View File

@@ -25,9 +25,10 @@ import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2;
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_3;
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_4;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -146,7 +147,11 @@ class GrpcGraphTest {
@Test
void resetCache() {
assertThatCode(() -> grpcGraph.resetCache()).doesNotThrowAnyException();
when(grpcService.describeGraph()).thenReturn(Optional.of(ChannelGraph.getDefaultInstance()));
grpcGraph.getChannelEdges();
grpcGraph.resetCache();
grpcGraph.getChannelEdges();
verify(grpcService, times(2)).describeGraph();
}
private RoutingPolicy policy(int feeRate, int baseFee, boolean disabled, int timeLockDelta) {

View File

@@ -6,7 +6,6 @@ dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
implementation project(':backend')
implementation project(':pickhardt-payments')
implementation project(':grpc-adapter')
implementation project(':model')
testImplementation testFixtures(project(':model'))
testImplementation testFixtures(project(':pickhardt-payments'))

View File

@@ -3,6 +3,7 @@ package de.cotto.lndmanagej.controller;
import de.cotto.lndmanagej.controller.dto.ObjectMapperConfiguration;
import de.cotto.lndmanagej.model.ChannelIdResolver;
import de.cotto.lndmanagej.service.ChannelService;
import de.cotto.lndmanagej.service.GraphService;
import de.cotto.lndmanagej.service.OwnNodeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,6 +38,10 @@ class ObjectMapperConfigurationIT {
@SuppressWarnings("unused")
private OwnNodeService ownNodeService;
@MockBean
@SuppressWarnings("unused")
private GraphService graphService;
@Test
void output_is_pretty_printed() throws Exception {
when(channelService.getOpenChannels()).thenReturn(Set.of(LOCAL_OPEN_CHANNEL));

View File

@@ -1,13 +1,13 @@
package de.cotto.lndmanagej.controller;
import de.cotto.lndmanagej.controller.dto.ObjectMapperConfiguration;
import de.cotto.lndmanagej.grpc.GrpcGraph;
import de.cotto.lndmanagej.model.ChannelIdResolver;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.HexString;
import de.cotto.lndmanagej.pickhardtpayments.MultiPathPaymentSender;
import de.cotto.lndmanagej.pickhardtpayments.MultiPathPaymentSplitter;
import de.cotto.lndmanagej.pickhardtpayments.model.PaymentStatus;
import de.cotto.lndmanagej.service.GraphService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
@@ -48,7 +48,7 @@ class PickhardtPaymentsControllerIT {
@MockBean
@SuppressWarnings("unused")
private GrpcGraph grpcGraph;
private GraphService graphService;
@MockBean
@SuppressWarnings("unused")

View File

@@ -2,6 +2,7 @@ package de.cotto.lndmanagej.controller;
import de.cotto.lndmanagej.model.ChannelIdResolver;
import de.cotto.lndmanagej.service.ChannelService;
import de.cotto.lndmanagej.service.GraphService;
import de.cotto.lndmanagej.service.OwnNodeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,6 +40,9 @@ class StatusControllerIT {
@MockBean
private OwnNodeService ownNodeService;
@MockBean
private GraphService graphService;
@Test
void isSyncedToChain() throws Exception {
when(ownNodeService.isSyncedToChain()).thenReturn(true);
@@ -96,4 +100,11 @@ class StatusControllerIT {
mockMvc.perform(get(PREFIX + "/all-channels/pubkeys"))
.andExpect(jsonPath("$.pubkeys", is(sortedPubkeys)));
}
}
@Test
void getKnownChannels() throws Exception {
when(graphService.getNumberOfChannels()).thenReturn(123);
mockMvc.perform(get(PREFIX + "/known-channels"))
.andExpect(jsonPath("$", is(123)));
}
}

View File

@@ -2,13 +2,13 @@ package de.cotto.lndmanagej.controller;
import com.codahale.metrics.annotation.Timed;
import de.cotto.lndmanagej.controller.dto.MultiPathPaymentDto;
import de.cotto.lndmanagej.grpc.GrpcGraph;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.pickhardtpayments.MultiPathPaymentSender;
import de.cotto.lndmanagej.pickhardtpayments.MultiPathPaymentSplitter;
import de.cotto.lndmanagej.pickhardtpayments.model.MultiPathPayment;
import de.cotto.lndmanagej.pickhardtpayments.model.PaymentStatus;
import de.cotto.lndmanagej.service.GraphService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -25,18 +25,18 @@ public class PickhardtPaymentsController {
private final MultiPathPaymentSplitter multiPathPaymentSplitter;
private final MultiPathPaymentSender multiPathPaymentSender;
private final PaymentStatusStream paymentStatusStream;
private final GrpcGraph grpcGraph;
private final GraphService graphService;
public PickhardtPaymentsController(
MultiPathPaymentSplitter multiPathPaymentSplitter,
MultiPathPaymentSender multiPathPaymentSender,
PaymentStatusStream paymentStatusStream,
GrpcGraph grpcGraph
GraphService graphService
) {
this.multiPathPaymentSplitter = multiPathPaymentSplitter;
this.multiPathPaymentSender = multiPathPaymentSender;
this.paymentStatusStream = paymentStatusStream;
this.grpcGraph = grpcGraph;
this.graphService = graphService;
}
@Timed
@@ -107,6 +107,6 @@ public class PickhardtPaymentsController {
@Timed
@GetMapping("/reset-graph-cache")
public void resetGraph() {
grpcGraph.resetCache();
graphService.resetCache();
}
}

View File

@@ -9,6 +9,7 @@ import de.cotto.lndmanagej.model.LocalChannel;
import de.cotto.lndmanagej.model.LocalOpenChannel;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.service.ChannelService;
import de.cotto.lndmanagej.service.GraphService;
import de.cotto.lndmanagej.service.OwnNodeService;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
@@ -23,10 +24,12 @@ import java.util.List;
public class StatusController {
private final OwnNodeService ownNodeService;
private final ChannelService channelService;
private final GraphService graphService;
public StatusController(OwnNodeService ownNodeService, ChannelService channelService) {
public StatusController(OwnNodeService ownNodeService, ChannelService channelService, GraphService graphService) {
this.ownNodeService = ownNodeService;
this.channelService = channelService;
this.graphService = graphService;
}
@Timed
@@ -85,4 +88,9 @@ public class StatusController {
return new PubkeysDto(pubkeys);
}
@Timed
@GetMapping("/known-channels")
public int getKnownChannels() {
return graphService.getNumberOfChannels();
}
}

View File

@@ -1,12 +1,12 @@
package de.cotto.lndmanagej.controller;
import de.cotto.lndmanagej.controller.dto.MultiPathPaymentDto;
import de.cotto.lndmanagej.grpc.GrpcGraph;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.HexString;
import de.cotto.lndmanagej.pickhardtpayments.MultiPathPaymentSender;
import de.cotto.lndmanagej.pickhardtpayments.MultiPathPaymentSplitter;
import de.cotto.lndmanagej.pickhardtpayments.model.PaymentStatus;
import de.cotto.lndmanagej.service.GraphService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -45,7 +45,7 @@ class PickhardtPaymentsControllerTest {
private PaymentStatusStream paymentStatusStream;
@Mock
private GrpcGraph grpcGraph;
private GraphService graphService;
private final PaymentStatus paymentStatus = new PaymentStatus(HexString.EMPTY);
@@ -115,6 +115,6 @@ class PickhardtPaymentsControllerTest {
@Test
void resetCache() {
controller.resetGraph();
verify(grpcGraph).resetCache();
verify(graphService).resetCache();
}
}

View File

@@ -5,6 +5,7 @@ import de.cotto.lndmanagej.controller.dto.PubkeysDto;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.service.ChannelService;
import de.cotto.lndmanagej.service.GraphService;
import de.cotto.lndmanagej.service.OwnNodeService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -36,6 +37,9 @@ class StatusControllerTest {
@Mock
private ChannelService channelService;
@Mock
private GraphService graphService;
@Test
void isSyncedToChain() {
when(ownNodeService.isSyncedToChain()).thenReturn(true);
@@ -98,4 +102,10 @@ class StatusControllerTest {
when(ownNodeService.getBlockHeight()).thenReturn(123_456);
assertThat(statusController.getBlockHeight()).isEqualTo(123_456);
}
}
@Test
void getKnownChannels() {
when(graphService.getNumberOfChannels()).thenReturn(123);
assertThat(statusController.getKnownChannels()).isEqualTo(123);
}
}