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

LW-10771 Conway era pre and post hard fork #1337

Merged
merged 3 commits into from
Jun 19, 2024
Merged
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
11 changes: 3 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,11 @@ FROM nodejs-builder as cardano-services-builder
RUN yarn --immutable --inline-builds --mode=skip-build
COPY packages packages
RUN \
echo "export const unused = 'unused';" > packages/e2e/src/index.ts &&\
NODE_OPTIONS=--max_old_space_size=10240 yarn build:cjs
yarn workspace @cardano-sdk/cardano-services build &&\
yarn workspace @cardano-sdk/cardano-services-client build

FROM nodejs-builder as cardano-services-production-deps
FROM nodejs-builder as cardano-services
RUN yarn workspaces focus --all --production

FROM ubuntu-nodejs as cardano-services
COPY --from=cardano-services-production-deps /app/node_modules /app/node_modules
COPY --from=cardano-services-production-deps /app/packages/cardano-services/node_modules /app/packages/cardano-services/node_modules
COPY --from=cardano-services-production-deps /app/packages/core/node_modules /app/packages/core/node_modules
COPY --from=cardano-services-builder /app/scripts /app/scripts
COPY --from=cardano-services-builder /app/packages/cardano-services/dist /app/packages/cardano-services/dist
COPY --from=cardano-services-builder /app/packages/cardano-services/package.json /app/packages/cardano-services/package.json
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/Cardano/types/Certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export const isCertType = <K extends keyof CertificateTypeMap>(
certificateTypes: readonly K[]
): certificate is CertificateTypeMap[K] => certificateTypes.includes(certificate.__typename as K);

// LW-10773 add the deposit and change StakeRegistration certificate into Registration certificate
/**
* Creates a stake key registration certificate from a given reward account.
*
Expand All @@ -254,6 +255,7 @@ export const createStakeRegistrationCert = (rewardAccount: RewardAccount): Certi
}
});

// LW-10773 change deposit from optional to required and remove StakeDeregistration certificate
/**
* Creates a stake key de-registration certificate from a given reward account.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Cardano/types/ProtocolParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type BabbageProtocolParameters = Omit<AlonzoProtocolParams, 'coinsPerUtxoWord' |
export interface PoolVotingThresholds {
motionNoConfidence: Fraction;
committeeNormal: Fraction;
commiteeNoConfidence: Fraction;
committeeNoConfidence: Fraction;
hardForkInitiation: Fraction;
}

Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/Serialization/Common/CborSet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CborReader, CborReaderState, CborTag, CborWriter } from '../CBOR';
import { HexBlob } from '@cardano-sdk/util';
import { inConwayEra } from '../../util';

/** Represents a cbor serialization wrapper for a Core type <{@link C}> */
interface CborSerializable<C> {
Expand All @@ -16,8 +17,6 @@ interface CborSerializable<C> {
export class CborSet<C, T extends CborSerializable<C>> {
#values: T[];

static useConwaySerialization = false;

// Prevent users from directly creating an instance. Only allow creating via fromCore or fromCbor.
private constructor(values: T[]) {
this.#values = [...values];
Expand Down Expand Up @@ -50,13 +49,13 @@ export class CborSet<C, T extends CborSerializable<C>> {
/**
* Serializes a CborSet<T> into CBOR format.
*
* @returns The CborSet in CBOR format, using the `258` tag representation if {@link useConwaySerialization} flag is set,
* @returns The CborSet in CBOR format, using the `258` tag representation if {@link inConwayEra} flag is set,
* or as an the array.
*/
toCbor(): HexBlob {
const writer = new CborWriter();

if (CborSet.useConwaySerialization) writer.writeTag(CborTag.Set);
if (inConwayEra) writer.writeTag(CborTag.Set);

writer.writeStartArray(this.size());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cardano } from '../../..';
import { Cardano, inConwayEra } from '../../..';
import { CborReader, CborReaderState, CborWriter } from '../../CBOR';
import { ExUnits } from '../../Common';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
Expand All @@ -13,27 +13,22 @@ const MAP_VALUE_EMBEDDED_GROUP_SIZE = 2;
export class Redeemers {
#values: Redeemer[];

// TODO: set to true or remove once mainnet is hardforked to mainnet
/**
* Enable encoding `redeemers` as `Map`:
* https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/conway/impl/cddl-files/conway.cddl#L480
*/
static useConwaySerialization = false;

private constructor(redeemers: Redeemer[]) {
this.#values = [...redeemers];
}

/**
* Serializes Redeemers into CBOR format.
* Redeemers are encoded as array when {@link useConwaySerialization} is false, and as map when
* {@link useConwaySerialization} is true.
* Redeemers are encoded as array when {@link inConwayEra} is false, and as map when
* {@link inConwayEra} is true.
*
* @returns The Redeemers in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (Redeemers.useConwaySerialization) {
// Encoding `redeemers` as `Map`:
// https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/conway/impl/cddl-files/conway.cddl#L480
if (inConwayEra) {
// { + [ tag: redeemer_tag, index: uint ] => [ data: plutus_data, ex_units: ex_units ] }
const redeemersMap = new Map(this.#values.map((redeemer) => [`${redeemer.tag()}:${redeemer.index()}`, redeemer]));

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
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class PoolVotingThresholds {
*/
toCore(): Cardano.PoolVotingThresholds {
return {
commiteeNoConfidence: this.#committeeNoConfidence.toCore(),
committeeNoConfidence: this.#committeeNoConfidence.toCore(),
committeeNormal: this.#committeeNormal.toCore(),
hardForkInitiation: this.#hardForkInitiation.toCore(),
motionNoConfidence: this.#motionNoConfidence.toCore()
Expand All @@ -129,7 +129,7 @@ export class PoolVotingThresholds {
return new PoolVotingThresholds(
UnitInterval.fromCore(core.motionNoConfidence),
UnitInterval.fromCore(core.committeeNormal),
UnitInterval.fromCore(core.commiteeNoConfidence),
UnitInterval.fromCore(core.committeeNoConfidence),
UnitInterval.fromCore(core.hardForkInitiation)
);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/util/conwayEra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// LW-10773 remove this file

/** Specifies if SDK should behave as in Conway era. */
// eslint-disable-next-line import/no-mutable-exports
export let inConwayEra = false as const;

export const setInConwayEra = (value: boolean) => (inConwayEra = value as false);
1 change: 1 addition & 0 deletions packages/core/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as util from './misc';
export * from './conwayEra';
export * from './slotCalc';
export * from './txInspector';
export * from './tokenTransferInspector';
Expand Down
7 changes: 4 additions & 3 deletions packages/core/test/Serialization/Common/CborSet.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CborReader, CborSet, CborTag, CborWriter } from '../../../src/Serialization';
import { HexBlob } from '@cardano-sdk/util';
import { setInConwayEra } from '../../../src';

class TestNumber {
#value: number;
Expand Down Expand Up @@ -54,14 +55,14 @@ describe('CborSet', () => {
testCborConway = writer.encodeAsHex();
});

afterEach(() => (CborSet.useConwaySerialization = false));
afterEach(() => setInConwayEra(false));

it('can serialize as array', () => {
expect(testCbor).toEqual(set.toCbor());
});

it('can serialize as 258 tag set', () => {
CborSet.useConwaySerialization = true;
setInConwayEra(true);
expect(testCborConway).toEqual(set.toCbor());
});

Expand All @@ -84,7 +85,7 @@ describe('CborSet', () => {
const emptyArrayCbor = writer.encodeAsHex();

const emptySet = CborSet.fromCore<number, TestNumber>([], TestNumber.fromCore);
CborSet.useConwaySerialization = useConwaySerialization;
setInConwayEra(useConwaySerialization);
expect(emptySet.toCbor()).toEqual(emptyArrayCbor);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const core = {
dRepDeposit: 2000,
dRepInactivityPeriod: EpochNo(5000),
dRepVotingThresholds: {
commiteeNoConfidence: { denominator: 2, numerator: 2 },
committeeNoConfidence: { denominator: 2, numerator: 2 },
committeeNormal: { denominator: 1, numerator: 1 },
hardForkInitiation: { denominator: 4, numerator: 4 },
motionNoConfidence: { denominator: 0, numerator: 0 },
Expand Down Expand Up @@ -81,7 +81,7 @@ const core = {
poolInfluence: '0.5',
poolRetirementEpochBound: 800,
poolVotingThresholds: {
commiteeNoConfidence: { denominator: 2, numerator: 2 },
committeeNoConfidence: { denominator: 2, numerator: 2 },
committeeNormal: { denominator: 1, numerator: 1 },
hardForkInitiation: { denominator: 3, numerator: 3 },
motionNoConfidence: { denominator: 0, numerator: 0 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cardano } from '../../../src';
import { Cardano, setInConwayEra } from '../../../src';
import { HexBlob } from '@cardano-sdk/util';
import { RedeemerPurpose } from '../../../src/Cardano';
import { Redeemers } from '../../../src/Serialization';
Expand Down Expand Up @@ -33,7 +33,7 @@ const cborInvalidMapIndex = HexBlob('a28308000082d8799f0102030405ff821821182c820
const cborInvalidMapValue = HexBlob('a28200008300d8799f0102030405ff821821182c82040082d8799f0102030405ff8218371842');

describe('Redeemers', () => {
afterEach(() => (Redeemers.useConwaySerialization = false));
afterEach(() => setInConwayEra(false));

it('can decode Redeemers from CBOR', () => {
const redeemers = Redeemers.fromCbor(cbor);
Expand All @@ -47,13 +47,12 @@ describe('Redeemers', () => {

it('can decode Redeemers from map encoded CBOR', () => {
const redeemers = Redeemers.fromCbor(cborConway);
// Redeemers.useConwaySerialization = true;
expect(redeemers.toCore()).toEqual(core);
});

it('can encode Redeemers as map in CBOR', () => {
const redeemers = Redeemers.fromCore(core);
Redeemers.useConwaySerialization = true;
setInConwayEra(true);
expect(redeemers.toCbor()).toEqual(cborConway);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cbor = HexBlob(
);

const core = {
commiteeNoConfidence: { denominator: 2, numerator: 2 },
committeeNoConfidence: { denominator: 2, numerator: 2 },
committeeNormal: { denominator: 1, numerator: 1 },
hardForkInitiation: { denominator: 4, numerator: 4 },
motionNoConfidence: { denominator: 0, numerator: 0 },
Expand All @@ -25,7 +25,7 @@ describe('DrepVotingThresholds', () => {
it('can decode DrepVotingThresholds from CBOR', () => {
const thresholds = DrepVotingThresholds.fromCbor(cbor);

expect(thresholds.committeeNoConfidence().toCore()).toEqual(core.commiteeNoConfidence);
expect(thresholds.committeeNoConfidence().toCore()).toEqual(core.committeeNoConfidence);
expect(thresholds.committeeNormal().toCore()).toEqual(core.committeeNormal);
expect(thresholds.hardForkInitiation().toCore()).toEqual(core.hardForkInitiation);
expect(thresholds.motionNoConfidence().toCore()).toEqual(core.motionNoConfidence);
Expand All @@ -40,7 +40,7 @@ describe('DrepVotingThresholds', () => {
it('can decode DrepVotingThresholds from Core', () => {
const thresholds = DrepVotingThresholds.fromCore(core);

expect(thresholds.committeeNoConfidence().toCore()).toEqual(core.commiteeNoConfidence);
expect(thresholds.committeeNoConfidence().toCore()).toEqual(core.committeeNoConfidence);
expect(thresholds.committeeNormal().toCore()).toEqual(core.committeeNormal);
expect(thresholds.hardForkInitiation().toCore()).toEqual(core.hardForkInitiation);
expect(thresholds.motionNoConfidence().toCore()).toEqual(core.motionNoConfidence);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PoolVotingThresholds } from '../../../src/Serialization';
const cbor = HexBlob('84d81e820000d81e820101d81e820202d81e820303');

const core = {
commiteeNoConfidence: { denominator: 2, numerator: 2 },
committeeNoConfidence: { denominator: 2, numerator: 2 },
committeeNormal: { denominator: 1, numerator: 1 },
hardForkInitiation: { denominator: 3, numerator: 3 },
motionNoConfidence: { denominator: 0, numerator: 0 }
Expand All @@ -17,7 +17,7 @@ describe('PoolVotingThresholds', () => {
it('can decode PoolVotingThresholds from CBOR', () => {
const thresholds = PoolVotingThresholds.fromCbor(cbor);

expect(thresholds.committeeNoConfidence().toCore()).toEqual(core.commiteeNoConfidence);
expect(thresholds.committeeNoConfidence().toCore()).toEqual(core.committeeNoConfidence);
expect(thresholds.committeeNormal().toCore()).toEqual(core.committeeNormal);
expect(thresholds.hardForkInitiation().toCore()).toEqual(core.hardForkInitiation);
expect(thresholds.motionNoConfidence().toCore()).toEqual(core.motionNoConfidence);
Expand All @@ -26,7 +26,7 @@ describe('PoolVotingThresholds', () => {
it('can decode PoolVotingThresholds from Core', () => {
const thresholds = PoolVotingThresholds.fromCore(core);

expect(thresholds.committeeNoConfidence().toCore()).toEqual(core.commiteeNoConfidence);
expect(thresholds.committeeNoConfidence().toCore()).toEqual(core.committeeNoConfidence);
expect(thresholds.committeeNormal().toCore()).toEqual(core.committeeNormal);
expect(thresholds.hardForkInitiation().toCore()).toEqual(core.hardForkInitiation);
expect(thresholds.motionNoConfidence().toCore()).toEqual(core.motionNoConfidence);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const conwayCore = {
dRepDeposit: 2000,
dRepInactivityPeriod: EpochNo(5000),
dRepVotingThresholds: {
commiteeNoConfidence: { denominator: 2, numerator: 2 },
committeeNoConfidence: { denominator: 2, numerator: 2 },
committeeNormal: { denominator: 1, numerator: 1 },
hardForkInitiation: { denominator: 4, numerator: 4 },
motionNoConfidence: { denominator: 0, numerator: 0 },
Expand Down Expand Up @@ -110,7 +110,7 @@ const conwayCore = {
poolInfluence: '0.5',
poolRetirementEpochBound: 800,
poolVotingThresholds: {
commiteeNoConfidence: { denominator: 2, numerator: 2 },
committeeNoConfidence: { denominator: 2, numerator: 2 },
committeeNormal: { denominator: 1, numerator: 1 },
hardForkInitiation: { denominator: 3, numerator: 3 },
motionNoConfidence: { denominator: 0, numerator: 0 }
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('ProtocolParamUpdate', () => {
expect(proposedParams.drepDeposit()).toEqual(2000);
expect(proposedParams.drepInactivityPeriod()).toEqual(5000);
expect(proposedParams.drepVotingThresholds()?.toCore()).toEqual({
commiteeNoConfidence: { denominator: 2, numerator: 2 },
committeeNoConfidence: { denominator: 2, numerator: 2 },
committeeNormal: { denominator: 1, numerator: 1 },
hardForkInitiation: { denominator: 4, numerator: 4 },
motionNoConfidence: { denominator: 0, numerator: 0 },
Expand All @@ -223,7 +223,7 @@ describe('ProtocolParamUpdate', () => {
updateConstitution: { denominator: 3, numerator: 3 }
});
expect(proposedParams.poolVotingThresholds()?.toCore()).toEqual({
commiteeNoConfidence: { denominator: 2, numerator: 2 },
committeeNoConfidence: { denominator: 2, numerator: 2 },
committeeNormal: { denominator: 1, numerator: 1 },
hardForkInitiation: { denominator: 3, numerator: 3 },
motionNoConfidence: { denominator: 0, numerator: 0 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Crypto from '@cardano-sdk/crypto';
import { BaseWallet } from '@cardano-sdk/wallet';
import { Cardano, Serialization } from '@cardano-sdk/core';
import { Cardano, setInConwayEra } from '@cardano-sdk/core';
import { logger } from '@cardano-sdk/util-dev';

import { firstValueFrom, map } from 'rxjs';
Expand Down Expand Up @@ -161,9 +161,7 @@ describe.skip('PersonalWallet/conwayTransactions', () => {
};

beforeAll(async () => {
// TODO: remove once mainnet hardforks to conway-era, and this becomes "the norm"
Serialization.CborSet.useConwaySerialization = true;
Serialization.Redeemers.useConwaySerialization = true;
setInConwayEra(true);

// TODO LW-10555: Revert this so that wallet is at account 0 and drepWallet at account 1, once the dbsync bug is fixed
// https://github.com/IntersectMBO/cardano-db-sync/issues/1702
Expand Down Expand Up @@ -191,9 +189,7 @@ describe.skip('PersonalWallet/conwayTransactions', () => {
wallet.shutdown();
dRepWallet.shutdown();

// TODO: remove once mainnet hardforks to conway-era, and this becomes "the norm"
Serialization.CborSet.useConwaySerialization = false;
Serialization.Redeemers.useConwaySerialization = false;
setInConwayEra(false);
});

it('can register a stake key and delegate stake using a combo certificate', async () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/tx-construction/src/tx-builder/TxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
WitnessedTx,
util
} from '@cardano-sdk/key-management';
import { Cardano, HandleProvider, HandleResolution, Serialization, metadatum } from '@cardano-sdk/core';
import { Cardano, HandleProvider, HandleResolution, Serialization, inConwayEra, metadatum } from '@cardano-sdk/core';
import {
CustomizeCb,
InsufficientRewardAccounts,
Expand Down Expand Up @@ -570,6 +570,7 @@ export class GenericTxBuilder implements TxBuilder {
return rewardAccountsWithWeights;
}

// eslint-disable-next-line sonarjs/cognitive-complexity
async #delegatePortfolio(): Promise<RewardAccountsAndWeights> {
const rewardAccountsWithWeights: RewardAccountsAndWeights = new Map();
if (!this.#requestedPortfolio) {
Expand Down Expand Up @@ -641,8 +642,9 @@ export class GenericTxBuilder implements TxBuilder {
this.#logger.debug(`De-registering ${availableRewardAccounts.length} stake keys`);
for (const rewardAccount of availableRewardAccounts) {
if (rewardAccount.credentialStatus === Cardano.StakeCredentialStatus.Registered) {
// TODO: re-enable conway stake deregistration cert, after the conway hardfork
certificates.push(Cardano.createStakeDeregistrationCert(rewardAccount.address /* , rewardAccount.deposit*/));
certificates.push(
Cardano.createStakeDeregistrationCert(rewardAccount.address, inConwayEra ? rewardAccount.deposit : undefined)
);
}
}
this.partialTxBody = { ...this.partialTxBody, certificates };
Expand Down
Loading