Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@ public interface TransactionMapper {
@Mapping(target = "operationIdentifier", source = "index", qualifiedByName = "OperationIdentifier")
Operation mapPoolRetirementToOperation(PoolRetirement model, OperationStatus status, int index);

StakePoolDelegation mapDelegationEntityToDelegation(DelegationEntity entity);
@Mapping(source = "txHash", target = "txHash")
@Mapping(source = "certIndex", target = "certIndex")
@Mapping(source = "address", target = "address")
@Mapping(source = "drepHash", target = "drep.drepId") // no this is not a mistake we don't have bech32 representation in rosetta
@Mapping(source = "drepType", target = "drep.drepType", qualifiedByName = "convertYaciDrepType")
DRepDelegation mapEntityToDRepDelegation(DrepVoteDelegationEntity entity);

@Mapping(source = "txHash", target = "txHash")
@Mapping(source = "certIndex", target = "certIndex")
@Mapping(source = "address", target = "address")
@Mapping(source = "drep.drepId", target = "drepHash") // no this is not a mistake we don't have bech32 representation in rosetta
@Mapping(source = "drep.drepType", target = "drepType", qualifiedByName = "convertClientDrepType")
DrepVoteDelegationEntity mapDRepDelegationToEntity(DRepDelegation dRepDelegation);

StakePoolDelegation mapPoolDelegationEntityToDelegation(PoolDelegationEntity entity);

@Mapping(target = "status", source = "status.status")
@Mapping(target = "type", constant = Constants.OPERATION_TYPE_STAKE_DELEGATION)
Expand All @@ -56,6 +70,50 @@ public interface TransactionMapper {
@Mapping(target = "metadata.poolKeyHash", source = "model.poolId")
Operation mapStakeDelegationToOperation(StakePoolDelegation model, OperationStatus status, int index);

default DRepDelegation mapDrepVoteDelegationEntityToDRepDelegation(DrepVoteDelegationEntity entity) {
if (entity == null) {
return null;
}

return DRepDelegation.builder()
.txHash(entity.getTxHash())
.certIndex(entity.getCertIndex())
.address(entity.getAddress())
.drep(new DRepDelegation.DRep(
entity.getDrepHash(),
convertYaciDrepType(entity.getDrepType())
))
.build();
}

@Named("convertYaciDrepType")
default com.bloxbean.cardano.client.transaction.spec.governance.DRepType convertYaciDrepType(
com.bloxbean.cardano.yaci.core.model.governance.DrepType yaciDrepType) {
if (yaciDrepType == null) {
return null;
}
return switch (yaciDrepType) {
case ADDR_KEYHASH -> com.bloxbean.cardano.client.transaction.spec.governance.DRepType.ADDR_KEYHASH;
case SCRIPTHASH -> com.bloxbean.cardano.client.transaction.spec.governance.DRepType.SCRIPTHASH;
case ABSTAIN -> com.bloxbean.cardano.client.transaction.spec.governance.DRepType.ABSTAIN;
case NO_CONFIDENCE -> com.bloxbean.cardano.client.transaction.spec.governance.DRepType.NO_CONFIDENCE;
};
}

@Named("convertClientDrepType")
default com.bloxbean.cardano.yaci.core.model.governance.DrepType convertClientDrepType(
com.bloxbean.cardano.client.transaction.spec.governance.DRepType clientDrepType) {
if (clientDrepType == null) {
return null;
}
return switch (clientDrepType) {
case ADDR_KEYHASH -> com.bloxbean.cardano.yaci.core.model.governance.DrepType.ADDR_KEYHASH;
case SCRIPTHASH -> com.bloxbean.cardano.yaci.core.model.governance.DrepType.SCRIPTHASH;
case ABSTAIN -> com.bloxbean.cardano.yaci.core.model.governance.DrepType.ABSTAIN;
case NO_CONFIDENCE -> com.bloxbean.cardano.yaci.core.model.governance.DrepType.NO_CONFIDENCE;
};
}

@Mapping(target = "status", source = "status.status")
@Mapping(target = "type", constant = Constants.OPERATION_TYPE_DREP_VOTE_DELEGATION)
@Mapping(target = "operationIdentifier", source = "index", qualifiedByName = "OperationIdentifier")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ public DRepParams convertDRepFromRosetta(DRepDelegation.DRep drep) {
return DRepDelegation.DRep.convertDRepFromRosetta(drep);
}

@Named("convertDrepTypeStringToEnum")
public com.bloxbean.cardano.client.transaction.spec.governance.DRepType convertDrepTypeStringToEnum(@Nullable String drepType) {
if (drepType == null) {
return null;
}

return switch (drepType) {
case "ADDR_KEYHASH" -> com.bloxbean.cardano.client.transaction.spec.governance.DRepType.ADDR_KEYHASH;
case "SCRIPTHASH" -> com.bloxbean.cardano.client.transaction.spec.governance.DRepType.SCRIPTHASH;
case "ABSTAIN" -> com.bloxbean.cardano.client.transaction.spec.governance.DRepType.ABSTAIN;
case "NO_CONFIDENCE" -> com.bloxbean.cardano.client.transaction.spec.governance.DRepType.NO_CONFIDENCE;
default -> null;
};
}

@Named("mapAmountsToOperationMetadataInputWithCache")
public OperationMetadata mapToOperationMetaDataInputWithCache(List<Amt> amounts,
@Context Map<AssetFingerprint, TokenRegistryCurrencyData> metadataMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Setter
public class DRepDelegation {

private String txHash;
Expand All @@ -20,6 +21,7 @@ public class DRepDelegation {
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public static class DRep {

private String drepId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.cardanofoundation.rosetta.api.block.model.entity;

import com.bloxbean.cardano.yaci.core.model.certs.StakeCredType;
import com.bloxbean.cardano.yaci.core.model.governance.DrepType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "delegation_vote")
@IdClass(DrepVoteDelegationId.class)
public class DrepVoteDelegationEntity {

@jakarta.persistence.Id
@Column(name = "tx_hash")
private String txHash;

@jakarta.persistence.Id
@Column(name = "cert_index")
private long certIndex;

@Column(name = "slot")
private Long slot;

@Column(name = "block")
private Long blockNumber;

@Column(name = "block_time")
private Long blockTime;

@Column(name = "update_datetime")
private LocalDateTime updateDateTime;

@Column(name = "address")
private String address;

@Column(name = "drep_hash") // actual drep id as hex hash
private String drepHash;

@Column(name = "drep_id") // bech 32
private String drepId;

@Column(name = "drep_type")
@Enumerated(EnumType.STRING)
private DrepType drepType;

@Column(name = "credential")
private String credential;

@Column(name = "cred_type")
@Enumerated(EnumType.STRING)
private StakeCredType credType;

@Column(name = "epoch")
private Integer epoch;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.cardanofoundation.rosetta.api.block.model.entity;

import jakarta.persistence.Column;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

@EqualsAndHashCode
public class DrepVoteDelegationId implements Serializable {

@Column(name = "tx_hash")
private String txHash;

@Column(name = "cert_index")
private long certIndex;

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
@NoArgsConstructor
@Entity
@Table(name = "delegation")
@IdClass(DelegationId.class)
public class DelegationEntity {
@IdClass(PoolDelegationId.class)
public class PoolDelegationEntity {

@Id
@Column(name = "tx_hash")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class DelegationId implements Serializable {
public class PoolDelegationId implements Serializable {

private String txHash;
private long certIndex;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cardanofoundation.rosetta.api.block.model.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import org.cardanofoundation.rosetta.api.block.model.entity.DrepVoteDelegationEntity;
import org.cardanofoundation.rosetta.api.block.model.entity.DrepVoteDelegationId;

@Repository
public interface DrepVoteDelegationRepository extends JpaRepository<DrepVoteDelegationEntity, DrepVoteDelegationId> {

List<DrepVoteDelegationEntity> findByTxHashInAndCertIndex(List<String> txHashes, long certIndex);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cardanofoundation.rosetta.api.block.model.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import org.cardanofoundation.rosetta.api.block.model.entity.PoolDelegationEntity;
import org.cardanofoundation.rosetta.api.block.model.entity.PoolDelegationId;

@Repository
public interface PoolDelegationRepository extends JpaRepository<PoolDelegationEntity, PoolDelegationId> {

List<PoolDelegationEntity> findByTxHashIn(List<String> txHashes);

}
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
package org.cardanofoundation.rosetta.api.block.service;

import java.time.Clock;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.StructuredTaskScope.ShutdownOnFailure;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jakarta.validation.constraints.NotNull;
import javax.annotation.PostConstruct;

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import org.cardanofoundation.rosetta.api.account.mapper.AddressUtxoEntityToUtxo;
import org.cardanofoundation.rosetta.api.account.model.domain.Utxo;
import org.cardanofoundation.rosetta.api.account.model.entity.AddressUtxoEntity;
Expand All @@ -36,6 +16,23 @@
import org.cardanofoundation.rosetta.api.block.model.entity.*;
import org.cardanofoundation.rosetta.api.block.model.repository.*;
import org.cardanofoundation.rosetta.common.exception.ExceptionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.time.Clock;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.StructuredTaskScope.ShutdownOnFailure;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Component
Expand All @@ -47,7 +44,8 @@ public class LedgerBlockServiceImpl implements LedgerBlockService {
private final BlockRepository blockRepository;
private final TxRepository txRepository;
private final StakeRegistrationRepository stakeRegistrationRepository;
private final DelegationRepository delegationRepository;
private final PoolDelegationRepository poolDelegationRepository;
private final DrepVoteDelegationRepository drepVoteDelegationRepository;
private final PoolRegistrationRepository poolRegistrationRepository;
private final PoolRetirementRepository poolRetirementRepository;
private final WithdrawalRepository withdrawalRepository;
Expand Down Expand Up @@ -233,10 +231,11 @@ private TransactionInfo findByTxHash(List<BlockTx> transactions) {
try (ShutdownOnFailure scope = new ShutdownOnFailure()) {
StructuredTaskScope.Subtask<List<AddressUtxoEntity>> utxos = scope.fork(() -> addressUtxoRepository.findByTxHashIn(utxHashes));
StructuredTaskScope.Subtask<List<StakeRegistrationEntity>> sReg = scope.fork(() -> stakeRegistrationRepository.findByTxHashIn(txHashes));
StructuredTaskScope.Subtask<List<DelegationEntity>> delegations = scope.fork(() -> delegationRepository.findByTxHashIn(txHashes));
StructuredTaskScope.Subtask<List<PoolDelegationEntity>> poolDelegations = scope.fork(() -> poolDelegationRepository.findByTxHashIn(txHashes));
StructuredTaskScope.Subtask<List<PoolRegistrationEntity>> pReg = scope.fork(() -> poolRegistrationRepository.findByTxHashIn(txHashes));
StructuredTaskScope.Subtask<List<PoolRetirementEntity>> pRet = scope.fork(() -> poolRetirementRepository.findByTxHashIn(txHashes));
StructuredTaskScope.Subtask<List<WithdrawalEntity>> withdrawals = scope.fork(() -> withdrawalRepository.findByTxHashIn(txHashes));
StructuredTaskScope.Subtask<List<DrepVoteDelegationEntity>> drepDelegations = scope.fork(() -> drepVoteDelegationRepository.findByTxHashInAndCertIndex(txHashes, 0L));
StructuredTaskScope.Subtask<List<InvalidTransactionEntity>> invalidTxs = scope.fork(() -> invalidTransactionRepository.findByTxHashIn(txHashes));

scope.joinUntil(Instant.now(clock).plusSeconds(blockTransactionApiTimeoutSecs));
Expand All @@ -245,7 +244,8 @@ private TransactionInfo findByTxHash(List<BlockTx> transactions) {
return new TransactionInfo(
utxos.get(),
sReg.get(),
delegations.get(),
poolDelegations.get(),
drepDelegations.get(),
pReg.get(),
pRet.get(),
withdrawals.get(),
Expand Down Expand Up @@ -293,10 +293,10 @@ void populateTransaction(BlockTx transaction,
.toList());

transaction.setStakePoolDelegations(
fetched.delegations
fetched.poolDelegations
.stream()
.filter(tx -> tx.getTxHash().equals(transaction.getHash()))
.map(transactionMapper::mapDelegationEntityToDelegation)
.map(transactionMapper::mapPoolDelegationEntityToDelegation)
.toList());

transaction.setWithdrawals(
Expand All @@ -319,8 +319,13 @@ void populateTransaction(BlockTx transaction,
.filter(tx -> tx.getTxHash().equals(transaction.getHash()))
.map(transactionMapper::mapEntityToPoolRetirement)
.toList());
// TODO dRep Vote Delegations
//transaction.setDRepDelegations(fetched.delegations

transaction.setDRepDelegations(
fetched.drepDelegations
.stream()
.filter(tx -> tx.getTxHash().equals(transaction.getHash()))
.map(transactionMapper::mapDrepVoteDelegationEntityToDRepDelegation)
.toList());

// TODO governance votes
//transaction.setGovernanceVotes(fetched.);
Expand All @@ -345,12 +350,12 @@ private static Map<UtxoKey, AddressUtxoEntity> getUtxoMapFromEntities(Transactio

record TransactionInfo(List<AddressUtxoEntity> utxos,
List<StakeRegistrationEntity> stakeRegistrations,
List<DelegationEntity> delegations,
List<PoolDelegationEntity> poolDelegations,
List<DrepVoteDelegationEntity> drepDelegations,
List<PoolRegistrationEntity> poolRegistrations,
List<PoolRetirementEntity> poolRetirements,
List<WithdrawalEntity> withdrawals,
List<InvalidTransactionEntity> invalidTransactions) {

}

record UtxoKey(String txHash, Integer outputIndex) {
Expand Down
Loading
Loading