From b6f79d7cb7e1808cffe29a3d68f2de52fb7c4d65 Mon Sep 17 00:00:00 2001 From: Carsten Otto Date: Sun, 28 Nov 2021 12:57:02 +0100 Subject: [PATCH] move feeconfiguration to policies (local+remote) --- .../{FeeService.java => PolicyService.java} | 19 ++--- ...erviceTest.java => PolicyServiceTest.java} | 76 +++++++++---------- .../lndmanagej/model/FeeConfiguration.java | 11 --- .../de/cotto/lndmanagej/model/Policies.java | 7 ++ .../de/cotto/lndmanagej/model/Policy.java | 4 + .../model/FeeConfigurationTest.java | 38 ---------- .../cotto/lndmanagej/model/PoliciesTest.java | 20 +++++ .../de/cotto/lndmanagej/model/PolicyTest.java | 23 ++++++ .../model/FeeConfigurationFixtures.java | 13 ---- .../lndmanagej/model/PolicyFixtures.java | 8 ++ .../controller/ChannelControllerIT.java | 38 +++++----- .../controller/ChannelController.java | 30 ++++---- .../controller/dto/ChannelDetailsDto.java | 10 +-- .../controller/dto/FeeConfigurationDto.java | 33 -------- .../controller/dto/PoliciesDto.java | 18 +++++ .../lndmanagej/controller/dto/PolicyDto.java | 24 ++++++ .../controller/ChannelControllerTest.java | 32 ++++---- .../controller/dto/ChannelDetailsDtoTest.java | 4 +- .../dto/FeeConfigurationDtoTest.java | 24 ------ .../controller/dto/PoliciesDtoTest.java | 19 +++++ .../controller/dto/PolicyDtoTest.java | 15 ++++ 21 files changed, 238 insertions(+), 228 deletions(-) rename backend/src/main/java/de/cotto/lndmanagej/service/{FeeService.java => PolicyService.java} (71%) rename backend/src/test/java/de/cotto/lndmanagej/service/{FeeServiceTest.java => PolicyServiceTest.java} (52%) delete mode 100644 model/src/main/java/de/cotto/lndmanagej/model/FeeConfiguration.java create mode 100644 model/src/main/java/de/cotto/lndmanagej/model/Policies.java create mode 100644 model/src/main/java/de/cotto/lndmanagej/model/Policy.java delete mode 100644 model/src/test/java/de/cotto/lndmanagej/model/FeeConfigurationTest.java create mode 100644 model/src/test/java/de/cotto/lndmanagej/model/PoliciesTest.java create mode 100644 model/src/test/java/de/cotto/lndmanagej/model/PolicyTest.java delete mode 100644 model/src/testFixtures/java/de/cotto/lndmanagej/model/FeeConfigurationFixtures.java create mode 100644 model/src/testFixtures/java/de/cotto/lndmanagej/model/PolicyFixtures.java delete mode 100644 web/src/main/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDto.java create mode 100644 web/src/main/java/de/cotto/lndmanagej/controller/dto/PoliciesDto.java create mode 100644 web/src/main/java/de/cotto/lndmanagej/controller/dto/PolicyDto.java delete mode 100644 web/src/test/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDtoTest.java create mode 100644 web/src/test/java/de/cotto/lndmanagej/controller/dto/PoliciesDtoTest.java create mode 100644 web/src/test/java/de/cotto/lndmanagej/controller/dto/PolicyDtoTest.java diff --git a/backend/src/main/java/de/cotto/lndmanagej/service/FeeService.java b/backend/src/main/java/de/cotto/lndmanagej/service/PolicyService.java similarity index 71% rename from backend/src/main/java/de/cotto/lndmanagej/service/FeeService.java rename to backend/src/main/java/de/cotto/lndmanagej/service/PolicyService.java index b7a738da..f386c8da 100644 --- a/backend/src/main/java/de/cotto/lndmanagej/service/FeeService.java +++ b/backend/src/main/java/de/cotto/lndmanagej/service/PolicyService.java @@ -3,25 +3,22 @@ package de.cotto.lndmanagej.service; import de.cotto.lndmanagej.grpc.GrpcFees; import de.cotto.lndmanagej.model.ChannelId; import de.cotto.lndmanagej.model.Coins; -import de.cotto.lndmanagej.model.FeeConfiguration; +import de.cotto.lndmanagej.model.Policies; +import de.cotto.lndmanagej.model.Policy; import org.springframework.stereotype.Component; @Component -public class FeeService { +public class PolicyService { private final GrpcFees grpcFees; - public FeeService(GrpcFees grpcFees) { + public PolicyService(GrpcFees grpcFees) { this.grpcFees = grpcFees; } - public FeeConfiguration getFeeConfiguration(ChannelId channelId) { - return new FeeConfiguration( - getOutgoingFeeRate(channelId), - getOutgoingBaseFee(channelId), - getIncomingFeeRate(channelId), - getIncomingBaseFee(channelId), - isEnabledLocal(channelId), - isEnabledRemote(channelId) + public Policies getPolicies(ChannelId channelId) { + return new Policies( + new Policy(getOutgoingFeeRate(channelId), getOutgoingBaseFee(channelId), isEnabledLocal(channelId)), + new Policy(getIncomingFeeRate(channelId), getIncomingBaseFee(channelId), isEnabledRemote(channelId)) ); } diff --git a/backend/src/test/java/de/cotto/lndmanagej/service/FeeServiceTest.java b/backend/src/test/java/de/cotto/lndmanagej/service/PolicyServiceTest.java similarity index 52% rename from backend/src/test/java/de/cotto/lndmanagej/service/FeeServiceTest.java rename to backend/src/test/java/de/cotto/lndmanagej/service/PolicyServiceTest.java index 63264e65..bfc7f089 100644 --- a/backend/src/test/java/de/cotto/lndmanagej/service/FeeServiceTest.java +++ b/backend/src/test/java/de/cotto/lndmanagej/service/PolicyServiceTest.java @@ -2,7 +2,8 @@ package de.cotto.lndmanagej.service; import de.cotto.lndmanagej.grpc.GrpcFees; import de.cotto.lndmanagej.model.Coins; -import de.cotto.lndmanagej.model.FeeConfiguration; +import de.cotto.lndmanagej.model.Policies; +import de.cotto.lndmanagej.model.Policy; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -17,57 +18,50 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) -class FeeServiceTest { +class PolicyServiceTest { @InjectMocks - private FeeService feeService; + private PolicyService policyService; @Mock private GrpcFees grpcFees; @Test - void getFeeConfiguration() { - FeeConfiguration expected = new FeeConfiguration( - 789, - Coins.ofMilliSatoshis(111), - 123, - Coins.ofMilliSatoshis(456), - true, - false + void getPolicies() { + Policies expected = new Policies( + new Policy(789, Coins.ofMilliSatoshis(111), true), + new Policy(123, Coins.ofMilliSatoshis(456), false) ); + mockFees(); + when(grpcFees.isEnabledLocal(CHANNEL_ID)).thenReturn(Optional.of(true)); + when(grpcFees.isEnabledRemote(CHANNEL_ID)).thenReturn(Optional.of(false)); + + assertThat(policyService.getPolicies(CHANNEL_ID)).isEqualTo(expected); + } + + @Test + void getPolicies_enabled_disabled_swapped() { + Policies expected = new Policies( + new Policy(789, Coins.ofMilliSatoshis(111), false), + new Policy(123, Coins.ofMilliSatoshis(456), true) + ); + + mockFees(); + when(grpcFees.isEnabledLocal(CHANNEL_ID)).thenReturn(Optional.of(false)); + when(grpcFees.isEnabledRemote(CHANNEL_ID)).thenReturn(Optional.of(true)); + + assertThat(policyService.getPolicies(CHANNEL_ID)).isEqualTo(expected); + } + + @Test + void getPolicies_not_found() { + assertThatIllegalStateException().isThrownBy(() -> policyService.getPolicies(CHANNEL_ID)); + } + + private void mockFees() { when(grpcFees.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(Optional.of(789L)); when(grpcFees.getOutgoingBaseFee(CHANNEL_ID)).thenReturn(Optional.of(Coins.ofMilliSatoshis(111))); when(grpcFees.getIncomingFeeRate(CHANNEL_ID)).thenReturn(Optional.of(123L)); when(grpcFees.getIncomingBaseFee(CHANNEL_ID)).thenReturn(Optional.of(Coins.ofMilliSatoshis(456))); - when(grpcFees.isEnabledLocal(CHANNEL_ID)).thenReturn(Optional.of(true)); - when(grpcFees.isEnabledRemote(CHANNEL_ID)).thenReturn(Optional.of(false)); - - assertThat(feeService.getFeeConfiguration(CHANNEL_ID)).isEqualTo(expected); - } - - @Test - void getFeeConfiguration_swapped() { - FeeConfiguration expected = new FeeConfiguration( - 123, - Coins.ofMilliSatoshis(456), - 789, - Coins.ofMilliSatoshis(111), - false, - true - ); - - when(grpcFees.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(Optional.of(123L)); - when(grpcFees.getOutgoingBaseFee(CHANNEL_ID)).thenReturn(Optional.of(Coins.ofMilliSatoshis(456))); - when(grpcFees.getIncomingFeeRate(CHANNEL_ID)).thenReturn(Optional.of(789L)); - when(grpcFees.getIncomingBaseFee(CHANNEL_ID)).thenReturn(Optional.of(Coins.ofMilliSatoshis(111))); - when(grpcFees.isEnabledLocal(CHANNEL_ID)).thenReturn(Optional.of(false)); - when(grpcFees.isEnabledRemote(CHANNEL_ID)).thenReturn(Optional.of(true)); - - assertThat(feeService.getFeeConfiguration(CHANNEL_ID)).isEqualTo(expected); - } - - @Test - void getFeeConfiguration_not_found() { - assertThatIllegalStateException().isThrownBy(() -> feeService.getFeeConfiguration(CHANNEL_ID)); } } \ No newline at end of file diff --git a/model/src/main/java/de/cotto/lndmanagej/model/FeeConfiguration.java b/model/src/main/java/de/cotto/lndmanagej/model/FeeConfiguration.java deleted file mode 100644 index 138e8dd5..00000000 --- a/model/src/main/java/de/cotto/lndmanagej/model/FeeConfiguration.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.cotto.lndmanagej.model; - -public record FeeConfiguration( - long outgoingFeeRate, - Coins outgoingBaseFee, - long incomingFeeRate, - Coins incomingBaseFee, - boolean enabledLocal, - boolean enabledRemote -) { -} diff --git a/model/src/main/java/de/cotto/lndmanagej/model/Policies.java b/model/src/main/java/de/cotto/lndmanagej/model/Policies.java new file mode 100644 index 00000000..dcc2c23b --- /dev/null +++ b/model/src/main/java/de/cotto/lndmanagej/model/Policies.java @@ -0,0 +1,7 @@ +package de.cotto.lndmanagej.model; + +public record Policies( + Policy local, + Policy remote +) { +} diff --git a/model/src/main/java/de/cotto/lndmanagej/model/Policy.java b/model/src/main/java/de/cotto/lndmanagej/model/Policy.java new file mode 100644 index 00000000..2333f279 --- /dev/null +++ b/model/src/main/java/de/cotto/lndmanagej/model/Policy.java @@ -0,0 +1,4 @@ +package de.cotto.lndmanagej.model; + +public record Policy(long feeRate, Coins baseFee, boolean enabled) { +} diff --git a/model/src/test/java/de/cotto/lndmanagej/model/FeeConfigurationTest.java b/model/src/test/java/de/cotto/lndmanagej/model/FeeConfigurationTest.java deleted file mode 100644 index cbaf7e52..00000000 --- a/model/src/test/java/de/cotto/lndmanagej/model/FeeConfigurationTest.java +++ /dev/null @@ -1,38 +0,0 @@ -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 { - @Test - void outgoingFeeRate() { - assertThat(FEE_CONFIGURATION.outgoingFeeRate()).isEqualTo(1); - } - - @Test - void outgoingBaseFee() { - assertThat(FEE_CONFIGURATION.outgoingBaseFee()).isEqualTo(Coins.ofMilliSatoshis(2)); - } - - @Test - void incomingFeeRate() { - assertThat(FEE_CONFIGURATION.incomingFeeRate()).isEqualTo(3); - } - - @Test - void incomingBaseFee() { - assertThat(FEE_CONFIGURATION.incomingBaseFee()).isEqualTo(Coins.ofMilliSatoshis(4)); - } - - @Test - void enabledLocal() { - assertThat(FEE_CONFIGURATION.enabledLocal()).isFalse(); - } - - @Test - void enabledRemote() { - assertThat(FEE_CONFIGURATION.enabledRemote()).isTrue(); - } -} \ No newline at end of file diff --git a/model/src/test/java/de/cotto/lndmanagej/model/PoliciesTest.java b/model/src/test/java/de/cotto/lndmanagej/model/PoliciesTest.java new file mode 100644 index 00000000..809aa00a --- /dev/null +++ b/model/src/test/java/de/cotto/lndmanagej/model/PoliciesTest.java @@ -0,0 +1,20 @@ +package de.cotto.lndmanagej.model; + +import org.junit.jupiter.api.Test; + +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICIES; +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICY_1; +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICY_2; +import static org.assertj.core.api.Assertions.assertThat; + +class PoliciesTest { + @Test + void local() { + assertThat(POLICIES.local()).isEqualTo(POLICY_1); + } + + @Test + void remote() { + assertThat(POLICIES.remote()).isEqualTo(POLICY_2); + } +} diff --git a/model/src/test/java/de/cotto/lndmanagej/model/PolicyTest.java b/model/src/test/java/de/cotto/lndmanagej/model/PolicyTest.java new file mode 100644 index 00000000..988992ae --- /dev/null +++ b/model/src/test/java/de/cotto/lndmanagej/model/PolicyTest.java @@ -0,0 +1,23 @@ +package de.cotto.lndmanagej.model; + +import org.junit.jupiter.api.Test; + +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICY_1; +import static org.assertj.core.api.Assertions.assertThat; + +class PolicyTest { + @Test + void feeRate() { + assertThat(POLICY_1.feeRate()).isEqualTo(100L); + } + + @Test + void baseFee() { + assertThat(POLICY_1.baseFee()).isEqualTo(Coins.ofMilliSatoshis(10)); + } + + @Test + void enabled() { + assertThat(POLICY_1.enabled()).isFalse(); + } +} \ 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 deleted file mode 100644 index ddb11de0..00000000 --- a/model/src/testFixtures/java/de/cotto/lndmanagej/model/FeeConfigurationFixtures.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.cotto.lndmanagej.model; - -public class FeeConfigurationFixtures { - public static final FeeConfiguration FEE_CONFIGURATION = - new FeeConfiguration( - 1, - Coins.ofMilliSatoshis(2), - 3, - Coins.ofMilliSatoshis(4), - false, - true - ); -} diff --git a/model/src/testFixtures/java/de/cotto/lndmanagej/model/PolicyFixtures.java b/model/src/testFixtures/java/de/cotto/lndmanagej/model/PolicyFixtures.java new file mode 100644 index 00000000..5a6caa1b --- /dev/null +++ b/model/src/testFixtures/java/de/cotto/lndmanagej/model/PolicyFixtures.java @@ -0,0 +1,8 @@ +package de.cotto.lndmanagej.model; + +public class PolicyFixtures { + public static final Policy POLICY_1 = new Policy(100, Coins.ofMilliSatoshis(10), false); + public static final Policy POLICY_2 = new Policy(222, Coins.ofMilliSatoshis(0), true); + + public static final Policies POLICIES = new Policies(POLICY_1, POLICY_2); +} diff --git a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelControllerIT.java b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelControllerIT.java index 7d9f7d00..adc7c2ee 100644 --- a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelControllerIT.java +++ b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelControllerIT.java @@ -4,9 +4,9 @@ import de.cotto.lndmanagej.metrics.Metrics; import de.cotto.lndmanagej.model.Coins; import de.cotto.lndmanagej.service.BalanceService; import de.cotto.lndmanagej.service.ChannelService; -import de.cotto.lndmanagej.service.FeeService; import de.cotto.lndmanagej.service.NodeService; import de.cotto.lndmanagej.service.OnChainCostService; +import de.cotto.lndmanagej.service.PolicyService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -20,7 +20,6 @@ import static de.cotto.lndmanagej.model.ChannelFixtures.CAPACITY; 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.ChannelPointFixtures.CHANNEL_POINT; -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_2; import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL_PRIVATE; @@ -29,6 +28,7 @@ import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.TOTAL_RECEIVED_ import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.TOTAL_SENT; import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.TOTAL_SENT_2; import static de.cotto.lndmanagej.model.NodeFixtures.ALIAS_2; +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICIES; import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2; import static org.hamcrest.core.Is.is; import static org.mockito.Mockito.when; @@ -60,7 +60,7 @@ class ChannelControllerIT { private BalanceService balanceService; @MockBean - private FeeService feeService; + private PolicyService policyService; @Test void getBasicInformation_not_found() throws Exception { @@ -96,7 +96,7 @@ class ChannelControllerIT { @Test void getChannelDetails() throws Exception { - when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FEE_CONFIGURATION); + when(policyService.getPolicies(CHANNEL_ID)).thenReturn(POLICIES); when(nodeService.getAlias(PUBKEY_2)).thenReturn(ALIAS_2); when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL_PRIVATE)); when(onChainCostService.getOpenCosts(CHANNEL_ID)).thenReturn(Optional.of(Coins.ofSatoshis(1000))); @@ -126,12 +126,12 @@ class ChannelControllerIT { .andExpect(jsonPath("$.balance.remoteBalance", is("223"))) .andExpect(jsonPath("$.balance.remoteReserve", is("20"))) .andExpect(jsonPath("$.balance.remoteAvailable", is("203"))) - .andExpect(jsonPath("$.feeConfiguration.enabledLocal", is(false))) - .andExpect(jsonPath("$.feeConfiguration.enabledRemote", is(true))) - .andExpect(jsonPath("$.feeConfiguration.outgoingFeeRatePpm", is(1))) - .andExpect(jsonPath("$.feeConfiguration.outgoingBaseFeeMilliSat", is(2))) - .andExpect(jsonPath("$.feeConfiguration.incomingFeeRatePpm", is(3))) - .andExpect(jsonPath("$.feeConfiguration.incomingBaseFeeMilliSat", is(4))); + .andExpect(jsonPath("$.policies.local.enabled", is(false))) + .andExpect(jsonPath("$.policies.remote.enabled", is(true))) + .andExpect(jsonPath("$.policies.local.feeRatePpm", is(100))) + .andExpect(jsonPath("$.policies.local.baseFeeMilliSat", is(10))) + .andExpect(jsonPath("$.policies.remote.feeRatePpm", is(222))) + .andExpect(jsonPath("$.policies.remote.baseFeeMilliSat", is(0))); } @Test @@ -154,15 +154,15 @@ class ChannelControllerIT { } @Test - void getFeeConfiguration() throws Exception { + void getPolicies() throws Exception { when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL)); - when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FEE_CONFIGURATION); - mockMvc.perform(get(CHANNEL_PREFIX + "/fee-configuration")) - .andExpect(jsonPath("$.outgoingFeeRatePpm", is(1))) - .andExpect(jsonPath("$.outgoingBaseFeeMilliSat", is(2))) - .andExpect(jsonPath("$.incomingFeeRatePpm", is(3))) - .andExpect(jsonPath("$.incomingBaseFeeMilliSat", is(4))) - .andExpect(jsonPath("$.enabledLocal", is(false))) - .andExpect(jsonPath("$.enabledRemote", is(true))); + when(policyService.getPolicies(CHANNEL_ID)).thenReturn(POLICIES); + mockMvc.perform(get(CHANNEL_PREFIX + "/policies")) + .andExpect(jsonPath("$.local.feeRatePpm", is(100))) + .andExpect(jsonPath("$.local.baseFeeMilliSat", is(10))) + .andExpect(jsonPath("$.remote.feeRatePpm", is(222))) + .andExpect(jsonPath("$.remote.baseFeeMilliSat", is(0))) + .andExpect(jsonPath("$.local.enabled", is(false))) + .andExpect(jsonPath("$.remote.enabled", is(true))); } } \ No newline at end of file diff --git a/web/src/main/java/de/cotto/lndmanagej/controller/ChannelController.java b/web/src/main/java/de/cotto/lndmanagej/controller/ChannelController.java index 7a41ff99..ee857fde 100644 --- a/web/src/main/java/de/cotto/lndmanagej/controller/ChannelController.java +++ b/web/src/main/java/de/cotto/lndmanagej/controller/ChannelController.java @@ -4,22 +4,22 @@ import com.codahale.metrics.MetricRegistry; import de.cotto.lndmanagej.controller.dto.BalanceInformationDto; import de.cotto.lndmanagej.controller.dto.ChannelDetailsDto; import de.cotto.lndmanagej.controller.dto.ChannelDto; -import de.cotto.lndmanagej.controller.dto.FeeConfigurationDto; import de.cotto.lndmanagej.controller.dto.ObjectMapperConfiguration; import de.cotto.lndmanagej.controller.dto.OnChainCostsDto; +import de.cotto.lndmanagej.controller.dto.PoliciesDto; import de.cotto.lndmanagej.metrics.Metrics; import de.cotto.lndmanagej.model.BalanceInformation; import de.cotto.lndmanagej.model.ChannelId; import de.cotto.lndmanagej.model.Coins; -import de.cotto.lndmanagej.model.FeeConfiguration; import de.cotto.lndmanagej.model.LocalChannel; import de.cotto.lndmanagej.model.OpenCloseStatus; +import de.cotto.lndmanagej.model.Policies; import de.cotto.lndmanagej.model.Pubkey; import de.cotto.lndmanagej.service.BalanceService; import de.cotto.lndmanagej.service.ChannelService; -import de.cotto.lndmanagej.service.FeeService; import de.cotto.lndmanagej.service.NodeService; import de.cotto.lndmanagej.service.OnChainCostService; +import de.cotto.lndmanagej.service.PolicyService; import org.springframework.context.annotation.Import; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -37,14 +37,14 @@ public class ChannelController { private final Metrics metrics; private final BalanceService balanceService; private final OnChainCostService onChainCostService; - private final FeeService feeService; + private final PolicyService policyService; public ChannelController( ChannelService channelService, NodeService nodeService, BalanceService balanceService, OnChainCostService onChainCostService, - FeeService feeService, + PolicyService policyService, Metrics metrics ) { this.channelService = channelService; @@ -52,7 +52,7 @@ public class ChannelController { this.balanceService = balanceService; this.onChainCostService = onChainCostService; this.metrics = metrics; - this.feeService = feeService; + this.policyService = policyService; } @GetMapping("/") @@ -79,7 +79,7 @@ public class ChannelController { remoteAlias, getBalanceInformation(channelId), getOnChainCosts(channelId), - getFeeConfiguration(localChannel) + getPolicies(localChannel) ); } @@ -91,19 +91,19 @@ public class ChannelController { return BalanceInformationDto.createFrom(balanceInformation); } - @GetMapping("/fee-configuration") - public FeeConfigurationDto getFeeConfiguration(@PathVariable ChannelId channelId) { - metrics.mark(MetricRegistry.name(getClass(), "getFeeConfiguration")); + @GetMapping("/policies") + public PoliciesDto getPolicies(@PathVariable ChannelId channelId) { + metrics.mark(MetricRegistry.name(getClass(), "getPolicies")); LocalChannel localChannel = channelService.getLocalChannel(channelId).orElse(null); - return getFeeConfiguration(localChannel); + return getPolicies(localChannel); } - private FeeConfigurationDto getFeeConfiguration(@Nullable LocalChannel channel) { + private PoliciesDto getPolicies(@Nullable LocalChannel channel) { if (channel == null || channel.getStatus().openCloseStatus() != OpenCloseStatus.OPEN) { - return FeeConfigurationDto.EMPTY; + return PoliciesDto.EMPTY; } - FeeConfiguration feeConfiguration = feeService.getFeeConfiguration(channel.getId()); - return FeeConfigurationDto.createFrom(feeConfiguration); + Policies policies = policyService.getPolicies(channel.getId()); + return PoliciesDto.createFrom(policies); } private BalanceInformation getBalanceInformation(ChannelId channelId) { diff --git a/web/src/main/java/de/cotto/lndmanagej/controller/dto/ChannelDetailsDto.java b/web/src/main/java/de/cotto/lndmanagej/controller/dto/ChannelDetailsDto.java index 0031a24b..eb603ee0 100644 --- a/web/src/main/java/de/cotto/lndmanagej/controller/dto/ChannelDetailsDto.java +++ b/web/src/main/java/de/cotto/lndmanagej/controller/dto/ChannelDetailsDto.java @@ -21,14 +21,14 @@ public record ChannelDetailsDto( ChannelStatusDto status, BalanceInformationDto balance, OnChainCostsDto onChainCosts, - FeeConfigurationDto feeConfiguration + PoliciesDto policies ) { public ChannelDetailsDto( ChannelDto channelDto, String remoteAlias, BalanceInformation balanceInformation, OnChainCostsDto onChainCosts, - FeeConfigurationDto feeConfiguration + PoliciesDto policies ) { this( channelDto.channelIdShort(), @@ -45,7 +45,7 @@ public record ChannelDetailsDto( channelDto.status(), BalanceInformationDto.createFrom(balanceInformation), onChainCosts, - feeConfiguration + policies ); } @@ -54,14 +54,14 @@ public record ChannelDetailsDto( String remoteAlias, BalanceInformation balanceInformation, OnChainCostsDto onChainCosts, - FeeConfigurationDto feeConfiguration + PoliciesDto policies ) { this( new ChannelDto(localChannel), remoteAlias, balanceInformation, onChainCosts, - feeConfiguration + policies ); } } diff --git a/web/src/main/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDto.java b/web/src/main/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDto.java deleted file mode 100644 index 270a0a4a..00000000 --- a/web/src/main/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDto.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.cotto.lndmanagej.controller.dto; - -import de.cotto.lndmanagej.model.FeeConfiguration; - -public record FeeConfigurationDto( - long outgoingFeeRatePpm, - long outgoingBaseFeeMilliSat, - long incomingFeeRatePpm, - long incomingBaseFeeMilliSat, - boolean enabledLocal, - boolean enabledRemote -) { - public static final FeeConfigurationDto EMPTY = - new FeeConfigurationDto( - 0, - 0, - 0, - 0, - false, - false - ); - - public static FeeConfigurationDto createFrom(FeeConfiguration feeConfiguration) { - return new FeeConfigurationDto( - feeConfiguration.outgoingFeeRate(), - feeConfiguration.outgoingBaseFee().milliSatoshis(), - feeConfiguration.incomingFeeRate(), - feeConfiguration.incomingBaseFee().milliSatoshis(), - feeConfiguration.enabledLocal(), - feeConfiguration.enabledRemote() - ); - } -} diff --git a/web/src/main/java/de/cotto/lndmanagej/controller/dto/PoliciesDto.java b/web/src/main/java/de/cotto/lndmanagej/controller/dto/PoliciesDto.java new file mode 100644 index 00000000..3717da57 --- /dev/null +++ b/web/src/main/java/de/cotto/lndmanagej/controller/dto/PoliciesDto.java @@ -0,0 +1,18 @@ +package de.cotto.lndmanagej.controller.dto; + +import de.cotto.lndmanagej.model.Policies; + +public record PoliciesDto( + PolicyDto local, + PolicyDto remote +) { + public static final PoliciesDto EMPTY = + new PoliciesDto(PolicyDto.EMPTY, PolicyDto.EMPTY); + + public static PoliciesDto createFrom(Policies policies) { + return new PoliciesDto( + PolicyDto.createFrom(policies.local()), + PolicyDto.createFrom(policies.remote()) + ); + } +} diff --git a/web/src/main/java/de/cotto/lndmanagej/controller/dto/PolicyDto.java b/web/src/main/java/de/cotto/lndmanagej/controller/dto/PolicyDto.java new file mode 100644 index 00000000..ff2c8781 --- /dev/null +++ b/web/src/main/java/de/cotto/lndmanagej/controller/dto/PolicyDto.java @@ -0,0 +1,24 @@ +package de.cotto.lndmanagej.controller.dto; + +import de.cotto.lndmanagej.model.Policy; + +public record PolicyDto( + long feeRatePpm, + long baseFeeMilliSat, + boolean enabled +) { + public static final PolicyDto EMPTY = + new PolicyDto( + 0, + 0, + false + ); + + public static PolicyDto createFrom(Policy policy) { + return new PolicyDto( + policy.feeRate(), + policy.baseFee().milliSatoshis(), + policy.enabled() + ); + } +} diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/ChannelControllerTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/ChannelControllerTest.java index 1f357eea..8e14ad5a 100644 --- a/web/src/test/java/de/cotto/lndmanagej/controller/ChannelControllerTest.java +++ b/web/src/test/java/de/cotto/lndmanagej/controller/ChannelControllerTest.java @@ -3,17 +3,17 @@ package de.cotto.lndmanagej.controller; import de.cotto.lndmanagej.controller.dto.BalanceInformationDto; import de.cotto.lndmanagej.controller.dto.ChannelDetailsDto; import de.cotto.lndmanagej.controller.dto.ChannelDto; -import de.cotto.lndmanagej.controller.dto.FeeConfigurationDto; import de.cotto.lndmanagej.controller.dto.OnChainCostsDto; +import de.cotto.lndmanagej.controller.dto.PoliciesDto; import de.cotto.lndmanagej.metrics.Metrics; import de.cotto.lndmanagej.model.BalanceInformation; import de.cotto.lndmanagej.model.Coins; import de.cotto.lndmanagej.model.LocalChannel; import de.cotto.lndmanagej.service.BalanceService; import de.cotto.lndmanagej.service.ChannelService; -import de.cotto.lndmanagej.service.FeeService; import de.cotto.lndmanagej.service.NodeService; import de.cotto.lndmanagej.service.OnChainCostService; +import de.cotto.lndmanagej.service.PolicyService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -26,10 +26,10 @@ import java.util.Optional; import static de.cotto.lndmanagej.model.BalanceInformationFixtures.BALANCE_INFORMATION; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL; -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; +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICIES; import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2; import static de.cotto.lndmanagej.model.WaitingCloseChannelFixtures.WAITING_CLOSE_CHANNEL; import static org.assertj.core.api.Assertions.assertThat; @@ -44,7 +44,7 @@ class ChannelControllerTest { 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 FeeConfigurationDto FEE_CONFIGURATION_DTO = FeeConfigurationDto.createFrom(FEE_CONFIGURATION); + private static final PoliciesDto FEE_CONFIGURATION_DTO = PoliciesDto.createFrom(POLICIES); @InjectMocks private ChannelController channelController; @@ -65,13 +65,13 @@ class ChannelControllerTest { private OnChainCostService onChainCostService; @Mock - private FeeService feeService; + private PolicyService policyService; @BeforeEach void setUp() { lenient().when(onChainCostService.getOpenCosts(CHANNEL_ID)).thenReturn(Optional.of(OPEN_COSTS)); lenient().when(onChainCostService.getCloseCosts(CHANNEL_ID)).thenReturn(Optional.of(CLOSE_COSTS)); - lenient().when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FEE_CONFIGURATION); + lenient().when(policyService.getPolicies(CHANNEL_ID)).thenReturn(POLICIES); } @Test @@ -131,13 +131,13 @@ class ChannelControllerTest { @Test void getDetails_closed() throws NotFoundException { - ChannelDetailsDto expectedDetails = mockForChannelWithoutFeeConfiguration(CLOSED_CHANNEL); + ChannelDetailsDto expectedDetails = mockForChannelWithoutPolicies(CLOSED_CHANNEL); assertThat(channelController.getDetails(CHANNEL_ID)).isEqualTo(expectedDetails); } @Test void getDetails_waiting_close() throws NotFoundException { - ChannelDetailsDto expectedDetails = mockForChannelWithoutFeeConfiguration(WAITING_CLOSE_CHANNEL); + ChannelDetailsDto expectedDetails = mockForChannelWithoutPolicies(WAITING_CLOSE_CHANNEL); assertThat(channelController.getDetails(CHANNEL_ID)).isEqualTo(expectedDetails); } @@ -158,19 +158,19 @@ class ChannelControllerTest { } @Test - void getFeeConfiguration() { + void getPolicies() { when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL)); - when(feeService.getFeeConfiguration(CHANNEL_ID)).thenReturn(FEE_CONFIGURATION); - assertThat(channelController.getFeeConfiguration(CHANNEL_ID)).isEqualTo(FEE_CONFIGURATION_DTO); - verify(metrics).mark(argThat(name -> name.endsWith(".getFeeConfiguration"))); + when(policyService.getPolicies(CHANNEL_ID)).thenReturn(POLICIES); + assertThat(channelController.getPolicies(CHANNEL_ID)).isEqualTo(FEE_CONFIGURATION_DTO); + verify(metrics).mark(argThat(name -> name.endsWith(".getPolicies"))); } @Test - void getFeeConfiguration_waiting_close() { - assertThat(channelController.getFeeConfiguration(CHANNEL_ID)).isEqualTo(FeeConfigurationDto.EMPTY); + void getPolicies_waiting_close() { + assertThat(channelController.getPolicies(CHANNEL_ID)).isEqualTo(PoliciesDto.EMPTY); } - private ChannelDetailsDto mockForChannelWithoutFeeConfiguration(LocalChannel channel) { + private ChannelDetailsDto mockForChannelWithoutPolicies(LocalChannel channel) { when(nodeService.getAlias(PUBKEY_2)).thenReturn(ALIAS_2); when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(channel)); when(balanceService.getBalanceInformation(CHANNEL_ID)).thenReturn(Optional.empty()); @@ -179,7 +179,7 @@ class ChannelControllerTest { ALIAS_2, BalanceInformation.EMPTY, ON_CHAIN_COSTS, - FeeConfigurationDto.EMPTY + PoliciesDto.EMPTY ); } } \ No newline at end of file diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/dto/ChannelDetailsDtoTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/dto/ChannelDetailsDtoTest.java index db1b4d96..5934dd54 100644 --- a/web/src/test/java/de/cotto/lndmanagej/controller/dto/ChannelDetailsDtoTest.java +++ b/web/src/test/java/de/cotto/lndmanagej/controller/dto/ChannelDetailsDtoTest.java @@ -24,7 +24,7 @@ class ChannelDetailsDtoTest { ALIAS, BALANCE_INFORMATION, ON_CHAIN_COSTS, - FeeConfigurationDto.EMPTY + PoliciesDto.EMPTY ); @Test @@ -79,7 +79,7 @@ class ChannelDetailsDtoTest { ALIAS, BALANCE_INFORMATION, ON_CHAIN_COSTS, - FeeConfigurationDto.EMPTY + PoliciesDto.EMPTY ); ChannelStatusDto channelStatusDto = ChannelStatusDto.createFrom(new ChannelStatus(false, true, false, OPEN)); 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 deleted file mode 100644 index 519aca11..00000000 --- a/web/src/test/java/de/cotto/lndmanagej/controller/dto/FeeConfigurationDtoTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package de.cotto.lndmanagej.controller.dto; - -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 { - @Test - void createFrom() { - FeeConfigurationDto expected = new FeeConfigurationDto( - 1, - 2, - 3, - 4, - false, - true - ); - - FeeConfigurationDto dto = FeeConfigurationDto.createFrom(FEE_CONFIGURATION); - - assertThat(dto).isEqualTo(expected); - } -} \ No newline at end of file diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/dto/PoliciesDtoTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/dto/PoliciesDtoTest.java new file mode 100644 index 00000000..d97e0417 --- /dev/null +++ b/web/src/test/java/de/cotto/lndmanagej/controller/dto/PoliciesDtoTest.java @@ -0,0 +1,19 @@ +package de.cotto.lndmanagej.controller.dto; + +import org.junit.jupiter.api.Test; + +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICIES; +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICY_1; +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICY_2; +import static org.assertj.core.api.Assertions.assertThat; + +class PoliciesDtoTest { + @Test + void createFrom() { + PoliciesDto expected = new PoliciesDto(PolicyDto.createFrom(POLICY_1), PolicyDto.createFrom(POLICY_2)); + + PoliciesDto dto = PoliciesDto.createFrom(POLICIES); + + assertThat(dto).isEqualTo(expected); + } +} \ No newline at end of file diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/dto/PolicyDtoTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/dto/PolicyDtoTest.java new file mode 100644 index 00000000..d02b5ea3 --- /dev/null +++ b/web/src/test/java/de/cotto/lndmanagej/controller/dto/PolicyDtoTest.java @@ -0,0 +1,15 @@ +package de.cotto.lndmanagej.controller.dto; + +import org.junit.jupiter.api.Test; + +import static de.cotto.lndmanagej.model.PolicyFixtures.POLICY_1; +import static org.assertj.core.api.Assertions.assertThat; + +class PolicyDtoTest { + @Test + void createFrom() { + PolicyDto expected = new PolicyDto(100, 10, false); + PolicyDto dto = PolicyDto.createFrom(POLICY_1); + assertThat(dto).isEqualTo(expected); + } +} \ No newline at end of file