Skip to content

Commit

Permalink
feat(core): conway-era cddl changes new policy_hash field
Browse files Browse the repository at this point in the history
parameter_change_action and treasury_withdrawals_action got an extra field policy_hash / null.
parameter_change_action = (0, gov_action_id / null, protocol_param_update, policy_hash / null)
treasury_withdrawals_action = (2, { reward_account => coin }, policy_hash / null)
  • Loading branch information
mirceahasegan committed Feb 28, 2024
1 parent 3692734 commit eff403e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/Cardano/types/Governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type ParameterChangeAction = {
__typename: GovernanceActionType.parameter_change_action;
governanceActionId: GovernanceActionId | null;
protocolParamUpdate: ProtocolParametersUpdate;
policyHash: Crypto.Hash28ByteBase16 | null;
};

export type HardForkInitiationAction = {
Expand All @@ -58,6 +59,7 @@ export type TreasuryWithdrawalsAction = {
rewardAccount: RewardAccount;
coin: Lovelace;
}>;
policyHash: Crypto.Hash28ByteBase16 | null;
};

export type NoConfidence = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,35 @@ import * as Cardano from '../../../Cardano';
import { CborReader, CborReaderState, CborWriter } from '../../CBOR';
import { GovernanceActionId } from '../../Common/GovernanceActionId';
import { GovernanceActionKind } from './GovernanceActionKind';
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { ProtocolParamUpdate } from '../../Update';
import { hexToBytes } from '../../../util/misc';

const EMBEDDED_GROUP_SIZE = 3;
const EMBEDDED_GROUP_SIZE = 4;

/** Updates one or more updatable protocol parameters, excluding changes to major protocol versions (i.e., "hard forks"). */
export class ParameterChangeAction {
#protocolParamUpdate: ProtocolParamUpdate;
#govActionId: GovernanceActionId | undefined;
#policyHash: Hash28ByteBase16 | undefined;
#originalBytes: HexBlob | undefined = undefined;

/**
* Creates a new ParameterChangeAction instance.
*
* @param protocolParamUpdate The protocol parameter updates.
* @param govActionId The optional unique identifier for this governance action.
* @param policyHash The optional policyHash.
*/
constructor(protocolParamUpdate: ProtocolParamUpdate, govActionId?: GovernanceActionId) {
constructor(
protocolParamUpdate: ProtocolParamUpdate,
govActionId?: GovernanceActionId,
policyHash?: Hash28ByteBase16
) {
this.#protocolParamUpdate = protocolParamUpdate;
this.#govActionId = govActionId;
this.#policyHash = policyHash;
}

/**
Expand All @@ -36,11 +44,12 @@ export class ParameterChangeAction {
if (this.#originalBytes) return this.#originalBytes;

// CDDL
// parameter_change_action = (0, gov_action_id / null, protocol_param_update)
// parameter_change_action = (0, gov_action_id / null, protocol_param_update, policy_hash / null)
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(GovernanceActionKind.ParameterChange);
this.#govActionId ? writer.writeEncodedValue(hexToBytes(this.#govActionId.toCbor())) : writer.writeNull();
writer.writeEncodedValue(hexToBytes(this.#protocolParamUpdate.toCbor()));
this.#policyHash ? writer.writeByteString(hexToBytes(this.#policyHash as unknown as HexBlob)) : writer.writeNull();

return writer.encodeAsHex();
}
Expand Down Expand Up @@ -79,7 +88,16 @@ export class ParameterChangeAction {

const parameterUpdate = ProtocolParamUpdate.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));

const action = new ParameterChangeAction(parameterUpdate, govActionId);
let policyHash: Hash28ByteBase16 | undefined;
if (reader.peekState() === CborReaderState.Null) {
reader.readNull();
} else {
policyHash = HexBlob.fromBytes(reader.readByteString()) as unknown as Hash28ByteBase16;
}

reader.readEndArray();

const action = new ParameterChangeAction(parameterUpdate, govActionId, policyHash);
action.#originalBytes = cbor;

return action;
Expand All @@ -94,6 +112,7 @@ export class ParameterChangeAction {
return {
__typename: Cardano.GovernanceActionType.parameter_change_action,
governanceActionId: this.#govActionId ? this.#govActionId.toCore() : null,
policyHash: this.#policyHash ? this.#policyHash : null,
protocolParamUpdate: this.#protocolParamUpdate.toCore()
};
}
Expand All @@ -108,7 +127,8 @@ export class ParameterChangeAction {
ProtocolParamUpdate.fromCore(parameterChangeAction.protocolParamUpdate),
parameterChangeAction.governanceActionId !== null
? GovernanceActionId.fromCore(parameterChangeAction.governanceActionId)
: undefined
: undefined,
parameterChangeAction.policyHash !== null ? parameterChangeAction.policyHash : undefined
);
}

Expand All @@ -129,4 +149,11 @@ export class ParameterChangeAction {
protocolParamUpdate(): ProtocolParamUpdate {
return this.#protocolParamUpdate;
}

/**
* @returns the policyHash.
*/
policyHash(): Hash28ByteBase16 | undefined {
return this.#policyHash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ import * as Cardano from '../../../Cardano';
import { CborReader, CborReaderState, CborWriter } from '../../CBOR';
import { GovernanceActionKind } from './GovernanceActionKind';
import { GovernanceActionType } from '../../../Cardano';
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { SerializationError, SerializationFailure } from '../../../errors';
import { hexToBytes } from '../../../util/misc';

const EMBEDDED_GROUP_SIZE = 2;
const EMBEDDED_GROUP_SIZE = 3;

/** Withdraws funds from the treasury. */
export class TreasuryWithdrawalsAction {
#withdrawals: Map<Cardano.RewardAccount, Cardano.Lovelace>;
#policyHash: Hash28ByteBase16 | undefined;
#originalBytes: HexBlob | undefined = undefined;

/**
* Creates a new TreasuryWithdrawalsAction instance.
*
* @param withdrawals A map specifying which rewards accounts to transfer the funds to.
* @param policyHash The optional policyHash.
*/
constructor(withdrawals: Map<Cardano.RewardAccount, Cardano.Lovelace>) {
constructor(withdrawals: Map<Cardano.RewardAccount, Cardano.Lovelace>, policyHash?: Hash28ByteBase16) {
this.#withdrawals = withdrawals;
this.#policyHash = policyHash;
}

/**
Expand All @@ -32,7 +37,7 @@ export class TreasuryWithdrawalsAction {
if (this.#originalBytes) return this.#originalBytes;

// CDDL
// treasury_withdrawals_action = (2, { reward_account => coin })
// treasury_withdrawals_action = (2, { reward_account => coin }, policy_hash / null)
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(GovernanceActionKind.TreasuryWithdrawals);

Expand All @@ -50,6 +55,7 @@ export class TreasuryWithdrawalsAction {
writer.writeByteString(Buffer.from(rewardAddress.toAddress().toBytes(), 'hex'));
writer.writeInt(value);
}
this.#policyHash ? writer.writeByteString(hexToBytes(this.#policyHash as unknown as HexBlob)) : writer.writeNull();

return writer.encodeAsHex();
}
Expand Down Expand Up @@ -93,7 +99,16 @@ export class TreasuryWithdrawalsAction {

reader.readEndMap();

const action = new TreasuryWithdrawalsAction(amounts);
let policyHash: Hash28ByteBase16 | undefined;
if (reader.peekState() === CborReaderState.Null) {
reader.readNull();
} else {
policyHash = HexBlob.fromBytes(reader.readByteString()) as unknown as Hash28ByteBase16;
}

reader.readEndArray();

const action = new TreasuryWithdrawalsAction(amounts, policyHash);
action.#originalBytes = cbor;

return action;
Expand All @@ -114,6 +129,7 @@ export class TreasuryWithdrawalsAction {

return {
__typename: GovernanceActionType.treasury_withdrawals_action,
policyHash: this.#policyHash ? this.#policyHash : null,
withdrawals
};
}
Expand All @@ -125,7 +141,8 @@ export class TreasuryWithdrawalsAction {
*/
static fromCore(treasuryWithdrawalsAction: Cardano.TreasuryWithdrawalsAction) {
return new TreasuryWithdrawalsAction(
new Map([...treasuryWithdrawalsAction.withdrawals].map((value) => [value.rewardAccount, value.coin]))
new Map([...treasuryWithdrawalsAction.withdrawals].map((value) => [value.rewardAccount, value.coin])),
treasuryWithdrawalsAction.policyHash !== null ? treasuryWithdrawalsAction.policyHash : undefined
);
}

Expand All @@ -137,4 +154,11 @@ export class TreasuryWithdrawalsAction {
withdrawals(): Map<Cardano.RewardAccount, Cardano.Lovelace> {
return this.#withdrawals;
}

/**
* @returns the policyHash.
*/
policyHash(): Hash28ByteBase16 | undefined {
return this.#policyHash;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable sonarjs/no-duplicate-string */
import * as Cardano from '../../../../src/Cardano';
import { EpochNo, PlutusLanguageVersion } from '../../../../src/Cardano';
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob } from '@cardano-sdk/util';
import { ParameterChangeAction } from '../../../../src/Serialization';

// Test data used in the following tests was generated with the cardano-serialization-lib
const cbor = HexBlob(
'8300825820000000000000000000000000000000000000000000000000000000000000000003b8200018640118c80219012c03190190041901f4051a001e8480061a0bebc200071903200819038409d81e8201020ad81e8201030bd81e8201040cd81e8201050d8201582000000000000000000000000000000000000000000000000000000000000000000e820103101903e8111988b812a20098a61a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a0374f693194a1f0a0198af1a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a0011b22c1a0005fdde00021a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a0223accc0a1a0374f693194a1f0a1a02515e841980b30a1382d81e820102d81e82010214821b00000001000000001b000000010000000015821b00000001000000001b0000000100000000161903ba1719035418181864181984d81e820000d81e820101d81e820202d81e820303181a8ad81e820000d81e820101d81e820202d81e820303d81e820404d81e820505d81e820606d81e820707d81e820808d81e820909181b1864181c18c8181d19012c181e1903e8181f1907d01820191388'
'8400825820000000000000000000000000000000000000000000000000000000000000000003b8200018640118c80219012c03190190041901f4051a001e8480061a0bebc200071903200819038409d81e8201020ad81e8201030bd81e8201040cd81e8201050d8201582000000000000000000000000000000000000000000000000000000000000000000e820103101903e8111988b812a20098a61a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a0374f693194a1f0a0198af1a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a0011b22c1a0005fdde00021a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a0223accc0a1a0374f693194a1f0a1a02515e841980b30a1382d81e820102d81e82010214821b00000001000000001b000000010000000015821b00000001000000001b0000000100000000161903ba1719035418181864181984d81e820000d81e820101d81e820202d81e820303181a8ad81e820000d81e820101d81e820202d81e820303d81e820404d81e820505d81e820606d81e820707d81e820808d81e820909181b1864181c18c8181d19012c181e1903e8181f1907d01820191388581c8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d'
);

const vasilPlutusV1Costmdls = [
Expand Down Expand Up @@ -36,6 +37,7 @@ const vasilPlutusV2Costmdls = [
const core = {
__typename: Cardano.GovernanceActionType.parameter_change_action,
governanceActionId: { actionIndex: 3, id: '0000000000000000000000000000000000000000000000000000000000000000' },
policyHash: Hash28ByteBase16('8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d'),
protocolParamUpdate: {
coinsPerUtxoByte: 35_000,
collateralPercentage: 852,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* eslint-disable sonarjs/no-duplicate-string */
import * as Cardano from '../../../../src/Cardano';
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob } from '@cardano-sdk/util';
import { TreasuryWithdrawalsAction } from '../../../../src/Serialization';

// Test data used in the following tests was generated with the cardano-serialization-lib
const cbor = HexBlob('8202a1581de1cb0ec2692497b458e46812c8a5bfa2931d1a2d965a99893828ec810f01');
const cbor = HexBlob(
'8302a1581de1cb0ec2692497b458e46812c8a5bfa2931d1a2d965a99893828ec810f01581c8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d'
);

const core = {
__typename: Cardano.GovernanceActionType.treasury_withdrawals_action,
policyHash: Hash28ByteBase16('8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d'),
withdrawals: new Set([
{
coin: 1n,
Expand Down

0 comments on commit eff403e

Please sign in to comment.