Skip to content

Commit

Permalink
Refactor: Move models and services according to the new structure (RA…
Browse files Browse the repository at this point in the history
…-79)
  • Loading branch information
BerezinD authored May 20, 2024
1 parent efd1993 commit db46c38
Show file tree
Hide file tree
Showing 27 changed files with 86 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.domain.StakeAddressBalance;
import org.cardanofoundation.rosetta.api.block.service.LedgerBlockService;
import org.cardanofoundation.rosetta.api.construction.service.LedgerDataProviderService;
import org.cardanofoundation.rosetta.common.exception.ExceptionFactory;
import org.cardanofoundation.rosetta.common.mapper.DataMapper;
import org.cardanofoundation.rosetta.common.util.CardanoAddressUtils;
Expand All @@ -32,7 +31,7 @@
@RequiredArgsConstructor
public class AccountServiceImpl implements AccountService {

private final LedgerDataProviderService ledgerDataProviderService;
private final LedgerAccountService ledgerAccountService;
private final LedgerBlockService ledgerBlockService;

@Override
Expand Down Expand Up @@ -74,7 +73,7 @@ public AccountCoinsResponse getAccountCoins(AccountCoinsRequest accountCoinsRequ
log.debug("[accountCoins] Filter currency is {}", currenciesRequested);
Block latestBlock = ledgerBlockService.findLatestBlock();
log.debug("[accountCoins] Latest block is {}", latestBlock);
List<Utxo> utxos = ledgerDataProviderService.findUtxoByAddressAndCurrency(accountAddress,
List<Utxo> utxos = ledgerAccountService.findUtxoByAddressAndCurrency(accountAddress,
currenciesRequested);
log.debug("[accountCoins] found {} Utxos for Address {}", utxos.size(), accountAddress);
return DataMapper.mapToAccountCoinsResponse(latestBlock, utxos);
Expand All @@ -90,7 +89,7 @@ private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address,
blockDto.getHash());
if (CardanoAddressUtils.isStakeAddress(address)) {
log.debug("Address is StakeAddress, get balance for {}", address);
List<StakeAddressBalance> balances = ledgerDataProviderService.findStakeAddressBalanceByAddressAndBlock(
List<StakeAddressBalance> balances = ledgerAccountService.findStakeAddressBalanceByAddressAndBlock(
address, blockDto.getNumber());
if (Objects.isNull(balances) || balances.isEmpty()) {
log.error("[findBalanceDataByAddressAndBlock] No balance found for {}", address);
Expand All @@ -99,7 +98,7 @@ private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address,
return DataMapper.mapToStakeAddressBalanceResponse(blockDto, balances.getFirst());
} else {
log.debug("Address isn't StakeAddress");
List<AddressBalance> balances = ledgerDataProviderService.findBalanceByAddressAndBlock(
List<AddressBalance> balances = ledgerAccountService.findBalanceByAddressAndBlock(
address, blockDto.getNumber());
return DataMapper.mapToAccountBalanceResponse(blockDto, balances);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.api.construction.service;
package org.cardanofoundation.rosetta.api.account.service;


import java.util.List;
Expand All @@ -12,7 +12,7 @@
/**
* Exposes functions to access chain data that has been indexed according to Rosetta API needs.
*/
public interface LedgerDataProviderService {
public interface LedgerAccountService {

List<AddressBalance> findBalanceByAddressAndBlock(String address, Long number);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.api.construction.service;
package org.cardanofoundation.rosetta.api.account.service;

import java.util.List;

Expand All @@ -25,7 +25,7 @@
@Slf4j
@Component
@RequiredArgsConstructor
public class PostgresLedgerDataProviderService implements LedgerDataProviderService {
public class LedgerAccountServiceImpl implements LedgerAccountService {

private final AddressBalanceRepository addressBalanceRepository;
private final AddressUtxoRepository addressUtxoRepository;
Expand All @@ -34,6 +34,7 @@ public class PostgresLedgerDataProviderService implements LedgerDataProviderServ

@Override
public List<AddressBalance> findBalanceByAddressAndBlock(String address, Long number) {
log.debug("Finding balance for address {} at block {}", address, number);
List<AddressBalanceEntity> balances = addressBalanceRepository.findAddressBalanceByAddressAndBlockNumber(
address, number);
return balances.stream().map(AddressBalance::fromEntity).toList();
Expand All @@ -42,6 +43,7 @@ public List<AddressBalance> findBalanceByAddressAndBlock(String address, Long nu
@Override
public List<StakeAddressBalance> findStakeAddressBalanceByAddressAndBlock(String address,
Long number) {
log.debug("Finding stake address balance for address {} at block {}", address, number);
List<StakeAddressBalanceEntity> balances = stakeAddressRepository.findStakeAddressBalanceByAddressAndBlockNumber(
address, number);
return balances.stream().map(StakeAddressBalance::fromEntity).toList();
Expand All @@ -50,6 +52,7 @@ public List<StakeAddressBalance> findStakeAddressBalanceByAddressAndBlock(String

@Override
public List<Utxo> findUtxoByAddressAndCurrency(String address, List<Currency> currencies) {
log.debug("Finding UTXOs for address {} with currencies {}", address, currencies);
List<AddressUtxoEntity> addressUtxoEntities = addressUtxoRepository.findUtxosByAddress(address);
return addressUtxoEntities.stream()
.map(entity -> createUtxoModel(currencies, entity))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotNull;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.ObjectUtils;
import org.modelmapper.ModelMapper;

Expand Down Expand Up @@ -86,6 +86,7 @@ private Block toModelFrom(BlockEntity blockEntity) {
}

@Override
@Transactional(readOnly = true)
public List<BlockTx> findTransactionsByBlock(Long blk, String blkHash) {
log.debug("query blockNumber: {} blockHash: {}", blk, blkHash);
Optional<BlockEntity> blkEntity = blockRepository.findByNumberAndHash(blk, blkHash);
Expand All @@ -103,7 +104,6 @@ public List<BlockTx> findTransactionsByBlock(Long blk, String blkHash) {
return Collections.emptyList();
}


@Override
public Block findLatestBlock() {
log.debug("About to look for latest block");
Expand All @@ -120,8 +120,6 @@ public GenesisBlock findGenesisBlock() {
.orElseThrow(ExceptionFactory::genesisBlockNotFound);
}


@Transactional
private void populateTransaction(BlockTx transaction) {

Optional.ofNullable(transaction.getInputs())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.common.enumeration;
package org.cardanofoundation.rosetta.api.construction.enumeration;

import com.fasterxml.jackson.annotation.JsonValue;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.common.enumeration;
package org.cardanofoundation.rosetta.api.construction.enumeration;

import com.fasterxml.jackson.annotation.JsonValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.cardanofoundation.rosetta.api.block.model.domain.ProcessOperations;
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
import org.cardanofoundation.rosetta.common.enumeration.AddressType;
import org.cardanofoundation.rosetta.api.construction.enumeration.AddressType;
import org.cardanofoundation.rosetta.common.enumeration.EraAddressType;
import org.cardanofoundation.rosetta.common.enumeration.NetworkEnum;
import org.cardanofoundation.rosetta.common.enumeration.NetworkIdentifierType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import org.cardanofoundation.rosetta.api.block.model.domain.ProcessOperationsReturn;
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
import org.cardanofoundation.rosetta.api.block.service.LedgerBlockService;
import org.cardanofoundation.rosetta.common.enumeration.AddressType;
import org.cardanofoundation.rosetta.api.construction.enumeration.AddressType;
import org.cardanofoundation.rosetta.common.enumeration.EraAddressType;
import org.cardanofoundation.rosetta.common.enumeration.NetworkEnum;
import org.cardanofoundation.rosetta.common.enumeration.NetworkIdentifierType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.openapitools.client.model.TransactionIdentifierResponse;

import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
import org.cardanofoundation.rosetta.common.enumeration.AddressType;
import org.cardanofoundation.rosetta.api.construction.enumeration.AddressType;
import org.cardanofoundation.rosetta.common.enumeration.NetworkEnum;
import org.cardanofoundation.rosetta.common.enumeration.NetworkIdentifierType;
import org.cardanofoundation.rosetta.common.exception.ExceptionFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.common.model.cardano.transaction;
package org.cardanofoundation.rosetta.api.mempool.model;

import java.util.Arrays;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import com.bloxbean.cardano.yaci.helper.LocalTxMonitorClient;
import org.openapitools.client.model.TransactionIdentifier;

import org.cardanofoundation.rosetta.api.mempool.model.MemPoolTransaction;
import org.cardanofoundation.rosetta.api.network.service.NetworkService;
import org.cardanofoundation.rosetta.common.model.cardano.transaction.MemPoolTransaction;

@Service
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.common.model.cardano.network;
package org.cardanofoundation.rosetta.api.network.model;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.common.model.cardano.network;
package org.cardanofoundation.rosetta.api.network.model;

import javax.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.common.model.cardano.network;
package org.cardanofoundation.rosetta.api.network.model;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.common.model.cardano.network;
package org.cardanofoundation.rosetta.api.network.model;


import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,29 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
import org.json.JSONObject;
import org.openapitools.client.model.*;
import org.openapitools.client.model.Allow;
import org.openapitools.client.model.Error;
import org.openapitools.client.model.MetadataRequest;
import org.openapitools.client.model.NetworkIdentifier;
import org.openapitools.client.model.NetworkListResponse;
import org.openapitools.client.model.NetworkOptionsResponse;
import org.openapitools.client.model.NetworkRequest;
import org.openapitools.client.model.NetworkStatusResponse;
import org.openapitools.client.model.OperationStatus;
import org.openapitools.client.model.Peer;
import org.openapitools.client.model.Version;

import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.domain.GenesisBlock;
import org.cardanofoundation.rosetta.api.block.model.domain.NetworkStatus;
import org.cardanofoundation.rosetta.api.block.service.LedgerBlockService;
import org.cardanofoundation.rosetta.api.network.model.Producer;
import org.cardanofoundation.rosetta.api.network.model.PublicRoot;
import org.cardanofoundation.rosetta.api.network.model.TopologyConfig;
import org.cardanofoundation.rosetta.common.enumeration.OperationType;
import org.cardanofoundation.rosetta.common.enumeration.OperationTypeStatus;
import org.cardanofoundation.rosetta.common.exception.ExceptionFactory;
import org.cardanofoundation.rosetta.common.mapper.DataMapper;
import org.cardanofoundation.rosetta.common.model.cardano.network.Producer;
import org.cardanofoundation.rosetta.common.model.cardano.network.PublicRoot;
import org.cardanofoundation.rosetta.common.model.cardano.network.TopologyConfig;
import org.cardanofoundation.rosetta.common.util.Constants;
import org.cardanofoundation.rosetta.common.util.FileUtils;
import org.cardanofoundation.rosetta.common.util.RosettaConstants;
Expand Down Expand Up @@ -67,14 +76,13 @@ public void filePathExistingValidator() {
validator(genesisPath);
}

private void validator( String path) {
if(!new File(path).exists()) {
private void validator(String path) {
if (!new File(path).exists()) {
throw ExceptionFactory.configNotFoundException();
}
}



@Override
public NetworkListResponse getNetworkList(MetadataRequest metadataRequest) {
log.info("[networkList] Looking for all supported networks");
Expand Down Expand Up @@ -127,8 +135,8 @@ public NetworkOptionsResponse getNetworkOptions(NetworkRequest networkRequest)

@Override
public NetworkStatusResponse getNetworkStatus(NetworkRequest networkRequest)
throws IOException {
log.debug("[networkStatus] Request received:" + networkRequest.toString());
throws IOException {
log.debug("[networkStatus] Request received: {}", networkRequest.toString());
log.info("[networkStatus] Looking for latest block");
NetworkStatus networkStatus = networkStatus();
return datamapper.mapToNetworkStatusResponse(networkStatus);
Expand All @@ -155,13 +163,13 @@ public Network getSupportedNetwork() {
};
}

private NetworkStatus networkStatus() throws IOException {
private NetworkStatus networkStatus() throws IOException {
log.info("[networkStatus] Looking for latest block");
Block latestBlock = ledgerBlockService.findLatestBlock();
log.debug("[networkStatus] Latest block found " + latestBlock);
log.debug("[networkStatus] Latest block found {}", latestBlock);
log.debug("[networkStatus] Looking for genesis block");
GenesisBlock genesisBlock = ledgerBlockService.findGenesisBlock();
log.debug("[networkStatus] Genesis block found " + genesisBlock);
log.debug("[networkStatus] Genesis block found {}", genesisBlock);

ObjectMapper mapper = new ObjectMapper();
String content = FileUtils.fileReader(topologyFilepath);
Expand All @@ -179,10 +187,10 @@ private List<Peer> getPeerFromConfig(TopologyConfig topologyFile) {
List<Producer> producers = Optional.ofNullable(topologyFile).map(
TopologyConfig::getProducers)
.orElseGet(() -> {
assert topologyFile != null;
return getPublicRoots(topologyFile.getPublicRoots());
assert topologyFile != null;
return getPublicRoots(topologyFile.getPublicRoots());
});
log.debug("[getPeersFromConfig] Found " + producers.size() + " peers");
log.debug("[getPeersFromConfig] Found {} peers", producers.size());
return producers.stream().map(producer -> new Peer(producer.getAddr(), null)).toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonValue;

// [FutureUse] It could be used to generate address
public enum NonStakeAddressPrefix {
MAIN("addr"),
TEST("addr_test");
Expand Down
Loading

0 comments on commit db46c38

Please sign in to comment.