fix: return sane values for channels without any average liquidity

This commit is contained in:
Carsten Otto
2022-08-08 08:53:46 +02:00
parent 528499a20a
commit c285de77b8
2 changed files with 14 additions and 13 deletions

View File

@@ -90,15 +90,20 @@ public class RatingService {
return Optional.empty();
}
Duration durationForAnalysis = getDurationForAnalysis();
FeeReport feeReport =
feeService.getFeeReportForChannel(channelId, durationForAnalysis);
RebalanceReport rebalanceReport =
rebalanceService.getReportForChannel(channelId, durationForAnalysis);
Optional<Coins> averageLocalBalanceOptional =
balanceService.getLocalBalanceAverage(channelId, (int) durationForAnalysis.toDays());
if (averageLocalBalanceOptional.isEmpty()) {
return Optional.empty();
}
FeeReport feeReport = feeService.getFeeReportForChannel(channelId, durationForAnalysis);
RebalanceReport rebalanceReport = rebalanceService.getReportForChannel(channelId, durationForAnalysis);
long feeRate = policyService.getMinimumFeeRateTo(localChannel.getRemotePubkey()).orElse(0L);
long localAvailableMilliSat = getLocalAvailableMilliSat(localChannel);
double millionSat = 1.0 * localAvailableMilliSat / 1_000 / 1_000_000;
long averageSat = balanceService.getLocalBalanceAverage(channelId, (int) durationForAnalysis.toDays())
.orElse(Coins.NONE).satoshis();
long averageSat = averageLocalBalanceOptional.get().satoshis();
if (averageSat == 0) {
averageSat = 1;
}
long rating = feeReport.earned().milliSatoshis();
rating += feeReport.sourced().milliSatoshis();

View File

@@ -44,7 +44,6 @@ import static de.cotto.lndmanagej.model.OpenInitiator.LOCAL;
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.assertThatCode;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -306,9 +305,8 @@ class RatingServiceTest {
when(balanceService.getLocalBalanceAverage(CHANNEL_ID, ANALYSIS_DAYS))
.thenReturn(Optional.of(Coins.NONE));
when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL));
when(feeService.getFeeReportForChannel(CHANNEL_ID, DEFAULT_DURATION_FOR_ANALYSIS))
.thenReturn(new FeeReport(Coins.ofMilliSatoshis(100_000 * ANALYSIS_DAYS), Coins.NONE));
assertThatCode(() -> ratingService.getRatingForChannel(CHANNEL_ID)).doesNotThrowAnyException();
long averageSat = ratingService.getRatingForChannel(CHANNEL_ID).map(Rating::getRating).orElseThrow();
assertThat(averageSat).isLessThan(Integer.MAX_VALUE);
}
@Test
@@ -316,9 +314,7 @@ class RatingServiceTest {
when(balanceService.getLocalBalanceAverage(CHANNEL_ID, ANALYSIS_DAYS))
.thenReturn(Optional.empty());
when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL));
when(feeService.getFeeReportForChannel(CHANNEL_ID, DEFAULT_DURATION_FOR_ANALYSIS))
.thenReturn(new FeeReport(Coins.ofMilliSatoshis(200_000 * ANALYSIS_DAYS), Coins.NONE));
assertThatCode(() -> ratingService.getRatingForChannel(CHANNEL_ID)).doesNotThrowAnyException();
assertThat(ratingService.getRatingForChannel(CHANNEL_ID)).isEmpty();
}
@Test