From 919335370591db5e29fead7b6c662b57b9afdf1d Mon Sep 17 00:00:00 2001 From: Carsten Otto Date: Fri, 12 Nov 2021 11:38:36 +0100 Subject: [PATCH] add synced-to-chain endpoint remove caching from GrpcGetInfo use pubkey instead of node in channel model --- .../controller/LegacyControllerIT.java | 20 ++++- .../controller/LegacyController.java | 16 +++- .../lndmanagej/service/ChannelService.java | 19 +++-- .../lndmanagej/service/OwnNodeService.java | 17 ++++ .../controller/LegacyControllerTest.java | 16 ++++ .../service/ChannelServiceTest.java | 6 +- .../service/OwnNodeServiceTest.java | 40 ++++++++++ .../cotto/lndmanagej/grpc/GrpcChannels.java | 14 ++-- .../de/cotto/lndmanagej/grpc/GrpcGetInfo.java | 79 +++++++----------- .../lndmanagej/grpc/GrpcChannelsTest.java | 12 +-- .../lndmanagej/grpc/GrpcGetInfoTest.java | 80 +++++++------------ .../de/cotto/lndmanagej/model/Channel.java | 34 ++++---- .../cotto/lndmanagej/model/ChannelTest.java | 28 +++---- .../lndmanagej/model/ChannelFixtures.java | 16 ++-- 14 files changed, 227 insertions(+), 170 deletions(-) create mode 100644 application/src/main/java/de/cotto/lndmanagej/service/OwnNodeService.java create mode 100644 application/src/test/java/de/cotto/lndmanagej/service/OwnNodeServiceTest.java diff --git a/application/src/integrationTest/java/de/cotto/lndmanagej/controller/LegacyControllerIT.java b/application/src/integrationTest/java/de/cotto/lndmanagej/controller/LegacyControllerIT.java index 9a284c6f..26d403f7 100644 --- a/application/src/integrationTest/java/de/cotto/lndmanagej/controller/LegacyControllerIT.java +++ b/application/src/integrationTest/java/de/cotto/lndmanagej/controller/LegacyControllerIT.java @@ -2,12 +2,12 @@ package de.cotto.lndmanagej.controller; import de.cotto.lndmanagej.service.ChannelService; import de.cotto.lndmanagej.service.NodeService; +import de.cotto.lndmanagej.service.OwnNodeService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import java.util.List; @@ -18,6 +18,7 @@ import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(controllers = LegacyController.class) class LegacyControllerIT { @@ -30,6 +31,9 @@ class LegacyControllerIT { @MockBean private ChannelService channelService; + @MockBean + private OwnNodeService ownNodeService; + @Test void getAlias() throws Exception { when(nodeService.getAlias(PUBKEY)).thenReturn(ALIAS); @@ -38,7 +42,7 @@ class LegacyControllerIT { @Test void getAlias_error() throws Exception { - mockMvc.perform(get("/legacy/node/xxx/alias")).andExpect(MockMvcResultMatchers.status().isBadRequest()); + mockMvc.perform(get("/legacy/node/xxx/alias")).andExpect(status().isBadRequest()); } @Test @@ -47,4 +51,16 @@ class LegacyControllerIT { mockMvc.perform(get("/legacy/node/" + PUBKEY + "/open-channels")) .andExpect(content().string(CHANNEL_ID + "\n" + CHANNEL_ID_3)); } + + @Test + void isSyncedToChain_true() throws Exception { + when(ownNodeService.isSyncedToChain()).thenReturn(true); + mockMvc.perform(get("/legacy/synced-to-chain")).andExpect(content().string("true")); + } + + @Test + void isSyncedToChain_false() throws Exception { + when(ownNodeService.isSyncedToChain()).thenReturn(false); + mockMvc.perform(get("/legacy/synced-to-chain")).andExpect(content().string("false")); + } } \ No newline at end of file diff --git a/application/src/main/java/de/cotto/lndmanagej/controller/LegacyController.java b/application/src/main/java/de/cotto/lndmanagej/controller/LegacyController.java index 0e2f217f..ec91005c 100644 --- a/application/src/main/java/de/cotto/lndmanagej/controller/LegacyController.java +++ b/application/src/main/java/de/cotto/lndmanagej/controller/LegacyController.java @@ -4,6 +4,7 @@ import de.cotto.lndmanagej.model.ChannelId; import de.cotto.lndmanagej.model.Pubkey; import de.cotto.lndmanagej.service.ChannelService; import de.cotto.lndmanagej.service.NodeService; +import de.cotto.lndmanagej.service.OwnNodeService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -12,26 +13,33 @@ import org.springframework.web.bind.annotation.RestController; import java.util.stream.Collectors; @RestController -@RequestMapping("/legacy/node/{pubkey}/") +@RequestMapping("/legacy") public class LegacyController { private static final String NEWLINE = "\n"; private final NodeService nodeService; private final ChannelService channelService; + private final OwnNodeService ownNodeService; - public LegacyController(NodeService nodeService, ChannelService channelService) { + public LegacyController(NodeService nodeService, ChannelService channelService, OwnNodeService ownNodeService) { this.nodeService = nodeService; this.channelService = channelService; + this.ownNodeService = ownNodeService; } - @GetMapping("/alias") + @GetMapping("/node/{pubkey}/alias") public String getAlias(@PathVariable Pubkey pubkey) { return nodeService.getAlias(pubkey); } - @GetMapping("/open-channels") + @GetMapping("/node/{pubkey}/open-channels") public String getOpenChannelIds(@PathVariable Pubkey pubkey) { return channelService.getOpenChannelsWith(pubkey).stream() .map(ChannelId::toString) .collect(Collectors.joining(NEWLINE)); } + + @GetMapping("/synced-to-chain") + public boolean syncedToChain() { + return ownNodeService.isSyncedToChain(); + } } diff --git a/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java b/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java index f28b9bd4..badc8446 100644 --- a/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java +++ b/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java @@ -21,14 +21,14 @@ public class ChannelService { private static final int CACHE_EXPIRY_MINUTES = 5; private final GrpcChannels grpcChannels; - private final LoadingCache> channelsWithPeerCache; + private final LoadingCache> channelsWithPeerCache; public ChannelService(GrpcChannels grpcChannels) { this.grpcChannels = grpcChannels; - CacheLoader> loader = new CacheLoader<>() { + CacheLoader> loader = new CacheLoader<>() { @Nonnull @Override - public List load(@Nonnull Node peer) { + public List load(@Nonnull Pubkey peer) { return getOpenChannelsWithWithoutCache(peer); } }; @@ -39,17 +39,16 @@ public class ChannelService { } public List getOpenChannelsWith(Pubkey peer) { - Node peerNode = Node.forPubkey(peer); - return getOpenChannelsWith(peerNode); - } - - public List getOpenChannelsWith(Node peer) { return channelsWithPeerCache.getUnchecked(peer); } - public List getOpenChannelsWithWithoutCache(Node peer) { + public List getOpenChannelsWith(Node peer) { + return getOpenChannelsWith(peer.pubkey()); + } + + public List getOpenChannelsWithWithoutCache(Pubkey peer) { return grpcChannels.getChannels().stream() - .filter(c -> c.getNodes().contains(peer)) + .filter(c -> c.getPubkeys().contains(peer)) .map(Channel::getId) .sorted() .collect(Collectors.toList()); diff --git a/application/src/main/java/de/cotto/lndmanagej/service/OwnNodeService.java b/application/src/main/java/de/cotto/lndmanagej/service/OwnNodeService.java new file mode 100644 index 00000000..7dc444eb --- /dev/null +++ b/application/src/main/java/de/cotto/lndmanagej/service/OwnNodeService.java @@ -0,0 +1,17 @@ +package de.cotto.lndmanagej.service; + +import de.cotto.lndmanagej.grpc.GrpcGetInfo; +import org.springframework.stereotype.Component; + +@Component +public class OwnNodeService { + private final GrpcGetInfo grpcGetInfo; + + public OwnNodeService(GrpcGetInfo grpcGetInfo) { + this.grpcGetInfo = grpcGetInfo; + } + + public boolean isSyncedToChain() { + return grpcGetInfo.isSyncedToChain().orElse(false); + } +} diff --git a/application/src/test/java/de/cotto/lndmanagej/controller/LegacyControllerTest.java b/application/src/test/java/de/cotto/lndmanagej/controller/LegacyControllerTest.java index 75ddfa87..cf68ce25 100644 --- a/application/src/test/java/de/cotto/lndmanagej/controller/LegacyControllerTest.java +++ b/application/src/test/java/de/cotto/lndmanagej/controller/LegacyControllerTest.java @@ -2,6 +2,7 @@ package de.cotto.lndmanagej.controller; import de.cotto.lndmanagej.service.ChannelService; import de.cotto.lndmanagej.service.NodeService; +import de.cotto.lndmanagej.service.OwnNodeService; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -28,6 +29,9 @@ class LegacyControllerTest { @Mock private ChannelService channelService; + @Mock + private OwnNodeService ownNodeService; + @Test void getAlias() { when(nodeService.getAlias(PUBKEY)).thenReturn(ALIAS); @@ -41,4 +45,16 @@ class LegacyControllerTest { CHANNEL_ID + "\n" + CHANNEL_ID_3 ); } + + @Test + void syncedToChain() { + when(ownNodeService.isSyncedToChain()).thenReturn(true); + assertThat(legacyController.syncedToChain()).isTrue(); + } + + @Test + void syncedToChain_false() { + when(ownNodeService.isSyncedToChain()).thenReturn(false); + assertThat(legacyController.syncedToChain()).isFalse(); + } } \ No newline at end of file diff --git a/application/src/test/java/de/cotto/lndmanagej/service/ChannelServiceTest.java b/application/src/test/java/de/cotto/lndmanagej/service/ChannelServiceTest.java index a3fe3b4c..f4d7328b 100644 --- a/application/src/test/java/de/cotto/lndmanagej/service/ChannelServiceTest.java +++ b/application/src/test/java/de/cotto/lndmanagej/service/ChannelServiceTest.java @@ -16,10 +16,10 @@ import static de.cotto.lndmanagej.model.ChannelFixtures.CHANNEL_3; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID_2; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID_3; -import static de.cotto.lndmanagej.model.NodeFixtures.NODE; import static de.cotto.lndmanagej.model.NodeFixtures.NODE_2; -import static de.cotto.lndmanagej.model.NodeFixtures.NODE_3; +import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY; import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2; +import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_3; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; @@ -45,7 +45,7 @@ class ChannelServiceTest { @Test void getOpenChannelsWith_ignores_channel_to_other_node() { - Channel channel2 = ChannelFixtures.create(NODE, NODE_3, CHANNEL_ID_2); + Channel channel2 = ChannelFixtures.create(PUBKEY, PUBKEY_3, CHANNEL_ID_2); when(grpcChannels.getChannels()).thenReturn(Set.of(CHANNEL, channel2, CHANNEL_3)); assertThat(channelService.getOpenChannelsWith(NODE_2)).containsExactly(CHANNEL_ID, CHANNEL_ID_3); } diff --git a/application/src/test/java/de/cotto/lndmanagej/service/OwnNodeServiceTest.java b/application/src/test/java/de/cotto/lndmanagej/service/OwnNodeServiceTest.java new file mode 100644 index 00000000..cd212906 --- /dev/null +++ b/application/src/test/java/de/cotto/lndmanagej/service/OwnNodeServiceTest.java @@ -0,0 +1,40 @@ +package de.cotto.lndmanagej.service; + +import de.cotto.lndmanagej.grpc.GrpcGetInfo; +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 static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class OwnNodeServiceTest { + @InjectMocks + private OwnNodeService ownNodeService; + + @Mock + private GrpcGetInfo grpcGetInfo; + + @Test + void isSyncedToChain() { + when(grpcGetInfo.isSyncedToChain()).thenReturn(Optional.of(true)); + assertThat(ownNodeService.isSyncedToChain()).isTrue(); + } + + @Test + void isSyncedToChain_false() { + when(grpcGetInfo.isSyncedToChain()).thenReturn(Optional.of(false)); + assertThat(ownNodeService.isSyncedToChain()).isFalse(); + } + + @Test + void isSyncedToChain_empty() { + when(grpcGetInfo.isSyncedToChain()).thenReturn(Optional.empty()); + assertThat(ownNodeService.isSyncedToChain()).isFalse(); + } +} \ No newline at end of file diff --git a/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcChannels.java b/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcChannels.java index da86c747..3a3c876e 100644 --- a/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcChannels.java +++ b/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcChannels.java @@ -14,30 +14,28 @@ import static java.util.stream.Collectors.toSet; public class GrpcChannels { private final GrpcService grpcService; private final GrpcGetInfo grpcGetInfo; - private final GrpcNodeInfo grpcNodeInfo; public GrpcChannels( GrpcService grpcService, - GrpcGetInfo grpcGetInfo, - GrpcNodeInfo grpcNodeInfo + GrpcGetInfo grpcGetInfo ) { this.grpcService = grpcService; this.grpcGetInfo = grpcGetInfo; - this.grpcNodeInfo = grpcNodeInfo; } public Set getChannels() { + Pubkey ownPubkey = grpcGetInfo.getPubkey().orElseThrow(); return grpcService.getChannels().stream() - .map(this::toChannel) + .map(lndChannel -> toChannel(lndChannel, ownPubkey)) .collect(toSet()); } - private Channel toChannel(lnrpc.Channel lndChannel) { + private Channel toChannel(lnrpc.Channel lndChannel, Pubkey ownPubkey) { return Channel.builder() .withChannelId(ChannelId.fromShortChannelId(lndChannel.getChanId())) .withCapacity(Coins.ofSatoshis(lndChannel.getCapacity())) - .withNode1(grpcGetInfo.getNode()) - .withNode2(grpcNodeInfo.getNode(Pubkey.create(lndChannel.getRemotePubkey()))) + .withNode1(ownPubkey) + .withNode2(Pubkey.create(lndChannel.getRemotePubkey())) .build(); } diff --git a/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcGetInfo.java b/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcGetInfo.java index 7e0f3541..18602f76 100644 --- a/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcGetInfo.java +++ b/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcGetInfo.java @@ -1,93 +1,72 @@ package de.cotto.lndmanagej.grpc; -import de.cotto.lndmanagej.model.Node; import de.cotto.lndmanagej.model.Pubkey; import lnrpc.GetInfoResponse; -import lnrpc.GetInfoResponseOrBuilder; -import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import javax.annotation.Nullable; import java.time.Instant; -import java.util.Objects; +import java.util.Optional; @Component public class GrpcGetInfo { private final GrpcService grpcService; - @Nullable - private GetInfoResponse info; - public GrpcGetInfo(GrpcService grpcService) { this.grpcService = grpcService; - refreshInfo(); } - public Node getNode() { - return Node.builder().withPubkey(getPubkey()).withAlias(getAlias()).build(); + public Optional getPubkey() { + return grpcService.getInfo().map(GetInfoResponse::getIdentityPubkey).map(Pubkey::create); } - public Pubkey getPubkey() { - return Pubkey.create(getInfo().getIdentityPubkey()); + public Optional getAlias() { + return grpcService.getInfo().map(GetInfoResponse::getAlias); } - public String getAlias() { - return getInfo().getAlias(); + public Optional getBlockHeight() { + return grpcService.getInfo().map(GetInfoResponse::getBlockHeight); } - public int getBlockHeight() { - return getInfo().getBlockHeight(); + public Optional getBlockHash() { + return grpcService.getInfo().map(GetInfoResponse::getBlockHash); } - public String getBlockHash() { - return getInfo().getBlockHash(); + public Optional getBestHeaderTimestamp() { + return grpcService.getInfo().map(GetInfoResponse::getBestHeaderTimestamp).map(Instant::ofEpochSecond); } - public Instant getBestHeaderTimestamp() { - return Instant.ofEpochSecond(getInfo().getBestHeaderTimestamp()); + public Optional getVersion() { + return grpcService.getInfo().map(GetInfoResponse::getVersion); } - public String getVersion() { - return getInfo().getVersion(); + public Optional getCommitHash() { + return grpcService.getInfo().map(GetInfoResponse::getCommitHash); } - public String getCommitHash() { - return getInfo().getCommitHash(); + public Optional getNumberOfActiveChannels() { + return grpcService.getInfo().map(GetInfoResponse::getNumActiveChannels); } - public int getNumberOfActiveChannels() { - return getInfo().getNumActiveChannels(); + public Optional getNumberOfInactiveChannels() { + return grpcService.getInfo().map(GetInfoResponse::getNumInactiveChannels); } - public int getNumberOfInactiveChannels() { - return getInfo().getNumInactiveChannels(); + public Optional getNumberOfPendingChannels() { + return grpcService.getInfo().map(GetInfoResponse::getNumPendingChannels); } - public int getNumberOfPendingChannels() { - return getInfo().getNumPendingChannels(); + public Optional getNumberOfPeers() { + return grpcService.getInfo().map(GetInfoResponse::getNumPeers); } - public int getNumberOfPeers() { - return getInfo().getNumPeers(); + @SuppressWarnings("PMD.LinguisticNaming") + public Optional isSyncedToChain() { + return grpcService.getInfo().map(GetInfoResponse::getSyncedToChain); } - public boolean isSyncedToChain() { - return getInfo().getSyncedToChain(); + @SuppressWarnings("PMD.LinguisticNaming") + public Optional isSyncedToGraph() { + return grpcService.getInfo().map(GetInfoResponse::getSyncedToGraph); } - public boolean isSyncedToGraph() { - return getInfo().getSyncedToGraph(); - } - - @Scheduled(fixedDelay = 60_000) - final void refreshInfo() { - grpcService.getInfo().ifPresent(newInfo -> info = newInfo); - } - - private GetInfoResponseOrBuilder getInfo() { - if (info == null) { - refreshInfo(); - } - return Objects.requireNonNull(info); - } } diff --git a/grpc-adapter/src/test/java/de/cotto/lndmanagej/grpc/GrpcChannelsTest.java b/grpc-adapter/src/test/java/de/cotto/lndmanagej/grpc/GrpcChannelsTest.java index 32f6b193..79fe30bc 100644 --- a/grpc-adapter/src/test/java/de/cotto/lndmanagej/grpc/GrpcChannelsTest.java +++ b/grpc-adapter/src/test/java/de/cotto/lndmanagej/grpc/GrpcChannelsTest.java @@ -1,6 +1,7 @@ package de.cotto.lndmanagej.grpc; import lnrpc.Channel; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -8,12 +9,13 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import java.util.List; +import java.util.Optional; import static de.cotto.lndmanagej.model.ChannelFixtures.CAPACITY; import static de.cotto.lndmanagej.model.ChannelFixtures.CHANNEL; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; -import static de.cotto.lndmanagej.model.NodeFixtures.NODE; import static de.cotto.lndmanagej.model.NodeFixtures.NODE_2; +import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; @@ -28,8 +30,10 @@ class GrpcChannelsTest { @Mock private GrpcGetInfo grpcGetInfo; - @Mock - private GrpcNodeInfo grpcNodeInfo; + @BeforeEach + void setUp() { + when(grpcGetInfo.getPubkey()).thenReturn(Optional.of(PUBKEY)); + } @Test void no_channels() { @@ -38,9 +42,7 @@ class GrpcChannelsTest { @Test void one_channel() { - when(grpcGetInfo.getNode()).thenReturn(NODE); when(grpcService.getChannels()).thenReturn(List.of(channel())); - when(grpcNodeInfo.getNode(NODE_2.pubkey())).thenReturn(NODE_2); assertThat(grpcChannels.getChannels()).containsExactly(CHANNEL); } diff --git a/grpc-adapter/src/test/java/de/cotto/lndmanagej/grpc/GrpcGetInfoTest.java b/grpc-adapter/src/test/java/de/cotto/lndmanagej/grpc/GrpcGetInfoTest.java index 26632e6e..f1cfc67c 100644 --- a/grpc-adapter/src/test/java/de/cotto/lndmanagej/grpc/GrpcGetInfoTest.java +++ b/grpc-adapter/src/test/java/de/cotto/lndmanagej/grpc/GrpcGetInfoTest.java @@ -1,6 +1,5 @@ package de.cotto.lndmanagej.grpc; -import de.cotto.lndmanagej.model.Node; import lnrpc.GetInfoResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -31,13 +30,12 @@ class GrpcGetInfoTest { @BeforeEach void setUp() { grpcService = mock(GrpcService.class); - GetInfoResponse response1 = createResponse(BLOCK_HEIGHT, false, true); - GetInfoResponse response2 = createResponse(BLOCK_HEIGHT + 1, true, false); - when(grpcService.getInfo()).thenReturn(Optional.of(response1)).thenReturn(Optional.of(response2)); + GetInfoResponse response = createResponse(false, true); + when(grpcService.getInfo()).thenReturn(Optional.of(response)); grpcGetInfo = new GrpcGetInfo(grpcService); } - private GetInfoResponse createResponse(int blockHeight, boolean syncedToChain, boolean syncedToGraph) { + private GetInfoResponse createResponse(boolean syncedToChain, boolean syncedToGraph) { return GetInfoResponse.newBuilder() .setIdentityPubkey(PUBKEY.toString()) .setAlias(ALIAS) @@ -49,111 +47,95 @@ class GrpcGetInfoTest { .setCommitHash(COMMIT_HASH) .setVersion(VERSION) .setBlockHash(BLOCK_HASH) - .setBlockHeight(blockHeight) + .setBlockHeight(BLOCK_HEIGHT) .setSyncedToChain(syncedToChain) .setSyncedToGraph(syncedToGraph) .build(); } - @Test - void getNode() { - assertThat(grpcGetInfo.getNode()).isEqualTo(Node.builder().withPubkey(PUBKEY).withAlias(ALIAS).build()); - } - @Test void getPubkey() { - assertThat(grpcGetInfo.getPubkey()).isEqualTo(PUBKEY); + assertThat(grpcGetInfo.getPubkey()).contains(PUBKEY); } @Test void getAlias() { - assertThat(grpcGetInfo.getAlias()).isEqualTo(ALIAS); + assertThat(grpcGetInfo.getAlias()).contains(ALIAS); } @Test void getBlockHeight() { - assertThat(grpcGetInfo.getBlockHeight()).isEqualTo(BLOCK_HEIGHT); + assertThat(grpcGetInfo.getBlockHeight()).contains(BLOCK_HEIGHT); } @Test void getBlockHash() { - assertThat(grpcGetInfo.getBlockHash()).isEqualTo(BLOCK_HASH); + assertThat(grpcGetInfo.getBlockHash()).contains(BLOCK_HASH); } @Test void getNumberOfPeers() { - assertThat(grpcGetInfo.getNumberOfPeers()).isEqualTo(NUMBER_OF_PEERS); + assertThat(grpcGetInfo.getNumberOfPeers()).contains(NUMBER_OF_PEERS); } @Test void getNumberOfActiveChannels() { - assertThat(grpcGetInfo.getNumberOfActiveChannels()).isEqualTo(NUMBER_OF_ACTIVE_CHANNELS); + assertThat(grpcGetInfo.getNumberOfActiveChannels()).contains(NUMBER_OF_ACTIVE_CHANNELS); } @Test void getNumberOfInactiveChannels() { - assertThat(grpcGetInfo.getNumberOfInactiveChannels()).isEqualTo(NUMBER_OF_INACTIVE_CHANNELS); + assertThat(grpcGetInfo.getNumberOfInactiveChannels()).contains(NUMBER_OF_INACTIVE_CHANNELS); } @Test void getNumberOfPendingChannels() { - assertThat(grpcGetInfo.getNumberOfPendingChannels()).isEqualTo(NUMBER_OF_PENDING_CHANNELS); + assertThat(grpcGetInfo.getNumberOfPendingChannels()).contains(NUMBER_OF_PENDING_CHANNELS); } @Test void getVersion() { - assertThat(grpcGetInfo.getVersion()).isEqualTo(VERSION); + assertThat(grpcGetInfo.getVersion()).contains(VERSION); } @Test void getCommitHash() { - assertThat(grpcGetInfo.getCommitHash()).isEqualTo(COMMIT_HASH); + assertThat(grpcGetInfo.getCommitHash()).contains(COMMIT_HASH); } @Test void getBestHeaderTimestamp() { - assertThat(grpcGetInfo.getBestHeaderTimestamp()).isEqualTo(BEST_HEADER_INSTANT); + assertThat(grpcGetInfo.getBestHeaderTimestamp()).contains(BEST_HEADER_INSTANT); } @Test - void isSyncedToChain() { - assertThat(grpcGetInfo.isSyncedToChain()).isFalse(); - grpcGetInfo.refreshInfo(); - assertThat(grpcGetInfo.isSyncedToChain()).isTrue(); + void isSyncedToChain_true() { + when(grpcService.getInfo()).thenReturn(Optional.of(createResponse(true, true))); + assertThat(grpcGetInfo.isSyncedToChain()).contains(true); + } + + @Test + void isSyncedToChain_false() { + when(grpcService.getInfo()).thenReturn(Optional.of(createResponse(false, true))); + assertThat(grpcGetInfo.isSyncedToChain()).contains(false); } @Test void isSyncedToGraph_true() { - assertThat(grpcGetInfo.isSyncedToGraph()).isTrue(); - grpcGetInfo.refreshInfo(); - assertThat(grpcGetInfo.isSyncedToGraph()).isFalse(); + when(grpcService.getInfo()).thenReturn(Optional.of(createResponse(true, true))); + assertThat(grpcGetInfo.isSyncedToGraph()).contains(true); } @Test - void caches_response() { - assertThat(grpcGetInfo.getBlockHeight()).isEqualTo(BLOCK_HEIGHT); - assertThat(grpcGetInfo.getBlockHeight()).isEqualTo(BLOCK_HEIGHT); + void isSyncedToGraph_false() { + when(grpcService.getInfo()).thenReturn(Optional.of(createResponse(true, false))); + assertThat(grpcGetInfo.isSyncedToGraph()).contains(false); } @Test - void updates_response() { - assertThat(grpcGetInfo.getBlockHeight()).isEqualTo(BLOCK_HEIGHT); - grpcGetInfo.refreshInfo(); - assertThat(grpcGetInfo.getBlockHeight()).isEqualTo(BLOCK_HEIGHT + 1); - } - - @Test - void does_not_update_response_on_failure() { + void failure() { when(grpcService.getInfo()).thenReturn(Optional.empty()); - grpcGetInfo.refreshInfo(); - assertThat(grpcGetInfo.getBlockHeight()).isEqualTo(BLOCK_HEIGHT); - } - - @Test - void refreshesAfterFailure() { - GetInfoResponse response = createResponse(BLOCK_HEIGHT, false, true); - when(grpcService.getInfo()).thenReturn(Optional.empty()).thenReturn(Optional.of(response)); grpcGetInfo = new GrpcGetInfo(grpcService); - assertThat(grpcGetInfo.getBlockHeight()).isEqualTo(BLOCK_HEIGHT); + assertThat(grpcGetInfo.getBlockHeight()).isEmpty(); } } \ No newline at end of file diff --git a/model/src/main/java/de/cotto/lndmanagej/model/Channel.java b/model/src/main/java/de/cotto/lndmanagej/model/Channel.java index b46dce9d..e909e641 100644 --- a/model/src/main/java/de/cotto/lndmanagej/model/Channel.java +++ b/model/src/main/java/de/cotto/lndmanagej/model/Channel.java @@ -11,13 +11,13 @@ import static java.util.Objects.requireNonNull; public class Channel { private final ChannelId channelId; private final Coins capacity; - private final Set nodes = new LinkedHashSet<>(); + private final Set pubkeys = new LinkedHashSet<>(); - private Channel(ChannelId channelId, Coins capacity, Node node1, Node node2) { + private Channel(ChannelId channelId, Coins capacity, Pubkey pubkey1, Pubkey pubkey2) { this.channelId = channelId; this.capacity = Coins.ofMilliSatoshis(capacity.milliSatoshis()); - nodes.add(node1); - nodes.add(node2); + pubkeys.add(pubkey1); + pubkeys.add(pubkey2); } public static Builder builder() { @@ -32,8 +32,8 @@ public class Channel { return channelId; } - public Set getNodes() { - return nodes; + public Set getPubkeys() { + return pubkeys; } public static class Builder { @@ -44,10 +44,10 @@ public class Channel { private Coins capacity; @Nullable - private Node node1; + private Pubkey pubkey1; @Nullable - private Node node2; + private Pubkey pubkey2; public Builder withChannelId(ChannelId channelId) { this.channelId = channelId; @@ -59,13 +59,13 @@ public class Channel { return this; } - public Builder withNode1(Node node) { - node1 = node; + public Builder withNode1(Pubkey pubkey) { + this.pubkey1 = pubkey; return this; } - public Builder withNode2(Node node) { - node2 = node; + public Builder withNode2(Pubkey pubkey) { + pubkey2 = pubkey; return this; } @@ -73,8 +73,8 @@ public class Channel { return new Channel( requireNonNull(channelId), requireNonNull(capacity), - requireNonNull(node1), - requireNonNull(node2) + requireNonNull(pubkey1), + requireNonNull(pubkey2) ); } } @@ -90,12 +90,12 @@ public class Channel { Channel channel = (Channel) other; return Objects.equals(channelId, channel.channelId) && Objects.equals(capacity, channel.capacity) - && Objects.equals(nodes, channel.nodes); + && Objects.equals(pubkeys, channel.pubkeys); } @Override public int hashCode() { - return Objects.hash(channelId, capacity, nodes); + return Objects.hash(channelId, capacity, pubkeys); } @Override @@ -103,7 +103,7 @@ public class Channel { return "Channel[" + "channelId=" + channelId + ", capacity=" + capacity + - ", nodes=" + nodes + + ", pubkeys=" + pubkeys + ']'; } } diff --git a/model/src/test/java/de/cotto/lndmanagej/model/ChannelTest.java b/model/src/test/java/de/cotto/lndmanagej/model/ChannelTest.java index 0d723600..23f557bd 100644 --- a/model/src/test/java/de/cotto/lndmanagej/model/ChannelTest.java +++ b/model/src/test/java/de/cotto/lndmanagej/model/ChannelTest.java @@ -6,8 +6,8 @@ import org.junit.jupiter.api.Test; import static de.cotto.lndmanagej.model.ChannelFixtures.CAPACITY; import static de.cotto.lndmanagej.model.ChannelFixtures.CHANNEL; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; -import static de.cotto.lndmanagej.model.NodeFixtures.NODE; -import static de.cotto.lndmanagej.model.NodeFixtures.NODE_2; +import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY; +import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatNullPointerException; @@ -24,8 +24,8 @@ class ChannelTest { assertThatNullPointerException().isThrownBy( () -> Channel.builder() .withCapacity(CAPACITY) - .withNode1(NODE) - .withNode2(NODE_2) + .withNode1(PUBKEY) + .withNode2(PUBKEY_2) .build() ); } @@ -35,8 +35,8 @@ class ChannelTest { assertThatNullPointerException().isThrownBy( () -> Channel.builder() .withChannelId(CHANNEL_ID) - .withNode1(NODE) - .withNode2(NODE_2) + .withNode1(PUBKEY) + .withNode2(PUBKEY_2) .build() ); } @@ -47,7 +47,7 @@ class ChannelTest { () -> Channel.builder() .withChannelId(CHANNEL_ID) .withCapacity(CAPACITY) - .withNode2(NODE_2) + .withNode2(PUBKEY_2) .build() ); } @@ -58,7 +58,7 @@ class ChannelTest { () -> Channel.builder() .withChannelId(CHANNEL_ID) .withCapacity(CAPACITY) - .withNode1(NODE) + .withNode1(PUBKEY) .build() ); } @@ -68,8 +68,8 @@ class ChannelTest { Channel channel = Channel.builder() .withChannelId(CHANNEL_ID) .withCapacity(CAPACITY) - .withNode1(NODE) - .withNode2(NODE_2) + .withNode1(PUBKEY) + .withNode2(PUBKEY_2) .build(); assertThat(channel).isEqualTo(CHANNEL); } @@ -81,7 +81,7 @@ class ChannelTest { @Test void getNodes() { - assertThat(CHANNEL.getNodes()).containsExactlyInAnyOrder(NODE, NODE_2); + assertThat(CHANNEL.getPubkeys()).containsExactlyInAnyOrder(PUBKEY, PUBKEY_2); } @Test @@ -99,8 +99,8 @@ class ChannelTest { Channel channel = Channel.builder() .withChannelId(CHANNEL_ID) .withCapacity(CAPACITY) - .withNode1(NODE_2) - .withNode2(NODE) + .withNode1(PUBKEY_2) + .withNode2(PUBKEY) .build(); assertThat(CHANNEL).isEqualTo(channel); } @@ -111,7 +111,7 @@ class ChannelTest { "Channel[" + "channelId=" + CHANNEL_ID + ", capacity=" + CAPACITY + - ", nodes=[" + NODE.toString() + ", " + NODE_2.toString() + "]" + + ", pubkeys=[" + PUBKEY + ", " + PUBKEY_2 + "]" + "]" ); } diff --git a/model/src/testFixtures/java/de/cotto/lndmanagej/model/ChannelFixtures.java b/model/src/testFixtures/java/de/cotto/lndmanagej/model/ChannelFixtures.java index 495339fd..e8043bd5 100644 --- a/model/src/testFixtures/java/de/cotto/lndmanagej/model/ChannelFixtures.java +++ b/model/src/testFixtures/java/de/cotto/lndmanagej/model/ChannelFixtures.java @@ -3,26 +3,26 @@ package de.cotto.lndmanagej.model; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID_2; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID_3; -import static de.cotto.lndmanagej.model.NodeFixtures.NODE; -import static de.cotto.lndmanagej.model.NodeFixtures.NODE_2; +import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY; +import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2; public final class ChannelFixtures { public static final Coins CAPACITY = Coins.ofSatoshis(21_000_000L); - public static final Channel CHANNEL = create(NODE, NODE_2, CHANNEL_ID); - public static final Channel CHANNEL_2 = create(NODE, NODE_2, CHANNEL_ID_2); - public static final Channel CHANNEL_3 = create(NODE, NODE_2, CHANNEL_ID_3); + public static final Channel CHANNEL = create(PUBKEY, PUBKEY_2, CHANNEL_ID); + public static final Channel CHANNEL_2 = create(PUBKEY, PUBKEY_2, CHANNEL_ID_2); + public static final Channel CHANNEL_3 = create(PUBKEY, PUBKEY_2, CHANNEL_ID_3); private ChannelFixtures() { // do not instantiate } - public static Channel create(Node node1, Node node2, ChannelId channelId) { + public static Channel create(Pubkey pubkey1, Pubkey pubkey2, ChannelId channelId) { return Channel.builder() .withChannelId(channelId) .withCapacity(CAPACITY) - .withNode1(node1) - .withNode2(node2) + .withNode1(pubkey1) + .withNode2(pubkey2) .build(); } }