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

Typesafe types #2452

Merged
merged 8 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IBeaconConfig} from "@chainsafe/lodestar-config";
import {phase0, altair, DEPOSIT_CONTRACT_TREE_DEPTH} from "@chainsafe/lodestar-types";
import {phase0, altair} from "@chainsafe/lodestar-types";
import {DEPOSIT_CONTRACT_TREE_DEPTH} from "@chainsafe/lodestar-params";
import {assert, verifyMerkleBranch, bigIntMin} from "@chainsafe/lodestar-utils";
import {computeSigningRoot, computeDomain, increaseBalance} from "../../../util";
import bls from "@chainsafe/bls";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IBeaconConfig} from "@chainsafe/lodestar-config";
import {phase0, ATTESTATION_SUBNET_COUNT} from "@chainsafe/lodestar-types";
import {ATTESTATION_SUBNET_COUNT} from "@chainsafe/lodestar-params";
import {phase0} from "@chainsafe/lodestar-types";
import {computeSlotsSinceEpochStart} from "../../util";
import {EpochContext} from "./epochContext";

Expand Down
10 changes: 2 additions & 8 deletions packages/beacon-state-transition/src/util/attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@

import bls from "@chainsafe/bls";
import {IBeaconConfig} from "@chainsafe/lodestar-config";
import {
ATTESTATION_SUBNET_COUNT,
phase0,
Slot,
ValidatorIndex,
CommitteeIndex,
allForks,
} from "@chainsafe/lodestar-types";
import {ATTESTATION_SUBNET_COUNT} from "@chainsafe/lodestar-params";
import {phase0, Slot, ValidatorIndex, CommitteeIndex, allForks} from "@chainsafe/lodestar-types";
import {isSorted} from "@chainsafe/lodestar-utils";
import {BitList, List} from "@chainsafe/ssz";
import {getBeaconCommittee, getCommitteeCountAtSlot} from "./committee";
Expand Down
6 changes: 3 additions & 3 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {GENESIS_EPOCH, IBeaconParams} from "@chainsafe/lodestar-params";
import {createIBeaconSSZTypes, Slot, IAllForksSSZTypes, Version} from "@chainsafe/lodestar-types";
import {createIBeaconSSZTypes, Slot, AllForksSSZTypes, Version} from "@chainsafe/lodestar-types";
import {IBeaconConfig, ForkName} from "./interface";

export * from "./interface";
Expand Down Expand Up @@ -35,8 +35,8 @@ export function createIBeaconConfig(params: IBeaconParams): IBeaconConfig {
getForkVersion(slot: Slot): Version {
return this.getForkInfo(slot).version;
},
getTypes(slot: Slot): IAllForksSSZTypes {
return types[this.getForkName(slot)] as IAllForksSSZTypes;
getTypes(slot: Slot): AllForksSSZTypes {
return types[this.getForkName(slot)] as AllForksSSZTypes;
},
};
}
4 changes: 2 additions & 2 deletions packages/config/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IBeaconParams} from "@chainsafe/lodestar-params";
import {Epoch, IAllForksSSZTypes, IBeaconSSZTypes, Slot, Version} from "@chainsafe/lodestar-types";
import {Epoch, AllForksSSZTypes, IBeaconSSZTypes, Slot, Version} from "@chainsafe/lodestar-types";

export enum ForkName {
phase0 = "phase0",
Expand All @@ -23,5 +23,5 @@ export interface IBeaconConfig {
/** Get the hard-fork version at a given slot */
getForkVersion(slot: Slot): Version;
/** Get SSZ types by hard-fork */
getTypes(slot: Slot): IAllForksSSZTypes;
getTypes(slot: Slot): AllForksSSZTypes;
}
3 changes: 2 additions & 1 deletion packages/lodestar/src/network/gossip/gossipsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Gossipsub from "libp2p-gossipsub";
import {InMessage} from "libp2p-interfaces/src/pubsub";
import Libp2p from "libp2p";
import {IBeaconConfig} from "@chainsafe/lodestar-config";
import {ATTESTATION_SUBNET_COUNT, phase0} from "@chainsafe/lodestar-types";
import {ATTESTATION_SUBNET_COUNT} from "@chainsafe/lodestar-params";
import {phase0} from "@chainsafe/lodestar-types";
import {ILogger, toJson} from "@chainsafe/lodestar-utils";
import {computeEpochAtSlot} from "@chainsafe/lodestar-beacon-state-transition";

Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/network/gossip/validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {AbortSignal} from "abort-controller";
import {ForkName} from "@chainsafe/lodestar-config";
import {ATTESTATION_SUBNET_COUNT} from "@chainsafe/lodestar-types";
import {ATTESTATION_SUBNET_COUNT} from "@chainsafe/lodestar-params";
import {mapValues} from "@chainsafe/lodestar-utils";
import {IMetrics} from "../../metrics";
import {JobQueue, JobQueueOpts, QueueType} from "../../util/queue";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {GENESIS_SLOT} from "@chainsafe/lodestar-beacon-state-transition";
import {MAX_REQUEST_BLOCKS, phase0} from "@chainsafe/lodestar-types";
import {MAX_REQUEST_BLOCKS} from "@chainsafe/lodestar-params";
import {phase0} from "@chainsafe/lodestar-types";
import {IBlockFilterOptions} from "../../../db/repositories";
import {IBeaconChain} from "../../../chain";
import {IBeaconDb} from "../../../db";
Expand Down
3 changes: 2 additions & 1 deletion packages/lodestar/src/sync/utils/attestation-collector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {ChainEvent, IBeaconChain} from "../../chain";
import {IBeaconDb} from "../../db";
import {IBeaconConfig, ForkName} from "@chainsafe/lodestar-config";
import {phase0, CommitteeIndex, Slot, ATTESTATION_SUBNET_COUNT} from "@chainsafe/lodestar-types";
import {ATTESTATION_SUBNET_COUNT} from "@chainsafe/lodestar-params";
import {phase0, CommitteeIndex, Slot} from "@chainsafe/lodestar-types";
import {INetwork} from "../../network";
import {computeSubnetForSlot} from "@chainsafe/lodestar-beacon-state-transition";
import {ILogger} from "@chainsafe/lodestar-utils";
Expand Down
5 changes: 4 additions & 1 deletion packages/params/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ export const GENESIS_SLOT = 0;
export const GENESIS_EPOCH = 0;
export const FAR_FUTURE_EPOCH = Infinity;
export const BASE_REWARDS_PER_EPOCH = 4;
export const DEPOSIT_CONTRACT_TREE_DEPTH = 32;
export const DEPOSIT_CONTRACT_TREE_DEPTH = 2 ** 5; // 32
export const JUSTIFICATION_BITS_LENGTH = 4;
export const ATTESTATION_SUBNET_COUNT = 64;
export const MAX_REQUEST_BLOCKS = 2 ** 10; // 1024
export const P2P_ERROR_MESSAGE_MAX_LENGTH = 256;

export const MIN_SYNC_COMMITTEE_PARTICIPANTS = 1;
//~27 hours
Expand Down
14 changes: 8 additions & 6 deletions packages/spec-test-util/test/e2e/single/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {unlinkSync, writeFileSync} from "fs";
import {join} from "path";

import {ContainerType, Type, Json} from "@chainsafe/ssz";
import {Boolean, Number64} from "@chainsafe/lodestar-types/lib/primitive/ssz";
import {getPrimitiveTypes} from "@chainsafe/lodestar-types/src/primitive";
import {describeDirectorySpecTest, InputType} from "../../../src/single";
import {loadYamlFile} from "../../../src/util";

const primitive = getPrimitiveTypes();

export interface ISimpleStruct {
test: boolean;
number: number;
Expand All @@ -19,16 +21,16 @@ export interface ISimpleCase extends Iterable<string> {

const inputSchema = new ContainerType({
fields: {
test: Boolean,
number: Number64,
test: primitive.Boolean,
number: primitive.Number64,
},
});

before(() => {
yamlToSSZ(join(__dirname, "../_test_files/single/case0/input.yaml"), inputSchema);
yamlToSSZ(join(__dirname, "../_test_files/single/case0/output.yaml"), Number64);
yamlToSSZ(join(__dirname, "../_test_files/single/case0/output.yaml"), primitive.Number64);
yamlToSSZ(join(__dirname, "../_test_files/single/case1/input.yaml"), inputSchema);
yamlToSSZ(join(__dirname, "../_test_files/single/case1/output.yaml"), Number64);
yamlToSSZ(join(__dirname, "../_test_files/single/case1/output.yaml"), primitive.Number64);
});

after(() => {
Expand All @@ -51,7 +53,7 @@ describeDirectorySpecTest<ISimpleCase, number>(
},
sszTypes: {
input: inputSchema,
output: Number64,
output: primitive.Number64,
},
shouldError: (testCase) => !testCase.input.test,
getExpected: (testCase) => testCase.output,
Expand Down
29 changes: 0 additions & 29 deletions packages/types/src/IBeaconSSZTypes.ts

This file was deleted.

15 changes: 9 additions & 6 deletions packages/types/src/allForks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ContainerType} from "@chainsafe/ssz";

import {PrimitiveSSZTypes} from "../primitive";
import * as phase0 from "../phase0";
import * as altair from "../altair";

Expand All @@ -10,14 +11,16 @@ export type BeaconBlock = phase0.BeaconBlock | altair.BeaconBlock;
export type SignedBeaconBlock = phase0.SignedBeaconBlock | altair.SignedBeaconBlock;
export type BeaconState = phase0.BeaconState | altair.BeaconState;

// The difference between IAllSSZTypes and IAllForksSSZTypes:
// IAllSSZTypes["BeaconState"] = ContainerType<phase0.BeaconState & altair.BeaconState & phase1.BeaconState>
// IAllForksSSZTypes["BeaconState"] = ContainerType<phase0.BeaconState | altair.BeaconState | phase1.BeaconState>
// The difference between AllSSZTypes and AllForksSSZTypes:
// AllSSZTypes["BeaconState"] = ContainerType<phase0.BeaconState & altair.BeaconState & phase1.BeaconState>
// AllForksSSZTypes["BeaconState"] = ContainerType<phase0.BeaconState | altair.BeaconState | phase1.BeaconState>

type IAllSSZTypes = phase0.IPhase0SSZTypes | altair.IAltairSSZTypes;
type AllSSZTypes =
| (PrimitiveSSZTypes & phase0.Phase0SSZTypes)
| (PrimitiveSSZTypes & phase0.Phase0SSZTypes & altair.AltairSSZTypes);

export type IAllForksSSZTypes = Omit<
IAllSSZTypes,
export type AllForksSSZTypes = Omit<
AllSSZTypes,
"BeaconBlockBody" | "BeaconBlock" | "SignedBeaconBlock" | "BeaconState"
> & {
BeaconBlockBody: ContainerType<BeaconBlockBody>;
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/altair/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./types";
export * from "./ssz";
export * from "./sszTypes";