Improvements to some /blocks API endpoints

This commit is contained in:
CalDescent 2022-01-21 16:07:18 +00:00
parent ef43e78d54
commit d105613e51
2 changed files with 22 additions and 7 deletions

View File

@ -268,9 +268,15 @@ public class BlocksResource {
@ApiErrors({ @ApiErrors({
ApiError.REPOSITORY_ISSUE ApiError.REPOSITORY_ISSUE
}) })
public BlockData getLastBlock() { public BlockData getLastBlock(@QueryParam("includeOnlineSignatures") Boolean includeOnlineSignatures) {
try (final Repository repository = RepositoryManager.getRepository()) { try (final Repository repository = RepositoryManager.getRepository()) {
return repository.getBlockRepository().getLastBlock(); BlockData blockData = repository.getBlockRepository().getLastBlock();
if (includeOnlineSignatures == null || includeOnlineSignatures == false) {
blockData.setOnlineAccountsSignatures(null);
}
return blockData;
} catch (DataException e) { } catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
} }
@ -548,20 +554,25 @@ public class BlocksResource {
@ApiErrors({ @ApiErrors({
ApiError.BLOCK_UNKNOWN, ApiError.REPOSITORY_ISSUE ApiError.BLOCK_UNKNOWN, ApiError.REPOSITORY_ISSUE
}) })
public BlockData getByTimestamp(@PathParam("timestamp") long timestamp) { public BlockData getByTimestamp(@PathParam("timestamp") long timestamp,
@QueryParam("includeOnlineSignatures") Boolean includeOnlineSignatures) {
try (final Repository repository = RepositoryManager.getRepository()) { try (final Repository repository = RepositoryManager.getRepository()) {
BlockData blockData = null; BlockData blockData = null;
// Try the Blocks table // Try the Blocks table
int height = repository.getBlockRepository().getHeightFromTimestamp(timestamp); int height = repository.getBlockRepository().getHeightFromTimestamp(timestamp);
if (height > 0) { if (height > 1) {
// Found match in Blocks table // Found match in Blocks table
return repository.getBlockRepository().fromHeight(height); blockData = repository.getBlockRepository().fromHeight(height);
if (includeOnlineSignatures == null || includeOnlineSignatures == false) {
blockData.setOnlineAccountsSignatures(null);
}
return blockData;
} }
// Not found in Blocks table, so try the archive // Not found in Blocks table, so try the archive
height = repository.getBlockArchiveRepository().getHeightFromTimestamp(timestamp); height = repository.getBlockArchiveRepository().getHeightFromTimestamp(timestamp);
if (height > 0) { if (height > 1) {
// Found match in archive // Found match in archive
blockData = repository.getBlockArchiveRepository().fromHeight(height); blockData = repository.getBlockArchiveRepository().fromHeight(height);
} }
@ -571,6 +582,10 @@ public class BlocksResource {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.BLOCK_UNKNOWN); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.BLOCK_UNKNOWN);
} }
if (includeOnlineSignatures == null || includeOnlineSignatures == false) {
blockData.setOnlineAccountsSignatures(null);
}
return blockData; return blockData;
} catch (DataException e) { } catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);

View File

@ -77,7 +77,7 @@ public class BlockApiTests extends ApiCommon {
@Test @Test
public void testGetBlockByTimestamp() { public void testGetBlockByTimestamp() {
assertNotNull(this.blocksResource.getByTimestamp(System.currentTimeMillis())); assertNotNull(this.blocksResource.getByTimestamp(System.currentTimeMillis(), false));
} }
@Test @Test