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

Feat/lw 9927 review protocol params update #1212

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 @@ -67,11 +67,12 @@ export const toProtocolParams = ({
governance_action_validity_period,
governance_action_deposit,
drep_deposit,
drep_inactivity_period
drep_inactivity_period,
min_fee_ref_script_cost_per_byte
}: ProtocolParamsModel): Cardano.ProtocolParameters => ({
coinsPerUtxoByte: Number(coins_per_utxo_size),
collateralPercentage: collateral_percent,
committeeTermLimit: Number(committee_term_limit),
committeeTermLimit: Cardano.EpochNo(committee_term_limit),
costModels: mapCostModels(costs),
dRepDeposit: Number(drep_deposit),
dRepInactivityPeriod: Cardano.EpochNo(drep_inactivity_period),
Expand All @@ -96,6 +97,7 @@ export const toProtocolParams = ({
minCommitteeSize: Number(min_committee_size),
minFeeCoefficient: min_fee_a,
minFeeConstant: min_fee_b,
minFeeRefScriptCostPerByte: String(min_fee_ref_script_cost_per_byte),
minPoolCost: Number(min_pool_cost),
monetaryExpansion: String(monetary_expand_rate),
poolDeposit: Number(pool_deposit),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface ProtocolParamsModel {
max_bh_size: number;
optimal_pool_count: number;
influence: number;
min_fee_ref_script_cost_per_byte: number;
monetary_expand_rate: number;
treasury_growth_rate: number;
decentralisation: number;
Expand Down
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
19 changes: 11 additions & 8 deletions packages/core/src/Cardano/types/ProtocolParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ type BabbageProtocolParameters = Omit<AlonzoProtocolParams, 'coinsPerUtxoWord' |
export interface PoolVotingThresholds {
motionNoConfidence: Fraction;
committeeNormal: Fraction;
commiteeNoConfidence: Fraction;
committeeNoConfidence: Fraction;
hardForkInitiation: Fraction;
securityRelevantParamVotingThreshold: Fraction;
}

export interface DelegateRepresentativeThresholds extends PoolVotingThresholds {
export interface DelegateRepresentativeThresholds
extends Omit<PoolVotingThresholds, 'securityRelevantParamVotingThreshold'> {
updateConstitution: Fraction;
ppNetworkGroup: Fraction;
ppEconomicGroup: Fraction;
Expand All @@ -127,20 +129,21 @@ type NewProtocolParamsInConway = {
poolVotingThresholds: PoolVotingThresholds;
dRepVotingThresholds: DelegateRepresentativeThresholds;
minCommitteeSize: number;
committeeTermLimit: number;
committeeTermLimit: EpochNo;
governanceActionValidityPeriod: EpochNo;
governanceActionDeposit: number;
dRepDeposit: number;
dRepInactivityPeriod: EpochNo;
minFeeRefScriptCostPerByte: string;
};

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

export type ProtocolParameters = ConwayProtocolParameters;

// Even tho extraEntropy was deprecated on babbage era, it is still present in the ProtocolParametersUpdate structure
// since this structure is backward compatible with all eras.
// 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
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class DrepVotingThresholds {
*/
toCore(): Cardano.DelegateRepresentativeThresholds {
return {
commiteeNoConfidence: this.#committeeNoConfidence.toCore(),
committeeNoConfidence: this.#committeeNoConfidence.toCore(),
committeeNormal: this.#committeeNormal.toCore(),
hardForkInitiation: this.#hardForkInitiation.toCore(),
motionNoConfidence: this.#motionNoConfidence.toCore(),
Expand All @@ -190,7 +190,7 @@ export class DrepVotingThresholds {
return new DrepVotingThresholds(
UnitInterval.fromCore(core.motionNoConfidence),
UnitInterval.fromCore(core.committeeNormal),
UnitInterval.fromCore(core.commiteeNoConfidence),
UnitInterval.fromCore(core.committeeNoConfidence),
UnitInterval.fromCore(core.updateConstitution),
UnitInterval.fromCore(core.hardForkInitiation),
UnitInterval.fromCore(core.ppNetworkGroup),
Expand Down
34 changes: 26 additions & 8 deletions packages/core/src/Serialization/Update/PoolVotingThresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CborReader, CborWriter } from '../CBOR';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { UnitInterval } from '../Common';

const POOL_VOTING_THRESHOLDS_SIZE = 4;
const POOL_VOTING_THRESHOLDS_SIZE = 5;

/**
* Governance actions are ratified through on-chain voting. Different
Expand All @@ -17,6 +17,7 @@ export class PoolVotingThresholds {
#committeeNormal: UnitInterval;
#committeeNoConfidence: UnitInterval;
#hardForkInitiation: UnitInterval;
#securityRelevantParamVotingThreshold: UnitInterval;
#originalBytes: HexBlob | undefined = undefined;

/**
Expand All @@ -35,12 +36,14 @@ export class PoolVotingThresholds {
motionNoConfidence: UnitInterval,
committeeNormal: UnitInterval,
committeeNoConfidence: UnitInterval,
hardForkInitiation: UnitInterval
hardForkInitiation: UnitInterval,
securityRelevantParamVotingThreshold: UnitInterval
) {
this.#motionNoConfidence = motionNoConfidence;
this.#committeeNormal = committeeNormal;
this.#committeeNoConfidence = committeeNoConfidence;
this.#hardForkInitiation = hardForkInitiation;
this.#securityRelevantParamVotingThreshold = securityRelevantParamVotingThreshold;
}

/**
Expand All @@ -59,13 +62,15 @@ export class PoolVotingThresholds {
// , unit_interval ; committee normal
// , unit_interval ; committee no confidence
// , unit_interval ; hard fork initiation
// , unit_interval ; security relevant parameter voting threshold
// ]
writer.writeStartArray(POOL_VOTING_THRESHOLDS_SIZE);

writer.writeEncodedValue(Buffer.from(this.#motionNoConfidence.toCbor(), 'hex'));
writer.writeEncodedValue(Buffer.from(this.#committeeNormal.toCbor(), 'hex'));
writer.writeEncodedValue(Buffer.from(this.#committeeNoConfidence.toCbor(), 'hex'));
writer.writeEncodedValue(Buffer.from(this.#hardForkInitiation.toCbor(), 'hex'));
writer.writeEncodedValue(Buffer.from(this.#securityRelevantParamVotingThreshold.toCbor(), 'hex'));

return writer.encodeAsHex();
}
Expand All @@ -91,14 +96,16 @@ export class PoolVotingThresholds {
const committeeNormal = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
const committeeNoConfidence = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
const hardForkInitiation = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
const securityRelevantParamVotingThreshold = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));

reader.readEndArray();

const thresholds = new PoolVotingThresholds(
motionNoConfidence,
committeeNormal,
committeeNoConfidence,
hardForkInitiation
hardForkInitiation,
securityRelevantParamVotingThreshold
);

thresholds.#originalBytes = cbor;
Expand All @@ -107,16 +114,17 @@ export class PoolVotingThresholds {
}

/**
* Creates a Core PoolVotingThresholdsSHOLDS_SIZE object from the current PoolVotingThresholds object.
* Creates a Core PoolVotingThresholds object from the current PoolVotingThresholds object.
*
* @returns The Core Prices object.
*/
toCore(): Cardano.PoolVotingThresholds {
return {
commiteeNoConfidence: this.#committeeNoConfidence.toCore(),
committeeNoConfidence: this.#committeeNoConfidence.toCore(),
committeeNormal: this.#committeeNormal.toCore(),
hardForkInitiation: this.#hardForkInitiation.toCore(),
motionNoConfidence: this.#motionNoConfidence.toCore()
motionNoConfidence: this.#motionNoConfidence.toCore(),
securityRelevantParamVotingThreshold: this.#securityRelevantParamVotingThreshold.toCore()
};
}

Expand All @@ -129,8 +137,9 @@ export class PoolVotingThresholds {
return new PoolVotingThresholds(
UnitInterval.fromCore(core.motionNoConfidence),
UnitInterval.fromCore(core.committeeNormal),
UnitInterval.fromCore(core.commiteeNoConfidence),
UnitInterval.fromCore(core.hardForkInitiation)
UnitInterval.fromCore(core.committeeNoConfidence),
UnitInterval.fromCore(core.hardForkInitiation),
UnitInterval.fromCore(core.securityRelevantParamVotingThreshold)
);
}

Expand Down Expand Up @@ -223,4 +232,13 @@ export class PoolVotingThresholds {
hardForkInitiation(): UnitInterval {
return this.#hardForkInitiation;
}

/**
* Gets the security relevant parameter voting threshold
*
* @returns security relevant parameter voting threshold.
*/
securityRelevantParamVotingThreshold(): UnitInterval {
return this.#securityRelevantParamVotingThreshold;
}
}
38 changes: 32 additions & 6 deletions packages/core/src/Serialization/Update/ProtocolParamUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class ProtocolParamUpdate {
#governanceActionDeposit: number | undefined;
#drepDeposit: number | undefined;
#drepInactivityPeriod: number | undefined;
#minFeeRefScriptCostPerByte: UnitInterval | undefined;
#originalBytes: HexBlob | undefined = undefined;

/**
Expand Down Expand Up @@ -90,6 +91,7 @@ export class ProtocolParamUpdate {
// , ? 30: coin ; governance action deposit
// , ? 31: coin ; DRep deposit
// , ? 32: epoch ; DRep inactivity period
// , ? 33: nonnegative_interval ; MinFee RefScriptCostPerByte
// }
writer.writeStartMap(this.#getMapSize());

Expand Down Expand Up @@ -254,6 +256,12 @@ export class ProtocolParamUpdate {
writer.writeInt(32n);
writer.writeInt(this.#drepInactivityPeriod);
}

if (this.#minFeeRefScriptCostPerByte) {
writer.writeInt(33n);
writer.writeEncodedValue(Buffer.from(this.#minFeeRefScriptCostPerByte.toCbor(), 'hex'));
}

return writer.encodeAsHex();
}

Expand Down Expand Up @@ -373,6 +381,9 @@ export class ProtocolParamUpdate {
case 32n:
params.#drepInactivityPeriod = Number(reader.readInt());
break;
case 33n:
params.#minFeeRefScriptCostPerByte = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
break;
}
}

Expand All @@ -392,7 +403,7 @@ export class ProtocolParamUpdate {
return {
coinsPerUtxoByte: this.#adaPerUtxoByte ? Number(this.#adaPerUtxoByte) : undefined,
collateralPercentage: this.#collateralPercentage,
committeeTermLimit: this.#committeeTermLimit,
committeeTermLimit: this.#committeeTermLimit ? Cardano.EpochNo(this.#committeeTermLimit) : undefined,
costModels: this.#costModels?.toCore(),
dRepDeposit: this.#drepDeposit,
dRepInactivityPeriod: this.#drepInactivityPeriod ? Cardano.EpochNo(this.#drepInactivityPeriod) : undefined,
Expand All @@ -414,6 +425,9 @@ export class ProtocolParamUpdate {
minCommitteeSize: this.#minCommitteeSize,
minFeeCoefficient: this.#minFeeA ? Number(this.#minFeeA) : undefined,
minFeeConstant: this.#minFeeB ? Number(this.#minFeeB) : undefined,
minFeeRefScriptCostPerByte: this.#minFeeRefScriptCostPerByte
? this.#minFeeRefScriptCostPerByte.toFloat().toString()
: undefined,
minPoolCost: this.#minPoolCost ? Number(this.#minPoolCost) : undefined,
monetaryExpansion: this.#expansionRate ? this.#expansionRate.toFloat().toString() : undefined,
poolDeposit: this.#poolDeposit ? Number(this.#poolDeposit) : undefined,
Expand All @@ -432,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 @@ -456,14 +472,10 @@ export class ProtocolParamUpdate {
? UnitInterval.fromFloat(Number(parametersUpdate.decentralizationParameter))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

decentralizationParameter was also removed in conway era.
I am investigating this and a few other serialization issues on the conway-era branch.
I will update this PR once the implementation is stable.

Copy link
Member

@AngelCastilloB AngelCastilloB Apr 18, 2024

Choose a reason for hiding this comment

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

decentralizationParameter was actually deprecated in babbage era, but there shouldn't be a need to remove this, that way the serialization utils can be use to serialize/deserialize older transactions too. When this fields are deprecated their key is no longer used (in this case I believe is 12), but no other field can take its place, so there is no harm on leaving them there

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In my particular case (e2e test), the raw txBuilder is passing a manually built protocol params to a conway params update proposal.
I am trying to prevent this by building the typesafe version of the fromCore when called via conway governance.
Otherwise, the effect is that txbuilder will build the tx for you, even if those protocol params were deprecated.
But the change will not cripple the ser/des part, it will only make it more restrictive when called from the gov path

Copy link
Member

Choose a reason for hiding this comment

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

thats a pretty good solution 🚀

: 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 @@ -485,6 +497,15 @@ export class ProtocolParamUpdate {
params.#governanceActionDeposit = parametersUpdate.governanceActionDeposit;
params.#drepDeposit = parametersUpdate.dRepDeposit;
params.#drepInactivityPeriod = parametersUpdate.dRepInactivityPeriod;
params.#minFeeRefScriptCostPerByte = parametersUpdate.minFeeRefScriptCostPerByte
? 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 Expand Up @@ -1177,6 +1198,10 @@ export class ProtocolParamUpdate {
return this.#drepInactivityPeriod;
}

minFeeRefScriptCostPerByte(): UnitInterval | undefined {
return this.#minFeeRefScriptCostPerByte;
}

/**
* Gets the size of the serialized map.
*
Expand Down Expand Up @@ -1217,6 +1242,7 @@ export class ProtocolParamUpdate {
if (this.#governanceActionDeposit !== undefined) ++mapSize;
if (this.#drepDeposit !== undefined) ++mapSize;
if (this.#drepInactivityPeriod !== undefined) ++mapSize;
if (this.#minFeeRefScriptCostPerByte !== undefined) ++mapSize;

return mapSize;
}
Expand Down
Loading
Loading