Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
chore: rolled back block details
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo83 committed Oct 29, 2024
1 parent 69f20ac commit 2463f13
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cardanofoundation.explorer.api.model.response;

import java.io.Serializable;
import java.math.BigInteger;
import java.time.LocalDateTime;

import lombok.Getter;
Expand All @@ -22,5 +23,11 @@ public class BlockFilterResponse implements Serializable {

private LocalDateTime time;

private Long txCount;

private BigInteger totalFees;

private BigInteger totalOutput;

private String slotLeader;
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public BaseFilterResponse<BlockFilterResponse> filterBlock(Pageable pageable) {
} else {
blockPage = blockRepository.findAllBlock(pageable);
}
return mapperBlockToBlockFilterResponse(blockPage);
return mapperBlockToBlockFilterResponse(blockPage, true);
}

/**
Expand Down Expand Up @@ -306,7 +306,7 @@ public BaseFilterResponse<BlockFilterResponse> getBlockByEpoch(String no, Pageab
try {
Integer epochNo = Integer.parseInt(no);
Page<Block> blocks = blockRepository.findBlockByEpochNo(epochNo, pageable);
return mapperBlockToBlockFilterResponse(blocks);
return mapperBlockToBlockFilterResponse(blocks, false);
} catch (NumberFormatException e) {
throw new NoContentException(BusinessCode.EPOCH_NOT_FOUND);
}
Expand Down Expand Up @@ -334,18 +334,44 @@ public List<BlockPropagationResponse> getBlockPropagation(
* @return list block information in this page
*/
private BaseFilterResponse<BlockFilterResponse> mapperBlockToBlockFilterResponse(
Page<Block> blocks) {
Page<Block> blocks, boolean isGetAllBlock) {
// get slot leader for block
List<SlotLeader> slotLeaders =
slotLeaderRepository.findByIdIn(
blocks.getContent().stream().map(Block::getSlotLeaderId).collect(Collectors.toList()));
Map<Long, SlotLeader> slotLeaderMap =
slotLeaders.stream().collect(Collectors.toMap(BaseEntity::getId, Function.identity()));

List<Tx> txList = txRepository.findByBlockIn(blocks.toList());

// create map with key: block_id, value : total output of block
Map<Long, BigInteger> blockTotalOutputMap =
txList.stream()
.collect(
Collectors.groupingBy(
tx -> tx.getBlock().getId(),
Collectors.reducing(BigInteger.ZERO, Tx::getOutSum, BigInteger::add)));
// create map with key: block_id, value : total fee of block
Map<Long, BigInteger> blockTotalFeeMap =
txList.stream()
.collect(
Collectors.groupingBy(
tx -> tx.getBlock().getId(),
Collectors.reducing(BigInteger.ZERO, Tx::getFee, BigInteger::add)));

List<BlockFilterResponse> blockFilterResponseList = new ArrayList<>();

for (Block block : blocks) {
block.setSlotLeader(slotLeaderMap.get(block.getSlotLeaderId()));
BlockFilterResponse blockResponse = blockMapper.blockToBlockFilterResponse(block);
if (!isGetAllBlock) {
var totalOutput = blockTotalOutputMap.get(block.getId());
var totalFees = blockTotalFeeMap.get(block.getId());
blockResponse.setTotalOutput(Objects.requireNonNullElse(totalOutput, BigInteger.ZERO));
blockResponse.setTotalFees(Objects.requireNonNullElse(totalFees, BigInteger.ZERO));
} else {
blockResponse.setTxCount(null);
}
blockFilterResponseList.add(blockResponse);
}
return new BaseFilterResponse<>(blocks, blockFilterResponseList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public void testFilterBlock() {
when(blockMapper.blockToBlockFilterResponse(any(Block.class)))
.thenReturn(new BlockFilterResponse());
when(slotLeaderRepository.findByIdIn(anyList())).thenReturn(Collections.emptyList());
when(txRepository.findByBlockIn(anyList())).thenReturn(txList);

// Call the service method
BaseFilterResponse<BlockFilterResponse> response = blockService.filterBlock(pageable);
Expand All @@ -175,6 +176,7 @@ public void testFilterBlock() {
verify(blockRepository).findAllBlock(pageable);
verify(blockMapper).blockToBlockFilterResponse(any(Block.class));
verify(slotLeaderRepository).findByIdIn(anyList());
verify(txRepository).findByBlockIn(anyList());
assertEquals(response.getData().size(), 1);
assertEquals(response.getTotalItems(), 1);
}
Expand All @@ -192,6 +194,7 @@ public void testGetBlockByEpoch_WhenEpochNoExists() {
when(blockMapper.blockToBlockFilterResponse(any(Block.class)))
.thenReturn(new BlockFilterResponse());
when(slotLeaderRepository.findByIdIn(anyList())).thenReturn(Collections.emptyList());
when(txRepository.findByBlockIn(anyList())).thenReturn(Collections.emptyList());

// Call the service method
BaseFilterResponse<BlockFilterResponse> response = blockService.getBlockByEpoch(no, pageable);
Expand All @@ -200,6 +203,7 @@ public void testGetBlockByEpoch_WhenEpochNoExists() {
verify(blockRepository).findBlockByEpochNo(epochNo, pageable);
verify(blockMapper).blockToBlockFilterResponse(any(Block.class));
verify(slotLeaderRepository).findByIdIn(anyList());
verify(txRepository).findByBlockIn(anyList());
assertEquals(response.getData().size(), 1);
assertEquals(response.getTotalItems(), 1);
}
Expand Down

0 comments on commit 2463f13

Please sign in to comment.