Skip to content

Commit

Permalink
feat: serialize only valid conway params in gov action
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceahasegan committed Apr 17, 2024
1 parent 86bba7b commit 5e368f1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/Cardano/types/Governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Crypto from '@cardano-sdk/crypto';
import { Credential, CredentialType, RewardAccount } from '../Address';
import { EpochNo, Fraction, ProtocolVersion, TransactionId } from '.';
import { Lovelace } from './Value';
import { ProtocolParametersUpdate } from './ProtocolParameters';
import { ProtocolParametersUpdateConway } from './ProtocolParameters';

export type Anchor = {
url: string;
Expand Down Expand Up @@ -43,7 +43,7 @@ export type Constitution = {
export type ParameterChangeAction = {
__typename: GovernanceActionType.parameter_change_action;
governanceActionId: GovernanceActionId | null;
protocolParamUpdate: ProtocolParametersUpdate;
protocolParamUpdate: ProtocolParametersUpdateConway;
policyHash: Crypto.Hash28ByteBase16 | null;
};

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Cardano/types/ProtocolParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ type NewProtocolParamsInConway = {
minFeeRefScriptCostPerByte: string;
};

type ConwayProtocolParameters = BabbageProtocolParameters & NewProtocolParamsInConway;

export type ProtocolParameters = ConwayProtocolParameters;
export type ProtocolParameters = BabbageProtocolParameters & NewProtocolParamsInConway;
export type ConwayProtocolParameters = Omit<ProtocolParameters, 'protocolVersion'>;

// Even tho extraEntropy was deprecated on babbage era, and protocolVersion was deprecated in conway era,
// they are still present in the ProtocolParametersUpdate structure since this structure is backward compatible with all eras.
export type ProtocolParametersUpdate = Partial<ProtocolParameters & Pick<AlonzoProtocolParams, 'extraEntropy'>>;
export type ProtocolParametersUpdateConway = Partial<ConwayProtocolParameters>;

export type GenesisDelegateKeyHash = Crypto.Hash28ByteBase16;
export type ProposedProtocolParameterUpdates = Map<GenesisDelegateKeyHash, ProtocolParametersUpdate>;
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/Serialization/Update/ProtocolParamUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,9 @@ export class ProtocolParamUpdate {
*
* @param parametersUpdate core parametersUpdate object.
*/
static fromCore(parametersUpdate: Cardano.ProtocolParametersUpdate) {
static fromCore<T extends Cardano.ProtocolParametersUpdateConway = Cardano.ProtocolParametersUpdate>(
parametersUpdate: T
) {
const params = new ProtocolParamUpdate();

params.#minFeeA = parametersUpdate.minFeeCoefficient ? BigInt(parametersUpdate.minFeeCoefficient) : undefined;
Expand All @@ -470,14 +472,10 @@ export class ProtocolParamUpdate {
? UnitInterval.fromFloat(Number(parametersUpdate.decentralizationParameter))
: undefined;
params.#minPoolCost = parametersUpdate.minPoolCost ? BigInt(parametersUpdate.minPoolCost) : undefined;
params.#protocolVersion = parametersUpdate.protocolVersion
? ProtocolVersion.fromCore(parametersUpdate.protocolVersion)
: undefined;
params.#maxValueSize = parametersUpdate.maxValueSize;
params.#maxTxSize = parametersUpdate.maxTxSize;
params.#collateralPercentage = parametersUpdate.collateralPercentage;
params.#maxCollateralInputs = parametersUpdate.maxCollateralInputs;
params.#extraEntropy = parametersUpdate.extraEntropy ? HexBlob(parametersUpdate.extraEntropy) : undefined;
params.#costModels = parametersUpdate.costModels ? Costmdls.fromCore(parametersUpdate.costModels) : undefined;
params.#executionCosts = parametersUpdate.prices ? ExUnitPrices.fromCore(parametersUpdate.prices) : undefined;
params.#maxTxExUnits = parametersUpdate.maxExecutionUnitsPerTransaction
Expand All @@ -503,6 +501,12 @@ export class ProtocolParamUpdate {
? UnitInterval.fromFloat(Number(parametersUpdate.minFeeRefScriptCostPerByte))
: undefined;

const { protocolVersion, extraEntropy } = parametersUpdate as unknown as Cardano.ProtocolParametersUpdate;
if (protocolVersion !== undefined || extraEntropy !== undefined) {
params.#protocolVersion = protocolVersion ? ProtocolVersion.fromCore(protocolVersion) : undefined;
params.#extraEntropy = extraEntropy ? HexBlob(extraEntropy) : undefined;
}

return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ const vasilPlutusV2Costmdls = [

const core = {
__typename: Cardano.GovernanceActionType.parameter_change_action,
governanceActionId: { actionIndex: 3, id: '0000000000000000000000000000000000000000000000000000000000000000' },
governanceActionId: {
actionIndex: 3,
id: Cardano.TransactionId('0000000000000000000000000000000000000000000000000000000000000000')
},
policyHash: Hash28ByteBase16('8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d'),
protocolParamUpdate: {
coinsPerUtxoByte: 35_000,
collateralPercentage: 852,
committeeTermLimit: 200,
committeeTermLimit: Cardano.EpochNo(200),
costModels: new Map([
[PlutusLanguageVersion.V1, vasilPlutusV1Costmdls],
[PlutusLanguageVersion.V2, vasilPlutusV2Costmdls]
Expand Down Expand Up @@ -88,7 +91,6 @@ const core = {
securityRelevantParamVotingThreshold: { denominator: 1, numerator: 1 }
},
prices: { memory: 0.5, steps: 0.5 },
protocolVersion: { major: 1, minor: 3 },
stakeKeyDeposit: 2_000_000,
treasuryExpansion: '0.25'
}
Expand Down
2 changes: 1 addition & 1 deletion packages/util-dev/src/mockProviders/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const protocolParameters = {
prices: { memory: 0.0577, steps: 0.000_007_21 },
protocolVersion: { major: 5, minor: 0 },
stakeKeyDeposit: 2_000_000
} as Cardano.ProtocolParameters;
} as Required<Cardano.ProtocolParametersUpdate>;

export const epochRewards = [
{
Expand Down

0 comments on commit 5e368f1

Please sign in to comment.