Skip to content

Commit

Permalink
feat!: update EraSummary slotLength type to be Milliseconds
Browse files Browse the repository at this point in the history
EraSummary.parameters.slotLength unit is milliseconds.
Use the appropriate opaque type to make it clear.

BREAKING CHANGE: EraSummary.parameters.slotLength type changed from number
to Milliseconds
  • Loading branch information
mirceahasegan committed Jan 20, 2023
1 parent 666701c commit fb1f1a2
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 26 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/CardanoNode/types/CardanoNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { Block, Lovelace, PoolId, Tip, VrfVkHex } from '../../Cardano';
// @ts-ignore
import { CardanoNodeError } from './CardanoNodeErrors';
import { HealthCheckResponse } from '../../Provider';
import { Milliseconds } from '../../util';

export interface EraSummary {
parameters: {
epochLength: number;
slotLength: number;
slotLength: Milliseconds;
};
start: {
slot: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/util/slotCalc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const createSlotTimeCalc = (eraSummaries: EraSummary[]) => {
}
return new Date(
activeEraSummary.start.time.getTime() +
(slotNo.valueOf() - activeEraSummary.start.slot) * activeEraSummary.parameters.slotLength
(slotNo.valueOf() - activeEraSummary.start.slot) * activeEraSummary.parameters.slotLength.valueOf()
);
};
};
Expand Down
9 changes: 6 additions & 3 deletions packages/core/test/CardanoNode/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Cardano, EraSummary, HealthCheckResponse, StakeDistribution } from '../../src';
import { Cardano, EraSummary, HealthCheckResponse, Milliseconds, StakeDistribution } from '../../src';

const mockEraSummaries: EraSummary[] = [
{ parameters: { epochLength: 21_600, slotLength: 20_000 }, start: { slot: 0, time: new Date(1_563_999_616_000) } },
{
parameters: { epochLength: 432_000, slotLength: 1000 },
parameters: { epochLength: 21_600, slotLength: Milliseconds(20_000) },
start: { slot: 0, time: new Date(1_563_999_616_000) }
},
{
parameters: { epochLength: 432_000, slotLength: Milliseconds(1000) },
start: { slot: 1_598_400, time: new Date(1_595_964_016_000) }
}
];
Expand Down
46 changes: 36 additions & 10 deletions packages/core/test/util/slotCalc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,35 @@ import {
EpochInfo,
EraSummary,
EraSummaryError,
Milliseconds,
SlotEpochCalc,
SlotEpochInfoCalc,
SlotTimeCalc,
createSlotEpochCalc,
createSlotEpochInfoCalc,
createSlotTimeCalc
} from '../../src';
import { testnetEraSummaries } from '../../../util-dev/src';

import merge from 'lodash/merge';

// Era summaries copied from util-dev package.
// Importing directly from util-dev reports Milliseconds types incompatible.
// Type 'import("core/dist/cjs/util/time").Milliseconds' is not assignable to type
// 'import("core/src/util/time").Milliseconds'.
// Property '__opaqueNumber' is protected but type 'OpaqueNumber<T>' is not a class derived from 'OpaqueNumber<T>'.
// Duplicating the test era summaries here to work around it.

export const testnetEraSummaries: EraSummary[] = [
{
parameters: { epochLength: 21_600, slotLength: Milliseconds(20_000) },
start: { slot: 0, time: new Date(1_563_999_616_000) }
},
{
parameters: { epochLength: 432_000, slotLength: Milliseconds(1000) },
start: { slot: 1_598_400, time: new Date(1_595_967_616_000) }
}
];

describe('slotCalc utils', () => {
describe('slotTimeCalc', () => {
describe('testnet', () => {
Expand Down Expand Up @@ -61,11 +79,17 @@ describe('slotCalc utils', () => {
it('correctly computes epoch with multiple summaries starting from genesis', () => {
const eraSummaries: EraSummary[] = [
{
parameters: { epochLength: 100, slotLength: 3 },
parameters: { epochLength: 100, slotLength: Milliseconds(3) },
start: { slot: 0, time: new Date(1_563_999_616_000) }
},
{
parameters: { epochLength: 200, slotLength: Milliseconds(10) },
start: { slot: 0, time: new Date(1_563_999_616_000) }
},
{ parameters: { epochLength: 200, slotLength: 10 }, start: { slot: 0, time: new Date(1_563_999_616_000) } },
{ parameters: { epochLength: 200, slotLength: 1 }, start: { slot: 0, time: new Date(1_563_999_616_000) } }
{
parameters: { epochLength: 200, slotLength: Milliseconds(1) },
start: { slot: 0, time: new Date(1_563_999_616_000) }
}
];
const slotEpochCalc: SlotEpochCalc = createSlotEpochCalc(eraSummaries);

Expand All @@ -74,15 +98,15 @@ describe('slotCalc utils', () => {
it('correctly computes epoch with summaries indicating an upgrade after genesis, from the same slotNo', () => {
const eraSummaries: EraSummary[] = [
{
parameters: { epochLength: 100, slotLength: 3 },
parameters: { epochLength: 100, slotLength: Milliseconds(3) },
start: { slot: 0, time: new Date(1_563_999_616_000) }
},
{
parameters: { epochLength: 200, slotLength: 10 },
parameters: { epochLength: 200, slotLength: Milliseconds(10) },
start: { slot: 301, time: new Date(1_563_999_716_000) }
},
{
parameters: { epochLength: 200, slotLength: 1 },
parameters: { epochLength: 200, slotLength: Milliseconds(1) },
start: { slot: 301, time: new Date(1_563_999_716_000) }
}
];
Expand Down Expand Up @@ -143,15 +167,17 @@ describe('slotCalc utils', () => {
expect(firstSlot).toEqual({
date: new Date(
epochEraSummary.start.time.getTime() +
epochEraSummary.parameters.epochLength * epochEraSummary.parameters.slotLength * relativeEpoch
epochEraSummary.parameters.epochLength * epochEraSummary.parameters.slotLength.valueOf() * relativeEpoch
),
slot: epochEraSummary.start.slot + epochEraSummary.parameters.epochLength * relativeEpoch
});
expect(lastSlot).toEqual({
date: new Date(
epochEraSummary.start.time.getTime() +
epochEraSummary.parameters.epochLength * epochEraSummary.parameters.slotLength * (relativeEpoch + 1) -
epochEraSummary.parameters.slotLength
epochEraSummary.parameters.epochLength *
epochEraSummary.parameters.slotLength.valueOf() *
(relativeEpoch + 1) -
epochEraSummary.parameters.slotLength.valueOf()
),
slot: epochEraSummary.start.slot + epochEraSummary.parameters.epochLength * (relativeEpoch + 1) - 1
});
Expand Down
4 changes: 2 additions & 2 deletions packages/ogmios/src/CardanoNode/mappers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EraSummary } from '@cardano-sdk/core';
import { EraSummary, Seconds } from '@cardano-sdk/core';
import { Schema } from '@cardano-ogmios/client';

export const mapEraSummary = (eraSummary: Schema.EraSummary, systemStart: Date): EraSummary => ({
parameters: {
epochLength: eraSummary.parameters.epochLength,
slotLength: eraSummary.parameters.slotLength * 1000
slotLength: Seconds.toMilliseconds(Seconds(eraSummary.parameters.slotLength))
},
start: {
slot: eraSummary.start.slot,
Expand Down
4 changes: 2 additions & 2 deletions packages/ogmios/test/CardanoNode/mappers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as mappers from '../../src/CardanoNode/mappers';
import { EraSummary } from '@cardano-sdk/core';
import { EraSummary, Milliseconds } from '@cardano-sdk/core';
import { Schema } from '@cardano-ogmios/client';

describe('cardano node mappers', () => {
Expand All @@ -14,7 +14,7 @@ describe('cardano node mappers', () => {
expect(result).toEqual<EraSummary>({
parameters: {
epochLength: 432_000,
slotLength: 1000
slotLength: Milliseconds(1000)
},
start: {
slot: 1_598_400,
Expand Down
4 changes: 2 additions & 2 deletions packages/projection/test/events/genesisToEraSummary.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Cardano, EraSummary } from '@cardano-sdk/core';
import { Cardano, EraSummary, Seconds } from '@cardano-sdk/core';

export const genesisToEraSummary = ({ systemStart, epochLength, slotLength }: Cardano.CompactGenesis): EraSummary => ({
parameters: {
epochLength,
slotLength
slotLength: Seconds.toMilliseconds(slotLength)
},
start: {
slot: 0,
Expand Down
4 changes: 2 additions & 2 deletions packages/projection/test/operators/withEpochNo.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Cardano, ChainSyncEventType } from '@cardano-sdk/core';
import { Cardano, ChainSyncEventType, Milliseconds } from '@cardano-sdk/core';
import { RollForwardEvent, UnifiedProjectorEvent, operators } from '../../src';
import { createTestScheduler } from '@cardano-sdk/util-dev';

const eraSummaries = [
{
parameters: { epochLength: 432_000, slotLength: 1000 },
parameters: { epochLength: 432_000, slotLength: Milliseconds(1000) },
start: { slot: 0, time: new Date(1_595_967_616_000) }
}
];
Expand Down
9 changes: 6 additions & 3 deletions packages/util-dev/src/eraSummaries.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { EraSummary } from '@cardano-sdk/core';
import { EraSummary, Milliseconds } from '@cardano-sdk/core';

/**
* Valid at 2022-05-28
*/

export const testnetEraSummaries: EraSummary[] = [
{ parameters: { epochLength: 21_600, slotLength: 20_000 }, start: { slot: 0, time: new Date(1_563_999_616_000) } },
{
parameters: { epochLength: 432_000, slotLength: 1000 },
parameters: { epochLength: 21_600, slotLength: Milliseconds(20_000) },
start: { slot: 0, time: new Date(1_563_999_616_000) }
},
{
parameters: { epochLength: 432_000, slotLength: Milliseconds(1000) },
start: { slot: 1_598_400, time: new Date(1_595_967_616_000) }
}
];

0 comments on commit fb1f1a2

Please sign in to comment.