Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update changes for next Sanchonet release 8.11.0 #195

Merged
merged 12 commits into from
May 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class CostModelConverter {

public static final String PLUTUS_V1 = "PlutusV1";
public static final String PLUTUS_V2 = "PlutusV2";
public static final String PLUTUS_V3 = "PlutusV3";

public static String getCostModelHashFromGenesis(java.util.Map genesisMap) {
Map mapItem = new Map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class GenesisData {
List<Tx> txs;
List<TxOut> txOuts;
List<SlotLeader> slotLeaders;
CostModel costModel;
CostModel alonzoCostModel;
CostModel conwayCostModel;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to keep costModel in two separate fields or just keep one as both values are merged to get the final cost model ?

EpochParam shelley;
EpochParam alonzo;
EpochParam babbage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package org.cardanofoundation.ledgersync.service;

import org.cardanofoundation.ledgersync.consumercommon.entity.CostModel;
import org.cardanofoundation.ledgersync.aggregate.AggregatedTx;
import org.cardanofoundation.ledgersync.consumercommon.entity.CostModel;
import org.springframework.transaction.annotation.Transactional;

public interface CostModelService {
String PLUTUS_V1_KEY = "PlutusV1";
String PLUTUS_V2_KEY = "PlutusV2";

CostModel getGenesisCostModel();

void setGenesisCostModel(CostModel costModel);

@Transactional
void handleCostModel(AggregatedTx tx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.SimpleValue;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.bloxbean.cardano.client.plutus.spec.Language;
import com.bloxbean.cardano.client.util.HexUtil;
import com.bloxbean.cardano.client.util.Tuple;
import com.bloxbean.cardano.yaci.core.util.CborSerializationUtil;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.ledgersync.consumercommon.entity.CostModel;
import org.cardanofoundation.ledgersync.aggregate.AggregatedTx;
import org.cardanofoundation.ledgersync.common.common.cost.mdl.PlutusV1Keys;
import org.cardanofoundation.ledgersync.common.common.cost.mdl.PlutusV2Keys;
import org.cardanofoundation.ledgersync.common.util.JsonUtil;
import org.cardanofoundation.ledgersync.aggregate.AggregatedTx;
import org.cardanofoundation.ledgersync.consumercommon.entity.CostModel;
import org.cardanofoundation.ledgersync.repository.CostModelRepository;
import org.cardanofoundation.ledgersync.service.CostModelService;
import org.cardanofoundation.ledgersync.service.impl.plutus.PlutusKey;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigInteger;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -36,19 +37,6 @@
public class CostModelServiceImpl implements CostModelService {

final CostModelRepository costModelRepository;
CostModel genesisCostModel;


@Override
public CostModel getGenesisCostModel() {
return this.genesisCostModel;
}

@Override
public void setGenesisCostModel(CostModel costModel) {
setup(costModel);
}


@Override
public void handleCostModel(AggregatedTx tx) {
Expand All @@ -63,19 +51,28 @@ public void handleCostModel(AggregatedTx tx) {
.map(costModelMessage -> {
String hash = costModelMessage._1;
Map<Integer, String> costModelMap = costModelMessage._2;
var languageMap = costModelMap.keySet()

Map<String, Map<String, BigInteger>> languagePlutusV1V2Map = costModelMap.keySet()
.stream()
.filter(language -> language < 2)
.collect(Collectors.toMap(this::getPlutusKey,
language -> getPlutusValue(language,
convertCborCostModelToBigIntegerList(costModelMap.get(language)))));

Map<String, List<BigInteger>> languagePlutusV3Map = costModelMap.keySet().stream().
filter(language -> language == 3)
.collect(Collectors.toMap(this::getPlutusKey,
language -> convertCborCostModelToBigIntegerList(costModelMap.get(language))));
Map<String, Object> languageMap = new HashMap<>();
languageMap.putAll(languagePlutusV1V2Map);
languageMap.putAll(languagePlutusV3Map);
// var languageMap = costModelMessage.keySet()
// .stream()
// .collect(Collectors.toMap(this::getPlutusKey,
// language -> getPlutusValue(language,
// costModelMessage.getLanguages()
// .get(language))));
//

var json = JsonUtil.getPrettyJson(languageMap);
return CostModel.builder()
.costs(json)
Expand All @@ -99,25 +96,14 @@ public CostModel findCostModelByHash(String hash) {
return costModelOptional.orElse(null);
}

private String getPlutusKey(Language language) {
switch (language) {
case PLUTUS_V1:
return PLUTUS_V1_KEY;
case PLUTUS_V2:
return PLUTUS_V2_KEY;
default:
log.error("Un handle language {}", language);
System.exit(1);
}
return null;
}

private String getPlutusKey(int language) {
switch (language) {
case 0:
return PLUTUS_V1_KEY;
return PlutusKey.PLUTUS_V1.value;
case 1:
return PLUTUS_V2_KEY;
return PlutusKey.PLUTUS_V2.value;
case 2:
return PlutusKey.PLUTUS_V3.value;
default:
log.error("Un handle language {}", language);
System.exit(1);
Expand Down Expand Up @@ -145,11 +131,4 @@ private List<BigInteger> convertCborCostModelToBigIntegerList(String cborCostMod
.map(item -> ((UnsignedInteger) item).getValue())
.collect(Collectors.toList());
}

public void setup(CostModel costModel) {
costModelRepository.findByHash(costModel.getHash())
.ifPresentOrElse(cm -> genesisCostModel = cm, () ->
costModelRepository.save(costModel)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package org.cardanofoundation.ledgersync.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AccessLevel;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.ledgersync.consumercommon.entity.Block;
import org.cardanofoundation.ledgersync.consumercommon.entity.Epoch;
import org.cardanofoundation.ledgersync.consumercommon.entity.EpochParam;
import org.cardanofoundation.ledgersync.consumercommon.entity.ParamProposal;
import org.cardanofoundation.ledgersync.consumercommon.entity.*;
import org.cardanofoundation.ledgersync.consumercommon.enumeration.EraType;
import org.cardanofoundation.ledgersync.mapper.EpochParamMapper;
import org.cardanofoundation.ledgersync.repository.BlockRepository;
import org.cardanofoundation.ledgersync.repository.EpochParamRepository;
import org.cardanofoundation.ledgersync.repository.EpochRepository;
import org.cardanofoundation.ledgersync.repository.ParamProposalRepository;
import org.cardanofoundation.ledgersync.repository.*;
import org.cardanofoundation.ledgersync.service.CostModelService;
import org.cardanofoundation.ledgersync.service.EpochParamService;
import org.cardanofoundation.ledgersync.service.GenesisDataService;
import org.cardanofoundation.ledgersync.service.impl.plutus.PlutusKey;
import org.cardanofoundation.ledgersync.util.EpochParamUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


Expand All @@ -34,26 +34,29 @@ public class EpochParamServiceImpl implements EpochParamService {
final ParamProposalRepository paramProposalRepository;
final EpochParamRepository epochParamRepository;
final EpochRepository epochRepository;
final CostModelService costModelService;
final CostModelRepository costModelRepository;
// final CostModelService costModelService;
final GenesisDataService genesisDataService;
final EpochParamMapper epochParamMapper;
final ObjectMapper objectMapper;
EpochParam defShelleyEpochParam;
EpochParam defAlonzoEpochParam;
EpochParam defBabbageEpochParam;
EpochParam defConwayEpochParam;

public EpochParamServiceImpl(BlockRepository blockRepository, ParamProposalRepository paramProposalRepository,
EpochParamRepository epochParamRepository, EpochRepository epochRepository,
CostModelService costModelService,
CostModelRepository costModelRepository,
@Lazy GenesisDataService genesisDataService,
EpochParamMapper epochParamMapper) {
EpochParamMapper epochParamMapper, ObjectMapper objectMapper) {
this.blockRepository = blockRepository;
this.paramProposalRepository = paramProposalRepository;
this.epochParamRepository = epochParamRepository;
this.epochRepository = epochRepository;
this.costModelService = costModelService;
this.costModelRepository = costModelRepository;
this.genesisDataService = genesisDataService;
this.epochParamMapper = epochParamMapper;
this.objectMapper = objectMapper;
}

@Override
Expand Down Expand Up @@ -93,6 +96,7 @@ public void handleEpochParams() {
*
* @param epochNo
*/
@SneakyThrows
void handleEpochParam(int epochNo) {
EraType curEra = getEra(epochNo);
EraType prevEra = getEra(epochNo - BigInteger.ONE.intValue());
Expand All @@ -113,8 +117,9 @@ void handleEpochParam(int epochNo) {
if (curEra == EraType.ALONZO && prevEra == null) {
epochParamMapper.updateByEpochParam(curEpochParam, defShelleyEpochParam);
epochParamMapper.updateByEpochParam(curEpochParam, defAlonzoEpochParam);

curEpochParam.setCostModel(costModelService.getGenesisCostModel());
var costModel = defAlonzoEpochParam.getCostModel();
costModelRepository.save(costModel);
curEpochParam.setCostModel(costModel);
curEpochParam.setMinUtxoValue(null);
}

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

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

Expand All @@ -134,6 +141,34 @@ void handleEpochParam(int epochNo) {

if (curEra == EraType.CONWAY && prevEra == EraType.BABBAGE) {
epochParamMapper.updateByEpochParam(curEpochParam, defConwayEpochParam);
var genesisConwayCostModel = defConwayEpochParam.getCostModel();
var currentCostModel = CostModel.builder()
.hash(genesisConwayCostModel.getHash())
.build();
CostModel prevCostModel = prevEpochParam.map(EpochParam::getCostModel).orElse(null);
if (prevCostModel != null) {
// merge prev costs into genesis conway cost model
Map<String, Object> costs = objectMapper.readValue(prevCostModel.getCosts(), new TypeReference<>() {
});
Map<String, Object> genesisConwayCosts = objectMapper.readValue(genesisConwayCostModel.getCosts(), new TypeReference<>() {
});

Map<String, Object> mergedCosts = new HashMap<>();

costs.forEach((key, value) -> {
if (!mergedCosts.containsKey(key)) {
mergedCosts.put(key, value);
}
});
mergedCosts.put(PlutusKey.PLUTUS_V3.value, genesisConwayCosts.get(PlutusKey.PLUTUS_V3.value));

currentCostModel.setCosts(objectMapper.writeValueAsString(mergedCosts));
} else {
currentCostModel.setCosts(genesisConwayCostModel.getCosts());
}

costModelRepository.save(currentCostModel);
curEpochParam.setCostModel(currentCostModel);
}

List<ParamProposal> prevParamProposals = paramProposalRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private List<ParamProposal> handleParamProposal(AggregatedTx aggregatedTx, Tx tx
var govActionDeposit = protocolParamUpdate.getGovActionDeposit();
var drepDeposit = protocolParamUpdate.getDrepDeposit();
var drepActivity = toBigInteger(protocolParamUpdate.getDrepActivity());
var minFeeRefScriptCostPerByte = toBigInteger(protocolParamUpdate.getMinFeeRefScriptCostPerByte());
var minFeeRefScriptCostPerByte = toDouble(protocolParamUpdate.getMinFeeRefScriptCostPerByte());

return ParamProposal.builder()
.key(entrySet.getKey())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
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.yaci.core.model.certs.Certificate;
import com.bloxbean.cardano.yaci.core.model.certs.CertificateType;
import com.bloxbean.cardano.yaci.core.model.certs.StakeDelegation;
import com.bloxbean.cardano.yaci.core.model.certs.StakeDeregistration;
import com.bloxbean.cardano.yaci.core.model.certs.*;
import com.bloxbean.cardano.yaci.core.model.governance.ProposalProcedure;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -254,8 +251,17 @@ private String handleCertPtr(
}

// Stake de-registration
StakeDeregistration stakeDeregistration = (StakeDeregistration) certificate;
return stakeDeregistration.getStakeCredential().getHash();
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;
}

private String handleRewardPtr(List<String> rewardAccounts, int pointerIndex) {
Expand Down
Loading
Loading