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,7 @@ public interface TransactionMapper {
@Mapping(target = "operationIdentifier", source = "index", qualifiedByName = "OperationIdentifier")
Operation mapPoolRetirementToOperation(PoolRetirement model, OperationStatus status, int index);

StakePoolDelegation mapDelegationEntityToDelegation(DelegationEntity entity);
StakePoolDelegation mapPoolDelegationEntityToDelegation(PoolDelegationEntity entity);

@Mapping(target = "status", source = "status.status")
@Mapping(target = "type", constant = Constants.OPERATION_TYPE_STAKE_DELEGATION)
Expand All @@ -56,6 +56,24 @@ 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.getDrepId(),
convertDrepTypeStringToEnum(entity.getDrepType())
))
.build();
}

com.bloxbean.cardano.client.transaction.spec.governance.DRepType convertDrepTypeStringToEnum(String drepType);

@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
@@ -0,0 +1,49 @@
package org.cardanofoundation.rosetta.api.block.model.entity;

import jakarta.persistence.*;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

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

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

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

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

@Column(name = "block_hash")
private String blockHash;

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

@Column(name = "drep_hash")
private String drepHash;

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

@Column(name = "drep_type")
private String drepType;

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

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

import java.io.Serializable;

import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class DrepVoteDelegationId implements Serializable {

private String txHash;
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
Expand Up @@ -47,7 +47,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,7 +234,8 @@ 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<DrepVoteDelegationEntity>> drepDelegations = scope.fork(() -> drepVoteDelegationRepository.findByTxHashInAndCertIndex(txHashes, 0L));
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));
Expand All @@ -245,7 +247,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 +296,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 +322,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,7 +353,8 @@ 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void findTransactionsByBlock_Test_empty_tx() {
}

@Test
void findTransactionsByBlock_Test_delegation_tx() {
void findTransactionsByBlock_Test_pool_delegation_tx() {
//given
TransactionBlockDetails tx = generatedDataMap.get(POOL_DELEGATION_TRANSACTION.getName());
//when
Expand All @@ -141,12 +141,12 @@ void findTransactionsByBlock_Test_delegation_tx() {
assertThat(blockTx.getStakePoolDelegations().getFirst().getAddress())
.isEqualTo(STAKE_ADDRESS_WITH_EARNED_REWARDS);

List<DelegationEntity> delegations = entityManager
.createQuery("FROM DelegationEntity b where b.txHash=:hash", DelegationEntity.class)
List<PoolDelegationEntity> poolDelegations = entityManager
.createQuery("FROM PoolDelegationEntity b where b.txHash=:hash", PoolDelegationEntity.class)
.setParameter("hash", tx.txHash())
.getResultList();
assertThat(delegations).isNotNull().hasSize(1);
DelegationEntity expected = delegations.getFirst();
assertThat(poolDelegations).isNotNull().hasSize(1);
PoolDelegationEntity expected = poolDelegations.getFirst();
assertThat(expected.getAddress()).isEqualTo(STAKE_ADDRESS_WITH_EARNED_REWARDS);

StakePoolDelegation actual = blockTx.getStakePoolDelegations().getFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ class LedgerBlockServiceImplTest {
private StakeRegistrationRepository stakeRegistrationRepository;

@Mock
private DelegationRepository delegationRepository;
private PoolDelegationRepository poolDelegationRepository;

@Mock
private DrepVoteDelegationRepository drepVoteDelegationRepository;

@Mock
private PoolRegistrationRepository poolRegistrationRepository;
Expand Down Expand Up @@ -89,6 +92,7 @@ void populateTransaction_marksTransactionAsInvalid_ifFoundInInvalidTransactionRe
Collections.emptyList(), // utxos
Collections.emptyList(), // stakeRegistrations
Collections.emptyList(), // delegations
Collections.emptyList(), // drepDelegations
Collections.emptyList(), // poolRegistrations
Collections.emptyList(), // poolRetirements
Collections.emptyList(), // withdrawals
Expand All @@ -112,6 +116,7 @@ void populateTransaction_doesNotMarkTransactionAsInvalid_ifNotFoundInInvalidTran
Collections.emptyList(), // utxos
Collections.emptyList(), // stakeRegistrations
Collections.emptyList(), // delegations
Collections.emptyList(), // drepDelegations
Collections.emptyList(), // poolRegistrations
Collections.emptyList(), // poolRetirements
Collections.emptyList(), // withdrawals
Expand Down Expand Up @@ -149,6 +154,7 @@ void populateTransaction_populatesStakeRegistrations() {
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);

Expand All @@ -165,21 +171,22 @@ void populateTransaction_populatesDelegations() {
transaction.setOutputs(Collections.emptyList());

val utxoMap = new HashMap<LedgerBlockServiceImpl.UtxoKey, AddressUtxoEntity>();
DelegationEntity entity1 = new DelegationEntity();
PoolDelegationEntity entity1 = new PoolDelegationEntity();
entity1.setTxHash("txHash1");

DelegationEntity entity2 = new DelegationEntity();
PoolDelegationEntity entity2 = new PoolDelegationEntity();
entity2.setTxHash("txHash2");

List<DelegationEntity> delegations = List.of(entity1, entity2);
List<PoolDelegationEntity> poolDelegations = List.of(entity1, entity2);

when(transactionMapper.mapDelegationEntityToDelegation(entity1))
when(transactionMapper.mapPoolDelegationEntityToDelegation(entity1))
.thenReturn(new StakePoolDelegation());

val transactionInfo = new LedgerBlockServiceImpl.TransactionInfo(
Collections.emptyList(),
Collections.emptyList(),
delegations,
poolDelegations,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Expand Down Expand Up @@ -216,6 +223,7 @@ void populateTransaction_populatesWithdrawals() {
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
withdrawals,
Collections.emptyList()
);
Expand Down Expand Up @@ -247,6 +255,7 @@ void populateTransaction_populatesPoolRegistrations() {
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
poolRegistrations,
Collections.emptyList(),
Collections.emptyList(),
Expand Down Expand Up @@ -281,6 +290,7 @@ void populateTransaction_populatesPoolRetirements() {
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
poolRetirements,
Collections.emptyList(),
Collections.emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ public enum TestTransactionNames {
// Transaction names for PoolTransactions
POOL_REGISTRATION_TRANSACTION("pool_registration"),
POOL_DELEGATION_TRANSACTION("pool_delegation"),
POOL_RETIREMENT_TRANSACTION("pool_retirement");
POOL_RETIREMENT_TRANSACTION("pool_retirement"),

// Transaction names for GovernanceTransactions
DREP_REGISTER("drep_register"),
DREP_VOTE_DELEGATION("drep_vote_delegation");

private final String name;

Expand Down
Loading
Loading