From 3b21b3238ab02fa0d1b71194c3aeb368d3ceb536 Mon Sep 17 00:00:00 2001 From: Carsten Otto Date: Wed, 24 Nov 2021 10:50:31 +0100 Subject: [PATCH] use aggregated information from fee service --- .../cotto/lndmanagej/service/FeeService.java | 32 ++++++------- .../lndmanagej/service/FeeServiceTest.java | 48 ------------------- .../model/FeeConfigurationTest.java | 12 ++--- .../model/FeeConfigurationFixtures.java | 6 +++ .../ChannelDetailsControllerIT.java | 4 +- .../controller/LegacyControllerIT.java | 39 ++++++++------- .../controller/LegacyController.java | 8 ++-- .../ChannelDetailsControllerTest.java | 4 +- .../controller/LegacyControllerTest.java | 35 +++++++------- .../dto/FeeConfigurationDtoTest.java | 7 +-- 10 files changed, 72 insertions(+), 123 deletions(-) create mode 100644 model/src/testFixtures/java/de/cotto/lndmanagej/model/FeeConfigurationFixtures.java diff --git a/backend/src/main/java/de/cotto/lndmanagej/service/FeeService.java b/backend/src/main/java/de/cotto/lndmanagej/service/FeeService.java index 581a3a91..cd3d1185 100644 --- a/backend/src/main/java/de/cotto/lndmanagej/service/FeeService.java +++ b/backend/src/main/java/de/cotto/lndmanagej/service/FeeService.java @@ -14,22 +14,6 @@ public class FeeService { this.grpcFees = grpcFees; } - public long getIncomingFeeRate(ChannelId channelId) { - return grpcFees.getIncomingFeeRate(channelId).orElseThrow(IllegalStateException::new); - } - - public long getOutgoingFeeRate(ChannelId channelId) { - return grpcFees.getOutgoingFeeRate(channelId).orElseThrow(IllegalStateException::new); - } - - public Coins getOutgoingBaseFee(ChannelId channelId) { - return grpcFees.getOutgoingBaseFee(channelId).orElseThrow(IllegalStateException::new); - } - - public Coins getIncomingBaseFee(ChannelId channelId) { - return grpcFees.getIncomingBaseFee(channelId).orElseThrow(IllegalStateException::new); - } - public FeeConfiguration getFeeConfiguration(ChannelId channelId) { return new FeeConfiguration( getOutgoingFeeRate(channelId), @@ -38,4 +22,20 @@ public class FeeService { getIncomingBaseFee(channelId) ); } + + private long getIncomingFeeRate(ChannelId channelId) { + return grpcFees.getIncomingFeeRate(channelId).orElseThrow(IllegalStateException::new); + } + + private long getOutgoingFeeRate(ChannelId channelId) { + return grpcFees.getOutgoingFeeRate(channelId).orElseThrow(IllegalStateException::new); + } + + private Coins getOutgoingBaseFee(ChannelId channelId) { + return grpcFees.getOutgoingBaseFee(channelId).orElseThrow(IllegalStateException::new); + } + + private Coins getIncomingBaseFee(ChannelId channelId) { + return grpcFees.getIncomingBaseFee(channelId).orElseThrow(IllegalStateException::new); + } } diff --git a/backend/src/test/java/de/cotto/lndmanagej/service/FeeServiceTest.java b/backend/src/test/java/de/cotto/lndmanagej/service/FeeServiceTest.java index fc10336d..f1a31f55 100644 --- a/backend/src/test/java/de/cotto/lndmanagej/service/FeeServiceTest.java +++ b/backend/src/test/java/de/cotto/lndmanagej/service/FeeServiceTest.java @@ -46,52 +46,4 @@ class FeeServiceTest { void getFeeConfiguration_not_found() { assertThatIllegalStateException().isThrownBy(() -> feeService.getFeeConfiguration(CHANNEL_ID)); } - - @Test - void getIncomingFeeRate() { - when(grpcFees.getIncomingFeeRate(CHANNEL_ID)).thenReturn(Optional.of(123L)); - assertThat(feeService.getIncomingFeeRate(CHANNEL_ID)).isEqualTo(123); - } - - @Test - void getIncomingFeeRate_empty() { - when(grpcFees.getIncomingFeeRate(CHANNEL_ID)).thenReturn(Optional.empty()); - assertThatIllegalStateException().isThrownBy(() -> feeService.getIncomingFeeRate(CHANNEL_ID)); - } - - @Test - void getOutgoingFeeRate() { - when(grpcFees.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(Optional.of(123L)); - assertThat(feeService.getOutgoingFeeRate(CHANNEL_ID)).isEqualTo(123); - } - - @Test - void getOutgoingFeeRate_empty() { - when(grpcFees.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(Optional.empty()); - assertThatIllegalStateException().isThrownBy(() -> feeService.getOutgoingFeeRate(CHANNEL_ID)); - } - - @Test - void getIncomingBaseFee() { - when(grpcFees.getIncomingBaseFee(CHANNEL_ID)).thenReturn(Optional.of(Coins.ofMilliSatoshis(123L))); - assertThat(feeService.getIncomingBaseFee(CHANNEL_ID)).isEqualTo(Coins.ofMilliSatoshis(123L)); - } - - @Test - void getIncomingBaseFee_empty() { - when(grpcFees.getIncomingBaseFee(CHANNEL_ID)).thenReturn(Optional.empty()); - assertThatIllegalStateException().isThrownBy(() -> feeService.getIncomingBaseFee(CHANNEL_ID)); - } - - @Test - void getOutgoingBaseFee() { - when(grpcFees.getOutgoingBaseFee(CHANNEL_ID)).thenReturn(Optional.of(Coins.ofMilliSatoshis(123L))); - assertThat(feeService.getOutgoingBaseFee(CHANNEL_ID)).isEqualTo(Coins.ofMilliSatoshis(123L)); - } - - @Test - void getOutgoingBaseFee_empty() { - when(grpcFees.getOutgoingBaseFee(CHANNEL_ID)).thenReturn(Optional.empty()); - assertThatIllegalStateException().isThrownBy(() -> feeService.getOutgoingBaseFee(CHANNEL_ID)); - } } \ No newline at end of file diff --git a/model/src/test/java/de/cotto/lndmanagej/model/FeeConfigurationTest.java b/model/src/test/java/de/cotto/lndmanagej/model/FeeConfigurationTest.java index fe7ef960..75c120c2 100644 --- a/model/src/test/java/de/cotto/lndmanagej/model/FeeConfigurationTest.java +++ b/model/src/test/java/de/cotto/lndmanagej/model/FeeConfigurationTest.java @@ -2,29 +2,27 @@ package de.cotto.lndmanagej.model; import org.junit.jupiter.api.Test; +import static de.cotto.lndmanagej.model.FeeConfigurationFixtures.FEE_CONFIGURATION; import static org.assertj.core.api.Assertions.assertThat; class FeeConfigurationTest { - private final FeeConfiguration feeConfiguration = - new FeeConfiguration(10, Coins.ofMilliSatoshis(20), 30, Coins.ofMilliSatoshis(40)); - @Test void outgoingFeeRate() { - assertThat(feeConfiguration.outgoingFeeRate()).isEqualTo(10L); + assertThat(FEE_CONFIGURATION.outgoingFeeRate()).isEqualTo(1); } @Test void outgoingBaseFee() { - assertThat(feeConfiguration.outgoingBaseFee()).isEqualTo(Coins.ofMilliSatoshis(20)); + assertThat(FEE_CONFIGURATION.outgoingBaseFee()).isEqualTo(Coins.ofMilliSatoshis(2)); } @Test void incomingFeeRate() { - assertThat(feeConfiguration.incomingFeeRate()).isEqualTo(30L); + assertThat(FEE_CONFIGURATION.incomingFeeRate()).isEqualTo(3); } @Test void incomingBaseFee() { - assertThat(feeConfiguration.incomingBaseFee()).isEqualTo(Coins.ofMilliSatoshis(40)); + assertThat(FEE_CONFIGURATION.incomingBaseFee()).isEqualTo(Coins.ofMilliSatoshis(4)); } } \ No newline at end of file diff --git a/model/src/testFixtures/java/de/cotto/lndmanagej/model/FeeConfigurationFixtures.java b/model/src/testFixtures/java/de/cotto/lndmanagej/model/FeeConfigurationFixtures.java new file mode 100644 index 00000000..5873162a --- /dev/null +++ b/model/src/testFixtures/java/de/cotto/lndmanagej/model/FeeConfigurationFixtures.java @@ -0,0 +1,6 @@ +package de.cotto.lndmanagej.model; + +public class FeeConfigurationFixtures { + public static final FeeConfiguration FEE_CONFIGURATION = + new FeeConfiguration(1, Coins.ofMilliSatoshis(2), 3, Coins.ofMilliSatoshis(4)); +} diff --git a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelDetailsControllerIT.java b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelDetailsControllerIT.java index 072ca7c9..c55f999e 100644 --- a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelDetailsControllerIT.java +++ b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelDetailsControllerIT.java @@ -2,7 +2,6 @@ package de.cotto.lndmanagej.controller; import de.cotto.lndmanagej.metrics.Metrics; import de.cotto.lndmanagej.model.Coins; -import de.cotto.lndmanagej.model.FeeConfiguration; import de.cotto.lndmanagej.service.BalanceService; import de.cotto.lndmanagej.service.ChannelService; import de.cotto.lndmanagej.service.FeeService; @@ -20,6 +19,7 @@ import static de.cotto.lndmanagej.model.BalanceInformationFixtures.BALANCE_INFOR import static de.cotto.lndmanagej.model.ChannelFixtures.CAPACITY; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; import static de.cotto.lndmanagej.model.ChannelPointFixtures.CHANNEL_POINT; +import static de.cotto.lndmanagej.model.FeeConfigurationFixtures.FEE_CONFIGURATION; import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL_PRIVATE; import static de.cotto.lndmanagej.model.NodeFixtures.ALIAS_2; import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2; @@ -32,8 +32,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @WebMvcTest(controllers = ChannelDetailsController.class) class ChannelDetailsControllerIT { private static final String CHANNEL_PREFIX = "/api/channel/" + CHANNEL_ID.getShortChannelId(); - private static final FeeConfiguration FEE_CONFIGURATION = - new FeeConfiguration(1, Coins.ofMilliSatoshis(2), 3, Coins.ofMilliSatoshis(4)); @Autowired private MockMvc mockMvc; diff --git a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/LegacyControllerIT.java b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/LegacyControllerIT.java index 0b6c2fdd..7bde52b0 100644 --- a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/LegacyControllerIT.java +++ b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/LegacyControllerIT.java @@ -18,6 +18,7 @@ import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID_3; import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL; import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL_3; +import static de.cotto.lndmanagej.model.FeeConfigurationFixtures.FEE_CONFIGURATION; import static de.cotto.lndmanagej.model.ForceClosingChannelFixtures.FORCE_CLOSING_CHANNEL; import static de.cotto.lndmanagej.model.ForceClosingChannelFixtures.FORCE_CLOSING_CHANNEL_3; import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL; @@ -35,8 +36,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. class LegacyControllerIT { private static final String PUBKEY_BASE = "/legacy/node/" + PUBKEY; private static final String CHANNEL_BASE = "/legacy/channel/" + CHANNEL_ID; - private static final long FEE_RATE = 123L; - private static final Coins BASE_FEE = Coins.ofMilliSatoshis(10L); @Autowired private MockMvc mockMvc; @@ -101,32 +100,32 @@ class LegacyControllerIT { .andExpect(content().string(PUBKEY_2 + "\n" + PUBKEY_3)); } - @Test - void getIncomingFeeRate() throws Exception { - when(feeService.getIncomingFeeRate(CHANNEL_ID)).thenReturn(FEE_RATE); - mockMvc.perform(get(CHANNEL_BASE + "/incoming-fee-rate")) - .andExpect(content().string(Long.toString(FEE_RATE))); - } - @Test void getOutgoingFeeRate() throws Exception { - when(feeService.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(FEE_RATE); + when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FEE_CONFIGURATION); mockMvc.perform(get(CHANNEL_BASE + "/outgoing-fee-rate")) - .andExpect(content().string(Long.toString(FEE_RATE))); - } - - @Test - void getIncomingBaseFee() throws Exception { - when(feeService.getIncomingBaseFee(CHANNEL_ID)).thenReturn(BASE_FEE); - mockMvc.perform(get(CHANNEL_BASE + "/incoming-base-fee")) - .andExpect(content().string(String.valueOf(BASE_FEE.milliSatoshis()))); + .andExpect(content().string("1")); } @Test void getOutgoingBaseFee() throws Exception { - when(feeService.getOutgoingBaseFee(CHANNEL_ID)).thenReturn(BASE_FEE); + when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FEE_CONFIGURATION); mockMvc.perform(get(CHANNEL_BASE + "/outgoing-base-fee")) - .andExpect(content().string(String.valueOf(BASE_FEE.milliSatoshis()))); + .andExpect(content().string("2")); + } + + @Test + void getIncomingFeeRate() throws Exception { + when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FEE_CONFIGURATION); + mockMvc.perform(get(CHANNEL_BASE + "/incoming-fee-rate")) + .andExpect(content().string("3")); + } + + @Test + void getIncomingBaseFee() throws Exception { + when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FEE_CONFIGURATION); + mockMvc.perform(get(CHANNEL_BASE + "/incoming-base-fee")) + .andExpect(content().string("4")); } @Test diff --git a/web/src/main/java/de/cotto/lndmanagej/controller/LegacyController.java b/web/src/main/java/de/cotto/lndmanagej/controller/LegacyController.java index 5a9992b7..1f4e5d8a 100644 --- a/web/src/main/java/de/cotto/lndmanagej/controller/LegacyController.java +++ b/web/src/main/java/de/cotto/lndmanagej/controller/LegacyController.java @@ -108,25 +108,25 @@ public class LegacyController { @GetMapping("/channel/{channelId}/incoming-fee-rate") public long getIncomingFeeRate(@PathVariable ChannelId channelId) { mark("getIncomingFeeRate"); - return feeService.getIncomingFeeRate(channelId); + return feeService.getFeeConfiguration(channelId).incomingFeeRate(); } @GetMapping("/channel/{channelId}/outgoing-fee-rate") public long getOutgoingFeeRate(@PathVariable ChannelId channelId) { mark("getOutgoingFeeRate"); - return feeService.getOutgoingFeeRate(channelId); + return feeService.getFeeConfiguration(channelId).outgoingFeeRate(); } @GetMapping("/channel/{channelId}/incoming-base-fee") public long getIncomingBaseFee(@PathVariable ChannelId channelId) { mark("getIncomingBaseFee"); - return feeService.getIncomingBaseFee(channelId).milliSatoshis(); + return feeService.getFeeConfiguration(channelId).incomingBaseFee().milliSatoshis(); } @GetMapping("/channel/{channelId}/outgoing-base-fee") public long getOutgoingBaseFee(@PathVariable ChannelId channelId) { mark("getOutgoingBaseFee"); - return feeService.getOutgoingBaseFee(channelId).milliSatoshis(); + return feeService.getFeeConfiguration(channelId).outgoingBaseFee().milliSatoshis(); } @GetMapping("/channel/{channelId}/available-local-balance") diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/ChannelDetailsControllerTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/ChannelDetailsControllerTest.java index 5502120d..e73d3189 100644 --- a/web/src/test/java/de/cotto/lndmanagej/controller/ChannelDetailsControllerTest.java +++ b/web/src/test/java/de/cotto/lndmanagej/controller/ChannelDetailsControllerTest.java @@ -5,7 +5,6 @@ import de.cotto.lndmanagej.controller.dto.FeeConfigurationDto; import de.cotto.lndmanagej.controller.dto.OnChainCostsDto; import de.cotto.lndmanagej.metrics.Metrics; import de.cotto.lndmanagej.model.Coins; -import de.cotto.lndmanagej.model.FeeConfiguration; import de.cotto.lndmanagej.service.BalanceService; import de.cotto.lndmanagej.service.ChannelService; import de.cotto.lndmanagej.service.FeeService; @@ -21,6 +20,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.util.Optional; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; +import static de.cotto.lndmanagej.model.FeeConfigurationFixtures.FEE_CONFIGURATION; import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL; import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL_PRIVATE; import static de.cotto.lndmanagej.model.NodeFixtures.ALIAS_2; @@ -37,8 +37,6 @@ class ChannelDetailsControllerTest { private static final Coins OPEN_COSTS = Coins.ofSatoshis(1); private static final Coins CLOSE_COSTS = Coins.ofSatoshis(2); private static final OnChainCostsDto ON_CHAIN_COSTS = new OnChainCostsDto(OPEN_COSTS, CLOSE_COSTS); - private static final FeeConfiguration FEE_CONFIGURATION = - new FeeConfiguration(1, Coins.ofMilliSatoshis(2), 3, Coins.ofMilliSatoshis(4)); private static final FeeConfigurationDto FEE_CONFIGURATION_DTO = FeeConfigurationDto.createFrom(FEE_CONFIGURATION); @InjectMocks diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/LegacyControllerTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/LegacyControllerTest.java index 2b4736a6..4acd67fb 100644 --- a/web/src/test/java/de/cotto/lndmanagej/controller/LegacyControllerTest.java +++ b/web/src/test/java/de/cotto/lndmanagej/controller/LegacyControllerTest.java @@ -2,6 +2,7 @@ package de.cotto.lndmanagej.controller; import de.cotto.lndmanagej.metrics.Metrics; import de.cotto.lndmanagej.model.Coins; +import de.cotto.lndmanagej.model.FeeConfigurationFixtures; import de.cotto.lndmanagej.service.BalanceService; import de.cotto.lndmanagej.service.ChannelService; import de.cotto.lndmanagej.service.FeeService; @@ -166,32 +167,32 @@ class LegacyControllerTest { assertThat(legacyController.getPeerPubkeys()).isEqualTo(PUBKEY_2.toString()); } - @Test - void getIncomingFeeRate() { - when(feeService.getIncomingFeeRate(CHANNEL_ID)).thenReturn(123L); - assertThat(legacyController.getIncomingFeeRate(CHANNEL_ID)).isEqualTo(123); - verify(metrics).mark(argThat(name -> name.endsWith(".getIncomingFeeRate"))); - } - @Test void getOutgoingFeeRate() { - when(feeService.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(123L); - assertThat(legacyController.getOutgoingFeeRate(CHANNEL_ID)).isEqualTo(123); + when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FeeConfigurationFixtures.FEE_CONFIGURATION); + assertThat(legacyController.getOutgoingFeeRate(CHANNEL_ID)).isEqualTo(1); verify(metrics).mark(argThat(name -> name.endsWith(".getOutgoingFeeRate"))); } @Test - void getIncomingBaseFee() { - when(feeService.getIncomingBaseFee(CHANNEL_ID)).thenReturn(Coins.ofMilliSatoshis(10L)); - assertThat(legacyController.getIncomingBaseFee(CHANNEL_ID)).isEqualTo(10); - verify(metrics).mark(argThat(name -> name.endsWith(".getIncomingBaseFee"))); + void getOutgoingBaseFee() { + when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FeeConfigurationFixtures.FEE_CONFIGURATION); + assertThat(legacyController.getOutgoingBaseFee(CHANNEL_ID)).isEqualTo(2); + verify(metrics).mark(argThat(name -> name.endsWith(".getOutgoingBaseFee"))); } @Test - void getOutgoingBaseFee() { - when(feeService.getOutgoingBaseFee(CHANNEL_ID)).thenReturn(Coins.ofMilliSatoshis(10L)); - assertThat(legacyController.getOutgoingBaseFee(CHANNEL_ID)).isEqualTo(10); - verify(metrics).mark(argThat(name -> name.endsWith(".getOutgoingBaseFee"))); + void getIncomingFeeRate() { + when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FeeConfigurationFixtures.FEE_CONFIGURATION); + assertThat(legacyController.getIncomingFeeRate(CHANNEL_ID)).isEqualTo(3); + verify(metrics).mark(argThat(name -> name.endsWith(".getIncomingFeeRate"))); + } + + @Test + void getIncomingBaseFee() { + when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FeeConfigurationFixtures.FEE_CONFIGURATION); + assertThat(legacyController.getIncomingBaseFee(CHANNEL_ID)).isEqualTo(4); + verify(metrics).mark(argThat(name -> name.endsWith(".getIncomingBaseFee"))); } @Test diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDtoTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDtoTest.java index 39712fce..40d83272 100644 --- a/web/src/test/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDtoTest.java +++ b/web/src/test/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDtoTest.java @@ -1,9 +1,8 @@ package de.cotto.lndmanagej.controller.dto; -import de.cotto.lndmanagej.model.Coins; -import de.cotto.lndmanagej.model.FeeConfiguration; import org.junit.jupiter.api.Test; +import static de.cotto.lndmanagej.model.FeeConfigurationFixtures.FEE_CONFIGURATION; import static org.assertj.core.api.Assertions.assertThat; class FeeConfigurationDtoTest { @@ -11,9 +10,7 @@ class FeeConfigurationDtoTest { void createFrom() { FeeConfigurationDto expected = new FeeConfigurationDto(1, 2, 3, 4); - FeeConfiguration feeConfiguration = - new FeeConfiguration(1, Coins.ofMilliSatoshis(2), 3, Coins.ofMilliSatoshis(4)); - FeeConfigurationDto dto = FeeConfigurationDto.createFrom(feeConfiguration); + FeeConfigurationDto dto = FeeConfigurationDto.createFrom(FEE_CONFIGURATION); assertThat(dto).isEqualTo(expected); }