add get-blockheight to status controller

This commit is contained in:
Carsten Otto
2021-11-27 11:56:13 +01:00
parent 9800c2ac5b
commit c9e514e425
5 changed files with 38 additions and 0 deletions

View File

@@ -24,6 +24,10 @@ public class OwnNodeService {
return Boolean.TRUE.equals(syncedToChainCache.get(""));
}
public int getBlockHeight() {
return grpcGetInfo.getBlockHeight().orElseThrow();
}
private boolean isSyncedToChainWithoutCache() {
return grpcGetInfo.isSyncedToChain().orElse(false);
}

View File

@@ -7,9 +7,11 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.NoSuchElementException;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -37,4 +39,16 @@ class OwnNodeServiceTest {
when(grpcGetInfo.isSyncedToChain()).thenReturn(Optional.empty());
assertThat(ownNodeService.isSyncedToChain()).isFalse();
}
@Test
void getBlockHeight() {
when(grpcGetInfo.getBlockHeight()).thenReturn(Optional.of(123_456));
assertThat(ownNodeService.getBlockHeight()).isEqualTo(123_456);
}
@Test
void getBlockHeight_empty() {
when(grpcGetInfo.getBlockHeight()).thenReturn(Optional.empty());
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(ownNodeService::getBlockHeight);
}
}

View File

@@ -46,6 +46,13 @@ class StatusControllerIT {
.andExpect(content().string("true"));
}
@Test
void getBlockHeight() throws Exception {
when(ownNodeService.getBlockHeight()).thenReturn(123_456);
mockMvc.perform(get(PREFIX + "/block-height"))
.andExpect(content().string("123456"));
}
@Test
void getPubkeysForOpenChannels() throws Exception {
when(channelService.getOpenChannels()).thenReturn(Set.of(LOCAL_OPEN_CHANNEL_TO_NODE_3, LOCAL_OPEN_CHANNEL));

View File

@@ -37,6 +37,12 @@ public class StatusController {
return ownNodeService.isSyncedToChain();
}
@GetMapping("/block-height")
public int getBlockHeight() {
mark("getBlockHeight");
return ownNodeService.getBlockHeight();
}
@GetMapping("/open-channels/pubkeys")
public PubkeysDto getPubkeysForOpenChannels() {
mark("getPubkeysForOpenChannels");

View File

@@ -83,4 +83,11 @@ class StatusControllerTest {
when(channelService.getAllLocalChannels()).thenReturn(Stream.of(CLOSED_CHANNEL_2, CLOSED_CHANNEL));
assertThat(statusController.getPubkeysForAllChannels().pubkeys()).containsExactly(PUBKEY_2.toString());
}
@Test
void getBlockHeight() {
when(ownNodeService.getBlockHeight()).thenReturn(123_456);
assertThat(statusController.getBlockHeight()).isEqualTo(123_456);
verify(metrics).mark(argThat(name -> name.endsWith(".getBlockHeight")));
}
}