Skip to content

Commit

Permalink
chore: conway genesis cost model
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-HuyLe3a committed May 21, 2024
1 parent bca5fd6 commit 0711a8b
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 44 deletions.
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;
EpochParam shelley;
EpochParam alonzo;
EpochParam babbage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import org.cardanofoundation.ledgersync.consumercommon.entity.CostModel;
import org.cardanofoundation.ledgersync.aggregate.AggregatedTx;
import org.cardanofoundation.ledgersync.service.impl.plutus.PlutusKey;
import org.springframework.transaction.annotation.Transactional;

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

CostModel getGenesisCostModel();
CostModel getGenesisCostModel(PlutusKey plutusKey);

void setGenesisCostModel(CostModel costModel);
void setGenesisCostModel(PlutusKey plutusKey, CostModel costModel);

@Transactional
void handleCostModel(AggregatedTx tx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import org.cardanofoundation.ledgersync.aggregate.AggregatedTx;
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,17 +38,16 @@
public class CostModelServiceImpl implements CostModelService {

final CostModelRepository costModelRepository;
CostModel genesisCostModel;

final Map<PlutusKey, CostModel> genesisCostModelMap = new HashMap<>();

@Override
public CostModel getGenesisCostModel() {
return this.genesisCostModel;
public CostModel getGenesisCostModel(PlutusKey plutusKey) {
return this.genesisCostModelMap.get(plutusKey);
}

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


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

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

// TODO: plutus v3
// var languageMap = costModelMessage.keySet()
// .stream()
// .collect(Collectors.toMap(this::getPlutusKey,
// language -> getPlutusValue(language,
// costModelMessage.getLanguages()
// .get(language))));
//
var json = JsonUtil.getPrettyJson(languageMap);

var json = JsonUtil.getPrettyJson(languagePlutusV1V2Map);
return CostModel.builder()
.costs(json)
.hash(hash)
Expand All @@ -102,9 +106,9 @@ public CostModel findCostModelByHash(String hash) {
private String getPlutusKey(Language language) {
switch (language) {
case PLUTUS_V1:
return PLUTUS_V1_KEY;
return PlutusKey.PLUTUS_V1.value;
case PLUTUS_V2:
return PLUTUS_V2_KEY;
return PlutusKey.PLUTUS_V2.value;
default:
log.error("Un handle language {}", language);
System.exit(1);
Expand All @@ -115,9 +119,11 @@ private String getPlutusKey(Language language) {
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 @@ -146,10 +152,12 @@ private List<BigInteger> convertCborCostModelToBigIntegerList(String cborCostMod
.collect(Collectors.toList());
}

public void setup(CostModel costModel) {
public void setup(PlutusKey plutusKey, CostModel costModel) {
costModelRepository.findByHash(costModel.getHash())
.ifPresentOrElse(cm -> genesisCostModel = cm, () ->
costModelRepository.save(costModel)
.ifPresentOrElse(cm -> genesisCostModelMap.put(plutusKey, cm), () -> {
costModelRepository.save(costModel);
genesisCostModelMap.put(plutusKey, costModel);
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -114,7 +115,9 @@ void handleEpochParam(int epochNo) {
epochParamMapper.updateByEpochParam(curEpochParam, defShelleyEpochParam);
epochParamMapper.updateByEpochParam(curEpochParam, defAlonzoEpochParam);

curEpochParam.setCostModel(costModelService.getGenesisCostModel());
costModelService.setGenesisCostModel(PlutusKey.PLUTUS_V1, defAlonzoEpochParam.getCostModel());
curEpochParam.setCostModel(costModelService.getGenesisCostModel(PlutusKey.PLUTUS_V1));

curEpochParam.setMinUtxoValue(null);
}

Expand All @@ -124,7 +127,10 @@ void handleEpochParam(int epochNo) {

if (curEra == EraType.ALONZO && prevEra == EraType.MARY) {
epochParamMapper.updateByEpochParam(curEpochParam, defAlonzoEpochParam);
curEpochParam.setCostModel(costModelService.getGenesisCostModel());

costModelService.setGenesisCostModel(PlutusKey.PLUTUS_V1, defAlonzoEpochParam.getCostModel());
curEpochParam.setCostModel(costModelService.getGenesisCostModel(PlutusKey.PLUTUS_V1));

curEpochParam.setMinUtxoValue(null);
}

Expand All @@ -134,6 +140,8 @@ void handleEpochParam(int epochNo) {

if (curEra == EraType.CONWAY && prevEra == EraType.BABBAGE) {
epochParamMapper.updateByEpochParam(curEpochParam, defConwayEpochParam);
costModelService.setGenesisCostModel(PlutusKey.PLUTUS_V3, defConwayEpochParam.getCostModel());
curEpochParam.setCostModel(costModelService.getGenesisCostModel(PlutusKey.PLUTUS_V3));
}

List<ParamProposal> prevParamProposals = paramProposalRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public class GenesisDataServiceImpl implements GenesisDataService {
private final static String MEMBERS = "members";
private final static String QUORUM = "quorum";

private final static String PLUTUS_V_3_COST_MODEL = "plutusV3CostModel";

@Value("${genesis.byron}")
String genesisByron;
Expand Down Expand Up @@ -172,8 +173,8 @@ void init(){
epochParamService.setDefBabbageEpochParam(genesisData.getBabbage());
log.info("setup conway genesis");
epochParamService.setDefConwayEpochParam(genesisData.getConway());
log.info("setup genesis cost model");
costModelService.setGenesisCostModel(genesisData.getCostModel());
// log.info("setup genesis cost model");
// costModelService.setGenesisCostModel(genesisData.getAlonzoCostModel());
}

@Transactional
Expand Down Expand Up @@ -375,6 +376,13 @@ public void fetchConwayGenesis(GenesisData genesisData) {
final var poolVotingThresholds = (Map<String, Object>) genesisConwayJsonMap.get(POOL_VOTING_THRESHOLDS);
final var dRepVotingThresholds = (Map<String, Object>) genesisConwayJsonMap.get(D_REP_VOTING_THRESHOLDS);

final Map<String, List<Long>> plutusV3CostModel = new HashMap<>();
plutusV3CostModel.put(CostModelConverter.PLUTUS_V3, (ArrayList<Long>) genesisConwayJsonMap.get(PLUTUS_V_3_COST_MODEL));
final var costModel = CostModel.builder()
.costs(objectMapper.writeValueAsString(plutusV3CostModel))
.hash("genesis.conway") //TODO check later
.build();

EpochParam genesisShelleyProtocols = EpochParam.builder()
.pvtCommitteeNormal(convertObjectToBigDecimal(poolVotingThresholds.get(PVT_COMMITTEE_NORMAL)).doubleValue())
.pvtCommitteeNoConfidence(convertObjectToBigDecimal(poolVotingThresholds.get(PVT_COMMITTEE_NO_CONFIDENCE)).doubleValue())
Expand All @@ -397,6 +405,7 @@ public void fetchConwayGenesis(GenesisData genesisData) {
.govActionDeposit(convertObjecToBigInteger(genesisConwayJsonMap.get(GOV_ACTION_DEPOSIT)))
.drepDeposit(convertObjecToBigInteger(genesisConwayJsonMap.get(D_REP_DEPOSIT)))
.drepActivity(convertObjecToBigInteger(genesisConwayJsonMap.get(D_REP_ACTIVITY)))
.costModel(costModel)
.build();

genesisData.setConway(genesisShelleyProtocols);
Expand Down Expand Up @@ -457,7 +466,7 @@ public void fetchAlonzoGenesis(GenesisData genesisData) {
.build();

genesisData.setAlonzo(genesisShelleyProtocols);
genesisData.setCostModel(costModel);
genesisData.setAlonzoCostModel(costModel);
} catch (Exception e) {
log.error("Genesis data at {} can't parse from json to java object", genesisAlonzo);
log.error("{} value \n {}", genesisAlonzo, genesisAlonzoJson);
Expand Down
Loading

0 comments on commit 0711a8b

Please sign in to comment.