Skip to content

Commit

Permalink
Fix/rename construction metadata properties (#117)
Browse files Browse the repository at this point in the history
* fix: added ConstructionMetadataResponseMetadata Object to yaml and respective mapper.

* fix: added ConstructionMetadataResponseMetadata Object to yaml and respective mapper.

* fix: fixed mappers and removed unused fields like rho and tau.

* chore: added PeristenceAnnotation to DataMapper. Added Test for new Mapper

* fix: fixed setting of protocolversion as inner class
  • Loading branch information
Kammerlo authored Apr 11, 2024
1 parent c3d75de commit cef1f81
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.RequiredArgsConstructor;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -75,14 +76,6 @@ public class ProtocolParams {
private BigInteger drepDeposit; //31
private Integer drepActivity; //32

private Double a0;
private Double rho;
private Double tau;
@JsonProperty("eMax")
private Integer eMax;
@JsonProperty("maxUTxOValue")
private Integer maxUTxOValue;

@Data
public static class ExtraEntropy{
String tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ConstructionApiServiceImpl implements ConstructionApiService {
private final CardanoAddressService cardanoAddressService;
private final CardanoService cardanoService;
private final LedgerDataProviderService ledgerService;
private final DataMapper dataMapper;

@Override
public ConstructionDeriveResponse constructionDeriveService(
Expand Down Expand Up @@ -112,7 +113,7 @@ public ConstructionMetadataResponse constructionMetadataService(
protocolParams);
Long suggestedFee = cardanoService.calculateTxMinimumFee(updatedTxSize, protocolParams);
log.debug("[constructionMetadata] suggested fee is ${suggestedFee}");
return DataMapper.mapToMetadataResponse(protocolParams, ttl, suggestedFee);
return dataMapper.mapToMetadataResponse(protocolParams, ttl, suggestedFee);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class NetworkServiceImpl implements NetworkService {
private final RosettaConfig rosettaConfig;

private final LedgerDataProviderService ledgerDataProviderService;
private final DataMapper datamapper;

@Value("${cardano.rosetta.TOPOLOGY_FILEPATH}")
private String topologyFilepath;
Expand Down Expand Up @@ -79,7 +80,7 @@ private void validator( String path) throws ServerException {
public NetworkListResponse getNetworkList(MetadataRequest metadataRequest) {
log.info("[networkList] Looking for all supported networks");
Network supportedNetwork = getSupportedNetwork();
return DataMapper.mapToNetworkListResponse(supportedNetwork);
return datamapper.mapToNetworkListResponse(supportedNetwork);
}

@Override
Expand Down Expand Up @@ -132,7 +133,7 @@ public NetworkStatusResponse getNetworkStatus(NetworkRequest networkRequest)
log.debug("[networkStatus] Request received:" + networkRequest.toString());
log.info("[networkStatus] Looking for latest block");
NetworkStatus networkStatus = networkStatus();
return DataMapper.mapToNetworkStatusResponse(networkStatus);
return datamapper.mapToNetworkStatusResponse(networkStatus);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.cardanofoundation.rosetta.common.mapper;

import com.bloxbean.cardano.client.common.model.Network;
import java.math.BigDecimal;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.ObjectUtils;
import org.cardanofoundation.rosetta.api.account.model.domain.AddressBalance;
import org.cardanofoundation.rosetta.api.account.model.domain.Utxo;
import org.cardanofoundation.rosetta.api.block.model.domain.*;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.common.annotation.PersistenceMapper;
import org.cardanofoundation.rosetta.common.model.cardano.crypto.Signatures;
import org.cardanofoundation.rosetta.common.util.Constants;
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
Expand All @@ -19,18 +22,16 @@


@Slf4j
@Component
@PersistenceMapper
@RequiredArgsConstructor
public class DataMapper {

private DataMapper() {
}

private final ProtocolParamsToRosettaProtocolParameters protocolParamsToRosettaProtocolParameters;
/**
* Maps a NetworkRequest to a NetworkOptionsResponse.
* @param supportedNetwork The supported network
* @return The NetworkOptionsResponse
*/
public static NetworkListResponse mapToNetworkListResponse(Network supportedNetwork) {
public NetworkListResponse mapToNetworkListResponse(Network supportedNetwork) {
NetworkIdentifier identifier = NetworkIdentifier.builder().blockchain(Constants.CARDANO)
.network(Objects.requireNonNull(
NetworkEnum.fromProtocolMagic(supportedNetwork.getProtocolMagic())).getValue()).build();
Expand All @@ -42,7 +43,7 @@ public static NetworkListResponse mapToNetworkListResponse(Network supportedNetw
* @param networkStatus The network status
* @return The NetworkOptionsResponse
*/
public static NetworkStatusResponse mapToNetworkStatusResponse(NetworkStatus networkStatus) {
public NetworkStatusResponse mapToNetworkStatusResponse(NetworkStatus networkStatus) {
Block latestBlock = networkStatus.getLatestBlock();
GenesisBlock genesisBlock = networkStatus.getGenesisBlock();
List<Peer> peers = networkStatus.getPeers();
Expand Down Expand Up @@ -177,9 +178,12 @@ public static AccountCoinsResponse mapToAccountCoinsResponse(Block block,
.build();
}

public static ConstructionMetadataResponse mapToMetadataResponse(ProtocolParams protocolParams, Long ttl, Long suggestedFee) {
public ConstructionMetadataResponse mapToMetadataResponse(ProtocolParams protocolParams, Long ttl, Long suggestedFee) {
return ConstructionMetadataResponse.builder()
.metadata(Map.of("protocol_parameters", protocolParams, "ttl", ttl))
.metadata(ConstructionMetadataResponseMetadata.builder()
.ttl(new BigDecimal(ttl))
.protocolParameters(protocolParamsToRosettaProtocolParameters.toProtocolParameters(protocolParams))
.build())
.suggestedFee(List.of(Amount.builder()
.value(suggestedFee.toString())
.currency(Currency.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.cardanofoundation.rosetta.common.mapper;

import static java.util.Optional.ofNullable;

import lombok.AllArgsConstructor;
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
import org.cardanofoundation.rosetta.common.annotation.OpenApiMapper;
import org.modelmapper.ModelMapper;
import org.openapitools.client.model.ProtocolParameters;

@OpenApiMapper
@AllArgsConstructor
public class ProtocolParamsToRosettaProtocolParameters {

final ModelMapper modelMapper;

public ProtocolParameters toProtocolParameters(ProtocolParams model) {
return ofNullable(modelMapper.getTypeMap(ProtocolParams.class, ProtocolParameters.class))
.orElseGet(() -> modelMapper.createTypeMap(ProtocolParams.class, ProtocolParameters.class))
.addMappings(mapper -> {

mapper.map(ProtocolParams::getAdaPerUtxoByte, ProtocolParameters::setCoinsPerUtxoSize);
mapper.map(ProtocolParams::getMaxCollateralInputs, ProtocolParameters::setMaxCollateralInputs);
mapper.map(ProtocolParams::getMinFeeA, ProtocolParameters::setMinFeeCoefficient);
mapper.map(ProtocolParams::getMinFeeB, ProtocolParameters::setMinFeeConstant);
mapper.map(source -> source.getProtocolVersion().getMajor(), ProtocolParameters::setProtocol);


}).map(model);
}
}
14 changes: 10 additions & 4 deletions api/src/main/resources/rosetta-specifications-1.4.15/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1304,10 +1304,7 @@ components:
- metadata
properties:
metadata:
type: object
example:
account_sequence: 23
recent_block_hash: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5'
$ref: '#/components/schemas/ConstructionMetadataResponseMetadata'
suggested_fee:
type: array
items:
Expand Down Expand Up @@ -1945,3 +1942,12 @@ components:
transaction_size:
description: ''
type: number
ConstructionMetadataResponseMetadata:
description: ''
type: object
properties:
ttl:
description: ''
type: number
protocol_parameters:
$ref: '#/components/schemas/ProtocolParameters'
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,13 @@ void fromEntity_Test_ok() {

@Test
void merge_Test_ok() {
ProtocolParams from = ProtocolParams.builder().eMax(1).rho(2.0).costModels(Map.of("key3", new long[]{4})).build();
ProtocolParams to = ProtocolParams.builder().tau(5.0).costModelsHash("costHash6").build();
ProtocolParams from = ProtocolParams.builder().costModels(Map.of("key3", new long[]{4})).build();
ProtocolParams to = ProtocolParams.builder().costModelsHash("costHash6").build();
ProtocolParams merged = my.merge(from, to);
assertThat(merged.getEMax()).isEqualTo(from.getEMax());
assertThat(merged.getRho()).isEqualTo(from.getRho());
assertThat(merged.getCostModels().size()).isEqualTo(1);
assertThat(merged.getCostModels().keySet()).containsAll(from.getCostModels().keySet());
assertThat(merged.getCostModels().values()).containsAll(from.getCostModels().values());

assertThat(merged.getTau()).isEqualTo(to.getTau());
assertThat(merged.getCostModelsHash()).isEqualTo(to.getCostModelsHash());
}

Expand Down

0 comments on commit cef1f81

Please sign in to comment.