Skip to content

Commit

Permalink
Merge pull request #1172 from cardano-foundation/feat/MET-2019-block-…
Browse files Browse the repository at this point in the history
…producer

feat: add block producer to block details
  • Loading branch information
Sotatek-DucPhung committed May 6, 2024
2 parents 7292a3d + 7f2fc3d commit c98d01d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public class BlockResponse {
private String slotLeader;

private Integer confirmation;

private String poolView;

private String poolTicker;

private String poolName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.cardanofoundation.explorer.api.projection;

public interface PoolMintBlockProjection {
String getPoolView();

String getPoolTicker();

String getPoolName();

Long getBlockNo();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.stereotype.Repository;

import org.cardanofoundation.explorer.api.model.response.pool.projection.PoolCountProjection;
import org.cardanofoundation.explorer.api.projection.PoolMintBlockProjection;
import org.cardanofoundation.explorer.common.entity.ledgersync.Block;
import org.cardanofoundation.explorer.common.entity.ledgersync.Block_;

Expand Down Expand Up @@ -94,4 +95,16 @@ List<PoolCountProjection> findTopDelegationByEpochBlock(
+ " WHERE e.era != org.cardanofoundation.explorer.common.entity.enumeration.EraType.BYRON"
+ " ORDER BY b.blockNo ASC LIMIT 1")
Optional<Block> findFirstShellyBlock();

@Query(
value =
"""
select b.blockNo as blockNo, ph.view as poolView, po.poolName as poolName, po.tickerName as poolTicker
from Block b
left join SlotLeader sl on sl.id = b.slotLeaderId
left join PoolHash ph on ph.id = sl.poolHashId
left join PoolOfflineData po on ph.id = po.poolId AND po.id = (SELECT max(po2.id) FROM PoolOfflineData po2 WHERE po2.poolId = ph.id)
where b.blockNo = :blockNo
""")
PoolMintBlockProjection getPoolInfoThatMintedBlock(@Param("blockNo") Long blockNo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.cardanofoundation.explorer.api.model.response.BaseFilterResponse;
import org.cardanofoundation.explorer.api.model.response.BlockFilterResponse;
import org.cardanofoundation.explorer.api.model.response.BlockResponse;
import org.cardanofoundation.explorer.api.projection.PoolMintBlockProjection;
import org.cardanofoundation.explorer.api.repository.ledgersync.BlockRepository;
import org.cardanofoundation.explorer.api.repository.ledgersync.CustomBlockRepository;
import org.cardanofoundation.explorer.api.repository.ledgersync.SlotLeaderRepository;
Expand Down Expand Up @@ -77,10 +78,15 @@ public BlockResponse getBlockDetailByBlockId(String blockId) {
private BlockResponse getBlockResponse(Block block) {
BlockResponse blockResponse = blockMapper.blockToBlockResponse(block);
List<Tx> txList = txRepository.findAllByBlock(block);
PoolMintBlockProjection poolMintBlockProjection =
blockRepository.getPoolInfoThatMintedBlock(block.getBlockNo());
blockResponse.setTotalOutput(
txList.stream().map(Tx::getOutSum).reduce(BigInteger.ZERO, BigInteger::add));
blockResponse.setTotalFees(
txList.stream().map(Tx::getFee).reduce(BigInteger.ZERO, BigInteger::add));
blockResponse.setPoolName(poolMintBlockProjection.getPoolName());
blockResponse.setPoolView(poolMintBlockProjection.getPoolView());
blockResponse.setPoolTicker(poolMintBlockProjection.getPoolTicker());
Integer currentBlockNo =
blockRepository
.findCurrentBlock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import org.junit.jupiter.api.Test;
Expand All @@ -24,6 +25,7 @@
import org.cardanofoundation.explorer.api.model.response.BaseFilterResponse;
import org.cardanofoundation.explorer.api.model.response.BlockFilterResponse;
import org.cardanofoundation.explorer.api.model.response.BlockResponse;
import org.cardanofoundation.explorer.api.projection.PoolMintBlockProjection;
import org.cardanofoundation.explorer.api.repository.ledgersync.BlockRepository;
import org.cardanofoundation.explorer.api.repository.ledgersync.SlotLeaderRepository;
import org.cardanofoundation.explorer.api.repository.ledgersync.TxRepository;
Expand Down Expand Up @@ -52,11 +54,14 @@ public void testGetBlockDetailByBlockId_WhenBlockNoExists() {
block.setBlockNo(123L);
BlockResponse expectedResponse = new BlockResponse();

PoolMintBlockProjection projection = Mockito.mock(PoolMintBlockProjection.class);

// Mock repository method
when(blockRepository.findFirstByBlockNo(123L)).thenReturn(Optional.of(block));
when(blockMapper.blockToBlockResponse(block)).thenReturn(expectedResponse);
when(txRepository.findAllByBlock(block)).thenReturn(new ArrayList<>());
when(blockRepository.findCurrentBlock()).thenReturn(Optional.of(0));
when(blockRepository.getPoolInfoThatMintedBlock(any())).thenReturn(projection);

// Call the service method
BlockResponse response = blockService.getBlockDetailByBlockId(blockId);
Expand Down Expand Up @@ -85,18 +90,25 @@ public void testGetBlockDetailByBlockId_WhenBlockHashExists() {
Block block = new Block();
BlockResponse expectedResponse = new BlockResponse();

PoolMintBlockProjection projection = Mockito.mock(PoolMintBlockProjection.class);
when(projection.getPoolView()).thenReturn("poolView");
when(projection.getPoolTicker()).thenReturn("poolTicker");
when(projection.getPoolName()).thenReturn("poolName");

// Mock repository method
when(blockRepository.findFirstByHash("hash123")).thenReturn(Optional.of(block));
when(blockMapper.blockToBlockResponse(block)).thenReturn(expectedResponse);
when(txRepository.findAllByBlock(block)).thenReturn(new ArrayList<>());
when(blockRepository.findCurrentBlock()).thenReturn(Optional.of(0));
when(blockRepository.getPoolInfoThatMintedBlock(any())).thenReturn(projection);

// Call the service method
BlockResponse response = blockService.getBlockDetailByBlockId(blockId);

// Verify the repository method was called and the response is correct
verify(blockRepository).findFirstByHash("hash123");
assertEquals(expectedResponse, response);
assertEquals(response.getPoolView(), "poolView");
}

@Test
Expand All @@ -117,12 +129,14 @@ public void testGetBlockDetailByBlockId_ThrowCurrentBlock() {
String blockId = "hash123";
Block block = new Block();
BlockResponse expectedResponse = new BlockResponse();
PoolMintBlockProjection projection = Mockito.mock(PoolMintBlockProjection.class);

// Mock repository method
when(blockRepository.findFirstByHash("hash123")).thenReturn(Optional.of(block));
when(blockMapper.blockToBlockResponse(block)).thenReturn(expectedResponse);
when(txRepository.findAllByBlock(block)).thenReturn(new ArrayList<>());
when(blockRepository.findCurrentBlock()).thenReturn(Optional.empty());
when(blockRepository.getPoolInfoThatMintedBlock(any())).thenReturn(projection);

// Verify the repository method was called and the response is correct
assertThrows(BusinessException.class, () -> blockService.getBlockDetailByBlockId(blockId));
Expand Down

0 comments on commit c98d01d

Please sign in to comment.