Skip to content

Commit

Permalink
chore: #191 map voting and proposing redeemer to script hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-HuyLe3a committed Jul 3, 2024
1 parent 01b93a2 commit 123e104
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ void handleEpochParam(int epochNo) {
if (curEra == EraType.ALONZO && prevEra == null) {
epochParamMapper.updateByEpochParam(curEpochParam, defShelleyEpochParam);
epochParamMapper.updateByEpochParam(curEpochParam, defAlonzoEpochParam);
costModelRepository.save(defAlonzoEpochParam.getCostModel());
var costModel = defAlonzoEpochParam.getCostModel();
costModelRepository.save(costModel);
curEpochParam.setCostModel(costModel);
curEpochParam.setMinUtxoValue(null);
}

Expand All @@ -127,7 +129,9 @@ void handleEpochParam(int epochNo) {

if (curEra == EraType.ALONZO && prevEra == EraType.MARY) {
epochParamMapper.updateByEpochParam(curEpochParam, defAlonzoEpochParam);
costModelRepository.save(defAlonzoEpochParam.getCostModel());
var costModel = defAlonzoEpochParam.getCostModel();
costModelRepository.save(costModel);
curEpochParam.setCostModel(costModel);
curEpochParam.setMinUtxoValue(null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.cardanofoundation.ledgersync.service.impl;

import com.bloxbean.cardano.yaci.core.model.Amount;
import com.bloxbean.cardano.yaci.core.model.Datum;
import com.bloxbean.cardano.yaci.core.model.ExUnits;
import com.bloxbean.cardano.yaci.core.model.RedeemerTag;
import com.bloxbean.cardano.client.address.Address;
import com.bloxbean.cardano.yaci.core.model.*;
import com.bloxbean.cardano.yaci.core.model.certs.*;
import com.bloxbean.cardano.yaci.core.model.governance.ProposalProcedure;
import com.bloxbean.cardano.yaci.core.model.governance.Voter;
import com.bloxbean.cardano.yaci.core.model.governance.VotingProcedures;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
Expand Down Expand Up @@ -198,7 +198,8 @@ private String getRedeemerPointer(
case Cert -> handleCertPtr(aggregatedTx.getCertificates(), pointerIndex);
case Reward -> handleRewardPtr(new ArrayList<>(aggregatedTx.getWithdrawals().keySet()),
pointerIndex);
case Voting, Proposing -> null; //TODO check later
case Voting -> handleVotingPtr(aggregatedTx.getVotingProcedures(), pointerIndex);
case Proposing -> handleProposingPtr(aggregatedTx.getProposalProcedures(), pointerIndex);
default -> {
log.error("Unsupported redeemer tag {}", tag);
yield null;
Expand Down Expand Up @@ -244,24 +245,43 @@ private String handleCertPtr(
List<Certificate> certificates, int pointerIndex) {
Certificate certificate = certificates.get(pointerIndex);

// Stake delegation
if (certificate.getType() == CertificateType.STAKE_DELEGATION) {
StakeDelegation delegation = (StakeDelegation) certificate;
return delegation.getStakeCredential().getHash();
}

// Stake de-registration
if (certificate.getType() == CertificateType.STAKE_DEREGISTRATION) {
StakeDeregistration stakeDeregistration = (StakeDeregistration) certificate;
return stakeDeregistration.getStakeCredential().getHash();
}

if (certificate.getType() == CertificateType.AUTH_COMMITTEE_HOT_CERT) {
AuthCommitteeHotCert authCommitteeHotCert = (AuthCommitteeHotCert) certificate;
return authCommitteeHotCert.getCommitteeHotCredential().getHash(); // TODO: need to check again
}

return null;
String credentialHash;
if (certificate instanceof StakeRegistration cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof StakeDelegation cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof StakeDeregistration cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof RegCert cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof UnregCert cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof VoteDelegCert cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof StakeVoteDelegCert cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof StakeRegDelegCert cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof VoteRegDelegCert cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof StakeVoteRegDelegCert cert)
credentialHash = credentialHash(cert.getStakeCredential());
else if (certificate instanceof AuthCommitteeHotCert cert)
credentialHash = credentialHash(cert.getCommitteeColdCredential());
else if (certificate instanceof ResignCommitteeColdCert cert)
credentialHash = credentialHash(cert.getCommitteeColdCredential());
else if (certificate instanceof RegDrepCert cert)
credentialHash = credentialHash(cert.getDrepCredential());
else if (certificate instanceof UnregDrepCert cert)
credentialHash = credentialHash(cert.getDrepCredential());
else if (certificate instanceof UpdateDrepCert cert)
credentialHash = credentialHash(cert.getDrepCredential());
else
credentialHash = null;

//TODO -- For other certificate types ??
//PoolRegistration, PoolRetirement, GenesisKeyDelegation, MoveInstantaneousRewardsCert
return credentialHash;
}

private String handleRewardPtr(List<String> rewardAccounts, int pointerIndex) {
Expand All @@ -270,6 +290,34 @@ private String handleRewardPtr(List<String> rewardAccounts, int pointerIndex) {
return rewardAccount.substring(2);
}

private String handleVotingPtr(VotingProcedures votingProcedures, int pointerIndex) {
if (votingProcedures == null || votingProcedures.getVoting() == null || votingProcedures.getVoting().isEmpty())
return null;

Set<Voter> voters = ((LinkedHashMap)votingProcedures.getVoting()).keySet();
int i = 0;
Voter voter = null;
for (var v : voters) {
if (i == pointerIndex) {
voter = v;
break;
}
i++;
}

return voter != null ? voter.getHash() : null;
}

private String handleProposingPtr(List<ProposalProcedure> proposalProcedures, int pointerIndex) {
if (proposalProcedures == null || proposalProcedures.isEmpty() || pointerIndex > proposalProcedures.size() - 1)
return null;

//TODO - Get the constitution policy (script hash)
//For now set scriptHash to null

return null;
}

private RedeemerData buildRedeemerData(Datum plutusData, Tx tx) {
RedeemerDataBuilder<?, ?> redeemerDataBuilder = RedeemerData.builder();

Expand All @@ -281,4 +329,11 @@ private RedeemerData buildRedeemerData(Datum plutusData, Tx tx) {
return redeemerDataBuilder.build();
}

private String credentialHash(StakeCredential stakeCredential) {
return stakeCredential != null? stakeCredential.getHash() : null;
}

private String credentialHash(Credential credential) {
return credential != null? credential.getHash() : null;
}
}

0 comments on commit 123e104

Please sign in to comment.